The depth-first search algorithm of maze generation is frequently implemented using backtracking:. This algorithm is a randomized version of Kruskal's algorithm. There are several data structures that can be used to model the sets of cells.
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 of Prim's 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 edge 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. Although the classical Prim's algorithm keeps a list of edges, for maze generation we could instead maintain a list of adjacent cells.
Download Random Generation Of Trees Random Generators In Computer Science
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. Mazes can be created with recursive division , an algorithm which works as follows: Begin with the maze's space with no walls. Call this a chamber. Divide the chamber with a randomly positioned wall or multiple walls where each wall contains a randomly positioned passage opening within it. Then recursively repeat the process on the subchambers until all chambers are minimum sized.
This method results in mazes with long straight walls crossing their space, making it easier to see which areas to avoid. For example, in a rectangular maze, build at random points two walls that are perpendicular to each other.
These two walls divide the large chamber into four smaller chambers separated by four walls. Choose three of the four walls at random, and open a one cell-wide hole at a random point in each of the three. Continue in this manner recursively, until every chamber has a width of one cell in either of the two directions. Other algorithms exist that require only enough memory to store one line of a 2D maze or one plane of a 3D maze. They prevent loops by storing which cells in the current line are connected through cells in the previous lines, and never remove walls between any two cells already connected.
Most maze generation algorithms require maintaining relationships between cells within it, to ensure the end result will be solvable. Valid simply connected mazes can however be generated by focusing on each cell independently. A binary tree maze is a standard orthogonal maze where each cell always has a passage leading up or leading left, but never both.
To create a binary tree maze, for each cell flip a coin to decide whether to add a passage leading up or left. Always pick the same direction for cells on the boundary, and the end result will be a valid simply connected maze that looks like a binary tree , with the upper left corner its root. A related form of flipping a coin for each cell is to create an image using a random mix of forward slash and backslash characters. This doesn't generate a valid simply connected maze, but rather a selection of closed loops and unicursal passages. Certain types of cellular automata can be used to generate mazes.
In the latter, this means that cells survive if they have one to four neighbours.
Knuth: Recent News
If a cell has exactly three neighbours, it is born. It is similar to Conway's Game of Life in that patterns that do not have a living cell adjacent to 1, 4, or 5 other living cells in any generation will behave identically to it. For a random starting pattern, these maze-generating cellular automata will evolve into complex mazes with well-defined walls outlining corridors. This is a significant drawback since the mazes tend to be relatively predictable. Like some of the graph-theory based methods described above, these cellular automata typically generate mazes from a single starting pattern; hence it will usually be relatively easy to find the way to the starting cell, but harder to find the way anywhere else.
Prim's algorithm above starts with a grid full of walls and grows a single component of pathable tiles. In this example, we start with an open grid and grow multiple components of walls. This algorithm works by creating n density islands of length p complexity. An island is created by choosing a random starting point with odd coordinates, then a random direction is chosen. This mode of usage had never been considered before in the literature, as far as either of us knew, and he found that essentially every generator he tried was giving him problems when used in such a way.
Indeed, I had never tried testing a random number generator by looking only at the first few numbers that correspond to seeds 1, 2, Working with Gimeno, he carried out elaborate tests and found two cures for the problem: With either 1 or 2 , no statistical bias is detected in the resulting numbers by Marsaglia's famous "Die Hard" battery of tests. Brent and Gimeno recommended using BOTH fixes 1 and 2 , to be doubly confident that residual nonrandomness isn't lurking.
So I've done that. Fortunately the new version is shorter and easier to understand than the original. Please note, however, that the new version is NOT compatible with the old. As before, the programs have been carefully written so that identical results are obtained with both C and FORTRAN routines, in both the integer version and the double-precision floating point version.
The method can also be ported readily to other languages.
Related Video Shorts (0)
Hip, hip, hooray, a long-cherished dream is now a reality: See my CWEB page for full details. I strongly urge all CWEB users to upgrade to version 3. My present intention is that version 3. A glorious new printing of The Stanford GraphBase is now available from the publishers, with corrections to all known errors. More than half of the pages of the previous printing have been improved; and I've added a few new jokes too. I've been making some headway at last on actually writing Volume 4 of The Art of Computer Programming instead of merely preparing to write it, and some first drafts are now ready for beta-testing.
I've put them online primarily so that experts in the field can help me check the results, but brave souls who aren't afraid to look at relatively raw copy are welcome to look too. Yes, the usual rewards apply if you find any mistakes.