You are on page 1of 1

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment No. 1.3

Student Name: Vishal Kumar Singh UID: 21BCS2439


Branch: CSE Section/Group: 702/B
Semester: 5th Date of Performance: 03/09/2023
Subject Name: AIML Subject Code: 21CSH-316

Aim of the practical: To Implement the BFS algorithm in Python.

Objective of the practical: The objective of this experiment is to implement the


Breadth-First Search (BFS) algorithm and analyze its performance and characteristics.

Source code:
import queue
queue=[]
def bfs (visited, graph,node):
visited.add(node)
queue.append(node)
while queue:
m=queue.pop(0)
print(m,end=" ")
for neighbour in graph[m]:
if neighbour not in visited :
visited.add(neighbour)
queue.append(neighbour)

graph={
'A':['B','C','D'],
'B':['D','E'],
'C':['F'],
'D':['F'],
'E':['F'],
'F':[]
}
visited=set()
bfs (visited, graph,'A')

Output:
Path: A B C D E F

You might also like