Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Maze generation algorithm
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Randomized depth-first search === [[File:Depth-First Search Animation.ogv|thumb|right|Animation of generator using depth-first search]] [[File:Depth-First Search Animation-2.gif|thumb|A different animation of a generator using depth-first search]] This algorithm, also known as the "recursive backtracker" algorithm, is a randomized version of the [[depth-first search]] algorithm. Frequently implemented with a [[Stack (abstract data type)|stack]], this approach is one of the simplest ways to generate a maze using a computer. Consider the space for a maze being a large grid of cells (like a large chess board), each cell starting with four walls. Starting from a random cell, the computer then selects a random neighbouring cell that has not yet been visited. The computer removes the wall between the two cells and marks the new cell as visited, and adds it to the stack to facilitate backtracking. The computer continues this process, with a cell that has no unvisited neighbours being considered a dead-end. When at a dead-end it backtracks through the path until it reaches a cell with an unvisited neighbour, continuing the path generation by visiting this new, unvisited cell (creating a new junction). This process continues until every cell has been visited, causing the computer to backtrack all the way back to the beginning cell. We can be sure every cell is visited. As given above this algorithm involves deep recursion which may cause stack overflow issues on some computer architectures. The algorithm can be rearranged into a loop by storing 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 beginning. [[File:Horizontally Influenced Depth-First Search Generated Maze.png|thumb|right|Horizontal Passage Bias]] Mazes generated with a depth-first search have a low branching factor and contain many long corridors, because the algorithm explores as far as possible along each branch before backtracking. ==== Recursive implementation ==== [[File:Hexamaze.webm|thumb|Randomized depth-first search on a hexagonal grid]] The [[depth-first search]] algorithm of maze generation is frequently implemented using [[backtracking]]. This can be described with a following [[recursion (computer science)|recursive]] routine: # Given a current cell as a parameter # Mark the current cell as visited # While the current cell has any unvisited neighbour cells ## Choose one of the unvisited neighbours ## Remove the wall between the current cell and the chosen cell ## Invoke the routine recursively for the chosen cell which is invoked once for any initial cell in the area. ==== Iterative implementation (with stack) ==== A disadvantage of the first approach is a large depth of [[recursion]] β in the worst case, the routine may need to recur on every cell of the area being processed, which may exceed the maximum recursion stack depth in many environments. As a solution, the same backtracking method can be implemented with an explicit [[stack (abstract data type)|stack]], which is usually allowed to grow much bigger with no harm. # Choose the initial cell, mark it as visited and push it to the stack # While the stack is not empty ## Pop a cell from the stack and make it a current cell ## If the current cell has any neighbours which have not been visited ### Push the current cell to the stack ### Choose one of the unvisited neighbours ### Remove the wall between the current cell and the chosen cell ### Mark the chosen cell as visited and push it to the stack
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)