You are on page 1of 12

Name:- Sanket Harishchandra Dalvi AI Practical Std:- T.Y.B.Sc.

IT
Roll No:- 715 Div:- A
PRACTICAL NO:- 5

Practical 5(A):-

Artificial Intelligence Practical


Name:- Sanket Harishchandra Dalvi AI Practical Std:- T.Y.B.Sc.IT
Roll No:- 715 Div:- A

Aim:- Program to solve the problem of given graph traversal through depth first
search.

Source Code:-

print("Name : Sanket Dalvi Roll No : 715")

print("Program to solve the below given graph travesal problem through Depth

First search")

print("Searchig Across Depth--> Top to bottom --> All levels at once")

explorednode = set()

def DepthFirstSearch(graph, startnode, explorednode):

if startnode is None:

print("Start Node is None!")

return

if startnode not in graph:

print("Start Node is Not Present in the Current Graph")

return

if startnode not in explorednode:

print("Current Node Being visited is", startnode)

explorednode.add(startnode)

Artificial Intelligence Practical


Name:- Sanket Harishchandra Dalvi AI Practical Std:- T.Y.B.Sc.IT
Roll No:- 715 Div:- A
for nextnode in graph[startnode]:

DepthFirstSearch(graph, nextnode, explorednode)

return explorednode

sanket_graph_one={

'A':set(['B','C']),

'B':set(['D','E']),

'C':set(['F']),

'D':set([]),

'E':set(['F']),

'F':set([])}

sanket_graph_two ={

'0':set(['1','2']),

'1':set(['4','5']),

'2':set(['3','5']),

'3':set(['6']),

'4':set(['7']),

'5':set(['3','6']),

Artificial Intelligence Practical


Name:- Sanket Harishchandra Dalvi AI Practical Std:- T.Y.B.Sc.IT
Roll No:- 715 Div:- A
'6':set(['7']),

'7':set([])

sanket_graph_three = {

'Amy': set(['Jack','Jill','Joana']),

'Jack': set(['Lucy']),

'Lucy': set([]),

'Jill': set(['Cindy','Camilla']),

'Cindy': set(['Eleven', 'Emma']),

'Joana': set(['Will']),

'Camilla': set([]),

'Will': set([]),

'Eleven': set([]),

'Emma': set([])

search_graph = sanket_graph_three

print("\n problem graph is as follows.....\n", search_graph)

# assigning the start node as a variable

Artificial Intelligence Practical


Name:- Sanket Harishchandra Dalvi AI Practical Std:- T.Y.B.Sc.IT
Roll No:- 715 Div:- A
start_search_at = 'Amy'

print("\n solution path for the above problem using depth first graph is as follows

: \n")

DepthFirstSearch(graph = search_graph, startnode = start_search_at,

explorednode = explorednode)

Output:-

Artificial Intelligence Practical


Name:- Sanket Harishchandra Dalvi AI Practical Std:- T.Y.B.Sc.IT
Roll No:- 715 Div:- A

Artificial Intelligence Practical


Name:- Sanket Harishchandra Dalvi AI Practical Std:- T.Y.B.Sc.IT
Roll No:- 715 Div:- A
Practical 5(B):-

Artificial Intelligence Practical


Name:- Sanket Harishchandra Dalvi AI Practical Std:- T.Y.B.Sc.IT
Roll No:- 715 Div:- A
Aim:- Program to solve the problem of given graph traversal through breadth first
search algorithm.

Source Code:-

print("Name: Sanket Dalvi Roll No: 715")

print("Program to solve the problem of given graph traversal through Breadth

First Search")

print("searching access depth --> Left to Right --> all levels at Time")

explorednode = []

def BreadthFirstSearch(graph, startnode, explorednode):

if startnode not in graph:

print("Node is not a part of the current graph")

nodequeue = [startnode]

while len(nodequeue) > 0:

currentnode = nodequeue.pop(0)

print("\n Current visited node is:", currentnode, end="")

if currentnode not in explorednode:

explorednode.append(currentnode)

adjancentnode = graph[currentnode]

Artificial Intelligence Practical


Name:- Sanket Harishchandra Dalvi AI Practical Std:- T.Y.B.Sc.IT
Roll No:- 715 Div:- A
for node in adjancentnode:

if node not in explorednode: nodequeue.append(node)

return explorednode

sanket_graph_one = dict()

sanket_graph_one['A'] = ['B', 'G', 'D']

sanket_graph_one['B'] = ['A', 'F', 'E']

sanket_graph_one['C'] = ['F', 'H']

sanket_graph_one['D'] = ['F', 'A']

sanket_graph_one['E'] = ['B', 'G']

sanket_graph_one['F'] = ['B', 'D', 'C']

sanket_graph_one['G'] = ['A', 'E']

sanket_graph_one['H'] = ['C']

sanket_graph_two = {

'Amy': set(['Jack', 'Jill', 'Joana']),

'Jack': set(['Lucy', 'Amy']),

'Lucy': set(['Jack']),

Artificial Intelligence Practical


Name:- Sanket Harishchandra Dalvi AI Practical Std:- T.Y.B.Sc.IT
Roll No:- 715 Div:- A
'Jill': set(['Cindy', 'Camilla']),

'Cindy': set(['Eleven', 'Emma']),

'Joana': set(['Will', 'Lucy']),

'Camilla': set(['Jill']),

'Will': set(['Joana']),

'Eleven': set(['Cindy']),

'Emma': set(['Cindy'])

search_graph = sanket_graph_two

print('\n problem Graph is as follows. \n', search_graph)

start_search_at = 'Amy'

print("\n Solution path For The Above Problem Using Breadth First Search is..\n")

BreadthFirstSearch(graph=search_graph, startnode=start_search_at,

explorednode=explorednode)

Artificial Intelligence Practical


Name:- Sanket Harishchandra Dalvi AI Practical Std:- T.Y.B.Sc.IT
Roll No:- 715 Div:- A
Output:-

Artificial Intelligence Practical


Name:- Sanket Harishchandra Dalvi AI Practical Std:- T.Y.B.Sc.IT
Roll No:- 715 Div:- A

Artificial Intelligence Practical

You might also like