Answered step by step
Verified Expert Solution
Question
1 Approved Answer
' ' ' Breadth First and Depth First Search The objective is to write a Python program that traverses graphs in BFS and DFS manner.
Breadth First and Depth First Search
The objective is to write a Python program that traverses graphs in BFS
and DFS manner. BFS will determine the shortest path distance number of
edges from the root for each node reachable from the root. DFS will find
cycles in the graph of nodes reachable from the root. Study the lecture on
graphs, in particular graph traversals.
Some helper code is provided. Don't change it Don't change your main,
it is used to check your code's correctness.
It is your job to implement dfs and bfs In both dfs and bfs visit
children of a node in left to right order, ie if adj is the
adjacency list of a node, visit the children as follows: for nxt in adj
Given an input file in:
a b
b c
c a d
d c
and root a
python dfbfpy in a produces:
dfbfpy
BFS
Input graph: nodeName coloradj list dictionary
a whiteb
b whitec
c whitead
d whitec
Root node: a
BFS queue: node name, distance pairs
abcd
END BFS
DFS
Input graph: nodeName coloradj list dictionary
a whiteb
b whitec
c whitead
d whitec
Root node a
graph with root a is cyclic
END DFS
import sys
cyclic False #keeping track in dfs whether a cycle was found
def readfnm:
read file fnm into dictionary
each line has a nodeName followed by its adjacent nodeNames
f openfnm
gr #graph represented by dictionary
for line in f:
l line.stripsplit
# ignore empty lines
if l:continue
# dictionary: key: nodeName value: color adjList of names
grlwhitel:
return gr
def dumpgr:
printInput graph: nodeName coloradj list dictionary
for e in gr:
printe gre
def whitegr :
paint all gr nodes white
for e in gr :
grewhitegre
return bfs queue with node distance pairs
def bfsgr q:
visited
while q:
node, distance qpop
visited.appendnodedistance
for nxt in grnode:
if grnxt 'white':
grnxtgray grnxt
qappendnxt distance
grnodeblack grnode
return visited
def dfsgr node, parent:
global cyclic
if grnode 'gray':
cyclic true
return
grnodegray grnode
for nxt in grnode:
if nxt parent:
dfsgr nxt node
grnodeblack grnode
if namemain:
printsysargv
gr readsysargv # file name
root sysargv # root node
db lensysargv # debug?
printBFS
dumpgr
printRoot node:", root
grrootblackgrroot
q bfsgrroot
printBFS queue: node name, distance pairs"
printq
printEND BFS
print
printDFS
whitegr
dumpgr
printRoot node", root
dfsInitgrroot
if cyclic:
printgraph with root",root,"is cyclic"
else:
printgraph with root",root,"is not cyclic"
printEND DFS
The above code has been filled in for DFS and BFS but is continually getting the error: Traceback most recent call last:
File homerunnerlocalsubmissiondfbfpy line in
gr readsysargv # file name
IndexError: list index out of range
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started