Skip to content

This project utilizes a random Depth-First-Search approach to generate a maze with a guaranteed solution.

License

Notifications You must be signed in to change notification settings

Gyakobo/Maze-Generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pathfinding Algorithms

image image

Author: Andrew Gyakobo

Introduction

This project utilizes a randomized-Depth-First-Search approach to generate a maze with a guaranteed solution.

Methodology

This algorithm, also known as the "recursive backtracker" algorithm, is a randomized version of the depth-first search algorithm.

Frequently implemented with a 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.

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.

Delving into the program itself let's sort out some terminology:

  1. Grid Representation:

    • A 2D NumPy array is used, where 0 represents walls and 1 represents passages.
    • The grid size is odd to ensure passages are surrounded by walls.
  2. Randomized Depth-First Search:

    • The algorithm uses a stack to manage backtracking.
    • Walls are removed by marking the grid between cells.
  3. Animation:

    • The matplotlib.animation module is used to create an animated visualization of the maze generation process.
  4. Horizontal Passage Bias:

    • The nature of depth-first search results in long corridors and fewer junctions, visible in the generated maze.

Note

Mind you that the 0s in the grid symbolize the walls and the 1s symbolize the pathway. Moreover, the start position is at [1, 1] and end position at [height-2, width-2].

Result

Here are 3 maze samples:

This is a 20x20 grid

This is a 40x40 grid

This is a 100x100 grid

In case you'd like to save GIF just either uncomment or add this command at the end of the def animate_maze(frames) function.

ani.save("./miscellaneous/maze.gif", writer="pillow", fps=1000 // interval) 
print(f"Animation saved")

License

MIT

About

This project utilizes a random Depth-First-Search approach to generate a maze with a guaranteed solution.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published