0% found this document useful (0 votes)
9 views6 pages

"Sheikh Fazilatunnesa Mujib University": Computer Science & Engineering (CSE)

This document contains Python code implementations of four graph search algorithms: Depth-First Search (DFS), Breadth-First Search (BFS), A* search algorithm, and Alpha-Beta pruning. It includes code snippets demonstrating how each algorithm traverses a sample graph data structure using a stack for DFS, queue for BFS, priority queue for A*, and game tree pruning for Alpha-Beta. The code is submitted as part of an Artificial Intelligence course assignment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

"Sheikh Fazilatunnesa Mujib University": Computer Science & Engineering (CSE)

This document contains Python code implementations of four graph search algorithms: Depth-First Search (DFS), Breadth-First Search (BFS), A* search algorithm, and Alpha-Beta pruning. It includes code snippets demonstrating how each algorithm traverses a sample graph data structure using a stack for DFS, queue for BFS, priority queue for A*, and game tree pruning for Alpha-Beta. The code is submitted as part of an Artificial Intelligence course assignment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

“Sheikh Fazilatunnesa Mujib University”

Computer Science & Engineering (CSE)

Course Title: Artificial Intelligence (Sessional)

Course Code : CSE–346

Report Title : BFS Algorithm, DFS Algorithm, A* Algorithm, Alpha-

Beta Pruning Algorithm implementation in Python (Codes).

Submitted To :
Afrin Jahan Chowdhury
Lecturer
Department of Computer Science and Engineering (CSE)
Sheikh Fazilatunnesa Mujib University, Jamalpur.

Submitted By :
Name : Md. Ashraful Islam
Id : 0742010005101021
Batch : 6th

Submission Date : 21/11/2022


DFS uses a STACK (LIFO)
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F', 'G'],
'D': [],
'E': [],
'F': [],
'G': []
}
visited = []
stack = []
def dfs(visited, graph, node):
visited.append(node)
stack.append(node)
while stack:
s = stack.pop(0)
print(s, end=" ")
for neighbour in graph[s]:
if neighbour not in visited:
visited.append(neighbour)
stack.append(neighbour)
dfs(visited, graph, 'A')
BFS uses a QUEUE (FIFO)
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F', 'G'],
'D': [],
'E': [],
'F': [],
'G': []
}
visited = []
queue = []
def BFS(visited, graph, node):
visited.append(node)
queue.append(node)
while queue:
s = queue.pop(0)
print(s, end=" ")
for neighbour in graph[s]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
BFS(visited, graph, 'A')
Alpha-beta pruning Algorithm
MAX, MIN = 1000, -1000
def minimax(depth, nodeIndex, maximizingPlayer,
values, alpha, beta):
if depth == 3:
return values[nodeIndex]
if maximizingPlayer:
best = MIN
for i in range(0, 2):
val = minimax(depth + 1, nodeIndex * 2 + i,
False, values, alpha, beta)
best = max(best, val)
alpha = max(alpha, best)
# Alpha Beta Pruning
if beta <= alpha:
break
return best
else:
best = MAX
for i in range(0, 2):
val = minimax(depth + 1, nodeIndex * 2 + i,
True, values, alpha, beta)
best = min(best, val)
beta = min(beta, best)
# Alpha Beta Pruning
if beta <= alpha:
break
return best
if __name__ == "__main__":
values = [3, 5, 6, 9, 1, 2, 0, -1]
print("The optimal value is :", minimax(0, 0, True, values, MIN, MAX))
A* Algorithm
from collections import deque
class Graph:
def __init__(self, adjac_lis):
self.adjac_lis = adjac_lis
def get_neighbors(self, v):
return self.adjac_lis[v]
def h(self, n):
H={
'S': 5,
'A': 3,
'B': 4,
'C': 2,
'D': 6,
'G': 0
}
return H[n]
def a_star_algorithm(self, start, stop):
open_lst = set([start])
closed_lst = set([])
poo = {}
poo[start] = 0
par = {}
par[start] = start
while len(open_lst) > 0:
n = None
for v in open_lst:
if n == None or poo[v] + self.h(v) < poo[n] + self.h(n):
n = v;
if n == None:
print('Path does not exist!')
return None
if n == stop:
reconst_path = []
while par[n] != n:
reconst_path.append(n)
n = par[n]
reconst_path.append(start)
reconst_path.reverse()
print('Path found: {}'.format(reconst_path))
return reconst_path
for (m, weight) in self.get_neighbors(n):
if m not in open_lst and m not in closed_lst:
open_lst.add(m)
par[m] = n
poo[m] = poo[n] + weight
else:
if poo[m] > poo[n] + weight:
poo[m] = poo[n] + weight
par[m] = n
if m in closed_lst:
closed_lst.remove(m)
open_lst.add(m)
open_lst.remove(n)
closed_lst.add(n)
print('Path does not exist!')
return None
## DRiver code
adjac_lis = {
'S': [('A', 1), ('G', 10)],
'A': [('B', 2), ('C', 1)],
'C': [('D', 3), ('G', 0)],
'B': [('D', 5)],
'D': [('G', 2)]
}
graph1 = Graph(adjac_lis)
graph1.a_star_algorithm('S', 'G')

You might also like