Maze Generator
Generate perfect mazes with recursive backtracker algorithm. Visualize step-by-step, solve with BFS shortest path, and export as PNG.
About
A perfect maze has exactly one path between any two cells. This property emerges from the Recursive Backtracker algorithm, a randomized Depth-First Search that carves passages through a grid of R Γ C cells. The algorithm maintains a stack of visited cells and backtracks when it reaches a dead end. The result is a spanning tree of the grid graph. Getting maze generation wrong produces trivial mazes with obvious shortcuts or disconnected regions. This tool generates guaranteed-perfect mazes and solves them using BFS to find the shortest path in O(R β C) time. Note: animation speed affects only visualization. The underlying algorithm completes in milliseconds for grids up to 100 Γ 100.
Formulas
The Recursive Backtracker treats the grid as an implicit graph where each cell is a vertex and shared walls are edges. The algorithm is an iterative DFS:
push(start) β mark visited
while stack β β :
current = peek(stack)
neighbors = unvisited(current)
if |neighbors| > 0:
next = random(neighbors)
removeWall(current, next)
push(next) β mark visited
else: pop(stack)
Total cells visited: N = R Γ C. Walls removed: N β 1 (spanning tree property). Solution path found via BFS from cell (0, 0) to cell (R β 1, C β 1), guaranteeing shortest path length L in unweighted graph.
Where R = number of rows, C = number of columns, N = total cells, L = solution path length.
Reference Data
| Algorithm | Type | Bias | Dead Ends | Complexity | Perfect Maze |
|---|---|---|---|---|---|
| Recursive Backtracker (DFS) | Carving | Long corridors | Few | O(R β C) | Yes |
| Kruskalβs | Wall removal | Uniform | Moderate | O(E log E) | Yes |
| Primβs | Growing tree | Short corridors | Many | O(E log V) | Yes |
| Ellerβs | Row-by-row | Horizontal | Moderate | O(R β C) | Yes |
| Aldous-Broder | Random walk | Uniform (unbiased) | Moderate | O(R2 β C2) avg | Yes |
| Wilsonβs | Loop-erased walk | Uniform (unbiased) | Moderate | Variable | Yes |
| Hunt-and-Kill | Hybrid carving | Long corridors | Few | O(R β C) | Yes |
| Sidewinder | Row-by-row | Vertical top bias | Moderate | O(R β C) | Yes |
| Binary Tree | Per-cell | Diagonal (NE/NW) | Many | O(R β C) | Yes |
| Growing Tree | Configurable | Depends on selection | Varies | O(R β C) | Yes |
| Recursive Division | Wall adding | Long walls | Many | O(R β C log) | Yes |
| BFS (Solving) | Pathfinding | N/A | N/A | O(V + E) | N/A |