public class FloydWarshall extends Object
This implementation uses the Floyd-Warshall algorithm. The constructor takes time proportional to V3 in the worst case, where V is the number of vertices. Afterwards, the dist(), hasPath(), and hasNegativeCycle() methods take constant time; the path() and negativeCycle() method takes time proportional to the number of edges returned.
For additional documentation, see Section 4.4 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
| Constructor and Description |
|---|
FloydWarshall(AdjMatrixEdgeWeightedDigraph G)
Computes a shortest paths tree from each vertex to to every other vertex in
the edge-weighted digraph G.
|
| Modifier and Type | Method and Description |
|---|---|
double |
dist(int s,
int t)
Returns the length of a shortest path from vertex s to vertex t.
|
boolean |
hasNegativeCycle()
Is there a negative cycle?
|
boolean |
hasPath(int s,
int t)
Is there a path from the vertex s to vertex t?
|
static void |
main(String[] args)
Unit tests the FloydWarshall data type.
|
Iterable<DirectedEdge> |
negativeCycle()
Returns a negative cycle, or null if there is no such cycle.
|
Iterable<DirectedEdge> |
path(int s,
int t)
Returns a shortest path from vertex s to vertex t.
|
public FloydWarshall(AdjMatrixEdgeWeightedDigraph G)
G - the edge-weighted digraphpublic boolean hasNegativeCycle()
public Iterable<DirectedEdge> negativeCycle()
public boolean hasPath(int s,
int t)
s - the source vertext - the destination vertexpublic double dist(int s,
int t)
s - the source vertext - the destination vertexUnsupportedOperationException - if there is a negative cost cyclepublic Iterable<DirectedEdge> path(int s, int t)
s - the source vertext - the destination vertexUnsupportedOperationException - if there is a negative cost cyclepublic static void main(String[] args)
Copyright © 2014. All Rights Reserved.