What's a good algorithm to generate a maze? [closed]
Asked Answered
P

9

81

Say you want a simple maze on an N by M grid, with one path through, and a good number of dead ends, but that looks "right" (i.e. like someone made it by hand without too many little tiny dead ends and all that). Is there a known way to do this?

Pool answered 1/9, 2008 at 21:57 Comment(0)
M
50

From http://www.astrolog.org/labyrnth/algrithm.htm

Recursive backtracker: This is somewhat related to the recursive backtracker solving method described below, and requires stack up to the size of the Maze. When carving, be as greedy as possible, and always carve into an unmade section if one is next to the current cell. Each time you move to a new cell, push the former cell on the stack. If there are no unmade cells next to the current position, pop the stack to the previous position. The Maze is done when you pop everything off the stack. This algorithm results in Mazes with about as high a "river" factor as possible, with fewer but longer dead ends, and usually a very long and twisty solution. It runs quite fast, although Prim's algorithm is a bit faster. Recursive backtracking doesn't work as a wall adder, because doing so tends to result in a solution path that follows the outside edge, where the entire interior of the Maze is attached to the boundary by a single stem.

They produce only 10% dead ends

is an example of a maze generated by that method.

Military answered 1/9, 2008 at 22:4 Comment(2)
Recursive backtracker generates a pretty river-y solution, as noted. Of perhaps more interest is Eller's Algorithm, which has nice properties similar to the random algorithms (that give good river-y solution percentages and dead-end percentages) but runs waay faster.Pool
The example for this answer gives an error message.Trotter
T
79

It turns out there are 11 classic algorithms to generate "perfect" mazes. A maze is perfect if it has one, and only one, solution. Here are some links to each algorithm, in rough order of my preference.

  1. Kruskal's
  2. Prim's
  3. Recursive Backtracker
  4. Aldous-Broder
  5. Growing Tree
  6. Hunt-and-Kill
  7. Wilson's
  8. Eller's
  9. Recursive Division (Predictable)
  10. Sidewinder (Predictable)
  11. Binary Tree (Flawed)

For more info, check out mazelib on GitHub, a Python library implementing all the standard maze generating/solving algorithms.

Tryma answered 15/5, 2014 at 15:0 Comment(3)
You say here cellular automaton generates perfect mazes, but your library says otherwise. How come? github.com/theJollySin/mazelib/blob/master/docs/…Aleppo
@Aleppo You are totally correct. In the end, I suppose I just really want there to be an even 12 classic maze-generating algorithms. And while Cellular Automaton is really popular, it's definitely not "perfect".Tryma
Haha, it's ok...Aleppo
M
50

From http://www.astrolog.org/labyrnth/algrithm.htm

Recursive backtracker: This is somewhat related to the recursive backtracker solving method described below, and requires stack up to the size of the Maze. When carving, be as greedy as possible, and always carve into an unmade section if one is next to the current cell. Each time you move to a new cell, push the former cell on the stack. If there are no unmade cells next to the current position, pop the stack to the previous position. The Maze is done when you pop everything off the stack. This algorithm results in Mazes with about as high a "river" factor as possible, with fewer but longer dead ends, and usually a very long and twisty solution. It runs quite fast, although Prim's algorithm is a bit faster. Recursive backtracking doesn't work as a wall adder, because doing so tends to result in a solution path that follows the outside edge, where the entire interior of the Maze is attached to the boundary by a single stem.

They produce only 10% dead ends

is an example of a maze generated by that method.

Military answered 1/9, 2008 at 22:4 Comment(2)
Recursive backtracker generates a pretty river-y solution, as noted. Of perhaps more interest is Eller's Algorithm, which has nice properties similar to the random algorithms (that give good river-y solution percentages and dead-end percentages) but runs waay faster.Pool
The example for this answer gives an error message.Trotter
T
34

A pretty straightforward solution could be to assign random weights to the graph edges and apply Kruskal's algorithm to find a minimum spanning tree.

Best discussion ever on maze generation algorithms: http://www.jamisbuck.org/presentations/rubyconf2011/index.html (was on HN a couple days ago).

Tunicate answered 30/9, 2011 at 14:31 Comment(3)
That is not just a great discussion on maze generation. It's a great example of a technical presentation done perfectly.Sumac
Wow, this is the best discussion on maze generation I've ever seen. What a fantastic presentation!Dodd
Because of your answer, I was able to use the discussion to write a small program to accomplish this, without any need to refer to source code. Those links presents the material very elegantly. Thank you for such an awesome reply!Boschvark
I
10

My favorite way is to use Kruskal's algorithm, but when randomly choosing and edge to remove, weight the choice based on the types of edges it's connected to.

By varying the weights for different edge types, you can generate mazes with lots of distinct characteristics or "personalities". See my example here:

https://mtimmerm.github.io/webStuff/maze.html

Infielder answered 15/1, 2017 at 0:35 Comment(0)
P
6

Strangely enough, by slightly changing the 'canonical' rules and starting from a random configuration, Conway's Game of Life seems to generate pretty nice mazes!

(I don't remember the exact rule, but it's a very simple modification that tends to 'densify' the population of cells...)

Palmapalmaceous answered 1/9, 2008 at 22:0 Comment(1)
Nice solution, if the walls are one cell thick.Cruiser
A
3

Recursive Backtracking is the easiest algorithm to implement.

Here's a Java implementation:

Here Cell is a class representing a cell in a 2D grid and cells is a 2D array of Cell objects. Cell has boolean variables top, bottom, left and right to indicate whether a cell has walls on these sides, a boolean variable visited to check whether we have traversed it and two integer variables row and col to indicate its position in the grid.

Cell current = cells[0][0] , next;
    current.visited=true;
    do{
        next = getNeighbour(current);
        if(next!=null){
            removeWall(current , next);
            st.push(current);
            current = next;
            current.visited = true;
        }
        else {
            current = st.pop();
        }
    }
    while (!st.empty());


    private Cell getNeighbour(Cell cell){
    ArrayList<Cell> ara = new ArrayList<>();
    if(cell.col>0 && !cells[cell.col-1][cell.row].visited)
         ara.add(cells[cell.col-1][cell.row]);

    if(cell.row>0 && !cells[cell.col][cell.row-1].visited)
         ara.add(cells[cell.col][cell.row-1]);

    if(cell.col<col-1 && !cells[cell.col+1][cell.row].visited)
         ara.add(cells[cell.col+1][cell.row]);
    if(cell.row<row-1 && !cells[cell.col][cell.row+1].visited)
         ara.add(cells[cell.col][cell.row+1]); 


    if(ara.size()>0){
        return ara.get(new Random().nextInt(ara.size()));
    }else{
        return null;
    }
}
private void removeWall(Cell curr , Cell nxt){
    if((curr.col == nxt.col) && (curr.row == nxt.row+1)){/// top
        curr.top=nxt.botttom=false;
    }
    if(curr.col==nxt.col && curr.row == nxt.row-1){///bottom
        curr.botttom = nxt.top = false;
    }
    if(curr.col==nxt.col-1 && curr.row==nxt.row ){///right
        curr.right = nxt.left = false;
    }
    if(curr.col == nxt.col+1 && curr.row == nxt.row){///left
        curr.left = nxt.right = false;
    }
}
Arrhythmia answered 7/5, 2020 at 20:8 Comment(0)
C
2

One of the methods to generate a maze is the randomized version of Prim's algorithm.

Start with a grid full of walls. Pick a cell, mark it as part of the maze. 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 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.

If the cell on the opposite side already was in the maze, remove the wall from the list.

Cullie answered 3/6, 2014 at 11:45 Comment(0)
S
1

Here's the DFS algorithm written as pseudocode:

create a CellStack (LIFO) to hold a list of cell locations
set TotalCells = number of cells in grid
choose a cell at random and call it CurrentCell
set VisitedCells = 1

while VisitedCells < TotalCells find all neighbors of CurrentCell with all walls intact
if one or more found choose one at random
knock down the wall between it and CurrentCell
push CurrentCell location on the CellStack
make the new cell CurrentCell
add 1 to VisitedCells else pop the most recent cell entry off the CellStack
make it CurrentCell endIf endWhile

Spotty answered 11/4, 2015 at 4:20 Comment(0)
S
1

I prefer a version of the Recursive Division algorithm. It is described in detail here.

I will give a quick overview:
The original recursive division algorithm works as follows. First, start with an empty area for the maze. Add one straight wall to divide the chamber in two, and put one hole in that wall somewhere. Then, recursively repeat this process on each of the two new chambers until the desired passage size is reached. This is simple and works well, but there are obvious bottlenecks which make the maze easy to solve.

The variant solves this problem by drawing randomized, "curved" walls rather than straight ones, making the bottlenecks less obvious.

Sprat answered 29/10, 2019 at 23:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.