- Introduced by
- Peter E. Hart, Nils J. Nilsson, and Bertram Raphael
- Year
- 1968
- Type
- algorithm
- Family
- Graph Algorithms
What does it do?
A* is a best-first graph search algorithm, introduced by Peter E. Hart, Nils J. Nilsson, and Bertram Raphael in 1968, for finding minimum-cost paths.
Like Dijkstra's algorithm, it accounts for the cost already incurred from the start, and it adds a heuristic estimate of the remaining cost to the goal. When that estimate never overestimates the true remaining cost (an admissible heuristic), the path it returns is optimal, provided repeated nodes are handled correctly.
The intuition behind the method
A* ranks each frontier node by f(n) = g(n) + h(n), where g(n) is the known cost from the start to node n and h(n) is the heuristic estimate of the cost from n to the goal. At each step it expands the node with the lowest f(n), so the search is pulled toward the goal instead of spreading evenly in all directions. If h never overestimates and repeated nodes are handled correctly (see the evaluate step), the first time the goal is selected its path is optimal. If h is informative, A* can expand far fewer nodes than uninformed methods. If h is zero, the ranking reduces to the cost from the start alone, and A* behaves like Dijkstra's algorithm.
History
Hart, Nilsson, and Raphael published A* in 1968 in IEEE Transactions on Systems Science and Cybernetics, with the aim of giving a formal basis for incorporating heuristic information from the problem domain into graph searching, and of establishing an optimality property for a class of such strategies. Later variants such as IDA★ (a memory-bounded form of A*) are treated in standard textbooks on heuristic search; their origins are not traced here.
When to use it
Choose A* when you need a least-cost path from one start node to one goal in a graph, and you can design a heuristic that never overestimates the remaining cost. Typical cases are grid or road-network pathfinding and planning problems with a known lower-bound estimate. Do not choose it when no reliable heuristic is available; in that case Dijkstra's algorithm avoids the risk of an invalid estimate. The number of expanded nodes depends on how close the heuristic comes to the true remaining cost: a tight estimate keeps the search narrow, while a weak one pushes it toward Dijkstra's behaviour. Memory use is also a constraint, because A* stores the nodes it has generated, so very large search spaces may call for a memory-bounded variant such as IDA★.
Limitations and considerations
- Optimality depends on the heuristic being admissible; a heuristic that overestimates can cause the goal to be reached by a longer path
- Performance falls toward that of Dijkstra's algorithm as the heuristic becomes less informative
- Keeping the open and closed sets means memory grows with the number of generated nodes
- A good heuristic requires problem-specific knowledge, such as a relaxed version of the problem or a geometric distance bound
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Returns an optimal path when the heuristic is admissible; Hart, Nilsson, and Raphael (1968) establish an optimality property for this class of heuristic search strategies
- Can expand far fewer nodes than Dijkstra's algorithm when the heuristic is informative, because h(n) biases the search toward the goal
- Works with any computable heuristic function; admissibility is sufficient for optimality in tree search, and consistency or node reopening is needed for optimality in graph search
- Suited to single-goal problems, where the search stops once the goal is selected
Applications
- Video game pathfinding, where non-player characters navigate dynamic environments
- Robot motion planning, where the path must avoid obstacles and be of minimum cost
- Route planning on road networks, using estimated travel distance as the heuristic
- Puzzle solving, such as the 15-puzzle, where a lower bound on remaining moves serves as the heuristic
How it works
How it works
Place the start node in the open set, set its g-value to zero, and compute its h-value with the heuristic so that its f-value is g plus h.
Remove the node with the lowest f-value from the open set. If it is the goal, stop. Otherwise move it to the closed set and, for each neighbour, compute the tentative g-value through the current node; if this improves on the neighbour's known g-value, record the current node as its parent and add or update it in the open set. If the neighbour is already in the closed set and this improved g-value is lower, move it back to the open set so that it can be expanded again; with a consistent heuristic this reopening never occurs.
Repeat the evaluation step until the goal is selected from the open set, which is the point at which the optimality argument applies. Then reconstruct the path by following parent pointers from the goal back to the start.
Common pitfalls
- Using an inadmissible heuristic that overestimates true costs, which can produce suboptimal paths
- Using an admissible but inconsistent heuristic with a closed set and no node reopening, which can also break optimality, since a node may be closed with a non-optimal g-value
- Choosing a heuristic that is too weak or unrelated to the problem geometry, which removes most of the speed advantage over Dijkstra's algorithm
- Implementing the open set as an unsorted list instead of a priority queue, which makes selecting the lowest f-value slow
Frequently asked questions
What is an admissible heuristic and why does it matter?
An admissible heuristic never overestimates the true cost from a node to the goal. With an admissible heuristic, A* in tree search returns an optimal path. In graph search, the closed set must be handled correctly: optimality is preserved when the heuristic is consistent, or when a node is reopened whenever a cheaper path to it is found. If a heuristic overestimates, A* may accept a longer path and return a suboptimal solution.
How do you design a good heuristic for a problem?
A good heuristic is based on domain knowledge and problem structure, and it must stay below the true remaining cost. For grid pathfinding, Manhattan distance is admissible for 4-directional grids with unit step costs. Euclidean distance is admissible when each move costs at least its straight-line length. For harder problems, a common approach is to relax constraints so that the solution of the simpler problem gives a lower bound; pattern databases are another way to compute admissible estimates.
What is the difference between A* and Dijkstra's algorithm?
Both find least-cost paths. Dijkstra's algorithm orders nodes only by cost from the start and so spreads its search evenly in all directions. A* adds a heuristic estimate of the cost to the goal, so it tends to focus on promising directions and can expand fewer nodes, provided the heuristic is informative and admissible.
Can A* guarantee finding the shortest path?
A* finds the shortest path when the heuristic is admissible and the search handles repeated nodes correctly. With a closed set, the heuristic must be consistent, or nodes must be reopened when a cheaper path to them is found. With an inadmissible heuristic, optimality is not guaranteed.
Related methods
Sources
Hart, P. E., Nilsson, N. J., & Raphael, B. (1968). A formal basis for the heuristic determination of minimum cost paths. IEEE Transactions on Systems Science and Cybernetics, 4(2), 100-107.
Open DOI referenceRussell, S. J., & Norvig, P. (2009). Artificial Intelligence: A Modern Approach (3rd ed.). Pearson.
ISBN: 978-0-13-604259-4