What's a good algorithm to generate a maze?

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.


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.


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

Tags:

Algorithm

Maze