![]() |
|
|||||||||||||||||
Maze generation algorithmThere are a number of different maze generation algorithms, that is, automated methods for the creation of mazes. The maze shown on the right has been generated by the modified version of Prim's algorithm, below. For the source code for a Java applet that was responsible, click on the maze.
TheoryA maze is basically a graph which presents a traversal challenge between two points. If the graph is not connected, then there are regions of the graph that are wasted because they do not contribute to the search space. If the graph contains loops, then there may be multiple paths between the chosen paths. Because of this, maze generation is often approached as generating a random spanning tree on a connected graph. Loops which can confound naive maze solvers may be introduced by adding random edges to the result during the course of the algorithm. Common algorithms are based on search or minimal spanning tree algorithms for connected graphs, with edge weights chosen randomly. Because mazes are often approached from a different paradigm to traditional graph theory, different nomenclature is commonly used: graph edges not included in the resultant spanning tree are called "walls"; edges in the spanning tree are called "passages"; and vertices are typically called "cells" or "rooms". Although maze algorithms are often presented in the context of rectangular or hexagonal arrays, typically they perform equally well on any connected graph. Depth first searchThis algorithm is a randomized version of the depth first search algorithm.
If your computer architecture has a small stack and cannot effectively use recursion, you can store the backtracking information in the maze itself; this also provides a quick way to display a solution, by starting at any given point and backtracking to the exit. Mazes generated with a depth-first search have a low branching factor and contain many long corridors, which makes depth-first a good algorithm for generating mazes in video games. In mazes generated by that algorithm, it will typically be relatively easy to find the way to the square that was first picked at the beginning of the algorithm, since most paths lead to or from there, but hard to find the way out. Randomized Kruskal's AlgorithmThis algorithm is simply a randomized version of Kruskal's algorithm.
There are several data structures that can be used to model the sets of cells. An efficient implementation can perform each union and find operation on two sets in constant amortized time, so the running time of this algorithm is proportional to the number of walls available to the maze. 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. Randomized Prim's AlgorithmThis algorithm is a randomized version of Prim's algorithm.
Like the depth-first 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 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. Modified versionAlthough the classical Prim's algorithm keeps a list of edges, for maze generation we could instead maintain a list of adjacent cells. 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. Small-memory algorithmOther algorithms exist that require only enough memory to store one line of a 2D maze or one plane of a 3D maze; they work by storing which cells in the current line are connected through cells in the previous lines. This algorithm never knocks down a wall between any two cells already connected. External links
The contents of this article are licensed from Wikipedia.org under the GNU Free Documentation License.
How to see transparent copy 01-04-2007 01:21:04 |
|






