Maze generation algorithm

Maze generation algorithms are automated methods for the creation of mazes.

Graph theory based methods

A maze can be generated by starting with a predetermined arrangement of cells (most commonly a rectangular grid but other arrangements are possible) with wall sites between them. This predetermined arrangement can be considered as a connected graph with the edges representing possible wall sites and the nodes representing cells. The purpose of the maze generation algorithm can then be considered to be making a subgraph in which it is challenging to find a route between two particular nodes.

If the subgraph is not connected, then there are regions of the graph that are wasted because they do not contribute to the search space. If the graph contains loops, then there may be multiple paths between the chosen nodes. Because of this, maze generation is often approached as generating a random spanning tree. Loops, which can confound naive maze solvers, may be introduced by adding random edges to the result during the course of the algorithm.

The animation shows the maze generation steps for a graph that is not on a rectangular grid. First, the computer creates a random planar graph G shown in blue, and its dual F shown in yellow. Second, computer traverses F using a chosen algorithm, such as a depth-first search, coloring the path red. During the traversal, whenever a red edge crosses over a blue edge, the blue edge is removed. Finally, when all vertices of F have been visited, F is erased and two edges from G, one for the entrance and one for the exit, are removed.

Depth-first search Play media
Animation of generator's thinking process using Depth-First Search

This algorithm is a randomized version of the depth-first search algorithm. Frequently implemented with a stack, this approach is one of the simplest ways to generate a maze using a computer. Consider the space for a maze being a large grid of cells (like a large chess board), each cell starting with four walls. Starting from a random cell, the computer then selects a random neighbouring cell that has not yet been visited. The computer removes the wall between the two cells and marks the new cell as visited, and adds it to the stack to facilitate backtracking. The computer continues this process, with a cell that has no unvisited neighbours being considered a dead-end. When at a dead-end it backtracks through the path until it reaches a cell with an unvisited neighbour, continuing the path generation by visiting this new, unvisited cell (creating a new junction). This process continues until every cell has been visited, causing the computer to backtrack all the way back to the beginning cell. We can be sure every cell is visited.

As given above this algorithm involves deep recursion which may cause stack overflow issues on some computer architectures. The algorithm can be rearranged into a loop by storing backtracking information in the maze itself. This also provides a quick way to display a solution, by starting at any given point and backtracking to the beginning.

Mazes generated with a depth-first search have a low branching factor and contain many long corridors, because the algorithm explores as far as possible along each branch before backtracking.

Recursive backtracker Play media
Recursive backtracker on a hexagonal grid

The depth-first search algorithm of maze generation is frequently implemented using backtracking:

1. Make the initial cell the current cell and mark it as visited
2. While there are unvisited cells
1. If the current cell has any neighbours which have not been visited
1. Choose randomly one of the unvisited neighbours
2. Push the current cell to the stack
3. Remove the wall between the current cell and the chosen cell
4. Make the chosen cell the current cell and mark it as visited
2. Else if stack is not empty
1. Pop a cell from the stack
2. Make it the current cell

Randomized Kruskal's algorithm Play media
An animation of generating a 30 by 20 maze using Kruskal's algorithm.

This algorithm is a randomized version of Kruskal's algorithm.

1. Create a list of all walls, and create a set for each cell, each containing just that one cell.
2. For each wall, in some random order:
1. If the cells divided by this wall belong to distinct sets:
1. Remove the current wall.
2. Join the sets of the formerly divided cells.

There are several data structures that can be used to model the sets of cells. An efficient implementation using a disjoint-set data structure can perform each union and find operation on two sets in nearly constant amortized time (specifically, $O(\alpha (V))$ time; $\alpha (x)<5$ for any plausible value of $x$ ), so the running time of this algorithm is essentially proportional to the number of walls available to the maze.

It matters little whether the list of walls is initially randomized or if a wall is randomly chosen from a nonrandom list, either way is just as easy to code.

Because the effect of this algorithm is to produce a minimal spanning tree from a graph with equally weighted edges, it tends to produce regular patterns which are fairly easy to solve.

Randomized Prim's algorithm Play media
An animation of generating a 30 by 20 maze using Prim's algorithm.

This algorithm is a randomized version of Prim's algorithm.

2. Pick a cell, mark it as part of the maze. Add the walls of the cell to the wall list.
3. While there are walls in the list:
1. Pick a random wall from the list. If only one of the two cells that the wall divides is visited, then:
1. Make the wall a passage and mark the unvisited cell as part of the maze.
2. Add the neighboring walls of the cell to the wall list.
2. Remove the wall from the list.

It will usually be relatively easy to find the way to the starting cell, but hard to find the way anywhere else.

Note that simply running classical Prim's on a graph with random edge weights would create mazes stylistically identical to Kruskal's, because they are both minimal spanning tree algorithms. Instead, this algorithm introduces stylistic variation because the edges closer to the starting point have a lower effective weight.

Modified version

Although the classical Prim's algorithm keeps a list of edges, for maze generation we could instead maintain a list of adjacent cells. If the randomly chosen cell has multiple edges that connect it to the existing maze, select one of these edges at random. This will tend to branch slightly more than the edge-based version above.

Wilson's algorithm

All the above algorithms have biases of various sorts: depth-first search is biased toward long corridors, while Kruskal's/Prim's algorithms are biased toward many short dead ends. Wilson's algorithm, on the other hand, generates an unbiased sample from the uniform distribution over all mazes, using loop-erased random walks.

We begin the algorithm by initializing the maze with one cell chosen arbitrarily. Then we start at a new cell chosen arbitrarily, and perform a random walk until we reach a cell already in the maze—however, if at any point the random walk reaches its own path, forming a loop, we erase the loop from the path before proceeding. When the path reaches the maze, we add it to the maze. Then we perform another loop-erased random walk from another arbitrary starting cell, repeating until all cells have been filled.

This procedure remains unbiased no matter which method we use to arbitrarily choose starting cells. So we could always choose the first unfilled cell in (say) left-to-right, top-to-bottom order for simplicity.

Recursive division method

Illustration of Recursive Division
original chamber division by two walls holes in walls continue subdividing... completed

Mazes can be created with recursive division, an algorithm which works as follows: Begin with the maze's space with no walls. Call this a chamber. Divide the chamber with a randomly positioned wall (or multiple walls) where each wall contains a randomly positioned passage opening within it. Then recursively repeat the process on the subchambers until all chambers are minimum sized. This method results in mazes with long straight walls crossing their space, making it easier to see which areas to avoid.

For example, in a rectangular maze, build at random points two walls that are perpendicular to each other. These two walls divide the large chamber into four smaller chambers separated by four walls. Choose three of the four walls at random, and open a one cell-wide hole at a random point in each of the three. Continue in this manner recursively, until every chamber has a width of one cell in either of the two directions.

Simple algorithms 3D version of Prim's algorithm. Vertical layers are labeled 1 through 4 from bottom to top. Stairs up are indicated with "/"; stairs down with "\", and stairs up-and-down with "x". Source code is included with the image description.

Other algorithms exist that require only enough memory to store one line of a 2D maze or one plane of a 3D maze. Eller's algorithm prevents loops by storing which cells in the current line are connected through cells in the previous lines, and never removes walls between any two cells already connected. The Sidewinder algorithm starts with an open passage along the entire the top row, and subsequent rows consist of shorter horizontal passages with one connection to the passage above. The Sidewinder algorithm is trivial to solve from the bottom up because it has no upward dead ends. Given a starting width, both algorithm create perfect mazes of unlimited height.

Most maze generation algorithms require maintaining relationships between cells within it, to ensure the end result will be solvable. Valid simply connected mazes can however be generated by focusing on each cell independently. A binary tree maze is a standard orthogonal maze where each cell always has a passage leading up or leading left, but never both. To create a binary tree maze, for each cell flip a coin to decide whether to add a passage leading up or left. Always pick the same direction for cells on the boundary, and the end result will be a valid simply connected maze that looks like a binary tree, with the upper left corner its root. As with Sidewinder, the binary tree maze has no dead ends in the directions of bias.

A related form of flipping a coin for each cell is to create an image using a random mix of forward slash and backslash characters. This doesn't generate a valid simply connected maze, but rather a selection of closed loops and unicursal passages. (The manual for the Commodore 64 presents a BASIC program using this algorithm, but using PETSCII diagonal line graphic characters instead for a smoother graphic appearance.)

Cellular automaton algorithms

Certain types of cellular automata can be used to generate mazes. Two well-known such cellular automata, Maze and Mazectric, have rulestrings B3/S12345 and B3/S1234. In the former, this means that cells survive from one generation to the next if they have at least one and at most five neighbours. In the latter, this means that cells survive if they have one to four neighbours. If a cell has exactly three neighbours, it is born. It is similar to Conway's Game of Life in that patterns that do not have a living cell adjacent to 1, 4, or 5 other living cells in any generation will behave identically to it. However, for large patterns, it behaves very differently from Life.

For a random starting pattern, these maze-generating cellular automata will evolve into complex mazes with well-defined walls outlining corridors. Mazecetric, which has the rule B3/S1234 has a tendency to generate longer and straighter corridors compared with Maze, with the rule B3/S12345. Since these cellular automaton rules are deterministic, each maze generated is uniquely determined by its random starting pattern. This is a significant drawback since the mazes tend to be relatively predictable.

Like some of the graph-theory based methods described above, these cellular automata typically generate mazes from a single starting pattern; hence it will usually be relatively easy to find the way to the starting cell, but harder to find the way anywhere else.

Python code example

Example implementation of a variant of Prim's algorithm in Python/NumPy. Prim's algorithm above starts with a grid full of walls and grows a single component of pathable tiles. In this example, we start with an open grid and grow multiple components of walls.

This algorithm works by creating n (density) islands of length p (complexity). An island is created by choosing a random starting point with odd coordinates, then a random direction is chosen. If the cell two steps in the direction is free, then a wall is added at both one step and two steps in this direction. The process is iterated for n steps for this island. p islands are created. n and p are expressed as float to adapt them to the size of the maze. With a low complexity, islands are very small and the maze is easy to solve. With low density, the maze has more "big empty rooms".

import numpy
from numpy.random import randint as rand
import matplotlib.pyplot as pyplot

def maze(width=81, height=51, complexity=.75, density=.75):
# Only odd shapes
shape = ((height // 2) * 2 + 1, (width // 2) * 2 + 1)
# Adjust complexity and density relative to maze size
complexity = int(complexity * (5 * (shape + shape))) # number of components
density    = int(density * ((shape // 2) * (shape // 2))) # size of components
# Build actual maze
Z = numpy.zeros(shape, dtype=bool)
# Fill borders
Z[0, :] = Z[-1, :] = 1
Z[:, 0] = Z[:, -1] = 1
# Make aisles
for i in range(density):
x, y = rand(0, shape // 2) * 2, rand(0, shape // 2) * 2 # pick a random position
Z[y, x] = 1
for j in range(complexity):
neighbours = []
if x > 1:             neighbours.append((y, x - 2))
if x < shape - 2:  neighbours.append((y, x + 2))
if y > 1:             neighbours.append((y - 2, x))
if y < shape - 2:  neighbours.append((y + 2, x))
if len(neighbours):
y_,x_ = neighbours[rand(0, len(neighbours) - 1)]
if Z[y_, x_] == 0:
Z[y_, x_] = 1
Z[y_ + (y - y_) // 2, x_ + (x - x_) // 2] = 1
x, y = x_, y_
return Z

pyplot.figure(figsize=(10, 5))
pyplot.imshow(maze(80, 40), cmap=pyplot.cm.binary, interpolation='nearest')
pyplot.xticks([]), pyplot.yticks([])
pyplot.show()


C code example

The code below is an example of depth-first search maze generator in C.

//Code by Jacek Wieczorek

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

typedef struct
{
int x, y; //Node position - little waste of memory, but it allows faster generation
void *parent; //Pointer to parent node
char c; //Character to be displayed
char dirs; //Directions that still haven't been explored
} Node;

Node *nodes; //Nodes array
int width, height; //Maze dimensions

int init( )
{
int i, j;
Node *n;

//Allocate memory for maze
nodes = calloc( width * height, sizeof( Node ) );
if ( nodes == NULL ) return 1;

//Setup crucial nodes
for ( i = 0; i < width; i++ )
{
for ( j = 0; j < height; j++ )
{
n = nodes + i + j * width;
if ( i * j % 2 )
{
n->x = i;
n->y = j;
n->dirs = 15; //Assume that all directions can be explored (4 youngest bits set)
n->c = ' ';
}
else n->c = '#'; //Add walls between nodes
}
}
return 0;
}

{
//Connects node to random neighbor (if possible) and returns
//address of next node that should be visited

int x, y;
char dir;
Node *dest;

//Nothing can be done if null pointer is given - return
if ( n == NULL ) return NULL;

//While there are directions still unexplored
while ( n->dirs )
{
//Randomly pick one direction
dir = ( 1 << ( rand( ) % 4 ) );

//If it has already been explored - try again
if ( ~n->dirs & dir ) continue;

//Mark direction as explored
n->dirs &= ~dir;

//Depending on chosen direction
switch ( dir )
{
//Check if it's possible to go right
case 1:
if ( n->x + 2 < width )
{
x = n->x + 2;
y = n->y;
}
else continue;
break;

//Check if it's possible to go down
case 2:
if ( n->y + 2 < height )
{
x = n->x;
y = n->y + 2;
}
else continue;
break;

//Check if it's possible to go left
case 4:
if ( n->x - 2 >= 0 )
{
x = n->x - 2;
y = n->y;
}
else continue;
break;

//Check if it's possible to go up
case 8:
if ( n->y - 2 >= 0 )
{
x = n->x;
y = n->y - 2;
}
else continue;
break;
}

//Get destination node into pointer (makes things a tiny bit faster)
dest = nodes + x + y * width;

//Make sure that destination node is not a wall
if ( dest->c == ' ' )
{
if ( dest->parent != NULL ) continue;

dest->parent = n;

//Remove wall between nodes
nodes[n->x + ( x - n->x ) / 2 + ( n->y + ( y - n->y ) / 2 ) * width].c = ' ';

//Return address of the child node
return dest;
}
}

//If nothing more can be done here - return parent's address
return n->parent;
}

void draw( )
{
int i, j;

//Outputs maze to terminal - nothing special
for ( i = 0; i < height; i++ )
{
for ( j = 0; j < width; j++ )
{
printf( "%c", nodes[j + i * width].c );
}
printf( "\n" );
}
}

int main( int argc, char **argv )
{
Node *start, *last;

//Check argument count
if ( argc < 3 )
{
fprintf( stderr, "%s: please specify maze dimensions!\n", argv );
exit( 1 );
}

//Read maze dimensions from command line arguments
if ( sscanf( argv, "%d", &width ) + sscanf( argv, "%d", &height ) < 2 )
{
fprintf( stderr, "%s: invalid maze size value!\n", argv );
exit( 1 );
}

//Allow only odd dimensions
if ( !( width % 2 ) || !( height % 2 ) )
{
fprintf( stderr, "%s: dimensions must be odd!\n", argv );
exit( 1 );
}

//Do not allow negative dimensions
if ( width <= 0 || height <= 0 )
{
fprintf( stderr, "%s: dimensions must be greater than 0!\n", argv );
exit( 1 );
}

//Seed random generator
srand( time( NULL ) );

//Initialize maze
if ( init( ) )
{
fprintf( stderr, "%s: out of memory!\n", argv );
exit( 1 );
}

//Setup start node
start = nodes + 1 + width;
start->parent = start;
last = start;

//Connect nodes until start node is reached and can't be left
while ( ( last = link( last ) ) != start );
draw( );
}