Question

For the following problem, use your graph class from above. In class we studied BFS. Although we discussed the algorithm, we didnt provde the code. In the graph above, the BFS will visit 2 8 4 7 6 Figure 5: A graph of order 8. all nodes if starting with 1; however, any other node will not lead to all nodes being visited. BFS actually keeps track of not only visited nodes, but unvisited. Unvisited nodes are the nodes that are in the graph, but the BFS doesnt visit them when the visited doesnt change. For example, ,2,3, 4,5,6,7,8) (u is unvisited). We do a BFS(G, 5). Well see: 5,6,7,4 or 5,6,4,7 (either one). If u = {5, 6, 7, 4), then we must look at u-u = { 1, 2, 3, 8). You can use the BFS shown in lecture as a starting point-you must obviously include a queue class. 1 def bfsfull(g,node): 2 #TODO: Implement function 4 g Graph(1,2,3,4,5,6,7,8]) 5 elst ,2),1,3), (2,8), (3,5),(3,4),(5,6),(6,4),(6,7) 6 for i in elst: 7 8 print(g.edges) 9 bfsfull g.5) g. add-edge ( i )
Session Output 4 We start at 5; 6 is the only child. Then 4,7. We pick randomly 1; 2,3 are children and 8 last. Programming Problem 3: BFS Cnmnlete the functinn
0 0
Add a comment Improve this question Transcribed image text
Answer #1

BFS: Step 1: Step 2: Step 3: 3 2 3 4 4 7 7 7 6 6 6 Output: 12 Output: 123 Output: 1238 Step 4: Step 5: Step 6: 2 3 2 2 4 7 6 6 6 Output: 123845 Output: 12 3 8456 Output: 1238456 7 The BFS of a graph starting at vertex 1 is 12384567The BFS Tree as follows

Add a comment
Know the answer?
Add Answer to:
For the following problem, use your graph class from above. In class we studied BFS. Although...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • from collections import defaultdict    # This class represents a directed graph using # adjacency list...

    from collections import defaultdict    # This class represents a directed graph using # adjacency list representation class Graph:        # Constructor     def __init__(self):            # default dictionary to store graph         self.graph = defaultdict(list)        # function to add an edge to graph     def addEdge(self,u,v):         self.graph[u].append(v)        # Function to print a BFS of graph     def BFS(self, s):            # Mark all the vertices as not visited         visited = [False] * (len(self.graph))            # Create a queue for BFS         queue...

  • Please answer all three parts. And show step-by-step answers for each part. Draw anything if necessary....

    Please answer all three parts. And show step-by-step answers for each part. Draw anything if necessary. And please don't copy other answers to be at risk being downvoted. Thank you. Question 1 (50 POINTS): Given a graph G and the Breadth First Search (BFS) and Depth First Search (DFS) traversal algorithms as follows: BFSG) 1 for each vertex u € G.V – {3} 1 2 u.color = WHITE 3 u.d = 0 4 un = NIL 3 5 S.color =...

  • In Python 3 please Apply Breadth First Search (BFS) to traverse the following graph. Start your...

    In Python 3 please Apply Breadth First Search (BFS) to traverse the following graph. Start your traversal from vertex 0, and write down the order in which vertices will be visited during the traversal. 1 8 6 7 2 9 5 4 3

  • # Problem 4 problem4_breadth_first_traversal = [0,] problem4_depth_first_traversal = [0,] def bfs(g,start): start.set...

    # Problem 4 problem4_breadth_first_traversal = [0,] problem4_depth_first_traversal = [0,] def bfs(g,start): start.setDistance(0) start.setPred(None) vertQueue = Queue() vertQueue.enqueue(start) while (vertQueue.size() > 0): currentVert = vertQueue.dequeue() for nbr in currentVert.getConnections(): if (nbr.getColor() == 'white'): nbr.setColor('gray') nbr.setDistance(currentVert.getDistance() + 1) nbr.setPred(currentVert) vertQueue.enqueue(nbr) currentVert.setColor('black') class DFSGraph(Graph): def __init__(self): super().__init__() self.time = 0 def dfs(self): for aVertex in self: aVertex.setColor('white') aVertex.setPred(-1) for aVertex in self: if aVertex.getColor() == 'white': self.dfsvisit(aVertex) def dfsvisit(self,startVertex): startVertex.setColor('gray') self.time += 1 startVertex.setDiscovery(self.time) for nextVertex in startVertex.getConnections(): if nextVertex.getColor() == 'white': nextVertex.setPred(startVertex)...

  • (Problem R-14.16, page 678 of the text) Let G be a graph whose vertices are the...

    (Problem R-14.16, page 678 of the text) Let G be a graph whose vertices are the integers 1 through 8, and let the adjacent vertices of each vertex be given by the table below: Vertex                             adjacent vertices      1                                         (2,3,4)      2                                         (1,3,4)      3                                         (1,2,4)      4                                         (1,2,3,6)      5                                         (6,7,8)      6                                         (4,5,7)      7                                         (5,6,8)      8                                         (5,7) Assume that, in a traversal of G, the adjacent vertices...

  • Use the example Graph.javaPreview the document class and Edge.javaPreview the document class as a starting point...

    Use the example Graph.javaPreview the document class and Edge.javaPreview the document class as a starting point for this assignment, you'll need to make your own main class to create an instance of the Graph class. For this assignment we are going to create a map of a city subway system using a Graph data structure. The Metro Station Map that you'll use for this assignment is here: Assignment 9 - Metro Station Map.PNG Enter all the information from the Metro...

  • Q1: You can find a file that defines the CircularlyLinked List class similar to what we...

    Q1: You can find a file that defines the CircularlyLinked List class similar to what we discussed in the class. Download the file and work on it. Your task is to: 1. Complete the missing methods in the file as discussed in the class. Search for the comment/" MISSING / in the file to see the methods that need to be completed. 2. Add the following methods to the class a. public Node getMin 1. Task: find the node with...

  • Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from...

    Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from a file that contain positive integers and should insert those numbers into the RB tree in that order. Note that the input file will only contain distinct integers. Print your tree by level using positive values for Black color and negative values for Red color Do not print out null nodes. Format for a node: <Node_value>, <Parent_value>). For example, the following tree is represented...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT