org.jgrapht.alg

Class FloydWarshallShortestPaths<V,E>

• Type Parameters:
V - the graph vertex type
E - the graph edge type

public class FloydWarshallShortestPaths<V,E>
extends Object
The Floyd-Warshall algorithm finds all shortest paths (all n^2 of them) in O(n^3) time. It can also calculate the graph diameter. Note that during construction time, no computations are performed! All computations are performed the first time one of the member methods of this class is invoked. The results are stored, so all subsequent calls to the same method are computationally efficient. Warning: This code has not been tested (and probably doesn't work) on multi-graphs. Code should be updated to work properly on multi-graphs.
Author:
Tom Larkworthy, Soren Davidsen (soren@tanesha.net), Joris Kinable
• Constructor Detail

• FloydWarshallShortestPaths

public FloydWarshallShortestPaths(Graph<V,E> graph)
Create a new instance of the Floyd-Warshall all-pairs shortest path algorithm.
Parameters:
graph - the input graph
• Method Detail

• getGraph

public Graph<V,E> getGraph()
Returns:
the graph on which this algorithm operates
• getShortestPathsCount

public int getShortestPathsCount()
Returns:
total number of shortest paths
• shortestDistance

public double shortestDistance(V a,
V b)
Get the length of a shortest path.
Parameters:
a - first vertex
b - second vertex
Returns:
shortest distance between a and b
• getDiameter

public double getDiameter()
Returns:
the diameter (longest of all the shortest paths) computed for the graph. If the graph is vertexless, return 0.0.
• getShortestPath

public GraphPath<V,E> getShortestPath(V a,
V b)
Get the shortest path between two vertices.
Parameters:
a - from vertex
b - to vertex
Returns:
the path, or null if none found
• getShortestPathAsVertexList

public List<V> getShortestPathAsVertexList(V a,
V b)
Get the shortest path between two vertices as a list of vertices.
Parameters:
a - from vertex
b - to vertex
Returns:
the path, or null if none found
• getShortestPaths

public List<GraphPath<V,E>> getShortestPaths(V v)
Get shortest paths from a vertex to all other vertices in the graph.
Parameters:
v - the originating vertex
Returns:
List of paths
• getShortestPaths

public List<GraphPath<V,E>> getShortestPaths()
Get all shortest paths in the graph.
Returns:
List of paths
• getFirstHop

public V getFirstHop(V a,
V b)
Returns the first hop, i.e., the second node on the shortest path from a to b. Lookup time is O(1). If the shortest path from a to b is a,c,d,e,b, this method returns c. If the next invocation would query the first hop on the shortest path from c to b, vertex d would be returned, etc. This method is computationally cheaper than getShortestPathAsVertexList(a,b).get(0);
Parameters:
a - source vertex
b - target vertex
Returns:
next hop on the shortest path from a to b, or null when there exists no path from a to b.
• getLastHop

public V getLastHop(V a,
V b)
Returns the last hop, i.e., the second to last node on the shortest path from a to b. Lookup time is O(1). If the shortest path from a to b is a,c,d,e,b, this method returns e. If the next invocation would query the next hop on the shortest path from c to e, vertex d would be returned, etc. This method is computationally cheaper than getShortestPathAsVertexList(a,b).get(shortestPathListSize-1);. The first invocation of this method populates a last hop matrix.
Parameters:
a - source vertex
b - target vertex
Returns:
last hop on the shortest path from a to b, or null when there exists no path from a to b.