Loading demo...
algorithmsgraphsmapsinteractive

Search Algorithm Visualizer

Pick two places on a real city's streets and watch Dijkstra, A*, Greedy and two bidirectional searches find the route, with the work each one did and how long it took.

Pick a city, tap a start, tap an end, and the search runs. The streets it settles light up in its colour until the route appears, and the panel underneath says how many junctions it settled, how long the search took to compute, and how long the route is in minutes and kilometres. Compare mode runs all five side by side on the same two points, so "it searched the whole city" and "it searched a wedge" sit next to each other as pictures and as numbers. The panels also place in the order they finish, first to fifth, and Greedy tends to win the race and lose on the route.

There are seven cities: Portland, Chicago, New York, London, San Francisco, Paris and Tokyo. Each one opens with a pair of pins chosen to show its character, often a route that has to cross water. The start is a circle and the end is a square, both white, because lime and pink already belong to two of the searches.

It used to race five searches through a random maze. A maze is a grid where every step costs the same, and on a grid most of these algorithms look alike. Real streets have one-way systems, fast roads and slow ones, rivers and the few bridges over them, and that is where they come apart.

The map is the graph

There is no basemap. The roads themselves are the picture: dim line art, with the motorways brightest and the residential streets faintest.

Underneath, each city is a weighted, directed graph:

  • Nodes are junctions. A point counts as a junction if two roads meet there or a road ends there. The points in between only give each street its shape, and they never enter the search, so Portland's 69,000 map points become about 14,500 junctions.
  • Edges are directed. One-way streets and roundabouts are respected. A two-way street can be driven in either direction, and a one-way street in only one.
  • Each edge has two weights: its length in metres, and its driving time in seconds. Driving time is length divided by the speed limit, or by a default for the road's class where no limit is tagged. 96% of Portland's roads carry a tagged limit, against 13% of Chicago's, which is why the defaults matter.
  • Only the largest strongly connected part of each city is kept. From any junction you can reach any other. Without that, a one-way stub cut off at the edge of the map would be a dead end, and a search that fails there looks like a bug.

A tap snaps to the nearest junction. Fastest routes on time and Shortest on distance.

Five searches

  • Dijkstra (lime) settles junctions in order of their cost from the start, cheapest first, until it settles the end. It always finds the best route, and it spreads in every direction equally to get there. Every other optimal search is measured against it.
  • A* (pink) adds a guess of the remaining cost to each junction's priority, so junctions that lie towards the end come off the queue sooner. As long as the guess never overestimates, the route is still the best one.
  • Greedy best-first (orange) uses only the guess and ignores the cost so far. It is very fast and often wrong. Its panel says how much worse its route is.
  • Bidirectional Dijkstra (blue) runs one Dijkstra forward from the start and another backward from the end, over the streets reversed, and stops when they have proved where to meet.
  • Bidirectional A* (purple) does the same with A*'s guess on both sides.

A two-ended search draws its backward half in the same colour, dimmer, so you can see where the two halves met.

Why A* barely helps on Fastest

A*'s guess is the straight-line distance to the end. On Shortest that is a tight guess: no road beats a straight line, and most good routes are not far off one. On Fastest the guess has to be a time, so it is the straight-line distance divided by the fastest speed anywhere in the city. That keeps it safe, since no route can beat it, but the top speed belongs to a motorway, and most streets are a third as fast. So the guess underestimates badly, and A* behaves almost like Dijkstra.

On Portland's opening pair, NW 23rd to SE Woodstock:

  • Fastest: Dijkstra settles 13,798 junctions and A* 11,474, a saving of a sixth. Bidirectional Dijkstra settles 9,288 and bidirectional A* 7,054.
  • Shortest: Dijkstra settles 13,627 and A* 5,151, under two fifths. Bidirectional A* settles 3,746.

Greedy settles 166 junctions, and its route takes 20.7 minutes against the optimum's 14.5.

This is true of real road networks, not a weakness of the demo. Tuning the guess to be tighter would make A* faster and its routes sometimes wrong.

When two searches have met

The obvious way to stop a bidirectional search is the moment the two frontiers touch, at the junction both sides have reached. It is wrong. The first junction to be settled from both sides is often not on the best route: a slightly costlier street on one side can meet a much cheaper one on the other. That rule returned a worse route on 34 of 200 random pairs.

The correct rule keeps a running best: every time an edge joins the two sides, it records the total cost through that edge. The search stops when the two queues' cheapest entries add up to at least that best. At that point no route still unexplored can beat it.

Bidirectional A* needs one more step. Each side's guess points at a different target, so the two are averaged into a single potential that both directions share. With that, the same stopping rule still holds.

Before the demo shipped, 1,000 random pairs were run on every city and both weights. The four optimal searches agreed to within floating-point error on every one, and Greedy never found anything cheaper.

What a real router does

None of these is what a navigation app runs. Production routers do most of the work ahead of time, once, before anyone asks for a route. The best-known scheme is contraction hierarchies. Every junction is ranked by importance and contracted in turn, from least to most important. Each contraction adds shortcut edges, so that the remaining graph keeps every shortest distance. At query time, a bidirectional search that only ever climbs to more important junctions settles hundreds of them, not tens of thousands, and finishes in well under a millisecond on a whole country. Open-source routers such as OSRM and GraphHopper use contraction hierarchies or close relatives of them. So the bidirectional searches here are a step toward how routers work, not what they run. Google hasn't published how its router works.

How long it takes

Each panel shows two numbers, and they measure different things:

  • Settled is the work: how many junctions the search took off its queue. It is also what the animation shows.
  • Compute is the time that work takes when nothing is being drawn. It is measured before the replay starts. Each selected search first runs twice, untimed, taking turns with the others, so the JavaScript engine has optimised all of them before any timing begins. Then each one is run at least five times, and until 30 milliseconds have passed, and the total is divided by the number of runs. Browsers round off their timers, and one search over 15,000 junctions takes a millisecond or two, so a single run would mostly measure the rounding.

The replay is separate. Every panel advances at the same number of settled junctions per second, so a race between them is a race of work, not of compute time. The speed slider sets that rate.

What "fastest" leaves out

"Fastest" here means driving at the speed limit on empty roads. It leaves out:

  • Traffic. Live traffic and typical traffic both come from paid services.
  • Turn costs. A left turn across traffic and a wait at a red light both cost nothing here.
  • Turn restrictions. OpenStreetMap records "no left turn" and its relatives, but this graph ignores them, so a route can sometimes make a turn that a driver isn't allowed to.

Where the streets come from

Every city is drawn from OpenStreetMap, © OpenStreetMap contributors, under the Open Database License. The data was fetched once, when the site was built, through the public Overpass API. It covers drivable roads from motorways down to residential streets and living streets, and leaves out private roads, service roads, tracks and paths. Each city becomes a compact binary file, about 270 to 560 KB compressed. The demo downloads the file for the city you pick and nothing else. No request leaves the site.

There is no box for typing in any city, and that is on purpose. A working version was built. It fetched a 10 km square of streets straight from the public Overpass server, and it took 30 to 65 seconds a city. Overpass is a free service run by volunteers, and its usage policy counts all of a website's visitors together. For regular use, it asks a site to stay under about 100 queries and 10 MB a day. A dense city is about 2 MB compressed, so a few visitors a day would be over that. Live search would need a self-hosted or paid Overpass server, so the cities are baked instead.