BFS & DFS Answers
BFS & DFS Answers
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
Order in G2:
A,B,E,C,F,I,D,G,J,M,H,K,N,L,O,P
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.
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.
A,B,C,D,H,G,F,E,I,J,K,L,P,O,N,M