The maze shown on the right has been generated by the Some other algorithm maze generation algorithm, with a cell list. For the source code for a Java applet that was responsible, click on the maze.
Table of contents |
2 Some other algorithm 3 Kruskal's algorithm 4 Small-memory algorithm 5 External links |
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.
Choose between using a cell list or a wall list
The time can be reduced to O(n log n) by flood filling the smallest area with the number of the largest area, instead of looking at which number is largest. This requires remembering the size of the area associated with each number. Similarly, one could use pointers to other cells, forming trees, and when joining areas, point the root of the tree with lowest depth to the root of the other tree, storing the depth of the tree at the root, also in O(n log n) time.
Depth first search
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.Some other algorithm
It will usually be relatively easy to find the way to the starting cell, but hard to find the way anywhere else.
Kruskal's algorithm
At any point in this algorithm, the number in a cell uniquely identifies the region to which it belongs. However, note that a naïve implementation of this algorithm takes O(n2) time because of the repeated flood filling of areas with the higher number.Small-memory algorithm
Other 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