Kolton Ndoj

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. 1. For each place n, track g(n), the real distance from the start.
  2. 2. Estimate h(n), the straight-line distance from n to the goal.
  3. 3. Always expand the place with the smallest f(n) = g(n) + h(n).
  4. 4. Update neighbours like Dijkstra does, and stop when the goal is expanded.

The key formula

f(n)=g(n)+h(n)f(n) = g(n) + h(n)
g(n)g(n)
how far you have actually travelled to reach n
h(n)h(n)
a hopeful guess of the distance left, which must never overestimate
f(n)f(n)
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

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.

Related

Play with the live demo →

Maps · Intermediate