-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloydWarshall.java
More file actions
72 lines (63 loc) · 2.4 KB
/
FloydWarshall.java
File metadata and controls
72 lines (63 loc) · 2.4 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package graph_algorithms;
public class FloydWarshall 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(java.util.Arrays.deepToString(floydWarshall(graph))); // TODO: Verify that the output is correct
System.out.println(java.util.Arrays.deepToString(transitiveClosure(graph))); // TODO: Verify that the output is correct
}
public static double[][] floydWarshall(Graph graph) {
int n = graph.getVertexCount();
double[][][] table = new double[n + 1][n][n];
for (int u = 0; u < n; ++u) {
table[0][u][u] = 0;
}
for (int u = 0; u < n; ++u) {
for (int v = 0; v < n; ++v) {
table[0][u][v] = graph.cost(u, v);
}
}
for (int i = 1; i < n; ++i) {
for (int u = 0; u < n; ++u) {
for (int v = 0; v < n; ++v) {
double d1 = table[i - 1][u][v];
double d2 = table[i - 1][u][i] + table[i - 1][i][v];
if (u == v && Math.min(d1, d2) != 0) {
throw new NegativeCycleException();
}
table[i][u][v] = Math.min(d2, d1);
}
}
}
return table[n - 1]; // TODO: Compute actual shortest paths, not just their lengths
}
public static boolean[][] transitiveClosure(Graph graph) {
int n = graph.getVertexCount();
boolean[][][] table = new boolean[n + 1][n][n];
for (int u = 0; u < n; ++u) {
table[0][u][u] = true;
}
for (int u = 0; u < n; ++u) {
for (int v = 0; v < n; ++v) {
table[0][u][v] = graph.outEdges(u).contains(v);
}
}
for (int i = 1; i < n; ++i) {
for (int u = 0; u < n; ++u) {
for (int v = 0; v < n; ++v) {
table[i][u][v] = table[i - 1][u][v] || (table[i - 1][u][i] && table[i - 1][i][v]);
}
}
}
return table[n - 1];
}
}