0% found this document useful (0 votes)
6 views3 pages

Exp 1

The document outlines the implementation of uniformed search algorithms, specifically breadth-first search (BFS) and depth-first search (DFS) in Python. It provides the algorithms, sample code for both searches, and their outputs when executed on a given graph. The results confirm that both programs were executed successfully and their outputs verified.

Uploaded by

bhuvana19072005
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)
6 views3 pages

Exp 1

The document outlines the implementation of uniformed search algorithms, specifically breadth-first search (BFS) and depth-first search (DFS) in Python. It provides the algorithms, sample code for both searches, and their outputs when executed on a given graph. The results confirm that both programs were executed successfully and their outputs verified.

Uploaded by

bhuvana19072005
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/ 3

EX:NO:

IMPLEMENTATION OF UNIFORMED SEARCH ALGORITHM(BFS,DFS)


DATE:

AIM:

To write a python program to implement breadth first search in graph traversal

ALGORITHM:

1.Start by putting any one of the graph Vertices at the back of a queue.

2.Take the front item of the queue and add it to the visited list.

3.Create a list of that vertex’s adjacent nodes.Add the ones which aren’t in the visited list to
the back of the queue.

4.Keep repeating steps 2 and 3 until the queue is empty


PROGRAM:
graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'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
RESULT:
Thus the program was executed hence output was verified.
PROGRAM:
graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}
visited = set()
def dfs(visited, graph, node):
if node not in visited:
print (node)
visited.add(node)
for neighbour in graph[node]:
dfs(visited, graph, neighbour)
print("Following is the Depth-First Search")
dfs(visited, graph, '5')
OUTPUT:
Following is the Depth-First Search
5
3
2
4
8
7
RESULT:
Thus the program was executed hence output was verified.

You might also like