You are on page 1of 6

LAB #04

IMPLEMENTING UNINFORMED SEARCH TECHNIQUES


1. Apply Breadth First Search on following graph considering the initial state is A and final state is G.
Show results in form of open and closed list.

CODE:
graph = {
'A' : ['B','E','C'],
'B' : ['A', 'E','D'],
'E' : ['A','B','D'],
'C' : ['A','F','G'],
'D' : ['B','E'],
'G' : ['C'],
'F' : ['C']
}
visited = [] # List for visited nodes.
queue = [] #Initialize a queue
k=[]
def bfs(visited, graph, node): #function for BFS
visited.append(node)
queue.append(node)
while queue: # Creating loop to visit each node
print(*queue, end=" ")
m= queue.pop(0)
print(*k)
k.append(m)
#visited.append(k)
for neighbour in graph[m]:
if neighbour not in visited :
visited.append(neighbour)
queue.append(neighbour)
print("\t*********** Breadth-First Search **********\n")
print("OPEN\t\t| CLOSED")
bfs(visited, graph, 'A') # function calling

OUT PUT

2. Using Question no. 1 apply BFS by taking initial and final state as user input. Show results in form
of open and closed list.

CODE:
graph = {
'A' : ['B','E','C'],
'B' : ['A', 'E','D'],
'E' : ['A','B','D'],
'C' : ['A','F','G'],
'D' : ['B','E'],
'G' : ['C'],
'F' : ['C']
}
visited = [] # List for visited nodes.
queue = [] #Initialize a queue
k=[]
def bfs(visited, graph, node,node2): #function for BFS
visited.append(node)
queue.append(node)
while queue: # Creating loop to visit each node
print(*queue, end=" ")
m= queue.pop(0)
print(*k)
k.append(m)
#visited.append(k)
for neighbour in graph[m]:
if neighbour not in visited :
visited.append(neighbour)
queue.append(neighbour)
starti=input("Enter Initial state: ")
endi=input("Enter Final state: ")
print("\t********* Breadth-First Search *********\n")
print("OPEN\t\t| CLOSED")
bfs(visited, graph, starti,endi)

OUTPUT

3. Apply Depth First Search on the graph given in question 1. Considering the initial state is A
and final state is G. Show results in form of open and closed list.
CODE:
graph = {
'A' : ['B','E','C'],
'B' : ['A', 'E','D'],
'E' : ['A','B','D'],
'C' : ['A','F','G'],
'D' : ['B','E'],
'G' : ['C'],
'F' : ['C']
}

visited = [] # Set to keep track of visited nodes of graph.


m=[]
queue=["S"]
def dfs(visited, graph, node): #function for dfs
#queue.pop(0)
if node not in visited:
queue.pop(0)
queue.append(node)
print(*queue,end=" ")

print (*visited)
visited.append(node)
for neighbour in graph[node]:
#queue.append(neighbour)
dfs(visited, graph, neighbour)

# Driver Code
starti=input("Enter Initial state: ")
endi=input("Enter Final state: ")
print("\t*********** Deapth-First Search ***********\n")
print("OPEN\t\t| CLOSED")
print("------------------------------")
dfs(visited, graph, 'A')

OUTPUT
4. Using Question no. 1 apply DFS by taking initial and final state as user input. Show results in
form of open and closed list.

CODE
graph = {
'A' : ['B','E','C'],
'B' : ['A', 'E','D'],
'E' : ['A','B','D'],
'C' : ['A','F','G'],
'D' : ['B','E'],
'G' : ['C'],
'F' : ['C']
}

visited = [] # Set to keep track of visited nodes of graph.


m=[]
queue=["S"]
def dfs(visited, graph, node,node2): #function for dfs
#queue.pop(0)
if node not in visited:
queue.pop(0)
queue.append(node)
print(*queue,end=" ")

print (*visited)
visited.append(node)
for neighbour in graph[node]:
#queue.append(neighbour)
dfs(visited, graph, neighbour)

starti=input("Enter Initial state: ")


endi=input("Enter Final state: ")
print("\t\t===== Deapth-First Search ======\n")
print("OPEN\t\t|\t\tCLOSED")
print("------------------------------")
dfs(visited, graph, starti,endi)

OUTPUT

You might also like