What’s a good algorithm to generate a maze?


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?

Best Solution

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.