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, the 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.
Randomized 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. When this algorithm is implemented using a recursive procedure, (call) stack overflow will occur for larger grids. This can be avoided by using an explicit stack instead.
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.
The randomized depth-first search algorithm of maze generation can be implemented using a stack:
Make the initial cell the current cell Push the current cell to the stack and mark it as visited While the stack is not empty If the current cell has unvisited neighbours Choose one of these neighbours randomly Remove the wall (add an edge) between the current cell and the chosen neighbor Push the neighbor to the stack and mark is as visited Make the neighbor the current cell Else Pop a cell from the stack and make it the current cell The backtracking occurs when there are no more unvisited neighbors of the current cell. In that case the algorithm tracks back and continues with the cell at the top of the stack. This is a cell somewhere on the path from the current cell back to the start cell.
Randomized Kruskal's algorithm
This algorithm is a randomized version of Kruskal's algorithm.
Create a list of all walls, and create a set for each cell, each containing just that one cell. For each wall, in some random order: If the cells divided by this wall belong to distinct sets: Remove the current wall. 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(α(V)) time; α(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
This algorithm is a randomized version of Prim's algorithm.
Start with a grid full of walls. Pick a cell, mark it as visited. Add the walls of the cell to the wall list. While there are walls in the list: Pick a random wall from the list. If only one of the two cells that the wall divides is visited, then: Make the wall a passage and mark the unvisited cell as visited. Add the neighboring walls of the cell to the wall list. 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
… excerpt ends here. Continue reading the full article.





