java.lang.Object
org.jgrapht.alg.shortestpath.BaseShortestPathAlgorithm<V,E>
org.jgrapht.alg.shortestpath.FloydWarshallShortestPaths<V,E>
- Type Parameters:
V
- the graph vertex typeE
- the graph edge type
- All Implemented Interfaces:
ShortestPathAlgorithm<V,
E>
The Floyd-Warshall algorithm.
The Floyd-Warshall algorithm finds all shortest paths (all $n^2$ of them) in $O(n^3)$ time. 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 ClassesModifier and TypeClassDescription(package private) class
Nested classes/interfaces inherited from interface org.jgrapht.alg.interfaces.ShortestPathAlgorithm
ShortestPathAlgorithm.SingleSourcePaths<V,
E> -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Object[][]
private double[][]
private Object[][]
private final int
private final int
Fields inherited from class org.jgrapht.alg.shortestpath.BaseShortestPathAlgorithm
graph, GRAPH_CONTAINS_A_NEGATIVE_WEIGHT_CYCLE, GRAPH_MUST_CONTAIN_THE_SINK_VERTEX, GRAPH_MUST_CONTAIN_THE_SOURCE_VERTEX
-
Constructor Summary
ConstructorsConstructorDescriptionFloydWarshallShortestPaths
(Graph<V, E> graph) Create a new instance of the Floyd-Warshall all-pairs shortest path algorithm. -
Method Summary
Modifier and TypeMethodDescriptiongetFirstHop
(V a, V b) Returns the first hop, i.e., the second node on the shortest path from $a$ to $b$.getLastHop
(V a, V b) Returns the last hop, i.e., the second to last node on the shortest path from $a$ to $b$.Get a shortest path from a source vertex to a sink vertex.Compute all shortest paths starting from a single source vertex.double
getPathWeight
(V source, V sink) Get the weight of the shortest path from a source vertex to a sink vertex.int
Get the total number of shortest paths.private void
Calculates the matrix of all shortest paths, but does not populate the last hops matrix.private void
Populate the last hop matrix, using the earlier computed backtrace matrix.Methods inherited from class org.jgrapht.alg.shortestpath.BaseShortestPathAlgorithm
createEmptyPath
-
Field Details
-
vertices
-
degrees
-
vertexIndices
-
minDegreeOne
private final int minDegreeOne -
minDegreeTwo
private final int minDegreeTwo -
d
private double[][] d -
backtrace
-
lastHopMatrix
-
-
Constructor Details
-
FloydWarshallShortestPaths
Create a new instance of the Floyd-Warshall all-pairs shortest path algorithm.- Parameters:
graph
- the input graph
-
-
Method Details
-
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
-
getPath
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
Get the weight of the shortest path from a source vertex to a sink vertex. ReturnsDouble.POSITIVE_INFINITY
if no path exists.- Specified by:
getPathWeight
in interfaceShortestPathAlgorithm<V,
E> - Overrides:
getPathWeight
in classBaseShortestPathAlgorithm<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_INFINITY
if no path exists
-
getPaths
Compute all shortest paths starting from a single source vertex.- Specified by:
getPaths
in interfaceShortestPathAlgorithm<V,
E> - Overrides:
getPaths
in classBaseShortestPathAlgorithm<V,
E> - Parameters:
source
- the source vertex- Returns:
- the shortest paths
-
getFirstHop
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
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$.
-
lazyCalculateMatrix
private void lazyCalculateMatrix()Calculates the matrix of all shortest paths, but does not populate the last hops matrix. -
populateLastHopMatrix
private void populateLastHopMatrix()Populate the last hop matrix, using the earlier computed backtrace matrix.
-