Maze Generation Tool - Generate, Animate & Solve Mazes Online
Generate perfect mazes with 5 algorithms: Recursive Backtracker, Kruskal's, Prim's, Aldous-Broder & Binary Tree. Animate, solve with A*, and export as PNG.
About
A maze is a spanning tree of a grid graph. Every cell connects to every other cell by exactly one path. Getting the generation wrong produces either trivial puzzles (too few dead-ends) or unsolvable structures (cycles or isolated regions). This tool implements five distinct spanning-tree algorithms, each producing mazes with measurably different topological properties: corridor length distribution, branching factor, and bias direction. The Recursive Backtracker uses depth-first search and produces corridors with a high "river" factor (mean path length before a junction). Kruskal's randomized edge approach and Prim's frontier growth yield shorter, more branching passages. Aldous-Broder generates a provably uniform random spanning tree at the cost of O(n2) expected time. Binary Tree is O(n) but introduces a visible northeast diagonal bias. Solution overlay uses A* with Manhattan heuristic. The tool approximates ideal maze difficulty for a given grid size but does not account for human perceptual biases toward certain wall patterns.
Formulas
A* evaluates cells by minimizing the cost function:
f(n) = g(n) + h(n)
where g(n) is the actual cost from start to node n, and h(n) is the heuristic estimate to the goal. The Manhattan heuristic is:
h(n) = |xn ā xgoal| + |yn ā ygoal|
Total cells in the grid: V = r Ć c. A perfect maze (spanning tree) has exactly V ā 1 passages. Total possible internal edges in the grid graph: E = 2rc ā r ā c. The ratio of opened passages to total possible edges determines maze density.
Kruskal's uses a Union-Find (disjoint set) structure. Two cells merge only if find(a) ā find(b), preventing cycles. Path compression and union by rank give near-O(1) amortized operations via the inverse Ackermann function α(n).
Reference Data
| Algorithm | Time Complexity | Dead-End % | River Factor | Bias | Uniform? | Best For |
|---|---|---|---|---|---|---|
| Recursive Backtracker | O(n) | ~10% | High | None | No | Long winding corridors |
| Kruskal's | O(E log E) | ~25% | Low | None | No | Short branching paths |
| Prim's | O(E log V) | ~20% | Medium | None | No | Organic tree-like shapes |
| Aldous-Broder | O(n2) expected | ~15% | Medium | None | Yes | Unbiased uniform sampling |
| Binary Tree | O(n) | ~30% | Low | NE diagonal | No | Fast generation, teaching |
| A* Solver | O(E log V) | N/A | N/A | N/A | N/A | Optimal shortest path |
| Spanning Tree Properties | ||||||
| Edges in tree | V ā 1 where V = rows Ć cols | |||||
| Total grid edges | 2rc ā r ā c for r rows, c cols | |||||
| Dead-end cell | Cell with exactly 1 open passage | |||||
| Junction cell | Cell with 3 or 4 open passages | |||||
| River factor | Mean straight-line run before turn or junction | |||||
| Perfect maze | No loops, no isolated cells. Exactly one path between any two cells. | |||||
| Cycle detection | Union-Find (Kruskal's) prevents merging same-set cells | |||||
| A* heuristic | Manhattan: |x1 ā x2| + |y1 ā y2| | |||||