0% found this document useful (0 votes)
75 views6 pages

Assignmnet 1

The document contains Python code examples for implementing depth-first search (DFS), breadth-first search (BFS), and uniform cost search (UCS) on a graph data structure. DFS, BFS, and UCS are recursively defined functions that take a graph, starting node, and goal node as inputs and print out the path as they traverse the graph. The code provides a way to search graphs and find the shortest path between nodes using different graph traversal algorithms.

Uploaded by

Muhammad Usama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views6 pages

Assignmnet 1

The document contains Python code examples for implementing depth-first search (DFS), breadth-first search (BFS), and uniform cost search (UCS) on a graph data structure. DFS, BFS, and UCS are recursively defined functions that take a graph, starting node, and goal node as inputs and print out the path as they traverse the graph. The code provides a way to search graphs and find the shortest path between nodes using different graph traversal algorithms.

Uploaded by

Muhammad Usama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignmnet 1

Submitted to
Aizaz Akmal
Submitted by
M.Usama (2020-cs-611)
Department of Computer Science
University of Engineering and Technology,
Lahore, New-Campus
#DFS

graph = {
'A' : ['B','C'],
'B' : ['D', 'E'],
'C' : ['F', 'G'],
'D' : [],
'E' : [],
'F' : [],
'G' : []
}
goal = 'F'
visited = set()
def dfs(visited, graph, node):
if node not in visited:
print (node)
visited.add(node)
for neighbour in graph[node]:
if goal in visited:
break
else:
dfs(visited, graph, neighbour)
dfs(visited, graph, 'A')

#BFS
graph = {
'A' : ['B','C'],
'B' : ['D', 'E'],
'C' : ['F', 'G'],
'D' : [],
'E' : [],
'F' : [],
'G' : []
}
visited = []
queue = []
goal = 'F'
def bfs(visited, graph, node):
visited.append(node)
queue.append(node)
while queue:
s = queue.pop(0)
print (s, end = "\n")
for neighbour in graph[s]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
if goal in visited:
break
bfs(visited, graph, 'A')

#UCS

from queue import PriorityQueue


graph = {
'A' : [[10,'B'],[8,'C']],
'B' : [[5,'D'], [3,'F'],[2,'E']],
'C' : [[5,'F']],
'D' : [],
'E' : [[2,'F']],
'F' : []
}

mygoal='F'

def bfs(visited, graph, startnode,goal): #function for dfs

expque=PriorityQueue()
path=[startnode]
visited = set() # Set to keep track of visited nodes of graph.
expque.put((0,startnode,startnode))

while expque.qsize() > 0 :

node=expque.get()
curcost=node[0]
curname=node[1]
curpath=node[2]

if curname not in visited:


visited.add(curname)
# print('name:', curname, '\t gn:', curcost)

if curname==goal:
print ('---Goal Found---' )
print('Path Cost: ',curcost,'Path Track: ',curpath )
return

suclist=graph[curname]
for sucnode in suclist:
if sucnode[1] not in visited:
gn=sucnode[0]+curcost
st=''
st=node[2]+' '+sucnode[1]
expque.put((gn,sucnode[1],st))

# Driver Code
print("Following is the Uniform Cost Search")
bfs(visited, graph, 'A',mygoal)

You might also like