Algorithms, visualizedExhibit 3

How does Google Maps find the fastest route?

It turns the road map into a graph: intersections become dots, roads become lines, and every line gets a travel time. Then a shortest-path search finds the chain of lines with the smallest total, without trying every route. Dijkstra’s algorithm ripples outward from the start; A* aims that ripple at the goal. Draw some walls below and race them.

Try it: race Dijkstra against A*

Draw on either map with your finger or mouse. Drag the green start or the red goal to move them. A normal block takes 1 minute; a jammed block takes 5 minutes.

Draw
Dijkstraready
A*ready

Moves go up, down, left and right, like city blocks. A* guesses the time left as the block distance to the goal × 1 minute, which can never be more than the real time, so both always find a route with the same travel time.

Watch: how Google Maps finds the fastest route

From a 1956 coffee-break idea to traffic prediction, in 5 minutes.

The video loads from YouTube only when you press play. Watch on YouTube

Why can’t a computer just try every route?

Because the number of routes explodes. Take a tiny grid of ten by ten city blocks. Any shortest trip from one corner to the opposite one is 20 moves: 10 east and 10 south, in some order. Choosing which 10 of the 20 moves go east gives 184,756 different shortest routes, and that’s before counting any detours. Real road networks have millions of intersections. Listing every route and adding each one up would never finish.

How does a computer see a road map?

Not as a picture but as a graph. Every intersection is a dot, called a node. Every stretch of road between two intersections is a line, called an edge. And every edge gets a number: not how long the road is, but how long it takes to drive. A clear highway gets a small number; a crawling city street gets a big one.

The question then becomes: which chain of edges from your start to your destination adds up to the smallest total? In the demo, every square is an intersection, every step between neighbours is an edge, and the numbers are 1 minute for a clear block and 5 minutes for a jammed one.

How does Dijkstra’s algorithm work?

In 1956, a young Dutch programmer, Edsger Dijkstra, needed a demonstration for a new computer, the ARMAC. He picked a question anyone could check on a map: the shortest way from Rotterdam to Groningen. He worked out the method on a café terrace in Amsterdam with his fiancée, in about twenty minutes and without pencil or paper. He published it in 1959.

The idea fits in four steps:

  1. Start where you are, at a time of zero.
  2. Write down how long it takes to reach each neighbour.
  3. Pick the closest place you haven’t finished yet and lock it in. Its time is final, because any other way there would have to pass through somewhere at least as far away.
  4. From that place, check its neighbours. If going through it is quicker than what you wrote down, replace the number. Repeat.

The search spreads outward like a ripple in a pond, in order of travel time. When the destination gets locked in, its time is the fastest possible: guaranteed. That’s the blue ripple in the demo.

Why is A* faster than Dijkstra?

A ripple spreads in every direction. Drive from Amsterdam to Rotterdam and plain Dijkstra also explores roads toward Germany, just in case. Researchers test route planners on a model of Western Europe’s roads with about 18 million intersections, and a long trip can make plain Dijkstra scan millions of them.

The next idea came in 1968, from a robot. At the Stanford Research Institute, Peter Hart, Nils Nilsson and Bertram Raphael were building Shakey, a robot that had to plan its own path around a room. Their algorithm, A*, works like Dijkstra, but every place gets a second number: a guess of how much is still left, based on the straight-line distance to the goal. Places that are close to the start and point toward the goal get finished first, so the ripple stretches toward the destination.

The rule that keeps it exact: the guess must never overestimate. In the demo the guess is the block distance to the goal at 1 minute per block, the fastest you could possibly go, so A* always finds a route as fast as Dijkstra’s. Try an empty map: A* walks almost straight to the goal while Dijkstra floods the whole grid.

How can a route across a whole country take less than a second?

When millions of people ask for directions at once, even a smarter ripple isn’t enough. So routing engines do most of the work before you ask. One well-known technique is called contraction hierarchies.

Think about a long drive: small streets near the start, then bigger roads, then the highway, then back down at the end. So ahead of time, the computer ranks every intersection by importance and removes the least important ones one by one. Whenever removing one would break a fastest route, it adds a shortcut edge that remembers the drive through it. The result is layers of shortcuts, like express lanes stacked on top of the map.

A query then only climbs upward: one search from the start, one from the destination, until they meet near the top. On a continent-sized road network, that visits only a few hundred nodes and finishes in a fraction of a millisecond. Google doesn’t publish its exact routing code, but this kind of precomputation is how routing engines can answer instantly at that scale.

Four ways to find a route

MethodHow it worksAt scale
Check every routeList all routes, add up each one184,756 routes on a tiny 10 × 10 grid; hopeless on a real map
Dijkstra (1956)Ripple outward in order of travel timeExact; a long trip on Western Europe’s roads can scan millions of intersections
A* (1968)Ripple stretched toward the goal by a distance guessExact if the guess never overestimates; usually explores far less
Contraction hierarchiesPrecomputed shortcuts; search only climbs to bigger roadsExact; a few hundred nodes, a fraction of a millisecond on a continent

How does Google Maps predict traffic?

Travel times change. Rush hour can turn a four-minute stretch into a twenty-minute crawl, and what matters isn’t the traffic right now but the traffic when you get there. Google says Maps combines the history of each road with live conditions to predict traffic 10, 20, even 50 minutes into your trip.

In 2020, Google and DeepMind added a kind of AI called a graph neural network, which treats nearby roads as one connected system, so a slowdown on one stretch helps predict the next. Google says its arrival times were already accurate for over 97% of trips, and DeepMind says the new model improved accuracy by up to 50% in cities such as Berlin, Jakarta, São Paulo, Sydney, Tokyo and Washington, D.C. Those predicted times become the numbers on the edges, and the route search runs on top. In the demo, that’s the traffic tool: paint a jam and watch both algorithms route around it when going round is quicker.

Frequently asked questions

How does Google Maps find the fastest route?

It treats the road map as a graph: intersections are nodes, road segments are edges, and every edge carries a predicted travel time. A shortest-path search then finds the chain of edges with the smallest total. Google doesn’t publish its exact routing code, but the published building blocks are Dijkstra’s algorithm, A*, and precomputed shortcuts such as contraction hierarchies, with travel times that come from traffic prediction.

What is the difference between Dijkstra’s algorithm and A*?

Dijkstra’s algorithm grows outward from the start in every direction, always finishing the closest unfinished place first. A* does the same but adds a guess of the distance still left to the goal, so the search stretches toward the destination and usually explores far fewer places. As long as the guess never overestimates, both find exactly the same fastest route.

Who invented Dijkstra’s algorithm?

Edsger W. Dijkstra, a Dutch computer scientist. In 1956 he needed a demonstration for the new ARMAC computer and chose the shortest route from Rotterdam to Groningen. He designed the method in about twenty minutes on a café terrace in Amsterdam, without pencil and paper, and published it in 1959.

Why is it called A* (A-star)?

A* was published in 1968 by Peter Hart, Nils Nilsson and Bertram Raphael at the Stanford Research Institute, for Shakey, one of the first mobile robots, which had to plan its own path. The star marks it as the best version of the “A” family of search methods they were comparing.

How can a route across a whole country be found in under a second?

Most of the work is done in advance. With contraction hierarchies, a computer ranks every intersection by importance and adds shortcut edges so that a search only has to climb from small roads toward big ones. On a continent-sized road network, a query then visits only a few hundred nodes and takes a fraction of a millisecond.

Sources

Last updated

Keep exploring