This repository contains a Jupyter Notebook that demonstrates the implementation of various AI search algorithms. These algorithms form the backbone of many artificial intelligence applications and are essential for solving optimization and pathfinding problems.
The notebook includes implementations of the following search algorithms:
- Breadth-First Search (BFS): Explores all nodes at the present depth before moving to nodes at the next depth.
- Depth-First Search (DFS): Explores as far as possible along each branch before backtracking.
- Uniform Cost Search (UCS): A variant of BFS that considers the cost of the path.
- Greedy Best-First Search: Prioritizes nodes based on a heuristic.
- A Search*: Combines UCS and heuristic-based search for optimal pathfinding.
Make sure you have the following installed:
- Python 3.8+
- Jupyter Notebook
- Required libraries:
- NumPy
- Matplotlib
Install the dependencies using pip:
pip install numpy matplotlib
-
Clone the repository:
git clone https://github.com/ashithapallath/AI-Searches.git cd AI-Searches
-
Open the Jupyter Notebook:
jupyter notebook search.ipynb
-
Follow the step-by-step implementation of the search algorithms. Modify the sample problems to test the algorithms in various scenarios.
from collections import deque
def bfs(graph, start):
visited = set()
queue = deque([start])
while queue:
node = queue.popleft()
if node not in visited:
visited.add(node)
queue.extend(graph[node] - visited)
return visited
import heapq
def a_star_search(graph, start, goal, h):
open_set = []
heapq.heappush(open_set, (0 + h[start], start))
came_from = {}
g_score = {node: float('inf') for node in graph}
g_score[start] = 0
while open_set:
_, current = heapq.heappop(open_set)
if current == goal:
break
for neighbor in graph[current]:
tentative_g_score = g_score[current] + graph[current][neighbor]
if tentative_g_score < g_score[neighbor]:
came_from[neighbor] = current
g_score[neighbor] = tentative_g_score
heapq.heappush(open_set, (g_score[neighbor] + h[neighbor], neighbor))
Contributions are welcome! Feel free to fork the repository and submit pull requests for improvements, bug fixes, or additional features.
This project is licensed under the MIT License.
Thanks to the AI and open-source communities for providing resources and inspiration to build and share implementations of search algorithms.