Skip to content
Official PYQ · 2017

GATE CS 2017 Set 2 Q15 — Graph Traversals (BFS, DFS)

Explain with AI

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?

Choose one option
View worked solution

Answer: D

In a Breadth-First Search (BFS) traversal, vertices must be visited level-by-level in increasing order of their shortest path distance from the starting vertex. That is, all vertices at distance dd from the start vertex must be visited before any vertex at distance d+1d+1 is visited.

Let us analyze the graph's structure and the options:

  • Option D (extPOQNMRext{POQNMR}): If we start the BFS at vertex P\text{P}:
    • Level 0 (distance 0): P\text{P}
    • Level 1 (distance 1): O, Q\text{O, Q}
    • Level 2 (distance 2): N, M\text{N, M}
    • Level 3 (distance 3): R\text{R}
    Since the sequence P→O→Q→N→M→R\text{P} \to \text{O} \to \text{Q} \to \text{N} \to \text{M} \to \text{R} strictly visits vertices level-by-level, it is a valid BFS traversal.
  • Other Options: In options A, B, and C, a vertex at a greater distance is visited before all vertices at a closer distance are exhausted, which violates the BFS property. For example, in Option A (MNOPQR\text{MNOPQR}), starting at M\text{M}, the vertex N\text{N} (at distance 2) is visited before O\text{O} and R\text{R} (at distance 1).

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

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