A Python implementation of classic pathfinding algorithms for solving ASCII mazes. This project demonstrates and compares five different search algorithms: Breadth-First Search (BFS), Depth-First Search (DFS), Uniform Cost Search (UCS), Greedy Best-First Search, and A* Search.
- Five search algorithms: BFS, DFS, UCS, Greedy, and A*
- Multiple heuristics: Manhattan and Euclidean distance for informed search algorithms
- Visual output: Annotated ASCII mazes showing the solution path and explored nodes
- Performance metrics: Tracks path cost, nodes expanded, and optimality
- Flexible input: Supports any ASCII maze format with walls (
#), start (S), goal (G), and open spaces (.or space)
- Python 3.7 or higher
- Standard library only (no external dependencies)
Clone the repository:
git clone <repository-url>
cd maze-pathfinding-algorithmsNo additional installation required - the project uses only Python standard library.
Run a search algorithm on a maze:
python pumpkin_pie_maze.py --maze data/pumpkin.txt --algo bfsbfs- Breadth-First Searchdfs- Depth-First Searchucs- Uniform Cost Searchgreedy- Greedy Best-First Searchastar- A* Search
python pumpkin_pie_maze.py --maze <maze_file> --algo <algorithm> [options]Required arguments:
--maze: Path to ASCII maze file--algo: Algorithm to use (bfs,dfs,ucs,greedy,astar)
Optional arguments:
--heuristic: Heuristic for informed search algorithms (manhattanoreuclidean, default:manhattan)--outdir: Output directory for annotated mazes (default:outputs)
# Breadth-First Search
python pumpkin_pie_maze.py --maze data/pumpkin.txt --algo bfs
# Depth-First Search
python pumpkin_pie_maze.py --maze data/pumpkin.txt --algo dfs
# Uniform Cost Search
python pumpkin_pie_maze.py --maze data/pumpkin.txt --algo ucs
# Greedy Best-First Search with Manhattan heuristic
python pumpkin_pie_maze.py --maze data/pumpkin.txt --algo greedy --heuristic manhattan
# A* Search with Euclidean heuristic
python pumpkin_pie_maze.py --maze data/pumpkin.txt --algo astar --heuristic euclideanThe program generates annotated ASCII maze files in the outputs/ directory. Each output file shows:
#- WallsS- Start positionG- Goal position+- Solution path(space) - Explored nodes (closed set)O- Frontier nodes (when captured).- Existing dots in the original maze
The output also includes:
- Path cost
- Number of nodes expanded
- Whether the solution is optimal (for algorithms that guarantee optimality)
- Path length
Maze files should be ASCII text files with:
#for wallsSfor start position (exactly one)Gfor goal position (exactly one).or space for open cells- Rectangular grid (will be padded with walls if needed)
.
├── pumpkin_pie_maze.py # Main maze solver with all algorithms
├── format_outputs.py # Utility to format output files
├── data/
│ └── pumpkin.txt # Example maze file
├── outputs/ # Generated annotated mazes
│ ├── pumpkinpie-BFS.txt
│ ├── pumpkinpie-DFS.txt
│ ├── pumpkinpie-UCS.txt
│ ├── pumpkinpie-Greedy.txt
│ └── pumpkinpie-Astar.txt
└── README.md
- Uses a queue (FIFO)
- Guarantees shortest path (optimal for unweighted graphs)
- Explores all nodes at depth
dbefore depthd+1
- Uses a stack (LIFO)
- Not optimal, but memory efficient
- Explores as deep as possible before backtracking
- Uses a priority queue ordered by path cost
- Guarantees optimal solution for weighted graphs
- Equivalent to Dijkstra's algorithm
- Uses a priority queue ordered by heuristic value
- Not optimal, but often fast
- Chooses the node closest to the goal (by heuristic)
- Uses a priority queue ordered by
f(n) = g(n) + h(n) - Guarantees optimal solution with admissible heuristics
- Balances path cost and heuristic estimate
- Manhattan Distance:
|x1 - x2| + |y1 - y2|(L1 norm) - Euclidean Distance:
√((x1 - x2)² + (y1 - y2)²)(L2 norm)
Both heuristics are admissible (never overestimate) for 4-way movement in a grid.
This project is part of a CS445 (Artificial Intelligence) course assignment.