/****************************************************************************** * Compilation: javac FloydWarshall.java * Execution: java FloydWarshall V E * Dependencies: AdjMatrixEdgeWeightedDigraph.java * * Floyd-Warshall all-pairs shortest path algorithm. * * % java FloydWarshall 100 500 * * Should check for negative cycles during triple loop; otherwise * intermediate numbers can get exponentially large. * Reference: "The Floyd-Warshall algorithm on graphs with negative cycles" * by Stefan Hougardy * ******************************************************************************/ /** * The {@code FloydWarshall} class represents a data type for solving the * all-pairs shortest paths problem in edge-weighted digraphs with * no negative cycles. * The edge weights can be positive, negative, or zero. * This class finds either a shortest path between every pair of vertices * or a negative cycle. *
* This implementation uses the Floyd-Warshall algorithm. * The constructor takes Θ(V3) time, * where V is the number of vertices. * Each instance method takes Θ(1) time. * It uses Θ(V2) extra space * (not including the edge-weighted digraph). *
* This correctly computes shortest paths if all arithmetic performed is * without floating-point rounding error or arithmetic overflow. * This is the case if all edge weights are integers and if none of the * intermediate results exceeds 252. Since all intermediate * results are sums of edge weights, they are bounded by V C, * where V is the number of vertices and C is the maximum * absolute value of any edge weight. *
* For additional documentation,
* see Section 4.4 of
* Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class FloydWarshall {
private boolean hasNegativeCycle; // is there a negative cycle?
private double[][] distTo; // distTo[v][w] = length of shortest v->w path
private DirectedEdge[][] edgeTo; // edgeTo[v][w] = last edge on shortest v->w path
/**
* Computes a shortest paths tree from each vertex to every other vertex in
* the edge-weighted digraph {@code G}. If no such shortest path exists for
* some pair of vertices, it computes a negative cycle.
* @param G the edge-weighted digraph
*/
public FloydWarshall(AdjMatrixEdgeWeightedDigraph G) {
int V = G.V();
distTo = new double[V][V];
edgeTo = new DirectedEdge[V][V];
// initialize distances to infinity
for (int v = 0; v < V; v++) {
for (int w = 0; w < V; w++) {
distTo[v][w] = Double.POSITIVE_INFINITY;
}
}
// initialize distances using edge-weighted digraph's
for (int v = 0; v < G.V(); v++) {
for (DirectedEdge e : G.adj(v)) {
distTo[e.from()][e.to()] = e.weight();
edgeTo[e.from()][e.to()] = e;
}
// in case of self-loops
if (distTo[v][v] >= 0.0) {
distTo[v][v] = 0.0;
edgeTo[v][v] = null;
}
}
// Floyd-Warshall updates
for (int i = 0; i < V; i++) {
// compute shortest paths using only 0, 1, ..., i as intermediate vertices
for (int v = 0; v < V; v++) {
if (edgeTo[v][i] == null) continue; // optimization
for (int w = 0; w < V; w++) {
if (distTo[v][w] > distTo[v][i] + distTo[i][w]) {
distTo[v][w] = distTo[v][i] + distTo[i][w];
edgeTo[v][w] = edgeTo[i][w];
}
}
// check for negative cycle
if (distTo[v][v] < 0.0) {
hasNegativeCycle = true;
return;
}
}
}
assert check(G);
}
/**
* Is there a negative cycle?
* @return {@code true} if there is a negative cycle, and {@code false} otherwise
*/
public boolean hasNegativeCycle() {
return hasNegativeCycle;
}
/**
* Returns a negative cycle, or {@code null} if there is no such cycle.
* @return a negative cycle as an iterable of edges,
* or {@code null} if there is no such cycle
*/
public Iterable