Watch out for bull-men! |

Here's what I learned about mazes... wait, no. Instead let me point you at some much better sources of information:

Walter Pullen has such a variety of interests that I refuse to summarize his site, in one part of it you can find a very thorough maze generator program he wrote, and he has written extensively about maze generation. Aalso I just can't keep myself from mentioning his extensive files about the movie Labyrinth.

Now you know everything about mazes. But just in case you didn't spend hours reading through all that stuff, here are some highlights:

A maze is basically equivalent to a spanning tree of a graph based on a rectangular grid. Consequently, algorithms that compute spanning trees can be adapted to make mazes. Kruskal's algorithm is like this, and builds mazes in an seemingly magical way: It connects random adjacent cells, taking care not to create a loop, until a maze basically appears out of the noise.

Speaking of Kruskal's, the hardest part is quickly deciding whether two adjacent cells are already connected. It can be done with a disjoint-set forest, and the tricks they use to make that fast (union by rank and path compression) are neat.

One of the simplest algorithms, recursive-backtracking, is my pick for making the best-looking mazes. While generators like Kruskal's tend to make lots of clustered dead-ends, recursive-backtracking prefers meandering passages. It works as basically a randomized depth-first-search of the grid.

Eller's algorithm can generate the maze one entire row at a time. It works by keeping track of which cells connected to the previous row have already been connected (possibly by an earlier, now forgotten row). On the very last row, it patches up any not-yet-connected regions of the maze. In doing so, it may end up with a long straight passage along the bottom.

The Aldous-Broder algorithm generates all possible mazes with equal probibility, but you probably don't want to use it because it's very slow. It's interesting to think about though: Start in a random cell and perform the well-named "drunkard's walk", digging a passage when (and only when) you walk into a cell you've never visited before. Assuming you have enough patience, you'll eventually visit every cell and oh hey, there's a maze!

It's not a generator, but "dead end filler" is an amusing way to solve some mazes without ever visiting cells in the solution path. Find all the dead ends in the maze and fill them in. Then find any new dead ends you created, and fill those in too. Repeat until there are no more dead ends. The remaining unfilled spaces are the solution.

Back to my project: I implemented three algorithms (recursive-backtracking, modified Prim's, and Kruskal's). I also used this as an opportunity to learn about node and npm, so the algorithm packages also have unit tests, benchmarks, and demos, and I spent some time trying to optimize them for speed and also make them easy to plug in to another project without needing to bring a lot of dependencies along.

## No comments:

## Post a Comment