A* is an informed
search algorithm, or a
best-first search, meaning that it is formulated in terms of
weighted graphs: starting from a specific starting
node of a graph, it aims to find a path to the given goal node having the smallest cost (least distance travelled, shortest time, etc.). It does this by maintaining a
tree of paths originating at the start node and extending those paths one edge at a time until the goal node is reached. At each iteration of its main loop, A* needs to determine which of its paths to extend. It does so based on the cost of the path and an estimate of the cost required to extend the path all the way to the goal. Specifically, A* selects the path that minimizes :f(n) = g(n) + h(n) where is the next node on the path, is the cost of the path from the start node to , and is a
heuristic function that estimates the cost of the cheapest path from to the goal. The heuristic function is problem-specific. Typical implementations of A* use a
priority queue to perform the repeated selection of minimum (estimated) cost nodes to expand. This priority queue is known as the
open set,
fringe or
frontier. At each step of the algorithm, the node with the lowest value is removed from the queue, the and values of its neighbors are updated accordingly, and these neighbors are added to the queue. The algorithm continues until a removed node (thus the node with the lowest value out of all fringe nodes) is a goal node. The value of that goal is then also the cost of the shortest path, since at the goal is zero in an admissible heuristic. The algorithm described so far only gives the length of the shortest path. To find the actual sequence of steps, the algorithm can be easily revised so that each node on the path keeps track of its predecessor. After this algorithm is run, the ending node will point to its predecessor, and so on, until some node's predecessor is the start node. As an example, when searching for the shortest route on a map, might represent the
straight-line distance to the goal, since that is physically the smallest possible distance between any two points. For a grid map from a video game, using the
Taxicab distance or the
Chebyshev distance becomes better depending on the set of movements available (4-way or 8-way). If the heuristic satisfies the additional condition for every edge of the graph (where denotes the length of that edge), then is called
monotone, or consistent. With a consistent heuristic, A* is guaranteed to find an optimal path without processing any node more than once and A* is equivalent to running
Dijkstra's algorithm with the
reduced cost .
Pseudocode The following
pseudocode describes the algorithm: function reconstruct_path(came_from, current) total_path := {current} while current in came_from.keys: current := came_from[current] total_path.prepend(current) return total_path // A* finds a path from start to goal. // h is the heuristic function. h(n) estimates the cost to reach goal from node n. function a_star(start, goal, h) // The set of discovered nodes that may need to be (re-)expanded. // Initially, only the start node is known. // This is usually implemented as a min-heap or priority queue rather than a hash-set. open_set := {start} // For node n, came_from[n] is the node immediately preceding it on the cheapest path from the start // to n currently known. came_from := an empty map // For node n, g_score[n] is the currently known cost of the cheapest path from start to n. g_score := map with default value of Infinity g_score[start] := 0 // For node n, f_score[n] := g_score[n] + h(n). f_score[n] represents our current best guess as to // how cheap a path could be from start to finish if it goes through n. f_score := map with default value of Infinity f_score[start] := h(start) while open_set is not empty // This operation can occur in O(Log(N)) time if open_set is a min-heap or a priority queue current := the node in open_set having the lowest f_score[] value if current = goal return reconstruct_path(came_from, current) open_set.remove(current) for each neighbor of current // d(current,neighbor) is the weight of the edge from current to neighbor // tentative_g_score is the distance from start to the neighbor through current tentative_g_score := g_score[current] + d(current, neighbor) if tentative_g_score
Remark: In this pseudocode, if a node is reached by one path, removed from open_set, and subsequently reached by a cheaper path, it will be added to open_set again. This is essential to guarantee that the path returned is optimal if the heuristic function is
admissible but not
consistent. If the heuristic is consistent, when a node is removed from open_set the path to it is guaranteed to be optimal so the test ‘tentative_g_score ’ will always fail if the node is reached again. The pseudocode implemented here is sometimes called the
graph-search version of A*. This is in contrast with the version without the ‘tentative_g_score ’ test to add nodes back to open_set, which is sometimes called the
tree-search version of A* and require a consistent heuristic to guarantee optimality.
motion planning problem. The empty circles represent the nodes in the
open set, i.e., those that remain to be explored, and the filled ones are in the closed set. Color on each closed node indicates the distance from the goal: the greener, the closer. One can first see the A* moving in a straight line in the direction of the goal, then when hitting the obstacle, it explores alternative routes through the nodes from the open set.
Example An example of an A* algorithm in action where nodes are cities connected with roads and h(x) is the straight-line distance to the target point:
Key: green: start; blue: goal; orange: visited The A* algorithm has real-world applications. In this example, edges are railroads and h(x) is the
great-circle distance (the shortest possible distance on a sphere) to the target. The algorithm is searching for a path between Washington, D.C., and Los Angeles.
Implementation details There are a number of simple optimizations or implementation details that can significantly affect the performance of an A* implementation. The first detail to note is that the way the priority queue handles ties can have a significant effect on performance in some situations. If ties are broken so the queue behaves in a
LIFO manner, A* will behave like
depth-first search among equal cost paths (avoiding exploring more than one equally optimal solution). When a path is required at the end of the search, it is common to keep with each node a reference to that node's parent. At the end of the search, these references can be used to recover the optimal path. If these references are being kept then it can be important that the same node doesn't appear in the priority queue more than once (each entry corresponding to a different path to the node, and each with a different cost). A standard approach here is to check if a node about to be added already appears in the priority queue. If it does, then the priority and parent pointers are changed to correspond to the lower-cost path. A standard
binary heap based priority queue does not directly support the operation of searching for one of its elements, but it can be augmented with a
hash table that maps elements to their position in the heap, allowing this decrease-priority operation to be performed in logarithmic time. Alternatively, a
Fibonacci heap can perform the same decrease-priority operations in constant
amortized time.
Special cases Dijkstra's algorithm, as another example of a uniform-cost search algorithm, can be viewed as a special case of A* where for all
x. General
depth-first search can be implemented using A* by considering that there is a global counter
C initialized with a very large value. Every time we process a node we assign
C to all of its newly discovered neighbors. After every single assignment, we decrease the counter
C by one. Thus the earlier a node is discovered, the higher its value. Both Dijkstra's algorithm and depth-first search can be implemented more efficiently without including an value at each node. ==Properties==