Class AStarShortestPath<V,E>

java.lang.Object
org.jgrapht.alg.shortestpath.BaseShortestPathAlgorithm<V,E>
org.jgrapht.alg.shortestpath.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 BaseShortestPathAlgorithm<V,E>
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!

  • Field Details

    • heapSupplier

      protected final Supplier<org.jheaps.AddressableHeap<Double,V>> heapSupplier
    • openList

      protected org.jheaps.AddressableHeap<Double,V> openList
    • vertexToHeapNodeMap

      protected Map<V,org.jheaps.AddressableHeap.Handle<Double,V>> vertexToHeapNodeMap
    • closedList

      protected Set<V> closedList
    • gScoreMap

      protected Map<V,Double> gScoreMap
    • cameFrom

      protected Map<V,E> cameFrom
    • admissibleHeuristic

      protected AStarAdmissibleHeuristic<V> admissibleHeuristic
    • numberOfExpandedNodes

      protected int numberOfExpandedNodes
    • comparator

      protected Comparator<Double> comparator
  • Constructor Details

    • AStarShortestPath

      public AStarShortestPath(Graph<V,E> graph, AStarAdmissibleHeuristic<V> admissibleHeuristic)
      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.
    • AStarShortestPath

      public AStarShortestPath(Graph<V,E> graph, AStarAdmissibleHeuristic<V> admissibleHeuristic, Supplier<org.jheaps.AddressableHeap<Double,V>> heapSupplier)
      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.
      heapSupplier - supplier of the preferable heap implementation
  • Method Details

    • initialize

      private void initialize(AStarAdmissibleHeuristic<V> admissibleHeuristic)
      Initializes the data structures.
      Parameters:
      admissibleHeuristic - admissible heuristic
    • 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
    • expandNode

      private void expandNode(org.jheaps.AddressableHeap.Handle<Double,V> currentNode, V endVertex)
    • buildGraphPath

      private GraphPath<V,E> buildGraphPath(V startVertex, V targetVertex, double pathLength)
      Builds the graph path
      Parameters:
      startVertex - starting vertex of the path
      targetVertex - ending vertex of the path
      pathLength - length of the path
      Returns:
      the shortest path from startVertex to endVertex