๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜/์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ข…๋ฅ˜

[์ตœ๋‹จ๊ฒฝ๋กœ] Bellman-Ford Algorithm

by ๐Ÿณ Laboon 2024. 7. 6.

์ตœ๋‹จ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜

package com.study.datastructrue.mst;

import java.util.ArrayList;
import java.util.Arrays;

class Pair<T, V> {

    T node;
    V cost;

    public Pair(T node, V cost) {
        this.node = node;
        this.cost = cost;
    }

}

public class BellmanFord {

    static long[] dist = new long[6];

    public static void main(String[] args) {
        // ์ง‘ 1, ํ•™๊ต 2, ๋ฐ•๋ฌผ๊ด€ 3, ์˜ํ™”๊ด€ 4, ๋งˆํŠธ 5
        ArrayList<ArrayList<Pair<Integer, Integer>>> graph = input();
        for (int i = 1; i < graph.size(); i++) {
            dist = bellmanFord(graph, i);
            for (long dis: dist) {
                System.out.printf((dis == Integer.MAX_VALUE ? "INF" : dis) + " ");
            }
            System.out.println();
        }

        for (int i = 1; i < graph.size(); i++) {
            for (Pair<Integer, Integer> path : graph.get(i)) {
                if (dist[path.node] > dist[i] + path.cost) {
                    System.out.print("negative cycle exists ........");
                }
            }
        }
    }

    public static ArrayList<ArrayList<Pair<Integer, Integer>>> input() {
        ArrayList<ArrayList<Pair<Integer, Integer>>> graph = new ArrayList<>();
        for (int i = 0; i <= 5; i++) {
            graph.add(new ArrayList<>());
        }

        graph.get(1).add(new Pair<>(2, -1));
        graph.get(1).add(new Pair<>(5, 4));
        graph.get(2).add(new Pair<>(3, 2));
        graph.get(2).add(new Pair<>(4, 1));
        graph.get(2).add(new Pair<>(5, 3));
        graph.get(3).add(new Pair<>(4, -3)); // ์Œ์ˆ˜ ๊ฐ’์— ๋”ฐ๋ผ ์Œ์ˆ˜ ์‚ฌ์ดํด ํŒ๋ณ„ ๊ฐ€๋Šฅ
        graph.get(4).add(new Pair<>(2, 2));
        graph.get(4).add(new Pair<>(5, 5));

        return graph;
    }

    public static long[] bellmanFord(
            ArrayList<ArrayList<Pair<Integer, Integer>>> graph,
            int start
    ) {
        Arrays.fill(dist, Integer.MAX_VALUE);
        dist[start] = 0;

        for (int node = 0; node < 5; node++) {
            for (int i = 1; i < graph.size(); i++) {
            	if (dist[i] == Integer.MAX_VALUE) continue;
                for (Pair<Integer, Integer> path : graph.get(i)) {
                    if (dist[path.node] > dist[i] + path.cost) {
                        dist[path.node] = dist[i] + path.cost;
                    }
                }
            }
        }

        return dist;
    }

}

 

๋ฒจ๋งŒ ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํ•œ ์ ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ์ ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด๋‹ค.

ํ•œ ์ ์—์„œ Dynamic Programming์œผ๋กœ Flooding ํ•  ๊ฒฝ์šฐ ํ˜„์žฌ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐฑ์‹ ๋œ๋‹ค.

์ด๊ฒƒ์„ n-1๋ฒˆ ์งธ ์ ๊นŒ์ง€ ๋ชจ๋‘ ๋ฐ˜๋ณต์„ ํ•˜๋ฉด ์ „์ฒด ์ตœ๋‹จ ๊ฒฝ๋กœ๊ฐ€ ๊ฐฑ์‹ ์ด ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 

๋ฒจ๋งŒ ํฌ๋“œ๋Š” ์Œ์ˆ˜ ๊ฐ€์ค‘์น˜๋„ ๊ฐ–๊ณ  ์žˆ์œผ๋ฏ€๋กœ n๋ฒˆ์งธ์—์„œ ํ•œ๋ฒˆ ๋” ์ตœ๋‹จ ๊ฒฝ๋กœ๊ฐ€ ๊ฐฑ์‹ ์ด ๋œ๋‹ค๋ฉด, ์Œ์ˆ˜ ์‚ฌ์ดํด์„ ๊ฐ–๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋œ๋‹ค. ์Œ์ˆ˜ ์‚ฌ์ดํด๋งŒ ๊ณ„์† ๋Œ๋ฉด์„œ -INF ๊นŒ์ง€ ๋–จ์–ด์ง.

 

๋˜ ์Œ์ˆ˜ ๊ฐ€์ค‘์น˜๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— INF์ผ ๊ฒฝ์šฐ continue๋ฅผ ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค. ์•ˆ๊ทธ๋Ÿผ OverFlow ๋ฐœ์ƒ

 

์ „์ฒด์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ •๋ฆฌ

1. ํ˜„์žฌ ๋…ธ๋“œ์—์„œ ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐฑ์‹ ํ•œ๋‹ค.

-> ํ˜„์žฌ ๋…ธ๋“œ์˜ ๊ฐ€์ค‘์น˜ + ๋‹ค์Œ ๋…ธ๋“œ์˜ ๊ฐ€์ค‘์น˜ < ๋‹ค์Œ ๋…ธ๋“œ์˜ ๊ฐ€์ค‘์น˜๋ผ๋ฉด ํ˜„์žฌ ๋…ธ๋“œ์—์„œ ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์ด๋™ํ•˜๋Š” ๊ธธ์ด ์ตœ๋‹จ ๊ฒฝ๋กœ์ด๋ฏ€๋กœ ์—…๋ฐ์ดํŠธ ํ•ด์ค€๋‹ค.

2. ์œ„ ๊ณผ์ •์„ ๋…ธ๋“œ์˜ ๊ฐฏ์ˆ˜ - 1๋ฒˆ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋ฉด ์ตœ๋‹จ ๊ฒฝ๋กœ๊ฐ€ ๊ฐฑ์‹ ๋œ๋‹ค.

3. 2๋ฒˆ๊นŒ์ง€ ๊ณผ์ •์„ ๋งˆ์น˜๊ณ  ํ•œ๋ฒˆ ๋” ์ตœ๋‹จ ๊ฒฝ๋กœ๊ฐ€ ๊ฐฑ์‹ ๋œ๋‹ค๋ฉด ์Œ์ˆ˜ ์‚ฌ์ดํด์ด ์กด์žฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

 

์ฐธ๊ณ 

https://10000cow.tistory.com/entry/%EB%B2%A8%EB%A7%8C-%ED%8F%AC%EB%93%9C-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98-%ED%95%9C-%EC%82%B4%EB%8F%84-%EC%9D%B4%ED%95%B4%ED%95%98%EB%8A%94-%EB%B2%A8%EB%A7%8C-%ED%8F%AC%EB%93%9C-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98Bellman-Ford-Algorithm

 

[๋ฒจ๋งŒ-ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜] ํ•œ ์‚ด๋„ ์ดํ•ดํ•˜๋Š” ๋ฒจ๋งŒ-ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜(Bellman-Ford Algorithm)

๋ฒจ๋งŒ-ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ž€? ํ•œ ๋…ธ๋“œ์—์„œ ๋‹ค๋ฅธ ๋…ธ๋“œ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ์Œ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ–๋Š” edge(๊ฐ„์„ )์ด ์žˆ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ์ฐจ์ด๋Š” ๋ฌด์—‡

10000cow.tistory.com