A Python program that generates random mazes and runs three search algorithms sequentially — visualizing every node expansion in the terminal and measuring each algorithm's path length, nodes expanded, and execution time.
At startup the user specifies the number of rows, columns, and obstacle density as a percentage. Each cell is populated one by one — open cells, obstacles, a start state, and a goal state. The start and goal are placed probabilistically: when neither has been placed yet there is a 4% chance per cell of placing one, chosen randomly between the two.
Once one point is placed, the remaining point's placement probability depends on its Manhattan distance to the already-placed point. At a distance under 10 cells the probability drops to 0.5%, and 1% elsewhere — making it more likely the two endpoints are far apart and the search is non-trivial. If either point hasn't been placed by the time the grid is filled the maze iterates again until both are set.
After generation the user can accept or reject the maze. Rejecting generates a fresh one with the same parameters.
Each algorithm prints the maze to the terminal after every node expansion — marking visited cells as o and drawing the final solution path with - for horizontal moves and | for vertical moves, highlighted in red. Row and column indices are printed on all four sides of the grid for easy cell identification.
The video below runs all three algorithms on the same maze at a fixed timer interval, concatenated side by side — making the difference in how each algorithm explores the space directly visible.
Ten runs were recorded across independently generated 10×10 mazes at 30% obstacle density to compare the algorithms. One run failed across all three due to the start and goal being separated by obstacles — a case the program handles gracefully.
| Algorithm | Avg Path Length | Avg Nodes Expanded | Avg Execution Time | Failures |
|---|---|---|---|---|
| Depth First Search | 24.6 | 37.5 | 0.158s | 1 / 10 |
| Breadth First Search | 8.4 | 39.8 | 0.108s | 1 / 10 |
| A* Search | 8.4 | 16.8 | 0.072s | 1 / 10 |