IAE Academy

GATE 2025 CS2 Q.29: solution and answer

GATE 2025 · CS2Q.29 · 1 mark · MSQ

Which of the following statements regarding Breadth First Search (BFS) and Depth First Search (DFS) on an undirected simple graph is/are TRUE?

  1. (A)

    A DFS tree of is a Shortest Path tree of .

  2. (B)

    Every non-tree edge of with respect to a DFS tree is a forward/back edge.

  3. (C)

    If is a non-tree edge of with respect to a BFS tree, then the distances from the source vertex to and in the BFS tree are within of each other.

  4. (D)

    Both BFS and DFS can be used to find the connected components of .

See the question in the paper

Answer and solution

ANSWER

The TRUE statements are (B), (C) and (D); statement (A) is FALSE.

Official answer: B, C, DSource: Official answer key, GATE 2025 CS2 (IIT Roorkee)

  1. Statement (A): A DFS tree records the order/structure of recursive exploration (which branch was visited first), not edge counts or weights. There is no guarantee that the tree-path distance from the root to any vertex in a DFS tree equals the shortest-path (BFS) distance in — a DFS can plunge deep along one branch before backtracking to a vertex that is actually very close to the root. Hence (A) is FALSE.

  2. Statement (B): For DFS on an UNDIRECTED graph, every edge of is classified as either a tree edge or a back edge — forward edges and cross edges, which can occur in directed-graph DFS, cannot occur here. This is because an undirected edge is explored from whichever endpoint DFS reaches first; by the time the other endpoint is visited, the edge is already either a tree edge or connects back to an ancestor still on the recursion stack (a back edge). So every non-tree edge is a back edge, which is one of the two cases named in statement (B) ('forward/back edge'). Hence (B) is TRUE.

  3. Statement (C): This is the standard BFS-tree lemma. BFS assigns each vertex a level equal to its shortest-path distance from source , discovering all level- vertices before any level- vertex. If is a non-tree edge found while scanning 's neighbours, must already be at level , , or — it cannot be two or more levels away, because BFS would then have discovered (or ) earlier through that very edge, contradicting how BFS proceeds strictly level by level. Hence the distances from to and differ by at most 1, so (C) is TRUE.

  4. Statement (D): Running BFS (or equivalently DFS) from any unvisited vertex visits exactly the set of vertices reachable from it, which in an undirected graph is precisely its connected component. Repeating this process — pick an unvisited vertex, traverse, mark its whole component visited — until every vertex has been visited enumerates all connected components. This works identically whether BFS or DFS is used as the traversal. Hence (D) is TRUE.

  5. Combining all four: the TRUE statements are (B), (C), (D), and (A) is FALSE.

KEY IDEA

In an undirected graph, DFS classifies every non-tree edge as a back edge (never forward or cross), and BFS levels correspond exactly to shortest-path distances from the source, so any non-tree edge in a BFS tree can only connect vertices at levels differing by at most 1. Both traversals visit exactly the reachable set from a start vertex, so repeating from each unvisited vertex finds all connected components.

WATCH OUT FOR

Assuming forward and cross edges can occur in a DFS of an undirected graph (as they can in a directed graph), which leads students to wrongly mark (B) as false; also confusing a DFS tree (built for reachability, not distance) with a BFS tree when judging shortest-path claims.

From the GATE 2025 CS2 paper. Every question of this paper · Download the full paper · All 28 Graph algorithms questions, with notes

More Graph algorithms questions