Thought Toys · Strategy & computation · Exhibit 36

A* pathfinding

Dijkstra floods outward evenly until it happens to reach the goal. A* instead guesses — it favors squares that look closer to the goal — and explores far fewer of them for free. Push that guess too hard, though, and the search can lock in a longer, wrong route before the true shortest one is ever found.

A maze with a start cell on the left and a goal on the right, scattered walls between them. Squares fill in as the search visits them; the final route is drawn in amber, and when it is wrong, the true shortest route is drawn as a dashed cyan line for comparison.

Turn up the guess and watch the search narrow.

your turn — drag the guess dial past 1

What you're seeing

A maze of squares, some blocked. Getting from the start to the goal costs one step per square. Dijkstra (exhibit 26's engine) finds the cheapest route by growing outward from the start in every direction at once, like a ripple — reliable, but it wastes effort exploring squares that lead nowhere near the goal.

A* adds a guess: for every square it's considering, it adds a heuristic — an estimate of the remaining distance, here the straight-line (Manhattan) distance to the goal, and explores the most promising-looking square first. Turn the guess dial to 1 and the search is still provably exact — same answer as Dijkstra — but it visits far fewer squares, because the guess is never overconfident: it can under-promise (a wall might make the true route longer) but never over-promise. That's an admissible heuristic — one that never overestimates the true remaining cost, and it's the whole reason A* can be both fast and correct.

Push the dial past 1 and the guess starts to dominate the cost actually paid so far. On this maze, once it crosses roughly 4, the search convinces itself a longer route is fine because a few of its squares look close to the goal — and by the time the true shortest route would have been found, the search has already locked in the wrong answer and stopped. Hit show the true shortest route to see exactly how much longer the wrong one is.

The rule, exactly. Order the frontier by f(n) = g(n) + w·h(n) — cost-so-far plus w times the estimated remaining cost — and stop as soon as the goal is popped as the best candidate. Verified in node (improve/verify/36-astar.js): at w≤1 this matches an independent breadth-first reference on 263 solvable random mazes, every single time; on the maze shown here, w=1 finds the true shortest route (36 steps) while exploring markedly fewer squares than w=0 (plain Dijkstra); past w=4 the route jumps to 52 steps — a real 44% detour, not a rounding error. Counter-example: on an obstacle-free grid, no weight — however large — ever produces a wrong answer, because the straight-line guess is then exact, with nothing for it to overestimate around. It's the combination of a scaled-up guess and a wall that fools the search, not the weight alone.

Also in Strategy & computation: Braess's paradox →

All 23 in Strategy & computation
  1. 10The evolution of trust
  2. 23Sorting algorithms
  3. 24PageRank & the random surfer
  4. 25Huffman coding
  5. 26Dijkstra's shortest path
  6. 27Nash equilibria
  7. 33The learning-rate cliff
  8. 36A* pathfinding — you are here
  9. 37Braess's paradox
  10. 44Diffie–Hellman key exchange
  11. 45Preferential attachment
  12. 46Aliasing & the Nyquist limit
  13. 47The secretary problem
  14. 49Freeze too fast, stay stuck
  15. 51Cross one line, and its territory closes
  16. 56Catch one error, miss the next
  17. 57Why more processors stop helping
  18. 58Why a busy line explodes
  19. 59The set that's only sure when it says no
  20. 60The fit that memorizes instead of learns
  21. 61When the wire breaks, pick one
  22. 67Better at both, and still better off trading
  23. 69Everyone was consistent. The vote wasn't.

← the cabinet · Thought Toys — a cabinet of explorable explanations. Exhibit 36.