Depth-first search

Randomized depth-first search, also known as the recursive backtracker, is one of the simplest and most widely used ways to generate a maze. It is fast, easy to understand and short to implement. It is also the algorithm that builds every maze you can walk on mazeness.

The idea

Start with a grid where every cell is closed by four walls. Walk from cell to cell, knocking down the wall in front of you, and never step into a cell you have already visited. When you get stuck, step back along your own trail until you find a cell with an unvisited neighbour, and continue from there. When you are back at the start with nowhere to go, every cell has been visited and the maze is done.

The steps

The algorithm keeps a stack of cells, the trail it can step back along.

  1. Make the initial cell the current cell, mark it as visited and push it to the stack.
  2. While the stack is not empty, look at the cell on top of the stack.
  3. If it has unvisited neighbours, choose one of them at random, remove the wall between the two cells, mark the neighbour as visited and push it to the stack.
  4. Otherwise pop the cell from the stack: this is the step back.
Depth-first search on a 15 by 15 grid, step by step. The current cell is orange, the trail on the stack is pale orange, finished cells are pale.

Every cell except the first is joined to the maze through exactly one new passage, the one that led into it. So the result has no loops and no unreachable cells: it is a perfect maze.

Why not recursion

The algorithm is often written as a recursive function: carve into a neighbour, then call the same function for it. That is where the name “recursive backtracker” comes from. On large grids such a function overflows the call stack, because the trail can be as long as the number of cells. An explicit stack, as in the steps above, avoids the problem.

What the mazes look like

Depth-first search runs as far as it can before it turns back. The mazes it produces have a low branching factor and many long, winding corridors. In Pullen’s terms they have about as high a “river” factor as possible: fewer but longer dead ends (only about 10% of the cells are dead ends) and usually a very long and twisty solution.

This makes them pleasant to walk from the inside. The corridors are long enough to get lost in, and a wrong turn costs real time.

How mazeness uses it

mazeness builds each maze from its code, for example v1-15x15-8F3A2C1D:

  • v1 is the version of the generator
  • 15x15 is the size in cells
  • 8F3A2C1D is the seed of the random number generator

The v1 generator always starts at the top left cell and looks at the neighbours in a fixed order: north, east, south, west. Its random number generator is seeded with the last part of the code, so the same code always gives the same maze, in any browser. That is what makes a maze code worth sharing.

After carving, the generator picks a random start on the outer wall, then an exit on the outer wall that is far enough from it: at least half as far along the paths as the farthest border cell.

The v1 generator builds the maze v1-15x15-8F3A2C1D. The last step puts the start marker on the outer wall.

Walk v1-15x15-8F3A2C1D Try a 30 by 30 maze