Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In python repl.it Hw9 Please ve share your program enhance your work after submitting O export to back to classroom bef BFS(graph, start, goal): #

In python image text in transcribed
image text in transcribed
repl.it Hw9 Please ve share your program enhance your work after submitting O export to back to classroom bef BFS(graph, start, goal): # Step 1: initialize variables parent- () # holds mapping between every node and it's parent in the path current node start closed set h 6. # Step 2: initialize open queue open queue deque() open queue.append (start) 18 # Step 3: while loop setting current node 12 13while len(open queue)>a: 14 15 16 17 18~ 29 20 current node - open queue.popleft) step 3.1 return path if we're at the goal if current nodegoal: for neighbors in graph[current node]: if neighbors not in open queue and neighbors not in closed set: open queue.append (neighbors) parent[neighbors) current node closed set.add(current node) s step 3.2a: iterate over neighbors of current node #and add to open queue if not already there or inclosed a Remenber to store the parent information 23 25 26 28 3e e Step 3x20 add current node to closed set Step s: return failure condition if there 1s no path return Hone mail. Resend verification link Due: Mar 01, 2019 11:59pm Instructions from your teacher Breadth-First Search submit In this exercise, you will implement BFS for a adjacency list representation for a graph. The function, BFS(), takes three arguments: the graph to traverse, the start node, and the end node. It should return a shortest path from start to node, inclusive, as a list. For example, given the graph below, BFS (graph, e, 6) should returm [o, 1, 6] or [e, 5, 6]. If there is no path, return an empty list, [] 1. Create an empty set of closed nodes and an empty dictionary to store each node's parent 2. Initialize the open queue to contain the start node 3. While the open queue is not empty, dequeue the next node from the open queue and set it to the current node 1. If it is the goal node, reconstruct the path back to the start using the parent mapping 2. Otherwise, for each of its neighbors, add it to the open queue if it's not in there or in the closed set and store the current node as its parent. Add the current node to the set of closed nodes 4. If you empty the open queue without reaching the goal node, there is no path 8 0 7 1058 AM up

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

Joe Celkos Data And Databases Concepts In Practice

Authors: Joe Celko

1st Edition

1558604324, 978-1558604322

More Books

Students also viewed these Databases questions