org.jgrapht.alg.shortestpath
Class FloydWarshallShortestPaths<V,E>
- java.lang.Object
-
- org.jgrapht.alg.shortestpath.FloydWarshallShortestPaths<V,E>
-
- Type Parameters:
V- the graph vertex typeE- the graph edge type
- All Implemented Interfaces:
- ShortestPathAlgorithm<V,E>
public class FloydWarshallShortestPaths<V,E> extends java.lang.ObjectThe Floyd-Warshall algorithm.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.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jgrapht.alg.interfaces.ShortestPathAlgorithm
ShortestPathAlgorithm.SingleSourcePaths<V,E>
-
-
Constructor Summary
Constructors Constructor and Description FloydWarshallShortestPaths(Graph<V,E> graph)Create a new instance of the Floyd-Warshall all-pairs shortest path algorithm.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description doublegetDiameter()Compute the diameter of the graph.VgetFirstHop(V a, V b)Returns the first hop, i.e., the second node on the shortest path from a to b.VgetLastHop(V a, V b)Returns the last hop, i.e., the second to last node on the shortest path from a to b.GraphPath<V,E>getPath(V a, V b)Get a shortest path from a source vertex to a sink vertex.ShortestPathAlgorithm.SingleSourcePaths<V,E>getPaths(V source)Compute all shortest paths starting from a single source vertex.doublegetPathWeight(V source, V sink)Get the weight of the shortest path from a source vertex to a sink vertex.intgetShortestPathsCount()Get the total number of shortest paths.
-
-
-
Method Detail
-
getShortestPathsCount
public int getShortestPathsCount()
Get the total number of shortest paths. Does not count the paths from a vertex to itself.- Returns:
- total number of shortest paths
-
getDiameter
public double getDiameter()
Compute the diameter of the graph.- Returns:
- the diameter (longest of all the shortest paths) computed for the graph. If the graph
contains no vertices, return
Double.NaN. If there is no path between any two vertices, returnDouble.POSITIVE_INFINITY.
-
getPath
public GraphPath<V,E> getPath(V a, V b)
Get a shortest path from a source vertex to a sink vertex.- Parameters:
a- the source vertexb- the target vertex- Returns:
- a shortest path or null if no path exists
-
getPathWeight
public double getPathWeight(V source, V sink)
Get the weight of the shortest path from a source vertex to a sink vertex. ReturnsDouble.POSITIVE_INFINITYif no path exists.- Specified by:
getPathWeightin interfaceShortestPathAlgorithm<V,E>- Parameters:
source- the source vertexsink- the sink vertex- Returns:
- the weight of the shortest path from a source vertex to a sink vertex, or
Double.POSITIVE_INFINITYif no path exists
-
getPaths
public ShortestPathAlgorithm.SingleSourcePaths<V,E> getPaths(V source)
Compute all shortest paths starting from a single source vertex.- Specified by:
getPathsin interfaceShortestPathAlgorithm<V,E>- Parameters:
source- the source vertex- Returns:
- the shortest 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 callinggetPath(Object, Object)and then reading the first vertex.- Parameters:
a- source vertexb- 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 callinggetPath(Object, Object)and then reading the vertex. The first invocation of this method populates a last hop matrix.- Parameters:
a- source vertexb- target vertex- Returns:
- last hop on the shortest path from a to b, or null when there exists no path from a to b.
-
-
DataMelt 3.0 © DataMelt by jWork.ORG