Professional Documents
Culture Documents
Lab - Ai - DFS & BSF1
Lab - Ai - DFS & BSF1
Aim:
To implement uninformed search algorithms such as BFS and DFS.
Algorithm:
Step 1: = Initialize an empty list called 'visited' to keep track of the nodes visited during the
traversal.
Step 2: = Initialize an empty queue called 'queue' to keep track of the nodes to be traversed
in the future.
Step 3: = Add the starting node to the 'visited' list and the 'queue'.
Step 4: = While the 'queue' is not empty, do the following:
a. Dequeue the first node from the 'queue' and store it in a variable called 'current'.
b. Print 'current'.
c. For each of the neighbours of 'current' that have not been visited yet, do the
following:
i. Mark the neighbour as visited and add it to the 'queue'.
Step 5: = When all the nodes reachable from the starting node have been visited, terminate
the algorithm.
'2' : [],
'4' : ['8'],
'8' : [] 2 4 8
}
visited = []
queue = []
def bfs(visited, graph, node):
visited.append(node)
queue.append(node)
while queue:
m = queue.pop(0)
print (m, end = " ")
for neighbour in graph[m]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
print("Following is the Breadth-First Search")
bfs(visited, graph, '5')
Output: Following is the Breadth-First Search
537248
PROGRAM:
from collections import deque
class Graph:
def __init__(self, adjac_list):
self.adjac_list = adjac_list
def bfs(self, start):
visited = set()
queue = deque([start])
while queue:
vertex = queue.popleft()
if vertex not in visited:
print(vertex, end=' ')
visited.add(vertex)
neighbors = self.adjac_list[vertex]
queue.extend(neighbors)
# Example usage:
adjac_list = {
'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'],
'E': ['B', 'F'],
'F': ['C', 'E']
}
graph = Graph(adjac_list)
start_node = 'A'
print("BFS Traversal:")
graph.bfs(start_node)
OUTPUT:
BFS Traversal:
ABCDEF
To implement the DFS using python
PROGRAM:
class Graph:
def __init__(self, adjac_list):
self.adjac_list = adjac_list
def dfs(self, start, visited=None):
if visited is None:
visited = set()
visited.add(start)
print(start, end=' ')
for neighbor in self.adjac_list[start]:
if neighbor not in visited:
self.dfs(neighbor, visited)
# Example usage:
adjac_list = {
'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'],
'E': ['B', 'F'],
'F': ['C', 'E']
}
graph = Graph(adjac_list)
start_node = 'A'
print("DFS Traversal:")
graph.dfs(start_node)
OUTPUT:
DFS Traversal:
ABDEFC
Question 4.
A simple knowledgebase (KB) from the Kinship Domain Object relationships as a KB:
“Hasibis a parent ofRakib. Rakib is a parent ofSohel. Rakibis a parent ofRebeka. Rashidis a
parent ofHasib. If X is a parent of Y and Y is a parent of Z, then X is a grandparent of Z.”
List of tuples and sample procedure to manipulate the KB in Python:
tupleList1=[('parent', 'Hasib', 'Rakib'),('parent', 'Rakib', 'Sohel'),('parent', 'Rakib',
'Rebeka'),('parent', 'Rashid', 'Hasib')]
# Procedure to find the grandchildren of X
X=str(input("Grandparent:"))
print('Grandchildren:', end=' ')
i=0
while(i<=3):
if ((tupleList1[i][0] == 'parent')&( tupleList1[i][1] == X)):
for j in range(4):
if ((tupleList1[j][0] == 'parent') & ( tupleList1[i][2] == tupleList1[j][1])):
print(tupleList1[j][2], end=' ')