DFS vs Kruskal vs Prim: Which Maze Algorithm Makes the Best Puzzle Book?

We measured 200 mazes per setting from each classic algorithm — dead ends, route length, turns — to show how DFS, Kruskal and Prim mazes really differ, why the busiest-looking maze is the quickest to solve, and which style fits kids, adult and expert books.

Wisdom Editorial Team
Wisdom Editorial Team
· 8 min read

In short

Every perfect maze has exactly one route between any two cells, but the algorithm that carves it gives the maze its character. We generated 200 mazes per setting with the same engine that prints Wisdom Effect books and measured them. Depth-first search makes few dead ends and very long, twisting routes; Kruskal spreads branches evenly; Prim makes the most dead ends — and the shortest, most direct route. Here is what that means for the difficulty of your book.

On this page
  1. 1.How We Measured
  2. 2.Depth-First Search (Classic)
  3. 3.Kruskal
  4. 4.Prim
  5. 5.Side-by-Side Comparison
  6. 6.What Makes a Maze Hard?
  7. 7.Loops: Fewer Dead Ends, Shorter Routes
  8. 8.Choosing the Style in Wisdom Effect
  9. 9.Which Style for Which Book
  10. 10.Frequently Asked Questions

How We Measured

A "perfect" maze is a maze with no loops: from any cell there is exactly one way to any other cell. Mathematically it is a spanning tree of the grid, and there are many ways to build one. The three classic methods — randomized depth-first search, Kruskal's algorithm and Prim's algorithm — all produce perfect mazes, yet the mazes look and solve very differently.

Instead of repeating rules of thumb, we measured them. For every grid size and style below we generated 200 mazes with the Wisdom Effect maze engine — the exact code that draws the mazes in your book — with the entrance at the top left and the exit at the bottom right. For each maze we counted the dead ends (cells with a single opening), the length of the solution route in cells and the number of turns along it. The solution is always the shortest route, found by breadth-first search.

One number helps to read the results: on a 30×30 grid the shortest possible route from corner to corner is 59 cells (29 steps right, 29 down, plus the start). The closer a style gets to 59, the more direct its solution is.

Depth-First Search (Classic)

The recursive backtracker starts in a random cell and keeps carving into a random unvisited neighbor for as long as it can. Only when it is boxed in does it back up to the last cell with an unvisited neighbor and branch from there. The result is long, winding corridors — maze designers call them "rivers" — with few branches.

On a 30×30 grid a Classic maze has about 92 dead ends (10% of the cells) but a solution route of about 223 cells with around 140 turns: the route snakes through a quarter of the whole grid. It also varies the most — individual mazes ranged from 97 to 481 cells. This is the traditional puzzle-book look and the default style of every edition in Wisdom Effect.

Kruskal

Kruskal's algorithm shuffles all the inner walls and removes them one by one, skipping any wall whose removal would join two cells that are already connected (it tracks this with a union–find structure). Small corridors appear everywhere at once and slowly merge into one maze, so branches are spread evenly over the whole grid.

A 30×30 Kruskal maze has about 272 dead ends (30% of the cells), three times as many as Classic, and a route of about 96 cells with around 54 turns. There are many plausible wrong turns, but each of them is fairly short.

Prim

Randomized Prim grows the maze outward from one cell: it keeps a list of cells next to the maze, picks one of them at random and connects it to the maze. Because the growth spreads in all directions at once, the maze is full of very short side branches.

Prim makes the most dead ends of the three — about 316 on a 30×30 grid (35% of the cells) — but the route is the most direct: about 66 cells with only around 30 turns, barely longer than the 59-cell minimum. Its busy look is real; its difficulty is not, as the next sections show.

Side-by-Side Comparison

The picture shows one maze of each style on the same 30×30 grid and with the same book code. Dead ends are shaded yellow and the solution is drawn in red, as in a Wisdom Effect answer key.

Three 30×30 mazes from Wisdom Effect side by side — Classic (DFS), Kruskal and Prim — with dead ends shaded yellow and the solution in red
Same grid, same book code, three styles. Classic: 91 dead ends, a 227-cell route. Kruskal: 268 dead ends, 121 cells. Prim: 318 dead ends, a nearly diagonal 65-cell route.
Averages of 200 mazes per row, no loops, entrance top left, exit bottom right. Route = cells on the solution path.
GridStyleDead endsRoute (cells)Turns on the route
15×15Classic (DFS)24 (11%)7143
15×15Kruskal67 (30%)4022
15×15Prim78 (35%)3214
30×30Classic (DFS)92 (10%)223140
30×30Kruskal272 (30%)9654
30×30Prim316 (35%)6630
50×50Classic (DFS)252 (10%)566361
50×50Kruskal761 (30%)182104
50×50Prim884 (35%)11251

What the numbers say

  • The share of dead ends is a fingerprint of the style and does not change with size: about 10% Classic, 30% Kruskal, 35% Prim.
  • Route length is the opposite: Classic routes are about 2–3× longer than Kruskal and 2–5× longer than Prim, and the gap grows with the grid.
  • Prim routes stay close to the shortest possible path (99 cells on 50×50 — Prim averages 112).

What Makes a Maze Hard?

A common claim is that Prim and Kruskal make the hardest mazes because they have the most dead ends. Solving by hand does not work that way. "Hard" has at least three parts: how long the route is (how much work the solver does), how many wrong turns are on offer (how often the solver can get lost) and how busy the page looks.

Prim scores high only on the last two. Its dead ends are short — a wrong turn is usually visible after one or two cells — and because the route runs almost straight toward the exit, a solver who simply heads in the direction of the finish rarely goes far wrong. Prim mazes look the hardest and are usually solved the fastest.

Classic mazes are the opposite: few decisions, but a long journey that often runs away from the exit before it comes back, so the obvious direction is often the wrong one. Kruskal sits in between, with many wrong turns and a moderately winding route.

Grid size beats all of it. A 45×56 Classic maze has a route of about 570 cells; the same grid in Prim, about 112. Pick the size for the audience first, then use the style to shape the experience.

Loops: Fewer Dead Ends, Shorter Routes

Loops (also called braiding) knock through walls at dead ends, joining them to a neighbor, so the maze gets several routes. In Wisdom Effect the Loops slider sets the share of dead ends that get joined: 0% keeps a perfect maze, 100% removes every dead end.

Loops change a maze more than most people expect. At 50% a 30×30 Kruskal maze drops from about 272 to 87 dead ends, and its shortest route from 96 to 68 cells, because the new openings create shortcuts. A Classic maze with 50% loops drops from a 223-cell route to about 105 cells. With 100% loops there are no dead ends at all.

So loops make a maze measurably shorter and with fewer dead ends. What they add is uncertainty: the solver can no longer rely on "there is only one way", and can walk in circles. Use a little for adult and expert books; the answer key always shows the shortest route.

The same 30×30 Kruskal maze without loops (268 dead ends, 121-cell route) and with 50% loops (79 dead ends, 65-cell route)
The same Kruskal maze before and after 50% loops: dead ends fall from 268 to 79 and the shortest route from 121 to 65 cells.

Choosing the Style in Wisdom Effect

In the maze configurator the Maze Style panel offers Classic (depth-first search), Kruskal and Prim, each with a real miniature generated by the same engine. The style applies to every shape — rectangles, circular mazes and silhouettes such as the heart — and in every edition. Every maze is generated from the book code, so the same settings always reproduce the same mazes.

The Difficulty presets set the grid size, style and loops in one click. We built them on these measurements: all four Standard presets are Classic mazes, because only Classic makes the route grow together with the grid. Easy is 12×15, Medium 20×25, Hard 30×38 and Expert 45×56 with 10% loops for a little uncertainty. The Kids and Senior presets are Classic too.

The result is a real difficulty ladder — every level has a longer route and more dead ends than the one before: about 59, 145, 287 and 374 route cells, and 20, 52, 116 and 228 dead ends. Our earlier presets used Kruskal for Hard and Prim for Expert; they had more dead ends, but their routes (about 95 and 108 cells) were shorter than Medium's, so a reader could finish an "Expert" maze quicker than a "Medium" one. Kruskal and Prim are still one click away in the Maze Style panel when you want more decisions or a denser look.

The Maze Style panel of the Wisdom Effect maze configurator with Classic, Kruskal and Prim cards and a miniature maze on each
Maze Style: Classic, Kruskal or Prim, each with a real miniature.
The Difficulty presets of the maze configurator: Easy 12×15, Medium 20×25, Hard 30×38 and Expert 45×56
Difficulty presets: Easy 12×15, Medium 20×25, Hard 30×38 and Expert 45×56 (with 10% loops), all Classic. Everything can be fine-tuned afterwards.

Which Style for Which Book

There is no best algorithm, only a best fit for the reader. Use these rules as a starting point and check a few pages in the live preview before you export.

Quick guide

  • Kids: Classic on small grids. Few decisions and wide corridors; the long route keeps the activity going. Kids mazes have no loops.
  • Seniors: Classic, calm sizes (10×12 to 22×25), no or very few loops — one clear path that rewards patience.
  • Adults, relaxing books: Classic at 20×25, the traditional puzzle-book feel.
  • Adults who like decisions: Kruskal — many evenly spread wrong turns, a little looping for uncertainty.
  • Expert books: large grids first. Classic for the longest journeys, Kruskal for the most decisions, Prim when you want a dense, intimidating look.
  • Mix styles across a book or series so every page does not feel the same.
Put it into practice

Tools for This Guide

Frequently Asked Questions

Which maze algorithm is best for kids?

Classic depth-first mazes on small grids: few branches and wide corridors, so a child is rarely stuck. In Wisdom Effect Kids mazes are always perfect mazes without loops.

Which algorithm makes the hardest mazes?

It depends on what "hard" means. Classic (DFS) gives by far the longest routes, Kruskal the most evenly spread wrong turns, and Prim the most dead ends — but Prim's route runs almost straight to the exit, so it is usually the quickest to solve. Grid size matters more than the algorithm.

Why does a Prim maze look harder than it is?

About 35% of its cells are dead ends, which makes the page look dense, but the dead ends are very short and the solution is close to the shortest possible path. A solver heading toward the exit rarely goes far wrong.

Do loops make a maze harder?

They make it shorter and remove dead ends: at 50% loops a 30×30 Kruskal maze goes from about 272 to 87 dead ends and from a 96-cell to a 68-cell route. What they add is uncertainty, because there is more than one way through. The answer key shows the shortest route.

Can I use Kruskal or Prim for circular and shaped mazes?

Yes. In Wisdom Effect the chosen style applies to rectangles, circular mazes and silhouette shapes alike.

Three Maze Styles

Try Every Maze Style

Switch between Classic, Kruskal and Prim, add loops and see the difference instantly in the live preview.

No credit card required