0% found this document useful (0 votes)
207 views2 pages

BFS & DFS Answers

BFS searches levels of a graph starting from the root node. It explores all neighbors of the current node before moving to the next level. It uses a queue data structure. DFS searches deeper into the graph from the root by exploring edges until it reaches a leaf, then backtracks. It uses a stack data structure. Both algorithms are demonstrated on sample graphs G1 and G2, with the node exploration order provided.

Uploaded by

Uvindu Thilanka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
207 views2 pages

BFS & DFS Answers

BFS searches levels of a graph starting from the root node. It explores all neighbors of the current node before moving to the next level. It uses a queue data structure. DFS searches deeper into the graph from the root by exploring edges until it reaches a leaf, then backtracks. It uses a stack data structure. Both algorithms are demonstrated on sample graphs G1 and G2, with the node exploration order provided.

Uploaded by

Uvindu Thilanka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

BFS - Breadth-first search

BFS starts with examining the root node and all of its neighbors in first step. In next step, the
neighbours of the nearest node are explored and process continues in the further steps.
In above graph G1, we start with node A and push all its neighbors (who are not yet explored)
into queue.
 So we push B,D,I to queue. Now we pop node from queue and starts exploring that node.
 Since B is at the front end of queue we explore node B. Then we push all its neighbors.
So we push C,E to queue. (Note that we dont push node A again since it is already
explored and also we dont push D as it is already in queue).
 Now we pop node from queue and starts exploring that node.
 Since D is at the front end of queue we explore node D. Then we push its neighbors G to
queue.
 Since I is at the front end of queue we explore node I. Then we push its neighbor J to
queue.
 Since C is at the front end of queue we explore node C. Then we push its neighbor F to
queue.
 Since E is at the front end of queue we explore node E. Then we push its neighbor H to
queue. Since G is at the front end of queue we explore node G.
 Since all neighbors of node G are either explored (D,I,E) or already in queue(J,H), we
dont push anything into queue. In the same way we explore J, F and H.
So the order in BFS in graph G1:

A,B,D,I,C,E,G,J,F,H

Same approach for graph G2.

 First node A is explored. Queue will be {B,E}


 Node B is explored. Queue will be {E,C,F}
 Node E is explored. Queue will be {C,F,I}
 Node C is explored. Queue will be {F,I,D,G}
 Node F is explored. Queue will be {I,D,G,J}
 Node I is explored. Queue will be {D,G,J,M}
 Node D is explored. Queue will be {G,J,M,H}
 Node G is explored. Queue will be {J,M,H,K}
 Node J is explored. Queue will be {M,H,K,N}
 Node M is explored. Queue will be {H,K,N}
 Node H is explored. Queue will be {K,N,L}
 Node K is explored. Queue will be {N,L,O}
 Node N is explored. Queue will be {L,O}
 Node L is explored. Queue will be {O,P}
 Node O is explored. Queue will be {P}
 Node P is explored. Queue will be {}

Order in G2:

A,B,E,C,F,I,D,G,J,M,H,K,N,L,O,P

DFS - Depth-First search

This algorithm is similar to BFS, but in DFS we start with the initial node of the graph G, and
then goes to deeper and deeper until we find the goal node or the node which has no children.
Here we use stack instead of queue (that we used in BFS)

G1:

 In DFS for graph G1, we start with exploring node A. Then we explore it neighbors.
Since A has B,D,I as neighbors, we start exploring node B(as in alphabetical order).Since
B has C,D,E as neighbors, we start exploring node C. Since C has E,F (B is already
visited) as neighbors, we start exploring node E. Since E has D,F,G,H as neighbors, we
start exploring node D. Since D has G as neighbor (A,B,E are already visited), we start
exploring node G. Since G has H,I,J as neighbors , we start exploring node H. Since H
has F,J as neighbors , we start exploring node F. Since F has no unvisited neighbors, we
stop here and come back to H. Now H has J as neighbor (remaining neighbors are visited)
we explore J. Now J has I as unvisited neighbor, so we explore I. I has no unvisited
neighbors so we stop here. We backtrack till node A. Since all nodes are already explored
we stop it.

So the order for G1:

A,B,C,E,D,G,H,F,J,I

G2:

 Using same approach in DFS for graph G2, we start with node A. Then we explore node
B. From node B we explore node C. Then we visit node D. Then we explore node H.
Then we explore node G. Then we explore node F. Then we explore node E. Then we
explore node I. From I we go to node J. Then we explore node K. Then we go to node L.
Then we move on to node P. Then we explore O. Then we explore node N. From N we
explore node M. From M there are no unvisited nodes, so we backtrack till node A and
stop.

So the order for G2:

A,B,C,D,H,G,F,E,I,J,K,L,P,O,N,M

You might also like