-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBellmanFord.java
More file actions
50 lines (42 loc) · 1.58 KB
/
BellmanFord.java
File metadata and controls
50 lines (42 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package graph_algorithms;
import data_structures.LinkedList;
public class BellmanFord extends ShortestPath {
@SuppressWarnings("DuplicatedCode")
public static void main(String[] args) {
// Example:
Graph graph = new AdjacencyListGraph(6, true);
graph.addEdge(0, 1, 2);
graph.addEdge(0, 4, -1);
graph.addEdge(1, 2, -3);
graph.addEdge(2, 3, -4);
graph.addEdge(3, 1, 8);
graph.addEdge(3, 4, 1);
graph.addEdge(3, 5, 5);
graph.addEdge(4, 5, 3);
System.out.println(bellmanFord(graph, 0, 5)); // [0, 1, 2, 3, 4, 5]
}
public static LinkedList<Integer> bellmanFord(Graph graph, int from, int to) {
double[] distance = new double[graph.getVertexCount()];
int[] predecessor = new int[graph.getVertexCount()];
for (int i = 0; i < graph.getVertexCount(); ++i) {
distance[i] = Double.POSITIVE_INFINITY;
}
distance[from] = 0;
for (int i = 0; i < graph.getVertexCount(); ++i) {
graph.forEachEdge((Integer u, Integer v) -> {
double d1 = distance[v];
double d2 = distance[u] + graph.cost(u, v);
if (d2 < d1) {
distance[v] = d2;
predecessor[v] = u;
}
});
}
graph.forEachEdge((Integer u, Integer v) -> {
if (distance[v] > distance[u] + graph.cost(u, v)) {
throw new NegativeCycleException();
}
});
return ShortestPath.backtrack(predecessor, from, to);
}
}