๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[์ด๊ฒƒ์ด ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋‹ค with Python]_31_ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณธ๋ฌธ

๐Ÿฆฅ ์ฝ”ํ…Œ/์ด๊ฒƒ์ด ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋‹ค with python

[์ด๊ฒƒ์ด ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋‹ค with Python]_31_ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜

์ง•์ง•์•ŒํŒŒ์นด 2022. 2. 12. 00:40
728x90
๋ฐ˜์‘ํ˜•

220212 ์ž‘์„ฑ

<๋ณธ ๋ธ”๋กœ๊ทธ๋Š” ใ€Ž์ด๊ฒƒ์ด ์ทจ์—…์„ ์œ„ํ•œ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋‹คใ€ ์˜ youtube๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค>

https://www.youtube.com/watch?v=hw-SvAR3Zqg&list=PLVsNizTWUw7H9_of5YCB0FmsSc-K44y81&index=31 

 

 

 

 

 

 

 

 

1. ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜

: ๋ชจ๋“  ๋…ธ๋“œ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ๋…ธ๋“œ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ ๋ชจ๋‘ ๊ณ„์‚ฐ

: ๋‹จ๊ณ„๋ณ„๋กœ ๊ฑฐ์ณ ๊ฐ€๋Š” ๋…ธ๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆ˜ํ–‰ ( ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๊ฐ–๋Š” ๋…ธ๋“œ ์ฐพ๊ธฐ๋Š” ํ•„์š” X )

: 2์ฐจ์› ํ…Œ์ด๋ธ”์— ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์ •๋ณด ์ €์žฅ

: ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์œ ํ˜•์— ์†ํ•œ๋‹ค

: ๊ฐ ๋‹จ๊ณ„๋งˆ๋‹ค ํŠน์ •ํ•œ ๋…ธ๋“œ K ๋ฅผ ๊ฑฐ์ณ ๊ฐ€๋Š” ๊ฒฝ์šฐ๋ฅผ ํ™•์ธ

( a ์—์„œ b ๋กœ ๊ฐ€๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ณด๋‹ค a ์—์„œ k ๋ฅผ ๊ฑฐ์ณ b ๋กœ ๊ฐ€๋Š” ๊ฑฐ๋ฆฌ๊ฐ€ ๋” ์งง์€์ง€ ๊ฒ€์‚ฌ )

 

1) ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์ดˆ๊ธฐํ™”

2) k ๋…ธ๋“œ ๊ฑฐ์ณ ๊ฐ€๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•˜๋ฉฐ ํ…Œ์ด๋ธ” ๊ฐฑ์‹ 

 

    •  python
INF = int(1e9) # ๋ฌดํ•œ์„ ์˜๋ฏธํ•˜๋Š” ๊ฐ’์œผ๋กœ 10์–ต์„ ์„ค์ •

# ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ ๋ฐ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๊ธฐ
n = int(input())
m = int(input())
# 2์ฐจ์› ๋ฆฌ์ŠคํŠธ(๊ทธ๋ž˜ํ”„ ํ‘œํ˜„)๋ฅผ ๋งŒ๋“ค๊ณ , ๋ชจ๋“  ๊ฐ’์„ ๋ฌดํ•œ์œผ๋กœ ์ดˆ๊ธฐํ™”
graph = [[INF] * (n + 1) for _ in range(n + 1)]

# ์ž๊ธฐ ์ž์‹ ์—์„œ ์ž๊ธฐ ์ž์‹ ์œผ๋กœ ๊ฐ€๋Š” ๋น„์šฉ์€ 0์œผ๋กœ ์ดˆ๊ธฐํ™”
for a in range(1, n + 1):
    for b in range(1, n + 1):
        if a == b:
            graph[a][b] = 0

# ๊ฐ ๊ฐ„์„ ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„, ๊ทธ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”
for _ in range(m):
    # A์—์„œ B๋กœ ๊ฐ€๋Š” ๋น„์šฉ์€ C๋ผ๊ณ  ์„ค์ •
    a, b, c = map(int, input().split())
    graph[a][b] = c

# ์ ํ™”์‹์— ๋”ฐ๋ผ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ˆ˜ํ–‰
for k in range(1, n + 1):
    for a in range(1, n + 1):
        for b in range(1, n + 1):
            graph[a][b] = min(graph[a][b], graph[a][k] + graph[k][b])

# ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅ
for a in range(1, n + 1):
    for b in range(1, n + 1):
        # ๋„๋‹ฌํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ, ๋ฌดํ•œ(INFINITY)์ด๋ผ๊ณ  ์ถœ๋ ฅ
        if graph[a][b] == 1e9:
            print("INFINITY", end=" ")
        # ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ๊ฑฐ๋ฆฌ๋ฅผ ์ถœ๋ ฅ
        else:
            print(graph[a][b], end=" ")
    print()
  • c++
#include <bits/stdc++.h>
#define INF 1e9 // ๋ฌดํ•œ์„ ์˜๋ฏธํ•˜๋Š” ๊ฐ’์œผ๋กœ 10์–ต์„ ์„ค์ •

using namespace std;

// ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜(N), ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜(M)
// ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋Š” ์ตœ๋Œ€ 500๊ฐœ๋ผ๊ณ  ๊ฐ€์ •
int n, m;
// 2์ฐจ์› ๋ฐฐ์—ด(๊ทธ๋ž˜ํ”„ ํ‘œํ˜„)๋ฅผ ๋งŒ๋“ค๊ธฐ
int graph[501][501];

int main(void) {
    cin >> n >> m;

    // ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ”์„ ๋ชจ๋‘ ๋ฌดํ•œ์œผ๋กœ ์ดˆ๊ธฐํ™”
    for (int i = 0; i < 501; i++) {
        fill(graph[i], graph[i] + 501, INF);
    }

    // ์ž๊ธฐ ์ž์‹ ์—์„œ ์ž๊ธฐ ์ž์‹ ์œผ๋กœ ๊ฐ€๋Š” ๋น„์šฉ์€ 0์œผ๋กœ ์ดˆ๊ธฐํ™”
    for (int a = 1; a <= n; a++) {
        for (int b = 1; b <= n; b++) {
            if (a == b) graph[a][b] = 0;
        }
    }

    // ๊ฐ ๊ฐ„์„ ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„, ๊ทธ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”
    for (int i = 0; i < m; i++) {
        // A์—์„œ B๋กœ ๊ฐ€๋Š” ๋น„์šฉ์€ C๋ผ๊ณ  ์„ค์ •
        int a, b, c;
        cin >> a >> b >> c;
        graph[a][b] = c;
    }
    
    // ์ ํ™”์‹์— ๋”ฐ๋ผ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ˆ˜ํ–‰
    for (int k = 1; k <= n; k++) {
        for (int a = 1; a <= n; a++) {
            for (int b = 1; b <= n; b++) {
                graph[a][b] = min(graph[a][b], graph[a][k] + graph[k][b]);
            }
        }
    }

    // ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅ
    for (int a = 1; a <= n; a++) {
        for (int b = 1; b <= n; b++) {
            // ๋„๋‹ฌํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ, ๋ฌดํ•œ(INFINITY)์ด๋ผ๊ณ  ์ถœ๋ ฅ
            if (graph[a][b] == INF) {
                cout << "INFINITY" << ' ';
            }
            // ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ๊ฑฐ๋ฆฌ๋ฅผ ์ถœ๋ ฅ
            else {
                cout << graph[a][b] << ' ';
            }
        }
        cout << '\n';
    }
}
  • java
import java.util.*;

public class Main {

    public static final int INF = (int) 1e9; // ๋ฌดํ•œ์„ ์˜๋ฏธํ•˜๋Š” ๊ฐ’์œผ๋กœ 10์–ต์„ ์„ค์ •
    // ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜(N), ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜(M)
    // ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋Š” ์ตœ๋Œ€ 500๊ฐœ๋ผ๊ณ  ๊ฐ€์ •
    public static int n, m;
    // 2์ฐจ์› ๋ฐฐ์—ด(๊ทธ๋ž˜ํ”„ ํ‘œํ˜„)๋ฅผ ๋งŒ๋“ค๊ธฐ
    public static int[][] graph = new int[501][501];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        n = sc.nextInt();
        m = sc.nextInt();

        // ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ”์„ ๋ชจ๋‘ ๋ฌดํ•œ์œผ๋กœ ์ดˆ๊ธฐํ™”
        for (int i = 0; i < 501; i++) {
            Arrays.fill(graph[i], INF);
        }

        // ์ž๊ธฐ ์ž์‹ ์—์„œ ์ž๊ธฐ ์ž์‹ ์œผ๋กœ ๊ฐ€๋Š” ๋น„์šฉ์€ 0์œผ๋กœ ์ดˆ๊ธฐํ™”
        for (int a = 1; a <= n; a++) {
            for (int b = 1; b <= n; b++) {
                if (a == b) graph[a][b] = 0;
            }
        }

        // ๊ฐ ๊ฐ„์„ ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„, ๊ทธ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”
        for (int i = 0; i < m; i++) {
            // A์—์„œ B๋กœ ๊ฐ€๋Š” ๋น„์šฉ์€ C๋ผ๊ณ  ์„ค์ •
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            graph[a][b] = c;
        }

        // ์ ํ™”์‹์— ๋”ฐ๋ผ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ˆ˜ํ–‰
        for (int k = 1; k <= n; k++) {
            for (int a = 1; a <= n; a++) {
                for (int b = 1; b <= n; b++) {
                    graph[a][b] = Math.min(graph[a][b], graph[a][k] + graph[k][b]);
                }
            }
        }

        // ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅ
        for (int a = 1; a <= n; a++) {
            for (int b = 1; b <= n; b++) {
                // ๋„๋‹ฌํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ, ๋ฌดํ•œ(INFINITY)์ด๋ผ๊ณ  ์ถœ๋ ฅ
                if (graph[a][b] == INF) {
                    System.out.print("INFINITY ");
                }
                // ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ๊ฑฐ๋ฆฌ๋ฅผ ์ถœ๋ ฅ
                else {
                    System.out.print(graph[a][b] + " ");
                }
            }
            System.out.println();
        }
    }
}

 

 

=> ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๊ฐ€ N๊ฐœ์ผ ๋•Œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ƒ์œผ๋กœ N ๋ฒˆ ๋‹จ๊ณ„ ์ˆ˜ํ–‰

=> ๊ฐ ๋‹จ๊ณ„๋งˆ๋‹ค O(N^2) ์˜ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ๊ฑฐ์ณ ๊ฐ€๋Š” ๋ชจ๋“  ๊ฒฝ๋กœ ๊ณ ๋ ค

=> ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ด ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(N^3)

 

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•
Comments