Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The coding language is Python. 6. [10] Recall the breadth search first (bfs) algorithm discussed in class, presented below: def bfs (g, s) : q,

The coding language is Python.

image text in transcribedimage text in transcribed
6. [10] 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 g[u] : if v not in visited + q: q . append (v) return visited a) [3] 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: prev = { 'FI : '-1, 'A' : 'F', 'B': '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 } .. . s: start node # This function accesses global variables, initialized as follows: # - visited = list of nodes in visited order - prev = (s: '-" } : will be like: {'F': ' -1, 'A' : 'F', 'E': 'F' , ...} # to update with the sequence of visited nodes and previous nodes. global visited, prev q = [s] while q: NO u = q- pop (0) visited. append (u) for v in g[u] : if v not in visited + q: q . append (v) # END OF bis () b) [7] Complete the function below to return a string representing a path from node s to node e: def path (s, e, prev) : # s, e : start and end nodes in path from s to e # prev : dictionary, ex. { 'F1 : 1- ', , 'A' : 'F' , 'E': 'F', ..} # this returns a string like: "S->X->Y->Z->E") t = in whileNOTE: in all subsequent questions pertaining to graphs, assume the following is in effect: A, B, C, D, E, F, G, H, I, J = 'A', "B" 'C' 'D ' "E" 'G' "I "

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions