Maze Generation Algorithm

Embed Size (px)

Citation preview

  • 8/11/2019 Maze Generation Algorithm

    1/3

    Maze generation algorithm

    Maze generation algorithmsare 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 where 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.

    Depth-first search

    This algorithm is a randomized version of thedepth-first searchalgorithm. Frequently implemented witha stack, this approach is one of the simplest ways to generate a mazeusing a computer. Considerthe space for a maze being a large grid of cells (like a large chess board), each cell starting with fourwalls. Starting from a random cell, the computer then selects a random neighbouring cell that has notyet been visited. The computer removes the 'wall' between the two cells and adds the new cell to a

    stack (this is analogous to drawing the line on the floor). The computer continues thisprocess, with a

    cell that has no unvisited neighbours being considered a dead-end. When at a dead-end it backtracksthrough the path until it reaches a cell with an unvisited neighbour, continuing the path generation byvisiting this new, unvisited cell (creating a new junction). This process continues until every cell hasbeen visited, causing the computer to backtrack all the way back to the beginning cell. This approach

    guarantees that the maze space is completely visited.

    http://en.wikipedia.org/wiki/Depth-first_searchhttp://en.wikipedia.org/wiki/Depth-first_searchhttp://en.wikipedia.org/wiki/Depth-first_search
  • 8/11/2019 Maze Generation Algorithm

    2/3

    As stated, the algorithm is very simple and does not produce over-complex mazes. More specificrefinements to the algorithm can help to generate mazes that are harder to solve.

    1. Start at a particular cell and call it the "exit."

    2. Mark the current cell as visited, and get a list of its neighbors. For each neighbor, starting with a

    randomly selected neighbor:1. If that neighbor hasn't been visited, remove the wall between this cell and that neighbor,

    and thenrecurse with that neighbor as the current cell.The algorithm can be rearranged into a loop by storing backtracking information in the maze itself. Thisalso provides a quick way to display a solution, by starting at any given point and backtracking to theexit.

    Mazes generated with a depth-first search have a low branching factor and contain many long

    corridors. Also mazes will typically be relatively easy to find the way to the square that was first picked

    at the beginning of the algorithm, since most paths lead to or from there, but it is hard to find the way

    out.

    To add difficulty and a fun factor to depth-first search generated mazes, you can influence the likelihood

    of which neighbor you should visit, instead of it being completely random. By making it more likely to

    visit neighbors to your sides, you can have a more horizontal maze generation. Experimenting with

    directional "influence" in certain places could lead to creating fun designs, such as a checkerboard

    pattern, an X, and more.

    Recursive backtracker

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

    1. Make the initial cell the current cell and mark it as visited

    2. While there are unvisited cells1. 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

    3. Else

    1. Pick a random unvisited cell, make it the current cell and mark it as visited

    http://en.wikipedia.org/wiki/Recursionhttp://en.wikipedia.org/wiki/Backtrackinghttp://en.wikipedia.org/wiki/Backtrackinghttp://en.wikipedia.org/wiki/Recursion
  • 8/11/2019 Maze Generation Algorithm

    3/3

    Randomized Kruskal's algorithm

    This algorithm is a randomized version ofKruskal'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 adisjoint-set data structurecan perform each union and find operation on two sets

    in nearly constantamortized time(specifically, time; for any plausible value

    of ), 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.

    This algorithm is a randomized version ofPrim's algorithm.

    1. Start with a grid full of walls.

    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 the cell on the opposite side isn't in the maze yet:

    i. Make the wall a passage and mark the cell on the opposite side as part of the maze.

    ii. Add the neighboring walls of the cell to the wall list.2. If the cell on the opposite side already was in the maze, remove the wall from the list.

    Like the depth-first algorithm, 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 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.

    Modif ied version[edit]

    Although the classical Prim's algorithm keeps a list of edges, for maze generation we could insteadmaintain 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

    http://en.wikipedia.org/wiki/Kruskal%27s_algorithmhttp://en.wikipedia.org/wiki/Kruskal%27s_algorithmhttp://en.wikipedia.org/wiki/Kruskal%27s_algorithmhttp://en.wikipedia.org/wiki/Disjoint-set_data_structurehttp://en.wikipedia.org/wiki/Disjoint-set_data_structurehttp://en.wikipedia.org/wiki/Disjoint-set_data_structurehttp://en.wikipedia.org/wiki/Amortized_timehttp://en.wikipedia.org/wiki/Amortized_timehttp://en.wikipedia.org/wiki/Amortized_timehttp://en.wikipedia.org/wiki/Prim%27s_algorithmhttp://en.wikipedia.org/wiki/Prim%27s_algorithmhttp://en.wikipedia.org/wiki/Prim%27s_algorithmhttp://en.wikipedia.org/w/index.php?title=Maze_generation_algorithm&action=edit&section=6http://en.wikipedia.org/w/index.php?title=Maze_generation_algorithm&action=edit&section=6http://en.wikipedia.org/w/index.php?title=Maze_generation_algorithm&action=edit&section=6http://en.wikipedia.org/w/index.php?title=Maze_generation_algorithm&action=edit&section=6http://en.wikipedia.org/wiki/Prim%27s_algorithmhttp://en.wikipedia.org/wiki/Amortized_timehttp://en.wikipedia.org/wiki/Disjoint-set_data_structurehttp://en.wikipedia.org/wiki/Kruskal%27s_algorithm