Skip to content
Official PYQ · 2001

GATE CS 2001 Q2.14 — Graph Traversals (BFS, DFS)

Explain with AI

Consider an undirected, unweighted graph GG. Let a breadth-first traversal of GG be done starting from a node rr. Let d(r,u)d(r,u) and d(r,v)d(r,v) be the lengths of the shortest paths from rr to uu and vv respectively in GG. If uu is visited before vv during the breadth-first traversal, which of the following statements is correct?

Choose one option
View worked solution

Answer: C

Breadth-First Search (BFS) on an undirected, unweighted graph visits vertices in non-decreasing order of their shortest path distance from the starting node rr.

Let d(r,x)d(r, x) denote the shortest path distance from rr to any node xx. During BFS:

  • Nodes at distance kk are always visited before nodes at distance k+1k+1.
  • Nodes at the same distance kk are visited in the order they are enqueued, which depends on the structure of the adjacency list.

Thus, if uu is visited before vv, there are two possibilities:

  1. uu is closer to rr than vv, meaning d(r,u)<d(r,v)d(r, u) < d(r, v).
  2. uu and vv are at the same distance from rr, meaning d(r,u)=d(r,v)d(r, u) = d(r, v), but uu happened to be enqueued before vv.

Combining these two cases, we get:

If u is visited before v, then d(r,u)≤d(r,v)\text{If } u \text{ is visited before } v, \text{ then } d(r, u) \le d(r, v)

Counter-example for Option A:

Consider a graph with three vertices r,u,vr, u, v and edges (r,u)(r, u) and (r,v)(r, v).

  • The shortest path distances are d(r,u)=1d(r, u) = 1 and d(r,v)=1d(r, v) = 1.
  • Starting BFS from rr, we can visit uu first and then vv. Here, uu is visited before vv, but d(r,u)=d(r,v)=1d(r, u) = d(r, v) = 1.
  • This disproves option A (d(r,u)<d(r,v)d(r,u) < d(r,v)).

Therefore, the correct statement is d(r,u)≤d(r,v)d(r, u) \le d(r, v).

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

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