The search module is a Python framework designed to solve search problems using classic algorithms such as Breadth-First Search (BFS) and Depth-First Search (DFS).
This module is especially useful for tasks involving state-space search, such as path finding, game solving, or any scenario that requires systematic exploration of states.
It demonstrates step-by-step algorithm execution and logs internal data structures for educational purposes.
No installation is required; simply add search.py to your project folder and import the necessary classes:
from search import SearchProblem, Node
The module provides two core abstract classes, SearchProblem and Node, to be extended to implement custom search solutions. You have to create problem-specific classes by inheriting from SearchProblem and Node.
In this basic usage example, the classes Maze and MazeNode handle maze-related logic in a maze solving script:
class Maze(SearchProblem):
# ...
class MazeNode(Node):
# ...
Implement the required abstract methods in your subclasses to define your specific problem environment (refer to the documentation for more details).
class Maze(SearchProblem):
def __init__():
# Maze specific implementation here
def show_solution():
# Maze specific implementation here
class MazeNode(Node)
def __init__():
# Maze specific implementation here
def actions():
# Maze specific implementation here
def result():
# Maze specific implementation here
Instantiate a Maze object:
maze = Maze()
Solve the search problem using BFS or DFS:
maze.solve('BFS')
Print the discovered solution:
maze.show_solution('BFS')
Obtain a detailed log of algorithmic steps:
maze.save_algorithm_steps_to_file()
Full documentation, including the API specification and a working example that solves mazes, is available on Read The Docs
TOC:
-
WELLCOME
-
INTRODUCTION
-
USER GUIDE
-
USAGE EXAMPLE
-
ALL THE REST
The module follows OOP principles, encapsulating problem-solving elements into classes. Each class is responsible for specific functionality, ensuring modularity and reusability.
-
Abstract Base Classes (ABC)
Interface classes are defined as abstract using Python’s ABC module. This forces any concrete subclass to implement critical methods.
-
Protected Methods and Attributes
Methods and attributes prefixed with an underscore (_) are intended for internal use, keeping users focused on essential interfaces and abstracting lower-level details.
-
Explicit Subclassing Requirements
Marking methods as abstract in interface classes ensures subclasses provide the necessary implementations, promoting a controlled interface tailored to each search scenario.
-
Pluggable Search Algorithms
Different search strategies (BFS, DFS) are supported by selecting the type of frontier (stack, queue). This design simplifies adding or modifying search algorithms without changing the overall structure.
-
Audit Trail and Algorithm Steps Log
Dedicated class captures each step of the search process, including frontier and explored-node states. This comprehensive record aids in debugging and provides transparent insight into how the search progresses.
Method that prints solution can be used directly or overridden in subclasses to customize the solution's presentation format, allowing for flexible output that meets domain-specific or user-defined requirements.
(Not released in current version)
This project is a based on code from HarvardX:CS50’s Introduction to Artificial Intelligence with Python course refactored to achieve mentioned key features.
The Harvard course code is published under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) license