Class FloydWarshall
- Object
-
- edu.princeton.cs.algs4.FloydWarshall
-
public class FloydWarshall extends Object
TheFloydWarshallclass 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, Kevin Wayne
-
-
Constructor Summary
Constructors Constructor Description FloydWarshall(AdjMatrixEdgeWeightedDigraph G)Computes a shortest paths tree from each vertex to every other vertex in the edge-weighted digraphG.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description doubledist(int s, int t)Returns the length of a shortest path from vertexsto vertext.booleanhasNegativeCycle()Is there a negative cycle?booleanhasPath(int s, int t)Is there a path from the vertexsto vertext?static voidmain(String[] args)Unit tests theFloydWarshalldata type.Iterable<DirectedEdge>negativeCycle()Returns a negative cycle, ornullif there is no such cycle.Iterable<DirectedEdge>path(int s, int t)Returns a shortest path from vertexsto vertext.
-
-
-
Constructor Detail
-
FloydWarshall
public FloydWarshall(AdjMatrixEdgeWeightedDigraph G)
Computes a shortest paths tree from each vertex to every other vertex in the edge-weighted digraphG. If no such shortest path exists for some pair of vertices, it computes a negative cycle.- Parameters:
G- the edge-weighted digraph
-
-
Method Detail
-
hasNegativeCycle
public boolean hasNegativeCycle()
Is there a negative cycle?- Returns:
trueif there is a negative cycle, andfalseotherwise
-
negativeCycle
public Iterable<DirectedEdge> negativeCycle()
Returns a negative cycle, ornullif there is no such cycle.- Returns:
- a negative cycle as an iterable of edges,
or
nullif there is no such cycle
-
hasPath
public boolean hasPath(int s, int t)Is there a path from the vertexsto vertext?- Parameters:
s- the source vertext- the destination vertex- Returns:
trueif there is a path from vertexsto vertext, andfalseotherwise- Throws:
IllegalArgumentException- unless0 <= s < VIllegalArgumentException- unless0 <= t < V
-
dist
public double dist(int s, int t)Returns the length of a shortest path from vertexsto vertext.- Parameters:
s- the source vertext- the destination vertex- Returns:
- the length of a shortest path from vertex
sto vertext;Double.POSITIVE_INFINITYif no such path - Throws:
UnsupportedOperationException- if there is a negative cost cycleIllegalArgumentException- unless0 <= v < V
-
path
public Iterable<DirectedEdge> path(int s, int t)
Returns a shortest path from vertexsto vertext.- Parameters:
s- the source vertext- the destination vertex- Returns:
- a shortest path from vertex
sto vertextas an iterable of edges, andnullif no such path - Throws:
UnsupportedOperationException- if there is a negative cost cycleIllegalArgumentException- unless0 <= v < V
-
main
public static void main(String[] args)
Unit tests theFloydWarshalldata type.- Parameters:
args- the command-line arguments
-
-