Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Blog/Foundations

How do maps apps find the fastest route?

Dijkstra, A* and shortcut tricks: how a computer picks the best route out of millions of roads in a blink.

Derpx065 Nov · 5 min read
Contents
How does a computer even see a map?What is Dijkstra's method?Why is Dijkstra too slow for a whole country?What does A add?What if we build shortcuts in advance?How does one shortcut get made?What happens when you ask for a route?Which one should I use when?Sources
0% read · 5 min leftBack to top ↑
On this page0%
Contents
How does a computer even see a map?What is Dijkstra's method?Why is Dijkstra too slow for a whole country?What does A add?What if we build shortcuts in advance?How does one shortcut get made?What happens when you ask for a route?Which one should I use when?Sources

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.

How does a computer even see a map?

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?

What is Dijkstra's method?

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.

  1. Start at your dot. Its distance is 0. Every other dot is "unknown, probably far".
  2. Pick the closest dot you have not finished yet. Its distance is now locked in.
  3. Look at its neighbours. If going through it gives a shorter path to a neighbour, update that neighbour.
  4. Repeat until you lock in your destination.

Let's run it on the map above. Click Next and watch which dot gets locked at each step.

School
Back lane
Corner
Home

Start at School with distance 0

Step 1 of 4
Dijkstra on the small map. Updates are the 'if shorter, replace' step.

Home changed from 11 to 8. That is step 3 of the list at work.

A ripple in a pond

Drop a stone in a pond. The ripple reaches the nearest spots first, then the next nearest, and so on. Dijkstra works like that ripple, and it stops when the ripple touches your destination.

It is always correct as long as no road has a negative time. But a ripple has a problem.

Why is Dijkstra too slow for a whole country?

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.

DijkstraThe ripple fills a huge circle around you, in all directions.
→
What we wantYou only want one direction, towards the destination.
Most of the work is thrown away.

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?

What does A* add?

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.

1Cost so far (g)
2Add guess to the goal (h)
3Explore the dot with the smallest total
A* ranks dots by g + h.
Admissible guess

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?

What if we build shortcuts in advance?

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.

Highways and big junctions (most important)
Main roads
Local streets
Tiny lanes (least important)
Nodes are ranked by importance. The bottom is the least important.

What does "doing the work in advance" look like?

How does one shortcut get made?

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?

What happens when you ask for a route?

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.

Which one should I use when?

MethodIdeaCost
DijkstraRipple out from the startSlow on huge maps
A*Ripple, but lean towards the goalNeeds a good guess
Contraction hierarchiesAdd shortcuts in advance, search up from both endsPrep 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.

Key takeaways
  • A map is a graph: dots for junctions, weighted lines for roads.
  • Dijkstra always finds the shortest path, but spreads in every direction, which is too slow for a whole country.
  • A* adds a straight-line guess so the search leans towards the goal.
  • Contraction hierarchies add shortcuts in advance, so each question only needs a tiny search.

Sources

  • Dijkstra, A note on two problems in connexion with graphs, Numerische Mathematik, 1959
  • Hart, Nilsson, Raphael, A Formal Basis for the Heuristic Determination of Minimum Cost Paths, IEEE Transactions on Systems Science and Cybernetics, 1968
  • Geisberger, Sanders, Schultes, Delling, Contraction Hierarchies: Faster and Simpler Hierarchical Routing in Road Networks, WEA 2008
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
Foundations · 3 minWhy is 0.1 + 0.2 not 0.3 in code?Foundations · 4 minHow does Git work inside? (Snapshots, not changes)Foundations · 4 minWhy does one emoji have a length of 5, 7 or 17?
Start here
01Why is 0.1 + 0.2 not 0.3 in code?02How does Git work inside? (Snapshots, not changes)03How does your computer know the time? (And why it can be wrong)