In 1968, AI researcher Nils Nilsson was trying to improve the path planning done by Shakey the Robot, a prototype robot that could navigate through a room containing obstacles. This pathfinding algorithm, which Nilsson called A1, was a faster version of the then best known method, Dijkstra's algorithm, for finding shortest paths in graphs. Bertram Raphael suggested some significant improvements upon this algorithm, calling the revised version A2. Then Peter E. Hart introduced an argument that established A2, with only minor changes, to be the best possible algorithm for finding shortest paths. Hart, Nilsson and Raphael then jointly developed a proof that the revised A2 algorithm was optimal for finding shortest paths under certain welldefined conditions.
A* is an informed search algorithm, or a bestfirst search, meaning that it solves problems by searching among all possible paths to the solution (goal) for the one that incurs the smallest cost (least distance travelled, shortest time, etc.), and among these paths it first considers the ones that appear to lead most quickly to the solution. It is formulated in terms of weighted graphs: starting from a specific node of a graph, it constructs a tree of paths starting from that node, expanding paths one step at a time, until one of its paths ends at the predetermined goal node.
At each iteration of its main loop, A* needs to determine which of its partial paths to expand into one or more longer paths. It does so based on an estimate of the cost (total weight) still to go to the goal node. Specifically, A* selects the path that minimizes
f
(
n
)
=
g
(
n
)
+
h
(
n
)
where n is the last node on the path, g(n) is the cost of the path from the start node to n, and h(n) is a heuristic that estimates the cost of the cheapest path from n to the goal. The heuristic is problemspecific. For the algorithm to find the actual shortest path, the heuristic function must be admissible, meaning that it never overestimates the actual cost to get to the nearest goal node.
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 or fringe. At each step of the algorithm, the node with the lowest f(x) value is removed from the queue, the f and g values of its neighbors are updated accordingly, and these neighbors are added to the queue. The algorithm continues until a goal node has a lower f value than any node in the queue (or until the queue is empty). The f value of the goal is then the length of the shortest path, since h at the goal is zero in an admissible heuristic.
The algorithm described so far gives us only 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, h(x) might represent the straightline distance to the goal, since that is physically the smallest possible distance between any two points.
If the heuristic h satisfies the additional condition h(x) ≤ d(x, y) + h(y) for every edge (x, y) of the graph (where d denotes the length of that edge), then h is called monotone, or consistent. In such a case, A* can be implemented more efficiently—roughly speaking, no node needs to be processed more than once (see closed set below)—and A* is equivalent to running Dijkstra's algorithm with the reduced cost d'(x, y) = d(x, y) + h(y) − h(x).
The following pseudocode describes the algorithm:
Remark: the above pseudocode assumes that the heuristic function is monotonic (or consistent, see below), which is a frequent case in many practical problems, such as the Shortest Distance Path in road networks. However, if the assumption is not true, nodes in the closed set may be rediscovered and their cost improved. In other words, the closed set can be omitted (yielding a tree search algorithm) if a solution is guaranteed to exist, or if the algorithm is adapted so that new nodes are added to the open set only if they have a lower f value than at any previous iteration.
An example of an A* algorithm in action where nodes are cities connected with roads and h(x) is the straightline distance to target point:
Key: green: start; blue: goal; orange: visited
The A* algorithm also has realworld applications. In this example, edges are railroads and h(x) is the greatcircle 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.
Like breadthfirst search, A* is complete and will always find a solution if one exists provided
d
(
x
,
y
)
>
ε
>
0
for fixed
ε
.
If the heuristic function h is admissible, meaning that it never overestimates the actual minimal cost of reaching the goal, then A* is itself admissible (or optimal) if we do not use a closed set. If a closed set is used, then h must also be monotonic (or consistent) for A* to be optimal. This means that for any pair of adjacent nodes x and y, where
d
(
x
,
y
)
denotes the length of the edge between them, we must have:
h
(
x
)
≤
d
(
x
,
y
)
+
h
(
y
)
This ensures that for any path X from the initial node to x:
L
(
X
)
+
h
(
x
)
≤
L
(
X
)
+
d
(
x
,
y
)
+
h
(
y
)
=
L
(
Y
)
+
h
(
y
)
where L is a function that denotes the length of a path, and Y is the path X extended to include y. In other words, it is impossible to decrease (total distance so far + estimated remaining distance) by extending a path to include a neighboring node. (This is analogous to the restriction to nonnegative edge weights in Dijkstra's algorithm.) Monotonicity implies admissibility when the heuristic estimate at any goal node itself is zero, since (letting P = (f,v_{1},v_{2},...,v_{n},g) be a shortest path from any node f to the nearest goal g):
h
(
f
)
≤
d
(
f
,
v
1
)
+
h
(
v
1
)
≤
d
(
f
,
v
1
)
+
d
(
v
1
,
v
2
)
+
h
(
v
2
)
≤
…
≤
L
(
P
)
+
h
(
g
)
=
L
(
P
)
A* is also optimally efficient for any heuristic h, meaning that no optimal algorithm employing the same heuristic will expand fewer nodes than A*, except when there are multiple partial solutions where h exactly predicts the cost of the optimal path. Even in this case, for each graph there exists some order of breaking ties in the priority queue such that A* examines the fewest possible nodes.
Dijkstra's algorithm, as another example of a uniformcost search algorithm, can be viewed as a special case of A* where
h
(
x
)
=
0
for all x. General depthfirst search can be implemented using the 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 each single assignment, we decrease the counter C by one. Thus the earlier a node is discovered, the higher its
h
(
x
)
value. It should be noted, however, that both Dijkstra's algorithm and depthfirst search can be implemented more efficiently without including a
h
(
x
)
value at each node.
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 depthfirst 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 decreasepriority operation to be performed in logarithmic time. Alternatively, a Fibonacci heap can perform the same decreasepriority operations in constant amortized time.
A* is admissible and considers fewer nodes than any other admissible search algorithm with the same heuristic. This is because A* uses an "optimistic" estimate of the cost of a path through every node that it considers—optimistic in that the true cost of a path through that node to the goal will be at least as great as the estimate. But, critically, as far as A* "knows", that optimistic estimate might be achievable.
To prove the admissibility of A*, the solution path returned by the algorithm is used as follows:
When A* terminates its search, it has found a path whose actual cost is lower than the estimated cost of any path through any open node. But since those estimates are optimistic, A* can safely ignore those nodes. In other words, A* will never overlook the possibility of a lowercost path and so is admissible.
Suppose now that some other search algorithm B terminates its search with a path whose actual cost is not less than the estimated cost of a path through some open node. Based on the heuristic information it has, Algorithm B cannot rule out the possibility that a path through that node has a lower cost. So while B might consider fewer nodes than A*, it cannot be admissible. Accordingly, A* considers the fewest nodes of any admissible search algorithm.
This is only true if both:
A* uses an admissible heuristic. Otherwise, A* is not guaranteed to expand fewer nodes than another search algorithm with the same heuristic.
A* solves only one search problem rather than a series of similar search problems. Otherwise, A* is not guaranteed to expand fewer nodes than incremental heuristic search algorithms.
While the admissibility criterion guarantees an optimal solution path, it also means that A* must examine all equally meritorious paths to find the optimal path. It is possible to speed up the search at the expense of optimality by relaxing the admissibility criterion. Oftentimes we want to bound this relaxation, so that we can guarantee that the solution path is no worse than (1 + ε) times the optimal solution path. This new guarantee is referred to as εadmissible.
There are a number of εadmissible algorithms:
Weighted A*/Static Weighting. If h_{a}(n) is an admissible heuristic function, in the weighted version of the A* search one uses h_{w}(n) = ε h_{a}(n), ε > 1 as the heuristic function, and perform the A* search as usual (which eventually happens faster than using h_{a} since fewer nodes are expanded). The path hence found by the search algorithm can have a cost of at most ε times that of the least cost path in the graph.
Dynamic Weighting uses the cost function
f
(
n
)
=
g
(
n
)
+
(
1
+
ε
w
(
n
)
)
h
(
n
)
, where
w
(
n
)
=
{
1
−
d
(
n
)
N
d
(
n
)
≤
N
0
otherwise
, and where
d
(
n
)
is the depth of the search and N is the anticipated length of the solution path.
Sampled Dynamic Weighting uses sampling of nodes to better estimate and debias the heuristic error.
A
ε
∗
. uses two heuristic functions. The first is the FOCAL list, which is used to select candidate nodes, and the second h_{F} is used to select the most promising node from the FOCAL list.
A_{ε} selects nodes with the function
A
f
(
n
)
+
B
h
F
(
n
)
, where A and B are constants. If no nodes can be selected, the algorithm will backtrack with the function
C
f
(
n
)
+
D
h
F
(
n
)
, where C and D are constants.
AlphA* attempts to promote depthfirst exploitation by preferring recently expanded nodes. AlphA* uses the cost function
f
α
(
n
)
=
(
1
+
w
α
(
n
)
)
f
(
n
)
, where
w
α
(
n
)
=
{
λ
g
(
π
(
n
)
)
≤
g
(
n
~
)
Λ
otherwise
, where λ and Λ are constants with
λ
≤
Λ
, π(n) is the parent of n, and ñ is the most recently expanded node.
The time complexity of A* depends on the heuristic. In the worst case of an unbounded search space, the number of nodes expanded is exponential in the depth of the solution (the shortest path) d: O(b^{d}), where b is the branching factor (the average number of successors per state). This assumes that a goal state exists at all, and is reachable from the start state; if it is not, and the state space is infinite, the algorithm will not terminate.
The heuristic function has a major effect on the practical performance of A* search, since a good heuristic allows A* to prune away many of the b^{d} nodes that an uninformed search would expand. Its quality can be expressed in terms of the effective branching factor b*, which can be determined empirically for a problem instance by measuring the number of nodes expanded, N, and the depth of the solution, then solving
N
+
1
=
1
+
b
∗
+
(
b
∗
)
2
+
⋯
+
(
b
∗
)
d
.
Good heuristics are those with low effective branching factor (the optimal being b* = 1).
The time complexity is polynomial when the search space is a tree, there is a single goal state, and the heuristic function h meets the following condition:

h
(
x
)
−
h
∗
(
x
)

=
O
(
log
h
∗
(
x
)
)
where h^{*} is the optimal heuristic, the exact cost to get from x to the goal. In other words, the error of h will not grow faster than the logarithm of the "perfect heuristic" h^{*} that returns the true distance from x to the goal.
A* is commonly used for the common pathfinding problem in applications such as games, but was originally designed as a general graph traversal algorithm. It finds applications to diverse problems, including the problem of parsing using stochastic grammars in NLP. Other cases include an Informational search with online learning
What sets A* apart from a greedy bestfirst search algorithm is that it takes the cost/distance already traveled, g(n), into account.
Some common variants of Dijkstra's algorithm can be viewed as a special case of A* where the heuristic
h
(
n
)
=
0
for all nodes; in turn, both Dijkstra and A* are special cases of dynamic programming. A* itself is a special case of a generalization of branch and bound and can be derived from the primaldual algorithm for linear programming.
Anytime Repairing A* (ARA*)
Block A*
D*
Field D*
Fringe
Fringe Saving A* (FSA*)
Generalized Adaptive A* (GAA*)
IDA*
Informational search
Jump point search
Lifelong Planning A* (LPA*)
Simplified Memory bounded A* (SMA*)
Theta*
Anytime A*
Realtime A*
Anytime Dynamic A*
A* can also be adapted to a bidirectional search algorithm. Special care needs to be taken for the stopping criterion.