Searching wing · 1945 and 1959

Escaping a Maze

How does a map app find the shortest way home? One of the simplest answers is to spread out from the start like water: first every spot one step away, then every spot two steps away, and so on. The first time the water touches the goal, you've found a shortest way there.

Words to know
The recipe (breadth-first)
  1. Put the start in the line, marked 0 steps.
  2. Take the spot at the front of the line. Look at its open neighbors (up, down, left, right).
  3. Any neighbor you haven't seen before gets marked one more step than this spot, remembers where it came from, and joins the back of the line.
  4. Repeat until you take the goal from the line. Then follow the "came from" arrows backward to the start.
Click or drag on the grid to add or remove walls.

Spots taken from the to-do list: 0 Waiting in the to-do list: 0 Path length: –
S start, G goal looking from here waiting in the to-do list already explored the path found Numbers: steps from the start, along the way the search reached that spot
Detective questions
  1. Run breadth-first, then depth-first, on the same maze. Which finds a shorter path? Which looks at fewer spots?
  2. Press Open field and run breadth-first. What shape does the explored area make? Why that shape and not a circle?
  3. Why does breadth-first search always find a shortest path?
  4. Wall the goal in completely. How does each search find out there's no way through?
Curator's answers
  1. Breadth-first always finds a shortest path. Depth-first finds a path, often a long winding one. Sometimes depth-first gets lucky and looks at fewer spots, and sometimes it wanders through almost everything.
  2. A diamond. You can only move up, down, left or right, so the spots 5 steps away form a diamond, not a circle. (Distance measured like this is called taxicab distance, after a taxi driving on a grid of city streets.)
  3. It finishes every spot 1 step away before any spot 2 steps away, and so on. So the first time it reaches the goal, no shorter way could exist: that shorter way would have reached it in an earlier ring.
  4. The to-do list runs empty. Every spot the start can reach has been explored, and the goal wasn't among them.

Why it's clever

Breadth-first search looks at each spot at most once, so even a huge maze takes only as many steps as it has spots. Map apps use faster relatives of it, such as Dijkstra's algorithm (1956), for roads of different lengths. Social networks use it to find "friends of friends", and video games use it to steer characters around walls.

From the archive

The German engineer Konrad Zuse described breadth-first search in 1945, in work for his programming language Plankalkül, but it wasn't published until 1972. Edward F. Moore found it again in 1959 for "the shortest path through a maze". Depth-first search is older still: in the 1800s the French mathematician Charles Pierre Trémaux described a version for escaping a real maze with a piece of chalk.