org.jgrapht.alg.shortestpath

## Class AStarShortestPath<V,E>

• Type Parameters:
V - the graph vertex type
E - the graph edge type
All Implemented Interfaces:
ShortestPathAlgorithm<V,E>

public class AStarShortestPath<V,E>
extends 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 FibonacciHeap data structure 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. 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 FibonacciHeap, 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 isConsistentHeuristic(AStarAdmissibleHeuristic). 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!

Since:
Aug, 2015
Author:
Joris Kinable, Jon Robison, Thomas Breitbart
• ### Field Detail

• #### closedList

protected Set<V> closedList
• #### gScoreMap

protected Map<V,Double> gScoreMap
• #### cameFrom

protected Map<V,E> cameFrom
• #### numberOfExpandedNodes

protected int numberOfExpandedNodes
• #### graph

protected final Graph<V,E> graph
The underlying graph.
• ### Constructor Detail

• #### AStarShortestPath

public AStarShortestPath(Graph<V,E> graph,
Create a new instance of the A* shortest path algorithm.
Parameters:
graph - the input graph
admissibleHeuristic - admissible heuristic which estimates the distance from a node to the target node. The heuristic must never overestimate the distance.
• ### 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 vertex
targetVertex - 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
• #### isConsistentHeuristic

Returns true if the provided heuristic is a consistent or monotone heuristic wrt the graph provided at construction time. A heuristic is monotonic if its estimate is always less than or equal to the estimated distance from any neighboring vertex to the goal, plus the step cost of reaching that neighbor. For details, refer to https://en.wikipedia.org/wiki/Consistent_heuristic. In short, a heuristic is consistent iff h(u)≤ d(u,v)+h(v), for every edge $(u,v)$, where $d(u,v)$ is the weight of edge $(u,v)$ and $h(u)$ is the estimated cost to reach the target node from vertex u. Most natural admissible heuristics, such as Manhattan or Euclidean distance, are consistent heuristics.
Parameters:
Returns:
true is the heuristic is consistent, false otherwise
• #### getPathWeight

public double getPathWeight(V source,
V sink)
Get the weight of the shortest path from a source vertex to a sink vertex. Returns Double.POSITIVE_INFINITY if no path exists.
Specified by:
getPathWeight in interface ShortestPathAlgorithm<V,E>
Parameters:
source - the source vertex
sink - 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 vertex
sink - the sink vertex
Returns:
an empty path or null null if the source vertex is different than the target vertex