Boneyard Tools

Maze generation algorithms, explained

How recursive backtracker, Prim, Kruskal, binary tree and sidewinder each carve a perfect maze, and the texture each one leaves behind.

What every algorithm shares

Each method starts from a solid grid where every cell is boxed in by four walls, then carves passages until all cells connect. When the result is a spanning tree over the grid, with exactly one cell fewer carved passages than there are cells, you get a perfect maze: connected, loop-free and solvable.

Recursive backtracker

This is a depth-first walk. From the current cell it steps to a random unvisited neighbour, carving as it goes, and backtracks along its path when it hits a dead end. The long unbroken corridors and sparse junctions make it the classic choice for hand-drawn looking mazes.

Prim and Kruskal

Prim grows a single region outward, always carving a random frontier wall that touches it, which spreads evenly in all directions. Kruskal shuffles every wall and opens it only when it joins two separate regions, tracked with a union-find structure. Both feel more open and branchy than a backtracker.

Binary tree and sidewinder

These are the simplest passes. Binary tree visits each cell and carves north or east at random, leaving two perfectly open border edges. Sidewinder walks each row in runs and closes each run with a single northward door. Both are fast and biased toward the top and right, which makes the bias easy to see and teach.