-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathFloydWarshallSolver.java
More file actions
179 lines (157 loc) · 5.86 KB
/
Copy pathFloydWarshallSolver.java
File metadata and controls
179 lines (157 loc) · 5.86 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* Implementation of the Floyd-Warshall algorithm to find all pairs of shortest paths between nodes
* in a graph. Also demonstrates how to detect negative cycles and reconstruct the shortest path.
*
* <p>Time: O(V^3)
*
* <p>Space: O(V^2)
*
* @author Micah Stairs, William Fiset
*/
package com.williamfiset.algorithms.graphtheory;
import static java.lang.Double.NEGATIVE_INFINITY;
import static java.lang.Double.POSITIVE_INFINITY;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class FloydWarshallSolver {
private final int n;
private boolean solved;
private double[][] dp;
private Integer[][] next;
private static final int REACHES_NEGATIVE_CYCLE = -1;
/**
* Creates a Floyd-Warshall solver from an adjacency matrix with edge weights between nodes, where
* POSITIVE_INFINITY indicates that two nodes are not connected.
*
* <p>NOTE: Usually the diagonal of the adjacency matrix is all zeros (i.e. matrix[i][i] = 0 for
* all i) since there is typically no cost to go from a node to itself, but this may depend on the
* graph and the problem being solved.
*
* @param matrix an n x n adjacency matrix of edge weights.
* @throws IllegalArgumentException if the matrix is null or empty.
*/
public FloydWarshallSolver(double[][] matrix) {
if (matrix == null || matrix.length == 0)
throw new IllegalArgumentException("Matrix cannot be null or empty.");
n = matrix.length;
dp = new double[n][n];
next = new Integer[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] != POSITIVE_INFINITY)
next[i][j] = j;
dp[i][j] = matrix[i][j];
}
}
}
/**
* Runs Floyd-Warshall to compute the shortest distance between every pair of nodes.
*
* @return the solved All Pairs Shortest Path (APSP) matrix.
*/
public double[][] getApspMatrix() {
solve();
return dp;
}
/** Executes the Floyd-Warshall algorithm. */
public void solve() {
if (solved)
return;
// Compute all pairs shortest paths.
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (dp[i][k] + dp[k][j] < dp[i][j]) {
dp[i][j] = dp[i][k] + dp[k][j];
next[i][j] = next[i][k];
}
// Identify negative cycles by propagating NEGATIVE_INFINITY
// to every edge that is part of or reaches into a negative cycle.
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (dp[i][k] != POSITIVE_INFINITY && dp[k][j] != POSITIVE_INFINITY && dp[k][k] < 0) {
dp[i][j] = NEGATIVE_INFINITY;
next[i][j] = REACHES_NEGATIVE_CYCLE;
}
solved = true;
}
/**
* Reconstructs the shortest path (of nodes) from 'start' to 'end' inclusive.
*
* @return an array of node indexes of the shortest path from 'start' to 'end'. If 'start' and
* 'end' are not connected return an empty list. If the shortest path from 'start' to 'end'
* reaches a negative cycle return null.
*/
public List<Integer> reconstructShortestPath(int start, int end) {
solve();
List<Integer> path = new ArrayList<>();
if (dp[start][end] == POSITIVE_INFINITY)
return path;
int at = start;
for (; at != end; at = next[at][end]) {
if (at == REACHES_NEGATIVE_CYCLE)
return null;
path.add(at);
}
if (next[at][end] == REACHES_NEGATIVE_CYCLE)
return null;
path.add(end);
return path;
}
/** Creates an n x n adjacency matrix initialized with POSITIVE_INFINITY and zero diagonal. */
public static double[][] createGraph(int n) {
double[][] matrix = new double[n][n];
for (int i = 0; i < n; i++) {
Arrays.fill(matrix[i], POSITIVE_INFINITY);
matrix[i][i] = 0;
}
return matrix;
}
public static void main(String[] args) {
exampleWithNegativeCycle();
System.out.println();
exampleSimpleGraph();
}
// Example 1: 4-node graph with a negative cycle between nodes 2 and 3.
private static void exampleWithNegativeCycle() {
int n = 4;
double[][] m = createGraph(n);
m[0][1] = 4;
m[1][2] = 1;
m[2][3] = 2;
m[3][2] = -5; // Creates negative cycle: 2 -> 3 -> 2 (net cost -3).
FloydWarshallSolver solver = new FloydWarshallSolver(m);
double[][] dist = solver.getApspMatrix();
System.out.println("=== Example 1: Negative cycle ===");
System.out.printf("dist(0, 1) = %.0f\n", dist[0][1]); // 4
System.out.printf("dist(0, 2) = %.0f\n", dist[0][2]); // -Infinity (reaches negative cycle)
System.out.printf("path(0, 2) = %s\n", formatPath(solver.reconstructShortestPath(0, 2), 0, 2));
System.out.printf("path(0, 1) = %s\n", formatPath(solver.reconstructShortestPath(0, 1), 0, 1));
}
// Example 2: 4-node directed graph with no negative cycles.
private static void exampleSimpleGraph() {
int n = 4;
double[][] m = createGraph(n);
m[0][1] = 1;
m[1][2] = 3;
m[1][3] = 10;
m[2][3] = 2;
FloydWarshallSolver solver = new FloydWarshallSolver(m);
double[][] dist = solver.getApspMatrix();
System.out.println("=== Example 2: Simple directed graph ===");
// Shortest distance from 0 to 3 is 6 (0 -> 1 -> 2 -> 3), not 11 (0 -> 1 -> 3).
System.out.printf("dist(0, 3) = %.0f\n", dist[0][3]);
System.out.printf("path(0, 3) = %s\n", formatPath(solver.reconstructShortestPath(0, 3), 0, 3));
System.out.printf("path(3, 0) = %s\n", formatPath(solver.reconstructShortestPath(3, 0), 3, 0));
}
private static String formatPath(List<Integer> path, int start, int end) {
if (path == null)
return "NEGATIVE CYCLE";
if (path.isEmpty())
return String.format("NO PATH (%d doesn't reach %d)", start, end);
return path.stream().map(Object::toString).collect(Collectors.joining(" -> "));
}
}