Skip to content
— CH. 1 · INTRODUCTION —

A* search algorithm

~6 min read · Ch. 1 of 7
7 sections
  • A* (pronounced "A-star") is a pathfinding algorithm that can find the shortest route between two points on any weighted graph - from a road map to a railway network to a fantasy video game world. It was first published in 1968 by Peter Hart, Nils Nilsson, and Bertram Raphael of Stanford Research Institute. Half a century later, it remains the best solution across a wide range of pathfinding problems.

    What makes A* remarkable is that it is simultaneously complete, optimal, and optimally efficient. It will always find the shortest path if one exists, and it will do so while expanding fewer nodes than any comparable algorithm. How does a search algorithm achieve all three properties at once? The answer lies in a surprisingly elegant idea called a heuristic, and in the distinct contributions each of those three researchers made to what became the A* paper.

  • Shakey was a mobile robot developed at Stanford Research Institute with the goal of planning its own actions autonomously. Nils Nilsson originally proposed using an algorithm called the Graph Traverser to handle Shakey's path planning. Graph Traverser used a heuristic - an estimated distance from a given node to the goal - but it ignored one crucial piece of information: how far the robot had already traveled to get there.

    Bertram Raphael recognized the gap. He suggested combining both costs: the distance already traveled from the start, and the estimated distance still remaining. Peter Hart then contributed two foundational theoretical ideas - admissibility and consistency of heuristic functions - that would determine when the combined approach actually guarantees an optimal result.

    The resulting algorithm was more than just a practical tool for Shakey. It was later shown that A* can find optimal paths for any problem satisfying the conditions of a cost algebra, well beyond the least-cost path problems it was originally designed to solve.

  • At each step, A* selects the path that minimizes a score combining two values: g(n), the known cost of the path from the start to the current node, and h(n), a heuristic estimate of the remaining cost to the goal. That heuristic function is entirely problem-specific. When searching for the shortest route on a map, for example, h 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 in a video game, the right heuristic depends on what moves are available. A grid with four-way movement calls for the Taxicab distance, while eight-way movement makes Chebyshev distance a better choice. The point in each case is the same: the heuristic guides the search toward the goal without wasting time on obviously unpromising paths.

    This is what separates A* from a greedy best-first search, which uses only the heuristic estimate and ignores the cost already paid. A keeps both terms in play, which is why it does not fall into traps that greedy approaches can stumble into. Dijkstra's algorithm sits at the other extreme: it uses no heuristic at all, effectively setting h to zero for every node, making it a special case of A.

  • A heuristic is called admissible if it never overestimates the true cost to the goal. When A* uses an admissible heuristic, it is guaranteed to return an optimal solution. The intuitive reason is that an admissible heuristic is always optimistic: when the algorithm closes the goal node, its f-score can be no greater than the true distance, yet it also equals the actual length of the path found.

    Consistency, sometimes called monotonicity, is a stricter condition. A heuristic is consistent if, for every edge in the graph, the estimated cost at a node is no greater than the edge length plus the estimated cost at the neighbor. With a consistent heuristic, A* processes every node at most once and is equivalent to running Dijkstra's algorithm on a graph with reduced edge weights.

    The difference matters enormously for performance. When the heuristic is admissible but not consistent, a single node can be expanded many times - in the worst case, an exponential number of times. Dijkstra's algorithm could then outperform A* by a large margin on those inputs. Research has since found that this pathological case only arises in contrived situations where edge weights grow exponentially with graph size, which rarely occurs in practice.

  • The original 1968 A paper included a theorem claiming that no A-like algorithm could expand fewer nodes than A*, given a consistent heuristic and the right tie-breaking rule. A correction was published a few years afterward claiming that consistency was unnecessary for this result.

    That correction was shown to be false in 1985. Rina Dechter and Judea Pearl published a definitive study of A*'s optimal efficiency that year. They produced a concrete counterexample: an A variant using an admissible but inconsistent heuristic that expanded arbitrarily more nodes than an alternative A-like algorithm on the same problem.

    Dechter and Pearl's positive result is equally sharp. They proved that A with a consistent heuristic is optimally efficient with respect to all admissible A-like search algorithms on all non-pathological search problems. Optimality here refers strictly to the set of nodes expanded, not the total number of expansion iterations: a node that gets expanded repeatedly counts as one node in the set, but the work of re-expanding it still costs time.

  • The admissibility criterion guarantees the shortest path, but it also forces A* to examine every path that might be equally meritorious. When a good-enough path is acceptable, relaxing admissibility can dramatically speed up the search.

    The most common form is Weighted A*, which multiplies the heuristic by a constant factor. The resulting path may cost up to a factor of (1 + epsilon) more than optimal, but far fewer nodes are expanded to find it. This guarantee is called epsilon-admissibility, and several variants have been developed around it.

    Dynamic Weighting adjusts the weighting factor based on the depth of the search and the anticipated solution length. The Convex Upward/Downward Parabola variants, known as XUP and XDP, push the tradeoff toward near-optimal paths close to the goal or close to the start respectively, depending on which variant is used. AlphA* takes a different approach by promoting depth-first exploitation, preferring recently expanded nodes by building the search depth into the cost function itself.

  • A* keeps every generated node in memory, which means its space complexity grows at the same rate as its time complexity in the worst case. In practice, this turns out to be the most significant drawback of the algorithm, because memory runs out long before time does on large graphs.

    That limitation drove the development of memory-bounded variants. Iterative deepening A, or IDA, repeatedly restarts the search with a deepening cost threshold, keeping no nodes in memory between passes. Memory-bounded A* and SMA* take a different approach, retaining as many nodes as memory allows and discarding the least promising ones when space runs short.

    For real-world travel-routing systems at large scale, A* is generally outperformed by algorithms that can pre-process the graph offline to attain better runtime performance. Even so, A* remains the best solution across a wide range of cases where pre-processing is not an option or where graph structure changes too frequently to exploit it. The Washington, D.C. to Los Angeles railway routing problem is one illustrative example where A* applies the great-circle distance as its heuristic.

Common questions

Who invented the A* search algorithm?

Peter Hart, Nils Nilsson, and Bertram Raphael of Stanford Research Institute first published A* in 1968. Nilsson originally proposed using Graph Traverser for path planning; Raphael suggested combining traveled and estimated remaining cost; Hart contributed the theoretical concepts of admissibility and consistency.

What was A* originally designed for?

A* was created as part of the Shakey project, which aimed to build a mobile robot capable of planning its own actions. It was originally designed to find least-cost paths, but was later shown to find optimal paths for any problem satisfying the conditions of a cost algebra.

What is the difference between admissible and consistent heuristics in A*?

An admissible heuristic never overestimates the true cost to the goal, which guarantees A* returns an optimal path. A consistent (or monotone) heuristic additionally satisfies a stricter edge condition, ensuring A* processes every node at most once. Without consistency, a node can be expanded an exponential number of times in the worst case.

Why is the space complexity of A* a practical drawback?

A* keeps every generated node in memory, so its space complexity is roughly equal to its time complexity in the worst case. On large graphs, memory is exhausted long before time becomes the bottleneck. This led to the development of memory-bounded variants such as Iterative deepening A (IDA) and SMA*.

How does A* relate to Dijkstra's algorithm?

Dijkstra's algorithm is a special case of A* in which the heuristic function h is set to zero for all nodes. A* achieves better performance than Dijkstra by using a heuristic to guide the search, expanding fewer nodes on average. With a consistent heuristic, A* is equivalent to running Dijkstra on a graph with reduced edge weights.

What is epsilon-admissibility in A* search?

Epsilon-admissibility is a relaxed optimality guarantee used by approximate variants of A. A path found by an epsilon-admissible algorithm costs no more than (1 + epsilon) times the optimal solution. Weighted A, Dynamic Weighting, and the XUP/XDP parabola variants are among the algorithms offering this bound.

All sources

34 references cited across the entry

  1. 1bookArtificial intelligence a modern approachStuart J. Russell et al. — Pearson — 2018
  2. 2bookAlgorithmics of Large and Complex Networks: Design, Analysis, and SimulationD. Delling et al. — Springer — 2009
  3. 4journalA Formal Basis for the Heuristic Determination of Minimum Cost PathsP. E. Hart et al. — 1968
  4. 5journalExperiments with the Graph Traverser programJ. E. Doran et al. — 1966-09-20
  5. 6bookThe Quest for Artificial IntelligenceNils J. Nilsson — Cambridge University Press — 2009-10-30
  6. 9journalBidirectional A* search on time-dependent road networksGiacomo Nannicini et al. — 2012
  7. 10bookArtificial intelligence: A modern approachStuart J. Russell et al. — Pearson — 2009
  8. 11citationGeospatial Analysis: A Comprehensive Guide to Principles, Techniques and Software ToolsMichael John De Smith et al. — Troubadour Publishing Ltd — 2007
  9. 12citationPython Algorithms: Mastering Basic Algorithms in the Python LanguageMagnus Lie Hetland — Apress — 2010
  10. 13journalGeneralized best-first search strategies and the optimality of A*Rina Dechter — 1985
  11. 14journalOn the Complexity of Admissible Search AlgorithmsAlberto Martelli — 1977
  12. 15journalInconsistent heuristics in theory and practiceAriel Felner — 2011
  13. 16conferenceUsing Inconsistent Heuristics on A* SearchZhifu Zhang — 2009
  14. 17journalFirst results on the effect of error in heuristic searchIra Pohl — Edinburgh University Press — 1970
  15. 19journalConditions for Avoiding Node Re-expansions in Bounded Suboptimal SearchJingwei Chen et al. — International Joint Conferences on Artificial Intelligence Organization — 2019
  16. 22conferenceA new approach to dynamic weightingAndreas Köll — Wiley — August 1992
  17. 23journalStudies in semi-admissible heuristicsJudea Pearl — 1982
  18. 25reportAlphA*: An ε-admissible heuristic search algorithmBjørn Reese — Institute for Production Technology, University of Southern Denmark — 1999
  19. 26conferenceA* parsing: fast exact Viterbi parse selectionDan Klein et al. — 2003
  20. 28conferenceA Guide to Heuristic-based Path PlanningDave Ferguson et al. — 2005
  21. 31journalAnytime Heuristic SearchEric A. Hansen et al. — 2007
  22. 33tech reportYet another bidirectional algorithm for shortest pathsWim Pijls et al. — Econometric Institute, Erasmus University Rotterdam
  23. 34webEfficient Point-to-Point Shortest Path AlgorithmsAndrew V. Goldberg et al. — Princeton University