You are on page 1of 2

21131A4410 | LOKESH DUDDUKURI

WEEK – 11

AIM: Implement Breadth First Search


DESCRIPTION:
Breadth-first search is an algorithm for searching a tree data structure for a node that
satisfies a given property. It starts at the tree root and explores all nodes at the present depth
prior to moving on to the nodes at the next depth level.

PROGRAM:
graph = {}
OUTPUT:
n = int(input("Enter number of Nodes:
")) for i in range(n):
Enter number of Nodes: 7
Enterkey
the= parent
input("Enter
Node: 5the parent Node: ")
Entervalue = list(map(str,input("Enter
the child Nodes3 7 the child
EnterNodes").split()))
the parent Node: graph[key]
3 = value
Enter the child Nodes2 4
Enter the= parent Node: 7
visited
Enter the child Nodes8
[] queue =
Enter the parent Node: 2
[]
Enter the child Nodes

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(m)
21131A4410 | LOKESH DUDDUKURI

Enter the parent Node:


4 Enter the child
Nodes9 Enter the parent
Node: 8 Enter the child
Nodes Enter the parent
Node: 9 Enter the child
Nodes
Following is the Breadth-First
Search 5 3 7 2 4 8 9

You might also like