Mercyhurst UniversityMath DeptDr Williams Home

Prim's Algorithm Maze Generation

The Maze
Options Size:


Prim's Algorithm

This algorithm creates a new maze from a grid of cells. To begin, choose a random starting cell and add it to the maze (shown in white). Add all adjacent cells to a list of "border cells," shown in light blue in the applet.
  1. Randomly choose a border cell and add it to the maze.
  2. Randomly choose a wall between the newly added cell and any cell that's already in the maze to remove.
This algorithm tends to produce mazes with many short paths, which are therefore easier to solve than mazes generated with other techniques. The mazes are fully connected, meaning you can travel from any cell to any other cell.

About the Applet

This applet was created using JavaScript and the P5 library. No other libraries/dependencies are required. If you are having trouble viewing the applet, be sure JavaScript is enabled in your browser. Click here for instructions. Internet Explorer is not recommended for this applet.

This applet was last updated July 2019.