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.
- A queue is a waiting line: new arrivals join the back, and you always serve the front. First in, first out.
- A stack is a pile: you always take the newest thing from the top. Last in, first out.
- Breadth-first search keeps its to-do list in a queue. Breadth means width: it explores wide, a ring at a time.
- Depth-first search keeps its to-do list in a stack. It runs as deep as it can down one corridor before backing up.
- Put the start in the line, marked 0 steps.
- Take the spot at the front of the line. Look at its open neighbors (up, down, left, right).
- 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.
- Repeat until you take the goal from the line. Then follow the "came from" arrows backward to the start.
- Run breadth-first, then depth-first, on the same maze. Which finds a shorter path? Which looks at fewer spots?
- Press Open field and run breadth-first. What shape does the explored area make? Why that shape and not a circle?
- Why does breadth-first search always find a shortest path?
- Wall the goal in completely. How does each search find out there's no way through?
Curator's answers
- 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.
- 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.)
- 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.
- 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.