Class ContractionHierarchyBidirectionalDijkstra<V,E>
- java.lang.Object
-
- org.jgrapht.alg.shortestpath.BaseShortestPathAlgorithm<V,E>
-
- org.jgrapht.alg.shortestpath.ContractionHierarchyBidirectionalDijkstra<V,E>
-
- Type Parameters:
V
- the graph vertex typeE
- the graph edge type
- All Implemented Interfaces:
ShortestPathAlgorithm<V,E>
public class ContractionHierarchyBidirectionalDijkstra<V,E> extends BaseShortestPathAlgorithm<V,E>
Implementation of the hierarchical query algorithm based on the bidirectional Dijkstra search. This algorithm is designed to contracted graphs. The best speedup is achieved on sparse graphs with low average outdegree.The query algorithm is originally described the article: Robert Geisberger, Peter Sanders, Dominik Schultes, and Daniel Delling. 2008. Contraction hierarchies: faster and simpler hierarchical routing in road networks. In Proceedings of the 7th international conference on Experimental algorithms (WEA'08), Catherine C. McGeoch (Ed.). Springer-Verlag, Berlin, Heidelberg, 319-333.
During contraction graph is divided into 2 parts which are called upward and downward graphs. Both parts have all vertices of the original graph. The upward graph ($G_{\uparrow}$) contains only those edges which source has lower level than the target and vice versa for the downward graph ($G_{\downarrow}$).
For the shortest path query from $s$ to $t$, a modified bidirectional Dijkstra shortest path search is performed. The forward search from $s$ operates on $G_{\uparrow}$ and the backward search from $t$ - on the $G_{\downarrow}$. In each direction only the edges of the corresponding part of the graph are considered. Both searches eventually meet at the vertex $v$, which has the highest level in the shortest path from $s$ to $t$. Whenever a search in one direction reaches a vertex that has already been processed in other direction, a new candidate for a shortest path is found. Search is aborted in one direction if the smallest element in the corresponding priority queue is at least as large as the best candidate path found so far.
After computing a contracted path, the algorithm unpacks it recursively into the actual shortest path using the bypassed edges stored in the contraction hierarchy graph.
There is a possibility to provide an already computed contraction for the graph. For now there is no means to ensure that the specified contraction is correct, nor to fail-fast. If algorithm uses an incorrect contraction, the results of the search are unpredictable.
Comparing to usual shortest path algorithm, as
DijkstraShortestPath
,AStarShortestPath
, etc., this algorithm spends time for computing contraction hierarchy but offers significant speedup in shortest path query performance. Therefore it is efficient to use it in order to compute many shortest path on a single graph. Furthermore, on small graphs (i.e with less than 1.000 vertices) the overhead of precomputation is higher than the speed at the stage of computing shortest paths. Typically this algorithm is used to gain speedup for shortest path queries on graphs of middle and large size (i.e. starting at 1.000 vertices). If a further query performance improvement is needed take a look atTransitNodeRoutingShortestPath
.- Since:
- July 2019
- See Also:
ContractionHierarchyPrecomputation
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
ContractionHierarchyBidirectionalDijkstra.ContractionSearchFrontier<V,E>
Maintains search frontier during shortest path computation.-
Nested classes/interfaces inherited from interface org.jgrapht.alg.interfaces.ShortestPathAlgorithm
ShortestPathAlgorithm.SingleSourcePaths<V,E>
-
-
Field Summary
Fields Modifier and Type Field Description private Graph<ContractionHierarchyPrecomputation.ContractionVertex<V>,ContractionHierarchyPrecomputation.ContractionEdge<E>>
contractionGraph
Contracted graph, which is used during the queries.private ContractionHierarchyPrecomputation.ContractionHierarchy<V,E>
contractionHierarchy
Contraction hierarchy which is used to compute shortest paths.private java.util.Map<V,ContractionHierarchyPrecomputation.ContractionVertex<V>>
contractionMapping
Mapping from original to contracted vertices.private java.util.function.Supplier<org.jheaps.AddressableHeap<java.lang.Double,Pair<ContractionHierarchyPrecomputation.ContractionVertex<V>,ContractionHierarchyPrecomputation.ContractionEdge<E>>>>
heapSupplier
Supplier for preferable heap implementation.private double
radius
Radius of the search.-
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
Constructors Constructor Description ContractionHierarchyBidirectionalDijkstra(ContractionHierarchyPrecomputation.ContractionHierarchy<V,E> hierarchy)
Constructs a new instance of the algorithm for a givenhierarchy
.ContractionHierarchyBidirectionalDijkstra(ContractionHierarchyPrecomputation.ContractionHierarchy<V,E> hierarchy, double radius, java.util.function.Supplier<org.jheaps.AddressableHeap<java.lang.Double,Pair<ContractionHierarchyPrecomputation.ContractionVertex<V>,ContractionHierarchyPrecomputation.ContractionEdge<E>>>> heapSupplier)
Constructs a new instance of the algorithm for the givenhierarchy
,radius
andheapSupplier
.ContractionHierarchyBidirectionalDijkstra(Graph<V,E> graph, java.util.concurrent.ThreadPoolExecutor executor)
Constructs a new instance of the algorithm for a givengraph
andexecutor
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private GraphPath<V,E>
createPath(ContractionHierarchyBidirectionalDijkstra.ContractionSearchFrontier<ContractionHierarchyPrecomputation.ContractionVertex<V>,ContractionHierarchyPrecomputation.ContractionEdge<E>> forwardFrontier, ContractionHierarchyBidirectionalDijkstra.ContractionSearchFrontier<ContractionHierarchyPrecomputation.ContractionVertex<V>,ContractionHierarchyPrecomputation.ContractionEdge<E>> backwardFrontier, double weight, ContractionHierarchyPrecomputation.ContractionVertex<V> source, ContractionHierarchyPrecomputation.ContractionVertex<V> commonVertex, ContractionHierarchyPrecomputation.ContractionVertex<V> sink)
Builds shortest unpacked path betweensource
andsink
based on the information provided by search frontiers and common vertex.GraphPath<V,E>
getPath(V source, V sink)
Get a shortest path from a source vertex to a sink vertex.-
Methods inherited from class org.jgrapht.alg.shortestpath.BaseShortestPathAlgorithm
createEmptyPath, getPaths, getPathWeight
-
-
-
-
Field Detail
-
contractionHierarchy
private ContractionHierarchyPrecomputation.ContractionHierarchy<V,E> contractionHierarchy
Contraction hierarchy which is used to compute shortest paths.
-
contractionGraph
private Graph<ContractionHierarchyPrecomputation.ContractionVertex<V>,ContractionHierarchyPrecomputation.ContractionEdge<E>> contractionGraph
Contracted graph, which is used during the queries.
-
contractionMapping
private java.util.Map<V,ContractionHierarchyPrecomputation.ContractionVertex<V>> contractionMapping
Mapping from original to contracted vertices.
-
heapSupplier
private java.util.function.Supplier<org.jheaps.AddressableHeap<java.lang.Double,Pair<ContractionHierarchyPrecomputation.ContractionVertex<V>,ContractionHierarchyPrecomputation.ContractionEdge<E>>>> heapSupplier
Supplier for preferable heap implementation.
-
radius
private double radius
Radius of the search.
-
-
Constructor Detail
-
ContractionHierarchyBidirectionalDijkstra
public ContractionHierarchyBidirectionalDijkstra(Graph<V,E> graph, java.util.concurrent.ThreadPoolExecutor executor)
Constructs a new instance of the algorithm for a givengraph
andexecutor
. It is up to a user of this algorithm to handle the creation and termination of the providedexecutor
. For utility methods to manage aThreadPoolExecutor
seeConcurrencyUtil
.- Parameters:
graph
- the graphexecutor
- executor which is used for computing theContractionHierarchyPrecomputation.ContractionHierarchy
-
ContractionHierarchyBidirectionalDijkstra
public ContractionHierarchyBidirectionalDijkstra(ContractionHierarchyPrecomputation.ContractionHierarchy<V,E> hierarchy)
Constructs a new instance of the algorithm for a givenhierarchy
.- Parameters:
hierarchy
- contraction of thegraph
-
ContractionHierarchyBidirectionalDijkstra
public ContractionHierarchyBidirectionalDijkstra(ContractionHierarchyPrecomputation.ContractionHierarchy<V,E> hierarchy, double radius, java.util.function.Supplier<org.jheaps.AddressableHeap<java.lang.Double,Pair<ContractionHierarchyPrecomputation.ContractionVertex<V>,ContractionHierarchyPrecomputation.ContractionEdge<E>>>> heapSupplier)
Constructs a new instance of the algorithm for the givenhierarchy
,radius
andheapSupplier
.- Parameters:
hierarchy
- contraction of thegraph
radius
- search radiusheapSupplier
- supplier of the preferable heap implementation
-
-
Method Detail
-
getPath
public GraphPath<V,E> getPath(V source, V sink)
Get a shortest path from a source vertex to a sink vertex.- Parameters:
source
- the source vertexsink
- the target vertex- Returns:
- a shortest path or null if no path exists
-
createPath
private GraphPath<V,E> createPath(ContractionHierarchyBidirectionalDijkstra.ContractionSearchFrontier<ContractionHierarchyPrecomputation.ContractionVertex<V>,ContractionHierarchyPrecomputation.ContractionEdge<E>> forwardFrontier, ContractionHierarchyBidirectionalDijkstra.ContractionSearchFrontier<ContractionHierarchyPrecomputation.ContractionVertex<V>,ContractionHierarchyPrecomputation.ContractionEdge<E>> backwardFrontier, double weight, ContractionHierarchyPrecomputation.ContractionVertex<V> source, ContractionHierarchyPrecomputation.ContractionVertex<V> commonVertex, ContractionHierarchyPrecomputation.ContractionVertex<V> sink)
Builds shortest unpacked path betweensource
andsink
based on the information provided by search frontiers and common vertex.- Parameters:
forwardFrontier
- forward direction frontierbackwardFrontier
- backward direction frontierweight
- weight of the shortest pathsource
- path sourcecommonVertex
- path common vertexsink
- path sink- Returns:
- unpacked shortest path between source and sink
-
-