Measuring a maze

Two mazes of the same size can feel completely different. One is a long walk with nothing to decide, the other makes you stop at every corner. To compare them, or to tune a generator, we need numbers that describe a maze.

Every number on this page is computed by the same code that draws the figures. The tables under the figures are not typed in by hand.

Simple counts

The simplest numbers just count cells of each kind. Jamis Buck, in his book Mazes for Programmers, suggests counting dead ends, three-way and four-way junctions, straight cells and “elbow” cells where the passage turns, and measuring the longest path. Bellot and his co-authors, who list these statistics, quote Buck himself calling them “very superficial”.

We use a few of them:

  • cells: the size of the maze
  • dead ends: cells with one passage, not counting the start and the exit, and their share of all cells
  • junctions: cells with three or more passages
  • path length: the number of cells on the way from the entrance to the exit, and its share of all cells

These counts are cheap and useful. A maze with many dead ends offers many wrong turns. A path that covers most of the cells leaves little room for anything else.

Numbers about the path

The counts above describe the whole maze. The walker, though, only meets the part of the maze that touches the way out. So we also measure the path itself:

  • turns on the path: how many times the path changes direction, counted only on square grids
  • junctions on the path: places where the walker has to choose
  • longest side branch: the largest distance, in steps, from any cell to the nearest cell of the path

Taking a long side branch is an expensive mistake: take the wrong turn there and you walk far before you find out. In a maze with loops there can be several ways out; we measure the shortest one.

McClendon’s complexity and difficulty

In 2001 Michael Scott McClendon proposed two measures that look at the shape of the passages, not just at the number of cells. He works with the graph of a maze: a line drawn inside every passage, roughly along its middle. The places where the line splits are intersections, and the pieces between intersections and dead ends are hallways. The solution is the line from the entrance to the exit, and every piece of the maze hanging off it is a branch. He assumes the maze has no loops, so the solution is unique.

The complexity of a hallway grows with sharp turns that come quickly one after another. McClendon walks along the hallway and marks a turn wherever the direction changes by at least 45 degrees (π/4). For a hallway of total length D with turns of angles θ₁, θ₂, … each reached after a stretch of length d₁, d₂, …, the complexity is

γ = D × (θ₁ / (d₁ π) + θ₂ / (d₂ π) + …)

Each d is measured from the previous turn, or from the start of the hallway. The stretch after the last turn counts only in D. For example, a hallway of 2 cells, a right turn, then 2 more cells: D = 4, one turn after 2 cells, γ = 4 × (π/2) / (2π) = 1.

A straight hallway scores zero. On a square grid every turn is a right angle, θ = π/2, so each turn adds 1 / (2d), and short stretches between turns weigh the most. The result does not depend on the unit of length. McClendon’s three sample hallways score 0, 138 and 2432.

The complexity of a branch is the sum of the complexities of its hallways, each walked away from the solution. The direction matters because every d is measured from where the walk starts: the same hallway walked the other way can score differently.

The complexity of the maze adds everything up and takes a logarithm:

complexity = log (γ(solution) + γ(branch 1) + γ(branch 2) + …)

McClendon then notes that a simple solution with many complicated branches would score high even though the maze is easy. So for difficulty he multiplies instead of adding, with one added to each branch so that a branch of complexity below one cannot lower the result:

difficulty = log (γ(solution) × (γ(branch 1) + 1) × (γ(branch 2) + 1) × …)

The logarithm of a product is the sum of the logarithms, so every branch adds its own term log (γ + 1). Difficulty grows with every twisty branch, while in the complexity the largest term dominates. McClendon offers no further argument for the formula. Bellot and his co-authors note that the choice is not strongly motivated, but they still use it.

His three sample mazes have complexity 3.3, 3.1 and 3.4 and difficulty 3.3, 12.5 and 14.7. The complexity barely moves, while the difficulty separates them clearly.

How we compute it

The paper defines the measures for any curved drawing, so a program has to make some choices. McClendon allows many lines through a maze and defines the measures as the smallest value over all of them, so his published numbers, like ours, are upper bounds computed for one particular line. We compute it as follows:

  • the line of a passage runs through the centres of the cells, and the solution also runs half a cell further, to the middle of the entrance and the exit
  • the solution is one hallway from the entrance to the exit, and its turns at junctions count too
  • a dead-end hallway ends in the centre of the dead-end cell
  • the logarithm is decimal - the paper does not name the base, but base 10 is the one that fits: the sample hallways score in the hundreds and thousands, the sample mazes around 3
  • we compute the measures only for mazes without loops, as the paper does, and only on square grids, where the middle line of a passage is clear

As a check, the middle line of McClendon’s second sample hallway, traced from the scanned paper, gives 141 instead of the published 138; the drawing is hand-made, so the match cannot be exact.

A small example

A 4 by 4 maze with one branch
Cells16
Dead ends1 (6%)
Junctions0
Path length7 (44%)
Turns on the path2
Junctions on the path0
Longest side branch9
McClendon complexity1.3
McClendon difficulty1.6

Measure lengths in cells. The solution enters from the left, runs 3.5 cells along the top, turns down, runs 3 cells and turns again for the last half cell to the exit. Its length is D = 7, and the two right-angle turns come after stretches of 3.5 and 3:

γ(solution) = 7 × (1 / 7 + 1 / 6) ≈ 2.17

The rest of the maze is one branch that leaves the solution in the exit cell. It is a single hallway of 9 cells: 3 to the left, 2 up, then four one-cell steps that zigzag to a dead end. Its five turns come after stretches of 3, 2, 1, 1 and 1:

γ(branch) = 9 × (1 / 6 + 1 / 4 + 1 / 2 + 1 / 2 + 1 / 2) = 17.25

So complexity = log (2.17 + 17.25) ≈ 1.3, and difficulty = log (2.17 × 18.25) ≈ 1.6. Most of the complexity sits in the branch: a twisty dead end right before the exit. The exit cell has only two passages plus the opening, so the table counts no junction there.

Critique and further work

Bellot and his co-authors computed McClendon’s measures for mazes made by the eleven generation algorithms known from the literature. They found that the complexity is strongly correlated with the length of the solution. That is a problem if the goal is to measure the fun of solving: a maze with a very long path and no junctions scores high, yet the walker never has to choose. They point at depth-first search, the generator behind mazeness, as an example: long solutions with only short dead ends.

Their own measure counts non-significant walls, the walls a person can ignore while scanning the maze by eye. The more of them, the faster the maze is read, and the less fun it is. They then combine it with McClendon’s difficulty: their fun score is the number of non-significant walls divided by the difficulty, and the lower it is, the more fun the maze.

People who actually solve mazes were studied by Kadner and his colleagues at TU Darmstadt. Sixteen participants solved nine perfect mazes of 25 by 25 cells, and for seven of them an eye tracker also followed their gaze. Both a longer solution and more junctions on the solution made the mazes take longer. Participants stopped longer at cells with more ways to go. With more junctions they kept the breadth of their look-ahead but planned less deeply. So the path metrics above are not arbitrary: both factors the study varied, path length and junctions on the path, measurably slowed people down. The turns on the path come from earlier work that Kadner and his colleagues cite: Crowe and his co-authors found that a longer solution and more turns along it make a maze take longer to solve.

Three mazes of the same size

The three mazes below have 256 cells each. The first is perfect, the second is a partial braid with half of its dead ends opened into loops, and the third is unicursal, built from a perfect maze of 8 by 8 cells by splitting every passage in two.

Perfect
Cells256
Dead ends22 (9%)
Junctions21
Path length165 (64%)
Turns on the path102
Junctions on the path14
Longest side branch24
McClendon complexity3.8
McClendon difficulty10.9
Partial braid
Cells256
Dead ends11 (4%)
Junctions30
Path length59 (23%)
Turns on the path32
Junctions on the path11
Longest side branch32
McClendon complexitynot computed
McClendon difficultynot computed
Unicursal
Cells256
Dead ends0 (0%)
Junctions0
Path length256 (100%)
Turns on the path94
Junctions on the path0
Longest side branch0
McClendon complexity3.8
McClendon difficulty3.8
Same size, different numbers

The partial braid has the most junctions and the shortest way out: the loops open shortcuts. It gets no McClendon values, because his definitions assume there are no loops and so only one solution.

The unicursal maze has no junctions at all, and its path covers every cell. It has no loops either, so McClendon’s measures apply. Its complexity, 3.8, is the same as that of the perfect maze, but with no branches its difficulty equals its complexity. The perfect maze, with its many twisting dead ends, has a difficulty of 10.9. Bellot and his co-authors make the same observation: without branches, complexity and difficulty coincide. Complexity cannot tell a maze without choices from one full of them. Difficulty can.