There are several data structures that can be used to model the sets of cells. Join the sets of the formerly divided cells.If the cells divided by this wall belong to distinct sets:.Create a list of all walls, and create a set for each cell, each containing just that one cell.This algorithm is a randomized version of Kruskal's algorithm. Mark the chosen cell as visited and push it to the stackĪn animation of generating a 30 by 20 maze using Kruskal's algorithm.If the current cell has any neighbours which have not been visited.
Pop a cell from the stack and make it a current cell.Choose the initial cell, mark it as visited and push it to the stack.As a solution, the same backtracking method can be implemented with an explicit stack, which is usually allowed to grow much bigger with no harm. Which is invoked once for any initial cell in the area.Ī disadvantage of the first approach is a large depth of recursion – in the worst case, the routine may need to recur on every cell of the area being processed, which may exceed the maximum recursion stack depth in many environments. Invoke the routine recursively for a chosen cell.Remove the wall between the current cell and the chosen cell.While the current cell has any unvisited neighbour cells.This can be described with a following recursive routine: The depth-first search algorithm of maze generation is frequently implemented using backtracking. Randomized depth-first search on a hexagonal grid This also provides a quick way to display a solution, by starting at any given point and backtracking to the beginning. The algorithm can be rearranged into a loop by storing backtracking information in the maze itself. We can be sure every cell is visited.Īs given above this algorithm involves deep recursion which may cause stack overflow issues on some computer architectures. This process continues until every cell has been visited, causing the computer to backtrack all the way back to the beginning cell. 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). The computer continues this process, with a cell that has no unvisited neighbours being considered a dead-end. 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. Starting from a random cell, the computer then selects a random neighbouring cell that has not yet been visited. Consider the space for a maze being a large grid of cells (like a large chess board), each cell starting with four walls. This algorithm, also known as the "recursive backtracker" algorithm, is a randomized version of the depth-first search algorithm.įrequently implemented with a stack, this approach is one of the simplest ways to generate a maze using a computer. Second, computer traverses F using a chosenĪlgorithm, such as a depth-first search, coloring the path red.ĭuring the traversal, whenever a red edge crosses over a blue edge,įinally, when all vertices of F have been visited, F is erasedĪnd two edges from G, one for the entrance and one for the exit, are removed.Īnimation of generator using depth-first search
The animation shows the maze generation steps for aįirst, the computer creates a random planar graph G Loops, which can confound naive maze solvers, may be introduced by adding random edges to the result during the course of the algorithm. Because of this, maze generation is often approached as generating a random spanning tree. If the graph contains loops, then there may be multiple paths between the chosen 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. 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. This predetermined arrangement can be considered as a connected graph with the edges representing possible wall sites and the nodes representing cells.
Animation of graph theory based method (randomized depth-first search)Ī 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.