User Rating 0.0 ā˜…ā˜…ā˜…ā˜…ā˜…
Total Usage 0 times
Category Generators
70%
Space Generate S Solve R Reset
Is this tool helpful?

Your feedback helps us improve.

ā˜… ā˜… ā˜… ā˜… ā˜…

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.

maze generator maze solver labyrinth graph algorithms pathfinding A-star recursive backtracker spanning tree

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

AlgorithmTime ComplexityDead-End %River FactorBiasUniform?Best For
Recursive BacktrackerO(n)~10%HighNoneNoLong winding corridors
Kruskal'sO(E log E)~25%LowNoneNoShort branching paths
Prim'sO(E log V)~20%MediumNoneNoOrganic tree-like shapes
Aldous-BroderO(n2) expected~15%MediumNoneYesUnbiased uniform sampling
Binary TreeO(n)~30%LowNE diagonalNoFast generation, teaching
A* SolverO(E log V)N/AN/AN/AN/AOptimal shortest path
Spanning Tree Properties
Edges in treeV āˆ’ 1 where V = rows Ɨ cols
Total grid edges2rc āˆ’ r āˆ’ c for r rows, c cols
Dead-end cellCell with exactly 1 open passage
Junction cellCell with 3 or 4 open passages
River factorMean straight-line run before turn or junction
Perfect mazeNo loops, no isolated cells. Exactly one path between any two cells.
Cycle detectionUnion-Find (Kruskal's) prevents merging same-set cells
A* heuristicManhattan: |x1 āˆ’ x2| + |y1 āˆ’ y2|

Frequently Asked Questions

A perfect maze is a spanning tree of the grid graph. Every pair of cells is connected by exactly one path. There are no loops and no isolated regions. A grid of r Ɨ c cells produces exactly (r Ɨ c) āˆ’ 1 open passages in a perfect maze. All five algorithms in this tool generate perfect mazes.
Aldous-Broder performs a random walk visiting cells until all cells have been visited at least once. This is the Coupon Collector problem. For V cells, the expected number of steps is O(V Ɨ ln(V)), but worst-case can be O(V²). The tradeoff is that it produces a provably uniform random spanning tree, meaning every possible maze is equally likely. Other algorithms have statistical biases in their output distributions.
Binary Tree always carves either North or East for each cell. This creates two unbroken corridors along the entire north row and east column. The maze has a visible diagonal texture running from southwest to northeast. Dead-ends cluster in the south and west edges. This bias makes it unsuitable for fair game-level generation but excellent for teaching maze concepts due to its O(n) simplicity.
The tool caps at 100Ɨ100 (10,000 cells) to maintain interactive animation performance in the browser. At that size, Recursive Backtracker completes in under 50ms. Aldous-Broder may require several seconds. For static (non-animated) generation, the algorithms could handle larger grids, but canvas rendering at thousands of pixels becomes the bottleneck. Export to PNG preserves full resolution regardless of screen size.
In a perfect maze, there is only one path between any two cells. A* will find it. The Manhattan heuristic is admissible (never overestimates) for grid movement, so A* is guaranteed optimal. In this context, "shortest" and "only" are the same path. The solver animates exploration to show which cells A* evaluates, which is always a subset of total cells due to the heuristic pruning.