Question

The Breadth First Search (BFS) algorithm has been implemented using the queue data structure. Which one of the following is a possible order of visiting the nodes in the graph below?

Undirected graph with 6 vertices: M, N, O in the top row and R, Q, P in the bottom row. Edges connect M-N, N-O, M-R, M-Q, N-Q, O-Q, O-P, and Q-P.
Choose one option
Official answer and solution
Official answer
D
From
Official GATE 2017 answer key

Solution

Simulate the Breadth-First Search (BFS) queue mechanics starting from each candidate vertex and check against the given visitation orders.

Let us check each option:

  • Option A (MNOPQR): Starts at MM. The neighbors of MM are N,Q,RN, Q, R. After visiting MM and its neighbor NN, the remaining unvisited neighbors at distance 1 (QQ and RR) must be visited before any distance-2 vertex. However, OO (distance 2 from MM) is visited before QQ and RR, violating BFS.
  • Option B (NQMPOR): Starts at NN. The neighbors of NN are M,O,QM, O, Q. After visiting QQ and MM, the remaining neighbor OO at distance 1 must be visited before PP (distance 2). Visiting PP before OO violates BFS.
  • Option C (QMNROP): Starts at QQ. The neighbors of QQ are M,N,O,PM, N, O, P. Vertex RR is at distance 2 from QQ (via MM). Visiting RR before OO and PP violates BFS.
  • Option D (POQNMR):
    1. Start at vertex PP. Visit PP, enqueue neighbors of PP: OO, then QQ. Visited order: PP.
    2. Dequeue OO. Neighbors of OO are NN (unvisited), QQ (already queued), PP (visited). Enqueue NN. Visited order: P,OP, O.
    3. Dequeue QQ. Neighbors of QQ are MM (unvisited), NN (already queued), PP (visited), OO (visited). Enqueue MM. Visited order: P,O,QP, O, Q.
    4. Dequeue NN. Unvisited neighbors: none. Visited order: P,O,Q,NP, O, Q, N.
    5. Dequeue MM. Unvisited neighbor is RR. Enqueue RR. Visited order: P,O,Q,N,MP, O, Q, N, M.
    6. Dequeue RR. Queue empty. Visited order: P,O,Q,N,M,RP, O, Q, N, M, R.

The sequence POQNMR is a strictly valid BFS traversal order, matching option D.

Final answer

D

Common trap

Confusing BFS with DFS or visiting nodes across levels out of FIFO queue order.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2017 Computer Science and Information Technology (CS) Session 2
Question
15 of 65, printed as CS Q.15
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.