A comprehensive Python implementation of various artificial intelligence search algorithms for solving mazes. This project demonstrates the practical application of different search strategies, from basic uninformed search to advanced heuristic-based algorithms.
This maze solver supports multiple search algorithms and provides visual representation of both the maze and the solution path. The implementation includes performance metrics to compare the efficiency of different algorithms.
- Multiple Search Algorithms: Implements five different search strategies
- Visual Output: ASCII representation of mazes with solution paths
- Performance Metrics: Tracks steps explored and solution path length
- Flexible Input: Supports custom maze files with simple text format
- Command-line Interface: Easy-to-use CLI with algorithm selection
- Strategy: Explores as far as possible along each branch before backtracking
- Implementation: Uses a stack (LIFO) frontier
- Characteristics: Memory efficient, not guaranteed to find optimal solution
- Strategy: Explores all neighbors at current depth before moving to next level
- Implementation: Uses a queue (FIFO) frontier
- Characteristics: Guaranteed to find shortest path, higher memory usage
- Strategy: Always moves toward the goal using heuristic function
- Heuristic: Manhattan distance to goal
- Characteristics: Fast but not guaranteed to find optimal solution
- Strategy: Considers both distance from start and distance to goal
- Heuristic: Combines start distance and goal distance
- Characteristics: Better balance between exploration and exploitation
- Strategy: Optimal pathfinding using f(n) = g(n) + h(n)
- Heuristic: Actual cost from start + Manhattan distance to goal
- Characteristics: Guaranteed optimal solution, admissible heuristic
No external dependencies required. The script uses only Python standard library modules.
git clone <repository-url>
cd mazepython maze.py maze1.txtpython maze.py maze1.txt --search-type breadth-first-searchdepth-first-search(default)breadth-first-searchgreedy-best-first-searchsmart-greedy-best-first-searcha-star-search
python maze.py [-h] [--search-type {depth-first-search,breadth-first-search,greedy-best-first-search,smart-greedy-best-first-search,a-star-search}] filenameMazes are defined in simple text files using the following symbols:
A- Start position (exactly one required)B- Goal position (exactly one required)#- Wall (impassable)- Empty space (passable)
#####B#
##### #
#### #
#### ##
##
A######
The program displays:
- Original Maze: Shows the initial maze layout
- Solution: Maze with solution path marked
- Performance Metrics: Number of explored steps and solution path length
█- WallA- Start positionB- Goal position*- Solution path-- Explored but not part of solution- Empty space
The repository includes five sample mazes of varying complexity:
- maze1.txt: Simple 6x7 maze - good for testing basic functionality
- maze2.txt: Complex 16x29 maze - tests algorithm efficiency
- maze3.txt: Small 6x7 maze with different layout
- maze4.txt: Medium 7x12 maze with moderate complexity
- maze5.txt: Linear 7x12 maze - tests algorithm behavior on simple paths
Different algorithms will show varying performance on the same maze:
- BFS: Finds shortest path but explores many nodes
- DFS: May find longer paths but uses less memory
- Greedy: Fast execution but may not find optimal solution
- A*: Optimal solution with efficient exploration
- Node: Represents a state in the search space with parent tracking
- Maze: Handles maze loading, validation, and solution visualization
- Frontier Classes: Implement different search strategies
StackFrontier: DFS implementationQueueFrontier: BFS implementationClosestToGoalFrontier: Greedy best-first searchClosestToStartAndGoalFrontier: Smart greedy and A* search
- Heuristic Function: Uses Manhattan distance for informed search
- Path Reconstruction: Traces back from goal to start using parent pointers
- Validation: Ensures exactly one start and one goal position
- Error Handling: Handles unsolvable mazes gracefully
This project demonstrates:
- Search Algorithm Theory: Practical implementation of classical AI algorithms
- Heuristic Design: How different heuristics affect search performance
- Space-Time Tradeoffs: Comparison between different algorithmic approaches
- Problem Representation: Converting real-world problems to search spaces
Feel free to contribute by:
- Adding new search algorithms
- Improving maze generation
- Enhancing visualization
- Adding performance benchmarks
- Creating more test cases
This project is part of the CS50AI curriculum and is intended for educational purposes.
Originally developed as part of Harvard's CS50 Introduction to Artificial Intelligence with Python course.