๐Ÿค–
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 Depth First Search (DFS) 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']
}
# Set used to store visited nodes.
visitedNodes = list()
# function
def dfs(visitedNodes, graph, node):
	if node not in visitedNodes:
		print (node,end=" ")
		visitedNodes.append(node)
		for neighbour in graph[node]:
			dfs(visitedNodes, graph, neighbour)
# Driver Code
snode = input("Enter Starting Node(A, B, C, D, or E) :").upper()
# calling bfs function
print("RESULT :")
print("-"*20)
dfs(visitedNodes, graph, snode)

OUTPUT :

Sample Output 1: 

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

------------------------------------------------------------------
Sample Output 2: 

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

๐Ÿ’ป Run This Python Program

Write a Program to implement Depth First Search (DFS) 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