Pathfinding with A* Algorithm
A* is a widely used path-finding algorithm with a heuristic function to guide a search for the shortest path. This often results in fewer nodes being searched than with Dijkstra's algorithm. This Demonstration shows the computation of the neighbors of a node using different heuristic functions. See the Details section for more information.[more]
Click a square once to make it an obstacle (black). Click it again to remove the obstacle. The shortest path computed by the algorithm is shown in orange. The blue squares, together with the squares along the path, comprise a closed set.[less]
The A* algorithm is a path-finding algorithm that is widely used owing to its performance and accuracy. It can be seen as an extension of Dijkstra's algorithm, which it outperforms with the aid of a heuristic function to guide its search.
With an admissible heuristic, the algorithm is guaranteed to return the shortest path. If the heuristic is also monotonic, the algorithm is simplified considerably and the reconstruction of the shortest path becomes particularly easy. Otherwise, dynamic programming must be used to obtain the shortest path.
The number of nodes that the algorithm must visit depends, among other things, on the specific characteristics of the heuristic function as well as the manner in which the neighbors of a node are computed. This Demonstration allows you to try different combinations of neighboring nodes, computations and heuristic functions. Brief descriptions of these functions follow.
The function getNeighborsPlus returns all the non-obstacle nodes that are above, below, to the right and to the left of a given node.
The function getNeighborsPlusCross returns all the non-obstacle nodes that are returned by getNeighborsPlus, as well as the non-obstacle diagonal nodes to the top-right, top-left, bottom-left and bottom-right of a given node.
The heuristic function minOfXYDiff computes the and distances between a given node and the destination and returns the smaller of the two values as the estimate.
The heuristic function maxOfXYDiff computes the and distances between a given node and the destination and returns the larger of the two values as the estimate.
The better the heuristic estimates the actual distance between a node and the destination, the fewer the nodes that the algorithm must search in order to find a shortest path. It is easy to see that in this Demonstration, maxOfXYDiff is always the better estimate, as a result of which the algorithm expands fewer nodes than in the case of minOfXYDiff.