Dijkstra, A* and shortcut tricks: how a computer picks the best route out of millions of roads in a blink.
You type "school to home" and a route shows up before you finish blinking. But how does a computer pick the fastest way out of millions of roads that quickly?
A first guess is that it tries every route and picks the best. That would take far too long. The real answer is a chain of ideas, and each one fixes a problem with the one before it.
It does not see roads. It sees a graph: a set of dots (junctions) joined by lines (road pieces). Each line has a number on it, like minutes of travel. That number is called its weight.
"Fastest route" then means: find the path from dot A to dot B where the numbers add up to the smallest total.
School to Corner directly is 5, but through the back lane it is 2 + 2 = 4. The obvious road is not always the fastest.
So how do we search this graph without trying everything?
In 1959 Edsger Dijkstra published a short note called A note on two problems in connexion with graphs. One of its two problems is exactly this: find the shortest path between two nodes. The method is still taught in every algorithms class.
Let's run it on the map above. Click Next and watch which dot gets locked at each step.
Start at School with distance 0
Home changed from 11 to 8. That is step 3 of the list at work.
It is always correct as long as no road has a negative time. But a ripple has a problem.
The ripple spreads in every direction equally. It also explores roads that point away from your destination, just because they are equally close to you.
A whole country has an enormous number of junctions. Doing this for every "get directions" tap is like reading every page of a book to find one sentence. It works, but not fast enough.
So can the search lean towards the goal?
In 1968 Peter Hart, Nils Nilsson and Bertram Raphael described A*. It adds one thing: a guess of how far each dot still is from the goal. For maps, a good guess is the straight-line distance, because no road can be shorter than a straight line.
Dijkstra asks "which dot is closest to me?". A* asks "which dot is closest to me plus looks closest to the goal?". So the ripple leans towards your destination instead of spreading everywhere.
A guess that never claims "closer" than the truth. Straight-line distance is safe because the real road is never shorter. If your guess lies, A* can return a worse route.
This is better, but A* still searches from scratch every time. What if we did the hard work once?
That is the idea behind contraction hierarchies, from Geisberger, Sanders, Schultes and Delling in 2008. Do the heavy work once, so each question is cheap.
It is like making good notes once, and then just reading them every time you need the answer.
What does "doing the work in advance" look like?
The preprocessing goes through the dots one at a time, least important first. To contract a dot means to remove it from the graph. But first, check its neighbours. If the best way between two neighbours went through that dot, add a shortcut line that jumps over it, with the weight of the whole detour.
Look at the bottom arrow: A to B now costs 7 in one jump, the same as 3 + 4 through V. Nothing is lost. V is just hidden, and the shortcut remembers it so the full route can be rebuilt later.
Now, how do we use all these shortcuts?
The search runs from the start and from the end at the same time. Each side only moves up to more important dots. They meet somewhere near the top, like two people walking towards the same big landmark.
Shortcuts skip the small middle parts, so each side looks at very few dots. Then the shortcuts are unpacked back into real roads for the final route.
| Method | Idea | Cost |
|---|---|---|
| Dijkstra | Ripple out from the start | Slow on huge maps |
| A* | Ripple, but lean towards the goal | Needs a good guess |
| Contraction hierarchies | Add shortcuts in advance, search up from both ends | Prep work up front, and the prep has to be redone if the roads change a lot |
No specific app is claimed to use exactly this. These are the standard published ideas, and real systems mix and tweak them.