Making Mazes

Maze generation algorithms: how a maze is actually made

Updated 7 min readBy Rachel Bennett
algorithmsgeneratorbehind-the-scenesdifficulty

Learn how our recursive backtracker builds a maze, how other algorithms differ, and how seeds and braiding change the paths you solve.

In this article

An algorithm is a set of steps for a task. In a maze generator, those steps decide which walls to open. One method makes long, winding paths. Another leaves lots of short dead ends. This guide shows how our method works and why we chose it. It also explains seeds, which let you repeat a maze, and braiding, which adds loops.

To find your way out of a maze, see maze solving algorithms. To try the method we use, open the maze generator. Make a maze, then change its seed or braid setting to compare the results.

Paper-cut gears and graph nodes illustrating how maze generation algorithms work

a maze is a spanning tree wearing a costume

Think of a maze as cells joined by paths. In graph terms, each cell is a node, and each possible opening is an edge. To build a "perfect" maze, open enough walls to reach every cell but leave no loops. The result is a spanning tree: there is exactly one route between any two cells. That is why a perfect maze cannot offer two different simple routes to the finish.

Every maze we publish has at least one checked answer. Our unbraided mazes have just one route between any two cells. Braiding can add other routes, so a braided maze's key shows a shortest path, not always the only valid answer. We score both types using the measures in how maze difficulty works. For more terms, see what is a maze.

The methods below build this tree in different ways. Which walls they open or keep changes the feel of the maze.

the recursive backtracker: the algorithm we use

Maze Printables builds its mazes with a seeded randomized depth-first search, also called the recursive backtracker. It follows one path as far as it can before trying another:

  1. Start at a cell and mark it visited.
  2. Pick a random unvisited neighbor, knock down the wall between them, and step through.
  3. Keep walking until you hit a cell with no unvisited neighbors.
  4. Backtrack along your own trail until you find a cell that still has one, and continue from there.
  5. When you have backtracked all the way home, every cell has been visited and the maze is done.

This method tends to make long, winding paths with few branches. It keeps going until it has to turn back, rather than adding short paths all over the grid. The Wikipedia overview of maze generation describes this pattern too. We chose it because we like that feel on paper: the pencil can follow a path for a while before the next choice.

other famous algorithms, and how their mazes feel

Jamis Buck shows eleven maze-building methods in his animated algorithm recap. Here are some of the main ones and the kinds of paths they tend to make:

AlgorithmStrategyThe maze it produces
Recursive backtrackerDepth-first walk with backtrackingLong corridors, few but deep dead ends
Prim'sGrow the tree from a frontier of wallsStubby texture, many short dead ends
Kruskal'sMerge random cells until all connectSimilar: heavy on small cul-de-sacs
Wilson'sLoop-erased random walksStatistically unbiased, no stylistic tells
Eller'sBuild one row at a timeCan generate infinitely tall mazes
Recursive divisionAdd walls instead of carving passagesLong straight walls, boxy chambers

There is no single best method. Prim's and Kruskal's tend to give you more short dead ends to check. The backtracker favors longer trips along each branch. We use one method across the site to keep that basic feel consistent. Shape, size, seed, and braiding provide variety; the finished maze still needs its own difficulty score.

braiding: the dead-end dial

A wrong branch in a perfect maze leads to dead ends, so you have to trace back to the fork. Braiding can change that. After building the maze, we open an extra wall at some dead ends. A new path joins cells that already had a route between them, making a loop. The braid percent setting in the maze generator controls how many dead ends we try to open.

We measured what braiding actually does to the same maze, same seed, at 0% and 50%:

The same maze generated at braid 0 percent and braid 50 percent, showing fewer dead ends in the braided version

The same 14×14 maze from the same seed: braid 0% (left, 24 dead ends) vs braid 50% (right, 12 dead ends). The difficulty label drops from medium to easy.

In this pair, the dead-end count falls from 24 to 12, while the junction count rises from 21 to 33. Opening walls removes some dead ends but also creates more forks. That gives a solver more choices, yet fewer branches that force a return to the last fork. Our difficulty score drops from 0.29 to 0.13. These are measured results for this pair, not a promise that every maze will change by the same amount.

seeds: why the same maze comes back

A random process can give you a new maze each time. Ours takes a seed, a number that sets the sequence of choices. With the same seed and settings, the same generator code makes the same maze, wall for wall.

Think of the seed as a recipe code. It lets you keep a maze you like or change just one part of how you make it:

  • A saved maze link points at the same puzzle next month, so a shared worksheet stays shareable.
  • A lost page can be reprinted identically, which matters more than you would think the week a class loses three.
  • Changing only the seed produces a fresh maze with the same size, shape, and settings, which is how you build a five-maze set at one consistent level. The maze generator lets you run that routine, and our preschool themes post uses it to build theme weeks.

The seed feeds a random number generator called SplitMix64. We use it instead of the browser's built-in random function so we can replay the same choices.

To compare two seeds, keep the shape, size, braid setting, and endpoints fixed. Generate a maze, note its answer length, then change only the seed. You get a new layout, but it need not have the same answer length or score. The seed chooses the paths; it does not set how hard they will be.

Changing the size is a different test. The generator now has a different set of cells to visit. Even with the same seed, a 10-by-10 maze is not just a cropped copy of a 15-by-15 maze. Keep the settings with your seed when you want to repeat a result.

from algorithm to paper

Once the paths work, the maze still needs to print well. We draw its walls as vector lines and put them in a PDF. The lines stay sharp when scaled, unlike a small image stretched to fill a page. Use PDF vs SVG to choose a file type and print mazes without cropping to check your print settings.

FAQ

What algorithm do most maze generators use?

The randomized depth-first search (recursive backtracker) is the most common, because it is simple, fast, and produces long corridors that feel good to solve. Maze Printables uses a seeded version of it for every maze on the site.

What is the best maze generation algorithm?

It depends on the maze you want. The recursive backtracker tends to make long paths. Prim's and Kruskal's produce more short dead ends. Wilson's samples spanning trees without favoring one over another. We use the backtracker because we like its long paths for pencil-and-paper puzzles.

Is every maze on this site a perfect maze?

Every maze starts as a perfect maze, with exactly one route between any two cells. If braiding opens extra walls, it adds loops and can create more routes. We score the finished layout in either case.

What does braiding do to difficulty?

It can remove dead ends and shorten the answer. In our measured pair it also adds forks, so there are more choices but less need to retrace a wrong branch. Check the finished maze rather than assuming a braid setting alone tells you how hard it will feel.

Can I generate the same maze twice?

Yes. Same shape, size, settings, and seed produce the identical maze every time in the maze generator. That is also why saved maze links never change out from under you.

next step

  • Watch the algorithm work: open the maze generator, generate a maze, then change only the seed and generate again.
  • Slide braid percent up and down on the same seed to feel the dead-end dial in action.
  • For the other side of the story, read maze solving algorithms, or see how the output gets scored in how maze difficulty works.

Sources and further reading: Wikipedia: Maze generation algorithm and Jamis Buck's animated Maze Generation: Algorithm Recap. The braid measurements come from our own generator and difficulty scorer.