Class AStarShortestPath<V,E>
- java.lang.Object
-
- org.jgrapht.alg.shortestpath.AStarShortestPath<V,E>
-
- Type Parameters:
V
- the graph vertex typeE
- the graph edge type
- All Implemented Interfaces:
ShortestPathAlgorithm<V,E>
public class AStarShortestPath<V,E> extends java.lang.Object
A* shortest path.An implementation of A* shortest path algorithm. This class works for directed and undirected graphs, as well as multi-graphs and mixed-graphs. The graph can also change between invocations of the
getPath(Object, Object)
method; no new instance of this class has to be created. The heuristic is implemented using a PairingHeap data structure by default to maintain the set of open nodes. However, there still exist several approaches in literature to improve the performance of this heuristic which one could consider to implement. Custom heap implementation can be specified during the construction time. Another issue to take into consideration is the following: given two candidate nodes, $i$, $j$ to expand, where $f(i)=f(j)$, $g(i)$ > $g(j)$, $h(i)$ < $g(j)$, $f(i)=g(i)+h(i)$, $g(i)$ is the actual distance from the source node to $i$, $h(i)$ is the estimated distance from $i$ to the target node. Usually a depth-first search is desired, so ideally we would expand node $i$ first. Using the PairingHeap, this is not necessarily the case though. This could be improved in a later version.Note: This implementation works with both consistent and inconsistent admissible heuristics. For details on consistency, refer to the description of the method
AStarAdmissibleHeuristic.isConsistent(Graph)
. However, this class is not optimized for inconsistent heuristics. Several opportunities to improve both worst case and average runtime complexities for A* with inconsistent heuristics described in literature can be used to improve this implementation!- Author:
- Joris Kinable, Jon Robison, Thomas Breitbart
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jgrapht.alg.interfaces.ShortestPathAlgorithm
ShortestPathAlgorithm.SingleSourcePaths<V,E>
-
-
Field Summary
Fields Modifier and Type Field Description protected AStarAdmissibleHeuristic<V>
admissibleHeuristic
protected java.util.Map<V,E>
cameFrom
protected java.util.Set<V>
closedList
protected java.util.Comparator<java.lang.Double>
comparator
protected Graph<V,E>
graph
The underlying graph.protected static java.lang.String
GRAPH_CONTAINS_A_NEGATIVE_WEIGHT_CYCLE
Error message for reporting the existence of a negative-weight cycle.protected static java.lang.String
GRAPH_MUST_CONTAIN_THE_SINK_VERTEX
Error message for reporting that a sink vertex is missing.protected static java.lang.String
GRAPH_MUST_CONTAIN_THE_SOURCE_VERTEX
Error message for reporting that a source vertex is missing.protected java.util.Map<V,java.lang.Double>
gScoreMap
protected java.util.function.Supplier<org.jheaps.AddressableHeap<java.lang.Double,V>>
heapSupplier
protected int
numberOfExpandedNodes
protected org.jheaps.AddressableHeap<java.lang.Double,V>
openList
protected java.util.Map<V,org.jheaps.AddressableHeap.Handle<java.lang.Double,V>>
vertexToHeapNodeMap
-
Constructor Summary
Constructors Constructor Description AStarShortestPath(Graph<V,E> graph, AStarAdmissibleHeuristic<V> admissibleHeuristic)
Create a new instance of the A* shortest path algorithm.AStarShortestPath(Graph<V,E> graph, AStarAdmissibleHeuristic<V> admissibleHeuristic, java.util.function.Supplier<org.jheaps.AddressableHeap<java.lang.Double,V>> heapSupplier)
Create a new instance of the A* shortest path algorithm.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected GraphPath<V,E>
createEmptyPath(V source, V sink)
Create an empty path.int
getNumberOfExpandedNodes()
Returns how many nodes have been expanded in the A* search procedure in its last invocation.GraphPath<V,E>
getPath(V sourceVertex, V targetVertex)
Calculates (and returns) the shortest path from the sourceVertex to the targetVertex.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.
-
-
-
Field Detail
-
heapSupplier
protected final java.util.function.Supplier<org.jheaps.AddressableHeap<java.lang.Double,V>> heapSupplier
-
openList
protected org.jheaps.AddressableHeap<java.lang.Double,V> openList
-
vertexToHeapNodeMap
protected java.util.Map<V,org.jheaps.AddressableHeap.Handle<java.lang.Double,V>> vertexToHeapNodeMap
-
closedList
protected java.util.Set<V> closedList
-
gScoreMap
protected java.util.Map<V,java.lang.Double> gScoreMap
-
admissibleHeuristic
protected AStarAdmissibleHeuristic<V> admissibleHeuristic
-
numberOfExpandedNodes
protected int numberOfExpandedNodes
-
comparator
protected java.util.Comparator<java.lang.Double> comparator
-
GRAPH_CONTAINS_A_NEGATIVE_WEIGHT_CYCLE
protected static final java.lang.String GRAPH_CONTAINS_A_NEGATIVE_WEIGHT_CYCLE
Error message for reporting the existence of a negative-weight cycle.- See Also:
- Constant Field Values
-
GRAPH_MUST_CONTAIN_THE_SOURCE_VERTEX
protected static final java.lang.String GRAPH_MUST_CONTAIN_THE_SOURCE_VERTEX
Error message for reporting that a source vertex is missing.- See Also:
- Constant Field Values
-
GRAPH_MUST_CONTAIN_THE_SINK_VERTEX
protected static final java.lang.String GRAPH_MUST_CONTAIN_THE_SINK_VERTEX
Error message for reporting that a sink vertex is missing.- See Also:
- Constant Field Values
-
graph
protected final Graph<V,E> graph
The underlying graph.
-
-
Constructor Detail
-
AStarShortestPath
public AStarShortestPath(Graph<V,E> graph, AStarAdmissibleHeuristic<V> admissibleHeuristic)
Create a new instance of the A* shortest path algorithm.- Parameters:
graph
- the input graphadmissibleHeuristic
- admissible heuristic which estimates the distance from a node to the target node. The heuristic must never overestimate the distance.
-
AStarShortestPath
public AStarShortestPath(Graph<V,E> graph, AStarAdmissibleHeuristic<V> admissibleHeuristic, java.util.function.Supplier<org.jheaps.AddressableHeap<java.lang.Double,V>> heapSupplier)
Create a new instance of the A* shortest path algorithm.- Parameters:
graph
- the input graphadmissibleHeuristic
- admissible heuristic which estimates the distance from a node to the target node. The heuristic must never overestimate the distance.heapSupplier
- supplier of the preferable heap implementation
-
-
Method Detail
-
getPath
public GraphPath<V,E> getPath(V sourceVertex, V targetVertex)
Calculates (and returns) the shortest path from the sourceVertex to the targetVertex. Note: each time you invoke this method, the path gets recomputed.- Parameters:
sourceVertex
- source vertextargetVertex
- target vertex- Returns:
- the shortest path from sourceVertex to targetVertex
-
getNumberOfExpandedNodes
public int getNumberOfExpandedNodes()
Returns how many nodes have been expanded in the A* search procedure in its last invocation. A node is expanded if it is removed from the open list.- Returns:
- number of expanded nodes
-
getPaths
public ShortestPathAlgorithm.SingleSourcePaths<V,E> getPaths(V source)
Compute all shortest paths starting from a single source vertex.- Specified by:
getPaths
in interfaceShortestPathAlgorithm<V,E>
- Parameters:
source
- the source vertex- Returns:
- the shortest paths
-
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_INFINITY
if no path exists.- Specified by:
getPathWeight
in 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_INFINITY
if no path exists
-
createEmptyPath
protected final GraphPath<V,E> createEmptyPath(V source, V sink)
Create an empty path. Returns null if the source vertex is different than the target vertex.- Parameters:
source
- the source vertexsink
- the sink vertex- Returns:
- an empty path or null null if the source vertex is different than the target vertex
-
-