V
- the graph vertex typeE
- the graph edge typepublic class JohnsonShortestPaths<V,E> extends Object
Finds the shortest paths between all pairs of vertices in a sparse graph. Edge weights can be negative, but no negative-weight cycles may exist. It first executes the Bellman-Ford algorithm to compute a transformation of the input graph that removes all negative weights, allowing Dijkstra's algorithm to be used on the transformed graph.
Running time is $O(n m + n^2 \log n)$.
Since Johnson's algorithm creates additional vertices, this implementation requires the user to
provide a VertexFactory
. Since the graph already contains vertices, care must be taken so
that the provided vertex factory does not return nodes that are already contained in the original
input graph.
ShortestPathAlgorithm.SingleSourcePaths<V,E>
Modifier and Type | Field and Description |
---|---|
protected Graph<V,E> |
graph
The underlying graph.
|
Constructor and Description |
---|
JohnsonShortestPaths(Graph<V,E> graph,
Class<? extends V> vertexClass)
Construct a new instance.
|
JohnsonShortestPaths(Graph<V,E> graph,
VertexFactory<V> vertexFactory)
Construct a new instance.
|
JohnsonShortestPaths(Graph<V,E> graph,
VertexFactory<V> vertexFactory,
double epsilon)
Construct a new instance.
|
Modifier and Type | Method and Description |
---|---|
protected GraphPath<V,E> |
createEmptyPath(V source,
V sink)
Create an empty path.
|
GraphPath<V,E> |
getPath(V source,
V sink)
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.
|
double |
getPathWeight(V source,
V sink)
Get the weight of the shortest path from a source vertex to a sink vertex.
|
protected final Graph<V,E> graph
public JohnsonShortestPaths(Graph<V,E> graph, Class<? extends V> vertexClass)
graph
- the input graphvertexClass
- the graph vertex classpublic JohnsonShortestPaths(Graph<V,E> graph, VertexFactory<V> vertexFactory)
graph
- the input graphvertexFactory
- the vertex factory of the graphpublic JohnsonShortestPaths(Graph<V,E> graph, VertexFactory<V> vertexFactory, double epsilon)
graph
- the input graphvertexFactory
- the vertex factory of the graphepsilon
- tolerance when comparing floating point valuespublic GraphPath<V,E> getPath(V source, V sink)
source
- the source vertexsink
- the target vertexIllegalArgumentException
- in case the provided vertex factory creates vertices which
are already in the original graphpublic double getPathWeight(V source, V sink)
Double.POSITIVE_INFINITY
if no path exists.getPathWeight
in interface ShortestPathAlgorithm<V,E>
source
- the source vertexsink
- the sink vertexDouble.POSITIVE_INFINITY
if no path existsIllegalArgumentException
- in case the provided vertex factory creates vertices which
are already in the original graphpublic ShortestPathAlgorithm.SingleSourcePaths<V,E> getPaths(V source)
getPaths
in interface ShortestPathAlgorithm<V,E>
source
- the source vertexIllegalArgumentException
- in case the provided vertex factory creates vertices which
are already in the original graphprotected final GraphPath<V,E> createEmptyPath(V source, V sink)
source
- the source vertexsink
- the sink vertexCopyright © 2017. All rights reserved.