Maze generation algorithms

Four Maze Generation Algorithms and How They Work

Maze Foundry treats a maze as an undirected graph. Cells are vertices. Open passages are reciprocal edges. Generation carves a spanning tree (perfect mode) or a spanning tree plus selected extra edges (braided mode). Four algorithms share that model and a seeded Mulberry32 generator. They do not share a universal difficulty ranking. Measure dead ends, junctions, and solution length on the maze you actually built.

Representing mazes as graphs

Create a grid with all internal walls closed. Each cell lists legal neighbors. Rectangular cells have up to four. Hexagonal cells have up to six. Polar cells have clockwise, counterclockwise, inward, and possibly several outward children. A wall is the absence of a passage, not a separate object. Rendering reads the same passages the solver reads.

Connected graphs and spanning trees

A connected graph lets every cell reach every other cell. A spanning tree is a connected graph with no cycles. For N vertices it has N − 1 edges. That is why a perfect maze with 25 cells must have 24 internal passages. Independent cycles, when they exist, equal E − V + 1. Entrance holes on the outer boundary are not internal edges.

A 5-cell corridor has 4 passages: a tree. Adding one extra passage somewhere would create a cycle.

Randomized depth-first search

Also called a recursive backtracker, implemented here with an explicit stack so large grids do not overflow the JavaScript call stack. Mark a start cell visited, push it, then repeatedly pick a random unvisited neighbor, open the wall, and push. When a cell has no unvisited neighbors, pop. Continue until every cell is visited. The walk often produces long corridors. That is a tendency, not a law. Try 15 × 15 DFS seed 404 (Long Corridor) and count turns along the solution in the statistics panel.

Randomized Prim

Include one cell, then maintain a frontier of edges that touch an unvisited neighbor. Pick a frontier edge at random. If it joins the maze to a new cell, open it and add the new cell’s edges. Never connect two already included cells in perfect mode. Duplicate frontier keys are ignored. The maze grows as a region rather than a single snake. Compare 20 × 20 seed 606 (Branching Paths) with Classic Square seed 505 on DFS.

Randomized Kruskal and disjoint sets

Each cell starts in its own set. List every undirected adjacency once, shuffle with the seeded generator, then process edges. If find(a) differs from find(b), union the sets and open the passage. If they already match, skip. Stop at N − 1 passages. find uses path compression; union uses rank. The visualizer reports the remaining component count as merges occur. Example: 12 × 12 Kruskal seed 303 (Morning Puzzle).

Wilson’s algorithm

Mark one cell as the maze. From a random unvisited cell, walk to random neighbors. If the walk revisits itself, erase the loop using a position map. When the walk hits the maze, add the loop-erased path. Repeat. Under suitable random-walk assumptions this yields a uniform spanning tree; the browser uses deterministic Mulberry32, so the implementation is reproducible, not a cryptographic sample, and a handful of pictures does not prove uniformity. Large Wilson jobs can be cancelled. Incomplete walks are discarded.

Comparing actual maze characteristics

Open the laboratory, set grid size 8 and seed 12, then Compare algorithms. The table lists dead ends, junctions, solution length, and milliseconds on your device. Identical seeds do not mean identical decisions across DFS, Prim, Kruskal, and Wilson, because each algorithm consumes random numbers differently.

AlgorithmWhat you often noticeWhat you should measure
DFSLong runs of degree-2 cellsSolution length, turns
PrimMore local branchesDead ends vs junctions
KruskalRegions linking lateComponent merges in the log
WilsonUnbiased tree (in theory)Same ledger as the others

Perfect and braided mazes

Perfect mode must satisfy N − 1 and zero cycles. Braided mode processes 0%, 25%, 50%, 75%, or 100% of the initially listed dead ends. Some selected cells may already have gained a second opening; some may have no eligible extra wall. Outer walls stay closed except designated openings. Recalculate statistics after braiding.

A practical check on a 10 × 10 perfect maze is simple: 100 cells, 99 internal passages, cycle count 0, and BFS equals DFS on the unique start-to-goal path. Raise braid to 50% on the same seed and the passage count climbs; cycle count becomes E − V + 1. The solver still finds a route. The visualizer’s skip-to-final control should match a non-animated generateMaze call with the same options, because both call the same algorithm modules.

Resource limits matter for Wilson and for 60 × 60 boards. The worker posts a token with each job so a stale result cannot replace a newer maze. If you cancel, the workshop reports that no incomplete maze is shown. That is part of the algorithm contract, not a cosmetic spinner.

FAQ

Which algorithm should I pick?
Pick by the statistics you want to inspect, not by a claimed ranking.
Does Wilson prove uniformity here?
No. The article describes the algorithm; the site uses a seeded PRNG.
Why do two algorithms differ on seed 12?
They draw different numbers of random values in different orders.

Try the generator or read how to solve a maze.