๐Ÿค–
B.TECH III YEAR โ€ข I SEMESTER โ€ข R22

Artificial Intelligence Using Python - Lab Manual

Artificial Intelligence Laboratory โ€ข Program Solution

๐Ÿ“˜ R22 Regulation ๐ŸŽ“ III Year ๐Ÿ“š I Semester ๐Ÿค– Artificial Intelligence ๐Ÿงช Lab Program
Selected Program

Write a Program to implement Breadth First Search (BFS) using Python.

โ† All Artificial Intelligence Programs

Input Graph

SOURCE CODE :

 # Input Graph
graph = {
'A' : ['B','C'],
'B' : ['A','C','D'],
'C' : ['A','B','E'],
'D' : ['B','E'],
'E' : ['C','D']
}
# To store visited nodes.
visitedNodes = []
# To store nodes in queue
queueNodes = [] 
# function
def bfs(visitedNodes, graph, snode):
	visitedNodes.append(snode)
	queueNodes.append(snode)
	print()
	print("RESULT :")
	while queueNodes:
		s = queueNodes.pop(0)
		print (s, end = " ")
		for neighbour in graph[s]:
			if neighbour not in visitedNodes:
				visitedNodes.append(neighbour)
				queueNodes.append(neighbour)
		
# Main Code
snode = input("Enter Starting Node(A, B, C, D, or E) :").upper()
# calling bfs function
bfs(visitedNodes, graph, snode)

OUTPUT :

Sample Output 1: 
-------------------
Enter Starting Node(A, B, C, D, or E) :A

RESULT :
A B C D E
------------------------------------------------------------------
Sample Output 2: 
-------------------
Enter Starting Node(A, B, C, D, or E) :B

RESULT :
B A C D E

๐Ÿ’ป Run This Python Program

Write a Program to implement Breadth First Search (BFS) using Python.
The selected Python program is loaded automatically into the editor. Edit the program, provide input if required and click Run.

The compiler is embedded using OneCompiler. Your Python program runs inside the embedded Python environment; Study Glance does not execute Python code directly on your web server. If the program asks for input (for example the starting node), type the value in the STDIN box before clicking Run.
โ† Back to Artificial Intelligence Lab Programs ๐Ÿ“š All Lab Manuals