Skip to content

Commit 5fd7496

Browse files
authored
Update FloydWarshallSolver.java
1 parent d71a27e commit 5fd7496

File tree

1 file changed

+90
-92
lines changed

1 file changed

+90
-92
lines changed

com/williamfiset/algorithms/graphtheory/FloydWarshallSolver.java

Lines changed: 90 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -20,98 +20,6 @@
2020

2121
public class FloydWarshallSolver {
2222

23-
/* Example usage. */
24-
25-
// Creates a graph with n nodes. The adjacency matrix is constructed
26-
// such that the value of going from a node to itself is 0.
27-
public static double[][] createGraph(int n) {
28-
double[][] matrix = new double[n][n];
29-
for (int i = 0; i < n; i++) {
30-
java.util.Arrays.fill(matrix[i], POSITIVE_INFINITY);
31-
matrix[i][i] = 0;
32-
}
33-
return matrix;
34-
}
35-
36-
public static void main(String[] args) {
37-
// Construct graph.
38-
int n = 7;
39-
double[][] m = createGraph(n);
40-
41-
// Add some edge values.
42-
m[0][1] = 2;
43-
m[0][2] = 5;
44-
m[0][6] = 10;
45-
m[1][2] = 2;
46-
m[1][4] = 11;
47-
m[2][6] = 2;
48-
m[6][5] = 11;
49-
m[4][5] = 1;
50-
m[5][4] = -2;
51-
52-
FloydWarshallSolver solver = new FloydWarshallSolver(m);
53-
double[][] dist = solver.getApspMatrix();
54-
55-
for (int i = 0; i < n; i++)
56-
for (int j = 0; j < n; j++)
57-
System.out.printf("This shortest path from node %d to node %d is %.3f\n", i, j, dist[i][j]);
58-
59-
// Prints:
60-
// This shortest path from node 0 to node 0 is 0.000
61-
// This shortest path from node 0 to node 1 is 2.000
62-
// This shortest path from node 0 to node 2 is 4.000
63-
// This shortest path from node 0 to node 3 is Infinity
64-
// This shortest path from node 0 to node 4 is -Infinity
65-
// This shortest path from node 0 to node 5 is -Infinity
66-
// This shortest path from node 0 to node 6 is 6.000
67-
// This shortest path from node 1 to node 0 is Infinity
68-
// This shortest path from node 1 to node 1 is 0.000
69-
// This shortest path from node 1 to node 2 is 2.000
70-
// This shortest path from node 1 to node 3 is Infinity
71-
// ...
72-
73-
System.out.println();
74-
75-
// Reconstructs the shortest paths from all nodes to every other nodes.
76-
for (int i = 0; i < n; i++) {
77-
for (int j = 0; j < n; j++) {
78-
List<Integer> path = solver.reconstructShortestPath(i, j);
79-
String str;
80-
if (path == null) {
81-
str = "HAS AN ∞ NUMBER OF SOLUTIONS! (negative cycle case)";
82-
} else if (path.size() == 0) {
83-
str = String.format("DOES NOT EXIST (node %d doesn't reach node %d)", i, j);
84-
} else {
85-
str = String.join(" -> ", path.stream()
86-
.map(Object::toString)
87-
.collect(java.util.stream.Collectors.toList()));
88-
str = "is: [" + str + "]";
89-
}
90-
91-
System.out.printf("The shortest path from node %d to node %d %s\n", i, j, str);
92-
}
93-
}
94-
95-
// Prints:
96-
// The shortest path from node 0 to node 0 is: [0]
97-
// The shortest path from node 0 to node 1 is: [0 -> 1]
98-
// The shortest path from node 0 to node 2 is: [0 -> 1 -> 2]
99-
// The shortest path from node 0 to node 3 DOES NOT EXIST (node 0 doesn't reach node 3)
100-
// The shortest path from node 0 to node 4 HAS AN ∞ NUMBER OF SOLUTIONS! (negative cycle case)
101-
// The shortest path from node 0 to node 5 HAS AN ∞ NUMBER OF SOLUTIONS! (negative cycle case)
102-
// The shortest path from node 0 to node 6 is: [0 -> 1 -> 2 -> 6]
103-
// The shortest path from node 1 to node 0 DOES NOT EXIST (node 1 doesn't reach node 0)
104-
// The shortest path from node 1 to node 1 is: [1]
105-
// The shortest path from node 1 to node 2 is: [1 -> 2]
106-
// The shortest path from node 1 to node 3 DOES NOT EXIST (node 1 doesn't reach node 3)
107-
// The shortest path from node 1 to node 4 HAS AN ∞ NUMBER OF SOLUTIONS! (negative cycle case)
108-
// The shortest path from node 1 to node 5 HAS AN ∞ NUMBER OF SOLUTIONS! (negative cycle case)
109-
// The shortest path from node 1 to node 6 is: [1 -> 2 -> 6]
110-
// The shortest path from node 2 to node 0 DOES NOT EXIST (node 2 doesn't reach node 0)
111-
// ...
112-
113-
}
114-
11523
private int n;
11624
private boolean solved;
11725
private double[][] dp;
@@ -204,6 +112,96 @@ public List<Integer> reconstructShortestPath(int start, int end) {
204112
return path;
205113
}
206114

115+
/* Example usage. */
116+
117+
// Creates a graph with n nodes. The adjacency matrix is constructed
118+
// such that the value of going from a node to itself is 0.
119+
public static double[][] createGraph(int n) {
120+
double[][] matrix = new double[n][n];
121+
for (int i = 0; i < n; i++) {
122+
java.util.Arrays.fill(matrix[i], POSITIVE_INFINITY);
123+
matrix[i][i] = 0;
124+
}
125+
return matrix;
126+
}
127+
128+
public static void main(String[] args) {
129+
// Construct graph.
130+
int n = 7;
131+
double[][] m = createGraph(n);
132+
133+
// Add some edge values.
134+
m[0][1] = 2;
135+
m[0][2] = 5;
136+
m[0][6] = 10;
137+
m[1][2] = 2;
138+
m[1][4] = 11;
139+
m[2][6] = 2;
140+
m[6][5] = 11;
141+
m[4][5] = 1;
142+
m[5][4] = -2;
143+
144+
FloydWarshallSolver solver = new FloydWarshallSolver(m);
145+
double[][] dist = solver.getApspMatrix();
146+
147+
for (int i = 0; i < n; i++)
148+
for (int j = 0; j < n; j++)
149+
System.out.printf("This shortest path from node %d to node %d is %.3f\n", i, j, dist[i][j]);
150+
151+
// Prints:
152+
// This shortest path from node 0 to node 0 is 0.000
153+
// This shortest path from node 0 to node 1 is 2.000
154+
// This shortest path from node 0 to node 2 is 4.000
155+
// This shortest path from node 0 to node 3 is Infinity
156+
// This shortest path from node 0 to node 4 is -Infinity
157+
// This shortest path from node 0 to node 5 is -Infinity
158+
// This shortest path from node 0 to node 6 is 6.000
159+
// This shortest path from node 1 to node 0 is Infinity
160+
// This shortest path from node 1 to node 1 is 0.000
161+
// This shortest path from node 1 to node 2 is 2.000
162+
// This shortest path from node 1 to node 3 is Infinity
163+
// ...
164+
165+
System.out.println();
166+
167+
// Reconstructs the shortest paths from all nodes to every other nodes.
168+
for (int i = 0; i < n; i++) {
169+
for (int j = 0; j < n; j++) {
170+
List<Integer> path = solver.reconstructShortestPath(i, j);
171+
String str;
172+
if (path == null) {
173+
str = "HAS AN ∞ NUMBER OF SOLUTIONS! (negative cycle case)";
174+
} else if (path.size() == 0) {
175+
str = String.format("DOES NOT EXIST (node %d doesn't reach node %d)", i, j);
176+
} else {
177+
str = String.join(" -> ", path.stream()
178+
.map(Object::toString)
179+
.collect(java.util.stream.Collectors.toList()));
180+
str = "is: [" + str + "]";
181+
}
182+
183+
System.out.printf("The shortest path from node %d to node %d %s\n", i, j, str);
184+
}
185+
}
186+
187+
// Prints:
188+
// The shortest path from node 0 to node 0 is: [0]
189+
// The shortest path from node 0 to node 1 is: [0 -> 1]
190+
// The shortest path from node 0 to node 2 is: [0 -> 1 -> 2]
191+
// The shortest path from node 0 to node 3 DOES NOT EXIST (node 0 doesn't reach node 3)
192+
// The shortest path from node 0 to node 4 HAS AN ∞ NUMBER OF SOLUTIONS! (negative cycle case)
193+
// The shortest path from node 0 to node 5 HAS AN ∞ NUMBER OF SOLUTIONS! (negative cycle case)
194+
// The shortest path from node 0 to node 6 is: [0 -> 1 -> 2 -> 6]
195+
// The shortest path from node 1 to node 0 DOES NOT EXIST (node 1 doesn't reach node 0)
196+
// The shortest path from node 1 to node 1 is: [1]
197+
// The shortest path from node 1 to node 2 is: [1 -> 2]
198+
// The shortest path from node 1 to node 3 DOES NOT EXIST (node 1 doesn't reach node 3)
199+
// The shortest path from node 1 to node 4 HAS AN ∞ NUMBER OF SOLUTIONS! (negative cycle case)
200+
// The shortest path from node 1 to node 5 HAS AN ∞ NUMBER OF SOLUTIONS! (negative cycle case)
201+
// The shortest path from node 1 to node 6 is: [1 -> 2 -> 6]
202+
// The shortest path from node 2 to node 0 DOES NOT EXIST (node 2 doesn't reach node 0)
203+
// ...
204+
}
207205
}
208206

209207

0 commit comments

Comments
 (0)