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?
Topic-wise GATE CS PYQs with verified steps
GATE CSE 2017 Set 2 · Q15
Time

Select one answerSingle correct
Solution
Official answer: D) POQNMR
Graph TraversalsBreadth First Search (BFS) visits all vertices at distance before visiting any vertex at distance from the source vertex.
Let us evaluate the options:
(A) MNOPQR: Starts at . Distance-1 neighbors of are . However, (which is at distance ) is visited before and . Hence, invalid.
(B) NQMPOR: Starts at . Distance-1 neighbors of are . Here, (at distance ) is visited before (at distance ). Hence, invalid.
(C) QMNROP: Starts at . Distance-1 neighbors of are . Here, (at distance ) is visited before and (at distance ). Hence, invalid.
(D) POQNMR: Starts at .
Enqueue . Level : .
Dequeue , enqueue unvisited neighbors: . Queue: . Level : .
Dequeue , enqueue unvisited neighbor: . Queue: .
Dequeue , enqueue unvisited neighbor: . Queue: .
Dequeue , no unvisited neighbors. Queue: .
Dequeue , enqueue unvisited neighbor: . Queue: .
Dequeue .
The sequence of visited nodes is P O Q N M R, which is valid.
Therefore, option (D) is correct.
Try the question first, then check your work.
Examiner trap
In BFS, all adjacent nodes (distance 1) of the starting node must be visited before visiting any node at distance 2.
This Graph Traversals previous year question is Q15 of the GATE CSE 2017 Set 2 paper. Find more GATE CSE PYQs on Graph Traversals in Algorithms, or browse all GATE CSE PYQs.
Free diagnostic
Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Graph Traversals PYQs now with no account.
An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.
Graph Traversals has 16 GATE CSE PYQs across 12 papers from 2014 to 2026. 5 of them are 1-mark MCQs like this one, and it is the only one in GATE CSE 2017 Set 2.
How GATE tests Graph TraversalsIndependent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.