How the A* search algorithm works
How A* finds the cheapest route through a network by adding the cost so far to a careful guess of the cost still ahead.
Written by Amili, an AI writer, from the sources listed below · 24 October 2026 · 6 min read
A* (said 'A-star') is a pathfinding method that finds the lowest-cost route between two points in a weighted graph. It ranks each partial route by the distance already covered plus an estimate of the distance remaining, so it explores promising directions first while still guaranteeing the best answer under the right conditions.
In short
- A* scores every candidate node with f(n) = g(n) + h(n): cost so far plus estimated cost to go.
- If the estimate never overstates the true remaining cost, A* returns an optimal path.
- With an estimate of zero, A* behaves exactly like Dijkstra's algorithm.
- Its main weakness is memory: it may hold a huge number of partial paths at once.
- Inflating the estimate trades a guaranteed best route for a faster, near-best one.
How does A* decide where to look next?
A* treats a map or network as a graph: points are nodes, connections are edges, and each edge carries a cost such as distance or travel time. Starting from the source node, it grows a tree of partial paths, lengthening one of them by a single edge on every round until one reaches the goal.
The interesting part is how it picks which path to lengthen. For each frontier node n it adds two numbers. The first, g(n), is the real cost already paid to get from the start to n. The second, h(n), is a heuristic: a problem-specific guess at the cheapest remaining cost from n to the goal. The node with the smallest total, f(n), is expanded next.
In practice the frontier lives in a priority queue, often called the open set. The cheapest-looking node is removed, its neighbours get updated scores, and they join the queue. When the node pulled off the queue is the goal, the search stops, and its score is the cost of the shortest route. Storing a pointer from each node to its predecessor lets the program walk backwards and recover the route itself.
What does a worked example look like?
Imagine a small road map with five towns, where the task is to drive from town S to town G. A sensible heuristic is the straight-line distance to G, because no road can be shorter than a straight line. Suppose S connects to A and B. Road S to A is 4 km and A sits 6 km from G as the crow flies, so A scores 10. Road S to B is only 2 km, but B lies 11 km from G in a straight line, so B scores 13.
A* expands A first, even though B is closer to the start, because A looks better overall. If the roads beyond A then turn out to be long and winding, A's descendants pick up larger scores, and B eventually becomes the cheapest item in the queue. The search never commits early; it simply keeps opening whichever option currently looks least expensive in total.
On grid maps, such as those in video games, the right heuristic depends on how units can move. Taxicab distance suits four-way movement, while Chebyshev distance suits eight-way movement that includes diagonals.
Why does the quality of the guess matter so much?
A heuristic is called admissible when it never overestimates the true remaining cost. With an admissible heuristic, A* is guaranteed to return an optimal path. A stricter property, consistency, requires that the guess at one node is no larger than the edge cost to a neighbour plus the guess at that neighbour. With a consistent heuristic, A* never needs to process any node twice.
The closer the heuristic comes to the true distance, the fewer nodes A* has to examine, and an exact estimate would examine the fewest of all. Setting the heuristic to zero everywhere removes the guidance entirely and turns A* into Dijkstra's algorithm. Pushing the estimate above the true distance makes the search faster still, but the optimality guarantee is lost. Weighted A* does this on purpose, multiplying an admissible heuristic by a factor greater than one; the route it returns costs at most that factor times the best route.
Where is A* used?
The algorithm grew out of the Shakey project at Stanford Research Institute, which aimed to build a mobile robot able to plan its own actions. Peter Hart, Nils Nilsson and Bertram Raphael published it in 1968. It is now common across computer science, from route planning on maps to moving characters around video game worlds, where a slightly imperfect but fast route is often preferred.
For very large game maps, hierarchical variants such as Hierarchical Path-Finding A* split the map into clusters, plan a coarse route between them, and then refine it locally. This shrinks the search and yields routes that are close to optimal at much greater speed.
Where does A* struggle?
Memory is the main practical cost. Because A* keeps every frontier node in its queue, storage can grow exponentially with the depth of the shortest solution. Real travel-routing systems are generally beaten by methods that pre-process the road graph in advance, such as contraction hierarchies, and by memory-bounded approaches.
A poor heuristic also hurts. If the estimate is weak, A* wastes effort expanding nodes that lead nowhere. If it is admissible but not consistent, a single node can in the worst case be expanded an exponential number of times, though later research found this only arises in contrived graphs. Implementation details matter too: how the queue breaks ties between equal scores can noticeably change running time.
What does A* teach about thinking?
A* is a tidy model of balancing past and future. Judging an option only by what has already been spent ignores where it leads; judging only by how close the goal looks ignores the cost of getting there. The earlier Graph Traverser method used the second approach alone, and adding the two together was the key improvement behind A*.
It also shows the value of an honest, optimistic estimate. A guess that is never too pessimistic keeps the search both efficient and correct, while an inflated guess buys speed at the price of certainty. Knowing which of those trades a task can afford is the real decision.
Questions people ask
What is the difference between A* and Dijkstra's algorithm?
Dijkstra's algorithm always expands the node with the lowest cost from the start, spreading out evenly in every direction. A* adds a heuristic estimate of the remaining distance, which steers the search toward the goal and usually means fewer nodes are examined. When that estimate is zero for every node, the two algorithms behave identically, so Dijkstra's can be seen as a special case of A*.
Does A* always find the shortest path?
Only when its heuristic is admissible, meaning it never claims the remaining cost is larger than it really is. Under that condition A* returns an optimal path. On finite graphs with non-negative edge costs it will always find a route if one exists. If the heuristic overestimates, as in weighted A*, the search runs faster but may settle on a route that is somewhat longer than the best.
What heuristic should be used for A* on a grid?
It depends on the allowed moves. For four-way movement (up, down, left, right), taxicab distance is the natural choice. When diagonal steps are allowed, giving eight-way movement, Chebyshev distance fits better. On real road maps, straight-line distance works well because no route between two points can be shorter than the straight line joining them.
The thinking behind it
Its chapters on graphs and shortest paths give the formal grounding behind Dijkstra's algorithm, on which A* builds.
Read or listen to Introduction to Algorithms
Hear the whole book free: start an Audible trial and your first audiobook — this one, if you like — is on the house.
As an Amazon Associate, ReadGlobe earns from qualifying purchases and Audible trials — at no extra cost to you.
Sources
- A* search algorithm — Wikipedia
- Pathfinding — Wikipedia
How this was made: Amili, an AI writer, wrote this article in its own words from the sources above. Every link was checked before publishing. Spotted an error? Tell us and we will correct it.