Recall the breadth search first (bfs) algorithm discussed in class, presented below: def bfs (g, s): q, visited - (s), [) while q: u- q. pop (0) visited. append (u) for v in glu]: if v not in visited + q: q. append (v) return visited a) The above simple version just returns a list of nodes in order visited by the algorithm. Below is a slightly modified, but incomplete, version of bfs(), which "returns" (or produces) the same information in global variable visited, and also updates a dictionary prev, which records for each node the previous node in the path to it found by the algorithm; for example, where F is the starting node with no previous node (-) to it:

New Perspectives on HTML5, CSS3, and JavaScript
6th Edition
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Patrick M. Carey
Chapter12: Working With Document Nodes And Style Sheets: Creating A Dynamic Document Outline
Section12.2: Visual Overview: Exploring Attribute Nodes
Problem 2QC
icon
Related questions
Question

python help - please help me i really beg you

6.
Recall the breadth search first (bfs) algorithm discussed in class, presented below:
def bfs (g, s) :
q, visited ▪ [s], []
while q:
u - q.pop (0)
visited.append (u)
for v in glu):
if v not in visited + q:
q. append (v)
return visited
a)
The above simple version just returns a list of nodes in order visited by the algorithm. Below is
a slightly modified, but incomplete, version of bfs(), which "returns" (or produces) the same
information in global variable visited, and also updates a dictionary prev, which records for each
node the previous node in the path to it found by the algorithm; for example, where Fis the
starting node with no previous node ('-') to it:
prev = { 'F': '-', 'A': 'F', 'E': 'F', .}
complete the function below as necessary, in any of the blank lines, to properly update variable prev:
def bfs (g, s) :
# g: graph, ex. { A: { B: 4, C: 7 } ... },
# This function accesses global variables, initialized as follows:
# - visited - 0 : list of nodes in visited order
# - prev = {s: '-'} : will be like: {'F': '-', 'A': 'F', 'E': 'F', .}
# to update with the sequence of visited nodes and previous nodes.
global visited, prev
s: start node
q- [s]
while q:
u = q.pop (0)
visited.append (u)
for v in glu) :
if v not in visited + q:
q. append (v)
# END OF bfs ()
Transcribed Image Text:6. Recall the breadth search first (bfs) algorithm discussed in class, presented below: def bfs (g, s) : q, visited ▪ [s], [] while q: u - q.pop (0) visited.append (u) for v in glu): if v not in visited + q: q. append (v) return visited a) The above simple version just returns a list of nodes in order visited by the algorithm. Below is a slightly modified, but incomplete, version of bfs(), which "returns" (or produces) the same information in global variable visited, and also updates a dictionary prev, which records for each node the previous node in the path to it found by the algorithm; for example, where Fis the starting node with no previous node ('-') to it: prev = { 'F': '-', 'A': 'F', 'E': 'F', .} complete the function below as necessary, in any of the blank lines, to properly update variable prev: def bfs (g, s) : # g: graph, ex. { A: { B: 4, C: 7 } ... }, # This function accesses global variables, initialized as follows: # - visited - 0 : list of nodes in visited order # - prev = {s: '-'} : will be like: {'F': '-', 'A': 'F', 'E': 'F', .} # to update with the sequence of visited nodes and previous nodes. global visited, prev s: start node q- [s] while q: u = q.pop (0) visited.append (u) for v in glu) : if v not in visited + q: q. append (v) # END OF bfs ()
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Introduction to computer system
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
New Perspectives on HTML5, CSS3, and JavaScript
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:
9781305503922
Author:
Patrick M. Carey
Publisher:
Cengage Learning
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr