A*: aiming the wave, and how real map apps go even faster
When Google Maps picks the fastest route, or a game character walks around walls toward you
What problem does it solve?
Dijkstra explores in every direction, even away from the goal. Can we aim the search?
The big idea
A* ranks each place by the distance travelled so far plus a guess of the distance still to go, usually the straight-line distance. That guess pulls the wave toward the goal like a magnet, so it explores a narrow ellipse instead of a huge circle. As long as the guess never overestimates (and a straight line can't be longer than a real road), A* still finds the truly shortest path. Set the guess to 0 and you get Dijkstra back.
How it works, step by step
- 1. For each place n, track g(n), the real distance from the start.
- 2. Estimate h(n), the straight-line distance from n to the goal.
- 3. Always expand the place with the smallest f(n) = g(n) + h(n).
- 4. Update neighbours like Dijkstra does, and stop when the goal is expanded.
The key formula
- how far you have actually travelled to reach n
- a hopeful guess of the distance left, which must never overestimate
- the estimated total trip length if you go through n
How fast is it?
Same worst case as Dijkstra, but with a good heuristic it often explores a small fraction of the map. Continental routing needs more: techniques like contraction hierarchies (2008) preprocess the road network once, ranking roads by importance and adding 'shortcut' edges that skip minor roads. A query then climbs up to highways from both ends and meets in the middle, touching only hundreds of nodes instead of millions, often in under a millisecond.
Try it yourself
- Build a maze where A* is barely faster than Dijkstra. Why does a U-shaped wall between start and goal fool the heuristic?
- Why is straight-line distance a safe guess for driving distance, but not for driving time if some roads are faster than you assumed?
Fun fact
A* was invented in 1968 at SRI by Peter Hart, Nils Nilsson and Bertram Raphael to help Shakey, one of the first mobile robots, plan its moves. Google hasn't published exactly how Maps routes. Open-source planners like OSRM use contraction hierarchies, and Google has described using graph neural networks (with DeepMind, 2020) to predict live travel times, the numbers on the edges.