์ต๋จ๊ฒฝ๋ก ์๊ณ ๋ฆฌ์ฆ
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๋ฒ๊น์ง ๊ณผ์ ์ ๋ง์น๊ณ ํ๋ฒ ๋ ์ต๋จ ๊ฒฝ๋ก๊ฐ ๊ฐฑ์ ๋๋ค๋ฉด ์์ ์ฌ์ดํด์ด ์กด์ฌํ๋ ๊ฒ์ด๋ค.
์ฐธ๊ณ
[๋ฒจ๋ง-ํฌ๋ ์๊ณ ๋ฆฌ์ฆ] ํ ์ด๋ ์ดํดํ๋ ๋ฒจ๋ง-ํฌ๋ ์๊ณ ๋ฆฌ์ฆ(Bellman-Ford Algorithm)
๋ฒจ๋ง-ํฌ๋ ์๊ณ ๋ฆฌ์ฆ์ด๋? ํ ๋ ธ๋์์ ๋ค๋ฅธ ๋ ธ๋๊น์ง์ ์ต๋จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ ์๊ณ ๋ฆฌ์ฆ์ด๋ค. ์์ ๊ฐ์ค์น๋ฅผ ๊ฐ๋ edge(๊ฐ์ )์ด ์์ ๋ ์ฌ์ฉํ๋ ์๊ณ ๋ฆฌ์ฆ์ด๋ค. ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ๊ณผ ์ฐจ์ด๋ ๋ฌด์
10000cow.tistory.com
'์๊ณ ๋ฆฌ์ฆ > ์๊ณ ๋ฆฌ์ฆ ์ข ๋ฅ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[MST] ์ต์ ์คํจ๋ ํธ๋ฆฌ - ํฌ๋ฃจ์ค์นผ(Kruskal), ํ๋ฆผ(Prim) (0) | 2024.07.11 |
---|---|
[์ด๋ถ ํ์] ์ด๋ถ ํ์(Binary Search) ์ด๋ ? (0) | 2024.02.01 |