Skip to content

← Math Further 9–12

How Does a Map App Find the Quickest Route?

Topic 27 · Grades 9–12 · Shortest paths

What this builds on from class
In this unit 8 sections
  1. Watch the search spread
  2. Aim at the goal
  3. When minutes spoil the aim
  4. Landmarks
  5. Shortcuts
  6. Where this is used
  7. Questions
  8. Where to go next

Your cousin is getting married in Pokhara, and the family is driving from Bhaktapur. Your uncle types “Pokhara” into his phone, and before you have finished reading the word, a blue line appears along the roads, with a time beside it. The phone’s map holds every junction of every road in Nepal and far beyond. How did it pick the quickest way among all of them so fast, and how can it be sure?

The Grades 6–8 topic What Is the Quickest Way Across the Map? gives a method that is always right: nearest first, Dijkstra’s algorithm, which fixes the junction with the smallest time each round. Watch it search a made-up town.

Try it

Watch nearest first search

A town of streets in a grid, every block a kilometer, with a river crossed by three bridges, a lake and two highways. Press Watch it search: the blue dots are the junctions nearest first looks at, in order, and the red line is the quickest route it finds. Drag the Start or the Goal and watch again.

StartGoal
Quickest route
10 km
Junctions looked at
193 of 555

Nearest first looked at 193 of the 555 junctions before it reached the Goal.

Drag the Start or the Goal, or tap the map to move the nearer one.

Guess first. The Goal is moved twice as far from the Start, in open streets. About how many times as many junctions will nearest first look at?

Pick an answer to see why.

Watch the search spread

Nearest first fixes junctions in the order of their time from the Start. So by the time it fixes the Goal, it has fixed every junction that is quicker to reach than the Goal, in every direction, even behind the Start. The patch is centered on the Start, with the Goal on its edge: a diamond in this town, where streets run only east–west and north–south, and a rough circle on a map with roads every way.

Either way, its area grows as the square of its reach. A circle of radius r has area πr², and a diamond reaching r blocks each way has area 2r². Ten times the trip, a hundred times the work: fine for a town, hopeless for a continent. In 2015, eight route-planning researchers measured it on a standard test map, the roads of Western Europe, with 18 million junctions. For a trip between two junctions picked at random, nearest first looked at about 9.3 million junctions, half the continent, and took 2.2 seconds on a fast computer. A map app that answers millions of people can’t spend 2 seconds on each.

Search from both ends

The first trick: start a second search at the Goal, working backwards along the roads, and take turns. Each only has to reach about halfway.

Each search reaches about half as far as the one search did. What share of the junctions do the two searches look at together?

Pick an answer to see why.

But both searches still spread out in every direction, even away from each other. Looking for Pokhara from Bhaktapur, you wouldn’t check the roads to Biratnagar first: you know which way Pokhara is.

Aim at the goal

Can a search use the direction without losing the guarantee of the quickest route? Yes, with one more number for each junction: a lower bound on the time still left to the Goal, a number the real time can never be less than. Call it h(v) for junction v. Then, instead of fixing the junction with the smallest time so far, fix the one with the smallest

time so far+h(v), \text{time so far} + h(v),

the junction that seems to be on the quickest whole journey. This search is A* (said “A star”), and a lower bound used this way is called a heuristic.

The easiest lower bound is the straight line: no road is shorter than the straight line between its ends, so when kilometers are added up, the straight-line distance to the Goal, worked out from the junctions’ positions by the distance formula, is a lower bound.

Try it

Four ways to search

The same town. Choose a search and press Watch it search; the table compares how many junctions each one looks at for the same Start and Goal. Leave Add up on Kilometers for now.

StartGoal
Search:
Add up:
Quickest route
10 km
Junctions looked at
193 of 555
Nearest firstBoth endsAimLandmarks
193823711

Nearest first looked at 193 of the 555 junctions before it reached the Goal.

Drag the Start or the Goal, or tap the map to move the nearer one.

What you should see

Why aiming is still exactly right

Aiming changes the order in which junctions are fixed. Why is the answer still the quickest route? Because A* is nearest first in disguise.

Give every road from u to v a new weight:

new weight=old weight−h(u)+h(v). \text{new weight} = \text{old weight} - h(u) + h(v).

Look at what this does to a whole route. Here is a route from S through a and b to the Goal G, with the old weights 4, 3 and 5 minutes and the lower bounds h(S) = 9, h(a) = 6, h(b) = 4, h(G) = 0.

Road Old weight − h(start of road) + h(end of road) New weight
S to a 4 − 9 + 6 1
a to b 3 − 6 + 4 1
b to G 5 − 4 + 0 1
Total 12 3

Each h is added on one road and taken away on the next, so they cancel in pairs, like a sum that telescopes. Only the first and last survive:

new total=old total−h(S)+h(G)=12−9+0=3. \text{new total} = \text{old total} - h(\text{S}) + h(\text{G}) = 12 - 9 + 0 = 3.

The same happens on every route from S to G: each total goes down by the same amount, h(S) − h(G). (Another route, S to c to G, with old weights 6 and 7 and h(c) = 5, goes from 13 to 2 + 2 = 4, which is 13 − 9.) So the quickest route with the old weights is the quickest with the new.

Now the disguise. With the new weights, a junction’s time from S is its old time so far − h(S) + h(v). Taking the same h(S) from every junction changes nothing about which is smallest, so nearest first on the new weights fixes junctions in the order of time so far + h(v): it is A*.

And nearest first is right as long as no weight is negative, as the 6–8 topic showed with its treasure path. So A* is exactly right whenever every new weight is zero or more:

h(u)≤old weight of the road from u to v+h(v)for every road. h(u) \le \text{old weight of the road from } u \text{ to } v + h(v) \quad \text{for every road}.

A lower bound with this property is called consistent. In words: driving one road can’t bring the bound down by more than the road takes. The straight line passes, because of the triangle inequality: one side of a triangle is never longer than the other two together. The straight line from u to the Goal is at most the straight line from u to v plus the straight line from v to the Goal, and the road from u to v is at least as long as the straight line between them.

A search uses an estimate that is sometimes more than the real time left. What can go wrong?

Pick an answer to see why.

The ellipse

How much does aiming save? Picture open country where you can walk anywhere, with a lake between the Start and the Goal, so that the quickest way goes round it and is longer than the straight line D between them: k times as long, so of length kD, with k = 1.25.

A* only fixes a junction P whose time so far plus h(P) is at most the route’s length: until the Goal is fixed, some junction on the quickest route is waiting with a total of at most kD, so a larger total never comes first. The time so far is at least the straight line from the Start to P, and h(P) is the straight line from P to the Goal. So every junction A* fixes has

SP+PG≤kD. SP + PG \le kD.

The points whose distances to two fixed points add up to at most a fixed amount fill an ellipse, with the two fixed points as its foci. Gardeners draw one with a loop of string round two pegs, the pencil keeping the string tight. Grade 10 Optional Mathematics meets the ellipse as a slice through a cone; here it turns up as the shape of a search.

Nearest firstStartGoalArea: 100%Both endsStartGoalArea: 50%AimStartGoalArea: 15% Nearest firstStartGoalArea: 100%Both endsStartGoalArea: 50%AimStartGoalArea: 15%
Figure 27.1 Where each search can look, at the same scale, when the quickest route (red) goes round a lake and is 1.25 times the straight line. Nearest first: a circle round the Start, reaching as far as the route is long. Both ends: two circles of half that radius, half the area. Aim: the ellipse with the Start and the Goal as its foci, 15% of the area.

The ellipse’s long axis is the route’s length, kD, so its half-axes are a = kD/2 and b = (D/2)√(k² − 1), from Pythagoras’ theorem: the end of the short axis is equally far from both foci, so each distance is half of kD, and it makes a right triangle with the half-axis b and half the distance D between the foci. Its area is πab, against π(kD)² for nearest first’s circle, so

ellipsecircle=k2−14k. \frac{\text{ellipse}}{\text{circle}} = \frac{\sqrt{k^2 - 1}}{4k}.

For k = 1.25 that is 0.75 ÷ 5 = 15%. The straighter the quickest route (k close to 1), the thinner the ellipse; when the route is the straight line itself, k = 1 and the ellipse squeezes down to the line.

When minutes spoil the aim

A map app doesn’t add up kilometers. It adds up minutes, because a long highway can be quicker than a short road through a crowded bazaar. Then the straight line must be turned into a time, and it has to stay a lower bound: the straight-line distance divided by the fastest speed anywhere on the map. Nothing can go faster, so nothing can arrive sooner.

But most roads are much slower. On the Western Europe test map the fastest roads were given 130 km/h and the slowest 10 km/h, so for a trip through town streets, “straight line at 130 km/h” says a few minutes when the truth is half an hour. A bound that low hardly aims; the researchers found the gain on road maps timed this way “small or non-existent”.

Landmarks

The fix is to measure instead of guessing from geometry. Before anyone asks, pick a few landmarks, junctions near the edges of the map, and work out once, by nearest first, the quickest time from each landmark to every junction. Store the tables. Now the triangle inequality gives a much better lower bound.

Landmark LJunction vGoal Gd(L, v)d(v, G)d(L, G)
Figure 27.2 A landmark L, a junction v and the Goal G. The two purple times are in the landmark’s table; the red one is the time still to go.

Going from the landmark L to the Goal G by way of v can’t be quicker than the quickest way from L to G:

d(L,G)≤d(L,v)+d(v,G),sod(v,G)≥d(L,G)−d(L,v). d(L, G) \le d(L, v) + d(v, G), \quad \text{so} \quad d(v, G) \ge d(L, G) - d(L, v).

Both numbers on the right are in L’s table, and if the landmark lies behind v, seen from the Goal, the bound is close to the truth. On two-way roads the times the other way round give a bound too, d(L, v) − d(L, G); with several landmarks, take the largest bound. It is consistent, so A* stays exactly right, and it is measured in whatever the map adds up, so it aims as well with minutes as with kilometers.

This method is called ALT, for A*, landmarks and triangle inequality. Andrew Goldberg and Chris Harrelson published it in 2005. In the widget, choose Landmarks: the four purple diamonds are its landmarks, near the corners. Over the same 150 random pairs it looked at about 15% of nearest first’s junctions, with minutes and with kilometers alike.

The price is paid in advance, in tables. That idea, work done once before anyone asks, so that every question is quick, leads to the fastest method.

Shortcuts

Driving from Bhaktapur to Pokhara, you leave the side streets as soon as you can, stay on the highway, and leave it only near the end. Most junctions are side streets that no long trip passes through. The best method builds this into the map with shortcuts.

Remove the junctions one at a time, the least important first. When you remove a junction v, look at each pair of its neighbors. If the only quickest way between them went through v, add a shortcut road joining them directly, with the time of that way. Otherwise another way is as quick, and nothing is needed. Either way, every quickest time between the junctions left is kept.

Try it

Remove the junctions

Nine junctions and their roads, with times in minutes. Tap the junctions one at a time to remove them. A curved purple road is a shortcut. Can you remove all nine without adding a single shortcut? When all are removed, tap two junctions to ask for the quickest time between them.

647477762537ABCDEFGHI
Junctions removed
0 of 9
Shortcuts added
0

Tap a junction to remove it. If its neighbors’ only quickest way went through it, a shortcut keeps that time.

Hint
Answer

The order you removed the junctions in gives them a rank, 1 for the first removed up to 9 for the last. This is a contraction hierarchy (to contract a junction is to remove it this way). Robert Geisberger, Peter Sanders, Dominik Schultes and Daniel Delling published the method in 2008.

Asking a question

A question is then answered by two searches, one from each end, each moving only upward, along roads and shortcuts to junctions of higher rank. The smallest total through a junction both reached is the quickest time. In the widget, the blue and green roads were the two searches.

Why can they never miss? Take any quickest route. Wherever it dips to a junction ranked lower than both junctions next to it on the route, that junction was removed while both of them were still there. So either a shortcut joined them, or another way at least as quick went round through junctions removed later. Swap that in and the dip is gone, and the route is no slower. Each swap takes out one junction and puts in only junctions ranked higher, so the swapping can’t go on forever. When no dips are left, the route climbs from the Start to its highest junction and comes down to the Goal. The upward search from the Start finds the climb, the upward search from the Goal finds the descent backwards, and they meet at the top.

Where this is used

Questions

★ Nearest first is used on a large flat map with roads in every direction. The Goal is moved three times as far away. About how many times as many junctions does it look at?

Pick an answer to see why.

a.★

On a map where the fastest road allows 80 km/h, a junction is 12 km in a straight line from the Goal. What lower bound on the time left, in minutes, can A* use there? Why may it not use “12 km at 40 km/h, so 18 minutes”, even if most roads there are 40 km/h roads?

Answer
b.★★

A route goes P, Q, R, T with times 5, 8 and 6 minutes. A lower bound gives h(P) = 15, h(Q) = 11, h(R) = 4 and h(T) = 0. Work out each road’s new weight, old weight − h(start) + h(end), and check that the new total is the old total minus h(P). Is any new weight negative?

Answer
c.★★

Two landmarks have tables. From landmark 1: 50 minutes to the Goal, 18 minutes to junction v. From landmark 2: 12 minutes to the Goal, 40 minutes to v. What lower bound on the time from v to the Goal do they give?

Hint
Answer
d.★★★

Five junctions lie along one road in a row, A, B, C, D, E, with times 3, 4, 2 and 5 minutes between neighbors. They are removed one at a time as in the shortcut widget. Which order adds no shortcuts at all? Which shortcut does removing C first add? What is the most shortcuts any order can add?

Answer
e.★★★

Show that a consistent bound with h(Goal) = 0 never says more than the real time left. That is, show h(v) ≤ (the quickest time from v to the Goal).

Hint
Answer

Where to go next

Where these facts come from
  • The Western Europe test map (18.0 million junctions; road categories at 10 to 130 km/h), nearest first’s 9,326,696 junctions and 2.2 seconds, both ends’ 4,914,804 and about half, contraction hierarchies’ 5 minutes of preparation, 280 junctions and 110 microseconds, the straight-line bound divided by the top speed being “poor” with a gain “small or non-existent”, landmarks and the triangle inequality, the contraction rule (a shortcut when the quickest way is unique and passes the junction) and the upward query, companies’ secrecy, OSRM using contraction hierarchies, Bing Maps using CRP and TomTom a variant of arc flags with shortcuts, CRP splitting the map into regions and rerunning only its fast customization stage for new costs: H. Bast, D. Delling, A. Goldberg, M. Müller-Hannemann, T. Pajor, P. Sanders, D. Wagner and R. F. Werneck, “Route Planning in Transportation Networks”, arXiv:1504.05140 (2015), sections 2.1–2.3 and 3, Table 1, read October 2026.
  • ALT by Goldberg and Harrelson (SODA 2005), A* by Hart, Nilsson and Raphael (1968), and contraction hierarchies by Geisberger and colleagues: the same survey’s references 148, 156 and 142; the 2008 conference paper by Geisberger, Sanders, Schultes and Delling: Wikipedia, “Contraction hierarchies”, read October 2026.
  • OSRM offering Contraction Hierarchies and Multi-Level Dijkstra (with its partition and customize steps), and recommending the second by default: OSRM’s README, read October 2026.
  • Shakey, Nilsson’s first method, Raphael’s suggestion of the sum, Hart’s conditions on the estimate: Wikipedia, “A* search algorithm”, History, quoting N. J. Nilsson, The Quest for Artificial Intelligence (2009; not opened), read October 2026.
  • A*’s two properties, the rejections and the reviewers’ “ten nodes and … ten trillion nodes”, publication in IEEE Transactions on Systems Science and Cybernetics in 1968, Curiosity’s Field D*, “variants and elaborations of A*” in maps, and Steven Woodcock’s “far and away the most used”: B. Kuipers, E. A. Feigenbaum, P. E. Hart and N. J. Nilsson, “Shakey: From Conception to History”, AI Magazine 38(1), 88–103 (2017), read October 2026.
  • Shakey built at the Stanford Research Institute from about 1966 to 1972, now at the Computer History Museum: Wikipedia, “Shakey the robot”, read October 2026.
  • Every number from the town widget (its 555 junctions, the counts of junctions looked at, the 150 random pairs), the nine-junction puzzle (6 shortcuts for E first, 18 at most, 6,192 orders with none) and the questions was computed by programs of our own, which also check that every search gives the same quickest time.