Skip to content
Official PYQ · 2008

GATE CS 2008 Q19 — Graph Traversals (BFS, DFS)

Explain with AI

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:

Choose one option
View worked solution

Answer: C

Based on the graph structure, the connections between the vertices are as follows:

  • QQ is connected to MM and NN.
  • MM is connected to PP and RR.
  • NN is connected to PP and OO.
  • OO is connected to RR.

Let us trace the Breadth First Search (BFS) traversal starting from vertex QQ using a FIFO queue:

  1. Start at QQ: Visit QQ. Queue = [Q][Q].
  2. Dequeue QQ: Visit its neighbors MM and NN. Queue = [M,N][M, N].
  3. Dequeue MM: Visit its unvisited neighbors PP and RR. Queue = [N,P,R][N, P, R].
  4. Dequeue NN: Visit its unvisited neighbor OO. Queue = [P,R,O][P, R, O].
  5. Dequeue remaining: Dequeue and visit PP, then RR, and finally OO.

This sequence yields the traversal order QMNPRO\text{QMNPRO}.

Why Option D (QMNPOR\text{QMNPOR}) is incorrect:
Because MM is dequeued before NN, the neighbor of MM (which is RR) is enqueued before the neighbor of NN (which is OO). Due to the FIFO property of the queue, RR must be dequeued and visited before OO. Therefore, QMNPRO\text{QMNPRO} is a valid BFS order, but QMNPOR\text{QMNPOR} is not.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Graph Traversals (BFS, DFS) questions