Maze Generator Algorithms — Backtracker vs Prim vs Kruskal
Same seed, same size, three generators. The only thing that changes is the algorithm — so the dead-end count, junction count and solution length below are a fair comparison rather than three unrelated mazes.
Maze maker
A preset sets the grid size and the algorithm together.
20 × 20 cells
The seed is written into the URL, so a reload — or a shared link — restores this exact maze.
Three algorithms, one seed
All three mazes below use seed 410014 on the same grid, so the measured numbers compare the algorithms rather than the luck of the draw.
Recursive backtracker
- Dead ends
- 35
- Junctions
- 32
- Solution
- 154 steps
Prim's algorithm
- Dead ends
- 105
- Junctions
- 87
- Solution
- 36 steps
Kruskal's algorithm
- Dead ends
- 94
- Junctions
- 80
- Solution
- 48 steps
How to read the comparison
Every maze generator here builds a spanning tree over the same cell graph. The difference is the order in which edges are chosen, and that order decides the texture of the result: how long corridors get, how often the solver meets a junction, and how long the shortest solution is.
The comparison below runs all three algorithms on one seed. Because the cell graph and the random stream length differ, the three mazes are not identical in shape, but they are built from the same grid and the same seed, so the measured dead-end and solution numbers are directly comparable. Change the seed or the size and everything updates live.
- Recursive backtracker: a deep tree. Long corridors, few junctions, the lowest dead-end count for a given grid.
- Prim's algorithm: a wide tree. Many short dead ends, a high junction count, corridors that rarely run far.
- Kruskal's algorithm: the most uniform of the three. No directional bias, and a dead-end share that sits between the other two.
- Solution length is measured in cell steps along the BFS shortest path — it is not an estimate.
Why the same seed is the only fair comparison
Comparing two mazes generated from different seeds tells you almost nothing: the variation between seeds of the same algorithm is larger than the average difference between algorithms on a small grid. Holding the seed fixed removes that noise from the picture, which is why the numbers on this page move in a stable direction when you change the algorithm.
The same three generators are available in the tool at the top of every page. If one of these textures suits a classroom or a puzzle book, switch to it there, set a size, and print with the answer key.
Frequently asked questions
Which maze algorithm is hardest?
Prim's algorithm usually produces the highest dead-end count for a given grid, which is what solvers experience as difficulty. Kruskal is close behind and more uniform; a recursive backtracker is the easiest of the three.
Do all three produce perfect mazes?
Yes. All three build a spanning tree, so every maze has exactly one route between any two cells. The unit tests assert the cell count minus one edge property for every algorithm and every shape.
Why does a bigger grid not always mean a harder maze?
Difficulty tracks branch density, not area. A 30 × 30 backtracker can be easier than a 20 × 20 Prim maze because the solver has fewer decisions to make even though the page is bigger.