# GATE | GATE CS 2008 | Question 17

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 IT 2008 | Question 5
- GATE | Gate IT 2008 | Question 4
- GATE | Gate IT 2008 | Question 3
- GATE | Gate IT 2008 | Question 2
- GATE | Gate IT 2008 | Question 1
- GATE | Gate IT 2008 | Question 6
- GATE | Gate IT 2008 | Question 7
- GATE | Gate IT 2008 | Question 17
- GATE | Gate IT 2008 | Question 16
- GATE | Gate IT 2008 | Question 15
- GATE | Gate IT 2008 | Question 14
- GATE | Gate IT 2008 | Question 13
- GATE | Gate IT 2008 | Question 12
- GATE | Gate IT 2008 | Question 11
- GATE | Gate IT 2008 | Question 10