Question

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

DFS(G) 1 for each vertex u € G.V 2 u.color = WHITE 3 un = NIL 4 time = 0 5 for each vertex u € G.V 6 if u.color == WHITE 7 DF

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the answer,

I have tried my best to help you explain the answer, if you face any difficulty then do let me know I would try to explain that to you and update my answer.

1. BFS: No. of steps required: 6 steps

LI L2 L3 12 BES Initial Queue → 4151617 8910 Now, since we found our target 6, we stop & return here.

2. DFS: No. of steps required: 17 steps + 2 steps(for returning from recursive calls)

20 DES 9 @ 18 113 FOUND (retum) 19 12

The 1,2,3 marked in red without circle are the steps.

3. The DFS traversal will take more steps than BFS since in DFS we would have to move to the leaf node and backtrack from there. But in BFS we will be moving level by level.

But keep in mind, it depends. Suppose node 6 would have been in the place of node 2 then no. of steps would have been equal. Another case can be if node 6 would have been in the place of node 8 then DFS would take lesser no. of steps than the other.

I hope it answers your problem. Do let me know in the comments if you face any issue, also do upvote the solution.

Thank you

Add a comment
Know the answer?
Add Answer to:
Please answer all three parts. And show step-by-step answers for each part. Draw anything if necessary....
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
  • In the breadth first traversal procedure BFS, each vertex v has an attribute v.color. Modify BFS...

    In the breadth first traversal procedure BFS, each vertex v has an attribute v.color. Modify BFS so that instead of x.color, each vertex x has a Boolean attribute called x.mark, whose value is either TRUE or FALSE. The attribute x.mark must be FALSE if x has never been visited. It must be TRUE if x has been visited, but will not be visited again. Thank you!!! BFS(G, s) 1 for each vertex u e G.V-(s 11, color WHITE 4 5...

  • # 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)...

  • please I need it urgent thanks algorithms second picture is the graph 2.3 Graphs and BFS-DFS...

    please I need it urgent thanks algorithms second picture is the graph 2.3 Graphs and BFS-DFS 5 points each I. Draw the adjacency matrix for the graph A on the last page. 2. Show the order in which a breadth first traversal will print out the vertices? Assume that if the algorithm has a choice of which vertex to visit, it visits the vertex with the lower number. 3. Find a topological ordering for the graph B on the last...

  • Help !! I need help with Depth-First Search using an undirected graph. Write a program, IN JAVA, ...

    Help !! I need help with Depth-First Search using an undirected graph. Write a program, IN JAVA, to implement the depth-first search algorithm using the pseudocode given. Write a driver program, which reads input file mediumG.txt as an undirected graph and runs the depth-first search algorithm to find paths to all the other vertices considering 0 as the source. This driver program should display the paths in the following manner: 0 to ‘v’: list of all the vertices traversed to...

  • Show the operation of depth-first search (DFS) on the graph of Figure 1 starting from vertex...

    Show the operation of depth-first search (DFS) on the graph of Figure 1 starting from vertex q. Always process vertices in alphabetical order. Show the discovery and finish times for each vertex, and the classification of each edge. (b) A depth-first forest classifies the edges of a graph into tree, back, forward, and cross edges. A breadth-first search (BFS) tree can also be used to classify the edges reachable from the source of the search into the same four categories....

  • 1. Startingatvertex000, perform a BFSof Q3.Assume all adjacency lists are in numericalorder.For example, (000,001) occurs before...

    1. Startingatvertex000, perform a BFSof Q3.Assume all adjacency lists are in numericalorder.For example, (000,001) occurs before (000, 010). Showthe resulting spanningtrees. Draw the directed graphs and perform a. 2. Breadth-First Search (BFS)algorithm: VTo determine the shortest paths starting at vertex a to everyother node. Show the resulting spanning tree. b. Depth-First Search (DFS) to explore the whole graph: Record the start/end time for all the vertices. show the resulting spanning forest Label the name°fthe edges. V Writethetopologicalorderofthevertices(ifnocycle-nobackedge) (Showthestate of the...

  • 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...

  • Show how depth-first search works on the graph of Figure 22.6. Assume that the for loop of lines 5–7 of the DFS proced...

    Show how depth-first search works on the graph of Figure 22.6. Assume that the for loop of lines 5–7 of the DFS procedure considers the vertices in reverse alphabetical order, and assume that each adjacency list is ordered alphabetically. Show the discovery and finishing times for each vertex, and show the classification of each edge. DIJKSTRA(G,w,s) 1INITIALIZE-SINGLE-SOURCE(G,s) 2 S ?? 3 Q ? V[G] 4 while Q =? 5 do u ? EXTRACT-MIN(Q) 6 S ? S?{u} 7 for each...

  • Problem 1 (20 points). For each of the following statements, either give a (short) proof to show ...

    Problem 1 (20 points). For each of the following statements, either give a (short) proof to show that it 1. Let G- (V,E) be a directed graph. Let s E V. During a BFS run on G starting from s, vertex vis 2. Let G-(V,E) be a directed graph. Let e (u,v) E E. During a DFS nun on G, edge e is a cross 3. Let G (V,E) be a directed graph without negative cycles. Let e e E...

  • (c) Simulate breadth first search on the graph shown in Fig HW2Q1c. You can assume that...

    (c) Simulate breadth first search on the graph shown in Fig HW2Q1c. You can assume that the starting vertex is 1, and the neighbors of a vertex are examined in increasing numerical order (i.e. if there is a choice between two or more neighbors, we pick the smaller one). You have to show: both the order in which the vertices are visited and the breadth first search tree. No explanations necessary. (d) On the same graph, i.e. the graph in...

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