Case study demo

Maps routing

Find a shortest path across a small road graph.

An interactive demo of how this system behaves. It runs in your browser.

Case study · Live model

How a maps service picks a route

Cars drive across a small town, from A to I. The main road runs along the top and down the right side. A planner gives each car the route that is fastest by the travel times it knows. It learns those times from reports sent by the cars. Jam a road, slow the reports down, or send every car the same way, and watch the trips.

Try it

Lab time 3.0 s
3 s3 s3 s3 s4 s4 s4 s4 s5 s5 s5 s5 sA · home, cars startA · homecars startB, junctionBjunctionC, junctionCjunctionD, junctionDjunctionE, junctionEjunctionF, junctionFjunctionG, junctionGjunctionH, junctionHjunctionI · work, cars arriveI · workcars arrive
  • A · home — cars start — healthy
  • B — junction — healthy
  • C — junction — healthy
  • D — junction — healthy
  • E — junction — healthy
  • F — junction — healthy
  • G — junction — healthy
  • H — junction — healthy
  • I · work — cars arrive — healthy
  • A car
  • A car in the jam
Trip time
–
from A to I, cars that arrived in the last 10 s
Planner's fastest route
ABCFI
12 s by its reports; 12 s right now
Age of the reports
1.0 s
new ones every 2 s
Drove into the jam
0
there is no jam now
Route changes
0
times the planner's fastest route changed
Cars on the road
5
0 have arrived

Numbers add up:5 cars started = 0 arrived + 5 on the roadThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

Traffic
2 a second
The planner
every 2 s

How often the planner gets the travel time of every road from the cars on it.

Off: every new car gets the one route that is fastest by the reports.

Routes

RouteTakes nowPlanner believesShare of the last 20 carsNote
ABCFI12 s12 s60%the planner's fastest
ABEFI16 s16 s20%
ABEHI17 s17 s0%
ADGHI16 s16 s20%second fastest
ADEFI17 s17 s0%
ADEHI18 s18 s0%

A route is named by its junctions: ABCFI goes from A through B, C and F to I.

Road by road

RoadKindCars on itTakes nowPlanner believesState
A to Bmain road43 s3 sclear
B to Cmain road03 s3 sclear
C to Fmain road03 s3 sclear
F to Imain road03 s3 sclear
A to Dback road14 s4 sclear
D to Gback road04 s4 sclear
G to Hback road04 s4 sclear
H to Iback road04 s4 sclear
B to Etown street05 s5 sclear
D to Etown street05 s5 sclear
E to Ftown street05 s5 sclear
E to Htown street05 s5 sclear
Trip time from A to I (seconds), and cars on the road
Trip time (s)Cars on the road
02.557.5101s2s3s
Show data table
TimeTrip time (s)Cars on the road
1s01
1s02
2s03
2s03
3s03
3s05

What just happened

Nothing yet. Start traffic or run an experiment.

What this model simplifies
  • The town has 9 junctions and 12 one-way roads, and every car goes from A to I. A real map has hundreds of millions of roads, so the planner cannot try every route; it works out shortcuts between far-apart places ahead of time. One well-known way to do this is called contraction hierarchies.
  • A trip here takes seconds. The roads behave like real ones, only faster: a road takes longer the fuller it is.
  • A jam makes one road 6 times slower. Real jams grow and shrink, and spread back to the roads behind them.
  • A car keeps the route it was given at the start, and a car in the jam keeps its slow time when the jam clears. Real apps check the route again during the trip.
  • Every car reports, and the planner trusts every report. A real service hears from only some of the cars, and must guess the rest.
  • The planner here knows nothing about the cars it has already sent. Real planners also guess what a road will be like when the car gets there.

Design walkthrough: Maps routing

A maps app finds the fastest way to drive from A to B. Roads are stored as a graph. Each junction is a point, and each road between two junctions is a link with a travel time.

Requirements

What it must do

  • Return the fastest route between two points.
  • Use live traffic, so the route avoids a jam.
  • Give an arrival time and update it during the trip.

How well it must do it

  • Answer in under a second, even for a route across a country.
  • Traffic data may be a minute or two old.

Rough numbers

  • A country's road graph has tens of millions of junctions. A plain search could visit most of them for one long route.
  • If 1 million phones report every 5 seconds, that is 200,000 speed reports a second.

How a request flows

Read pathFind a route

  1. Maps app
  2. Routing API
  3. Route planner
  4. Road graph
  5. Live traffic
  1. Maps app to Routing API

    The app sends the start, the end and the travel mode.

  2. Routing API to Route planner

    The planner moves both points onto the nearest road.

  3. Route planner to Road graph

    It searches outward from the start, always extending the cheapest path so far. This is Dijkstra's algorithm.

  4. Route planner to Live traffic

    Each road's cost is its current travel time, so a jammed road costs more.

  5. Route planner to Maps app

    It returns the route, the distance and the arrival time.

Write pathLearn live traffic

  1. Phones on roads
  2. Live traffic
  3. Route planner
  4. Maps app
  1. Phones on roads to Live traffic

    Phones that are moving report their speed and position every few seconds.

  2. Live traffic to Route planner

    A stream job matches each report to a road. It averages the speed on each road over the last few minutes.

  3. Route planner to Maps app

    If a trip in progress now has a faster route, the app offers it.

Scaling it

  • Work out shortcuts between major junctions ahead of time. A long route then uses a few shortcut links, not millions of small roads. One method for this is called contraction hierarchies.
  • Split the map into regions. A route inside one city only loads that city's part of the graph.
  • Cache popular routes for a minute or two, such as airport to city centre.

What breaks, and what happens then

  • The traffic feed stops

    What users see
    Live speeds go stale.
    What the design does
    Fall back to the typical speed for that hour and day. Routes are still good, only less exact.
  • A road closes suddenly

    What users see
    Routes still send drivers down it.
    What the design does
    Many phones slowing down or turning around is itself a signal. Mark the road as blocked until speeds recover.
  • Long routes overload a planner server

    What users see
    Short routes wait behind slow ones.
    What the design does
    Give each search a time limit, and send long routes to a separate pool of servers.

Trade-offs

  • The exact shortest path, or a faster search that is close to it
    A good default
    Use shortcuts worked out ahead of time. Answers arrive in milliseconds.
    What it costs
    Shortcuts must be rebuilt when roads change, and live traffic is harder to mix in.
  • Send everyone down the fastest road, or spread them out
    A good default
    Spread drivers across routes that are nearly as fast.
    What it costs
    Some drivers get a route that is slightly slower on paper.

Common mistakes

  • Using distance as the cost. The shortest road is often not the fastest.
  • Running a full search over the whole graph for every request.

Questions an interviewer might ask

How do you estimate the arrival time?

Add up the current travel time of each road on the route. Better systems also predict how traffic will change before you reach each road.

How would you support walking and cycling?

Use the same graph. Each mode has its own allowed roads and its own cost per link.

How are the map pictures served?

The map is cut into small tiles for each zoom level and served from a CDN. Routing is separate: it needs the graph, not the pictures.