The Breadth First Search algorithm has been implemented using the queue data structure. One possible order of visiting the nodes of the following graph is

**(A)** MNOPQR

**(B)** NQMPOR

**(C)** QMNPRO

**(D)** QMNPOR

**Answer:** **(C)** **Explanation:** The Breadth First Search visits the “breadth” first, i.e. if it is visiting a node then after visiting that node, it will visit the neighbor nodes (children) first before moving on to the next level neighbors.

Let’s see how.

Initially : If BFS starts at node Q, it first visits Q and puts Q in the Queue.

So, Queue contains : Q.

And, Visiting Order yet is : Q.

Now it dequeues Q, and explores its neighbors, which are {M,N,P}. It checks for those neighbors of Q which are not visited yet, then it visits them, and puts them in Queue (so that later their neighbors can be visited).

Now, these neighbors can be visited and put them in Queue in any order (depends on the implementation).

Suppose it visits and put them in the Queue in the order ( M N P ) with M being at the FRONT and P at the REAR.

So, Visiting Order : QMNP.

Now, it looks for the next entry in Queue. As Queue follows FIFO principle, M gets dequeued.

Queue contains : ( N P )

It explores M, finds its neighbors { N, Q, R }, but before visiting them it checks whether these have been visited earlier, it will only visit and put those nodes in the Queue which are not visited yet. As N and Q have been visited earlier, it will only visit R and enqueue it.

Visiting Order : QMNPR.

Queue contains : ( N P R ).

Now, N gets dequeued.

Queue contains : ( P R ).

It explores N, finds its neighbors { M, O, Q }. As M and Q have been visited earlier, it will only visit and enqueue O.

Visiting Order : QMNPRO.

Queue contains : ( P R O ).

Now, P gets dequeued.

Queue contains : (R O).

It explores P, finds its neighbors { O, Q }. As O and Q have been visited earlier, it will NOT enqueue .

Now, R gets dequeued.

Queue contains : (O).

It explores R, finds its neighbor { M }. As M has been visited earlier, it will NOT enqueue.

Now, O gets dequeued.

Queue contains : ( ) .

It explores O, finds its neighbors { N, P }. As both have been visited earlier, it will NOT enqueue.

Now Queue is empty, hence BFS stops here.

And the visiting order has been : QMNPRO.

The Pseudocode for the above explanation can be found at https://en.wikipedia.org/wiki/Breadth-first_search

Quiz of this Question

## Recommended Posts:

- GATE | GATE CS 2008 | Question 84
- GATE | Gate IT 2008 | Question 73
- GATE | Gate IT 2008 | Question 72
- GATE | Gate IT 2008 | Question 71
- GATE | Gate IT 2008 | Question 70
- GATE | Gate IT 2008 | Question 69
- GATE | Gate IT 2008 | Question 68
- GATE | Gate IT 2008 | Question 67
- GATE | Gate IT 2008 | Question 66
- GATE | Gate IT 2008 | Question 65
- GATE | Gate IT 2008 | Question 74
- GATE | Gate IT 2008 | Question 75
- GATE | GATE CS 2008 | Question 83
- GATE | GATE CS 2008 | Question 34
- GATE | Gate IT 2008 | Question 82
- GATE | Gate IT 2008 | Question 81
- GATE | Gate IT 2008 | Question 80
- GATE | Gate IT 2008 | Question 79
- GATE | Gate IT 2008 | Question 78
- GATE | GATE CS 2008 | Question 85