HomeGuides › Algorithms

Backtracker, Prim, Kruskal or hunt-and-kill: which maze algorithm is best?

The four generation algorithms in the tool produce visibly different mazes. Here is what each one does and when to use it.

Published 02 September 2026 · Maze Generator team
Maze illustration for: Backtracker, Prim, Kruskal or hunt-and-kill: which maze algorithm is best?

A maze is a spanning tree drawn on a grid: every cell is connected, there are no loops, and there is exactly one route between any two cells. There are many ways to build such a tree, and each way leaves a fingerprint in the finished picture. The generator offers four.

Recursive backtracker

Start in a random cell, walk to an unvisited neighbour, keep walking until you get stuck, then back up to the last cell that still has an unvisited neighbour and continue from there. The result is a maze of long corridors with relatively few branches and a long, winding solution. It is the algorithm people picture when they think of a hand-drawn maze, and it is the default here. The twistiness slider biases the walk toward straight lines or turns.

Prim's algorithm

Begin with one cell and keep a list of walls that border the growing region. Pick a random wall from the list, open it if the cell on the other side is new, and add that cell's walls to the list. Because growth happens anywhere on the frontier, the maze fans out from its origin like frost on glass: many short branches, many dead ends, short solution. It is the best choice when you want a maze that looks busy and rewards careful reading.

Kruskal's algorithm

List every wall in the grid, shuffle the list, then go through it opening any wall that joins two separate regions. This is the classic minimum spanning tree procedure applied to a grid. Kruskal mazes have a uniform texture with no sense of an origin. Branch length is moderate and dead ends are common. Because the shuffle is global, it also produces the most uniformly random result, which matters if you are generating hundreds of pages for a book and want no two to feel alike.

Hunt and kill

Like the backtracker, walk from cell to cell until stuck. Instead of backing up, scan the grid for the first unvisited cell that touches a visited one, connect it, and start a new walk from there. The scan gives it a subtle grain: new corridors tend to begin in the same part of the grid. The result has long runs like the backtracker but more frequent reversals of direction.

Side by side

AlgorithmCorridorsDead endsSolutionFeel
BacktrackerLongFewLongClassic, winding
PrimShortManyShortBranching, busy
KruskalMediumManyMediumUniform, random
Hunt and killLongSomeMediumRuns with reversals

All four run in linear time in the number of cells, so even a 150 by 150 maze appears in a fraction of a second. If you are unsure, generate the same seed with each algorithm and compare; the seed field keeps the layout of openings identical so only the corridor pattern changes.