Skip to content
Official PYQ · 2026

GATE CS 2026 Set 1 Q40 — Graph Traversals (BFS, DFS)

Explain with AI

Consider the following pseudocode for depth-first search (DFS) algorithm which takes a directed graph G(V,E)G(V, E) as input, where d[v]d[v] and f[v]f[v] are the discovery time and finishing time, respectively, of the vertex v∈Vv \in V.

DFS(G):unmark all v∈Vt←0for each v∈Vif v is unmarkedt←Explore(G,v,t)end ifend forExplore(G,v,t):mark vt←t+1d[v]←tfor each (v,w)∈Eif w is unmarkedt←Explore(G,w,t)end ifend fort←t+1f[v]←treturn t\begin{array}{|l|l|} \hline \textbf{DFS}(G): \begin{array}{l} \text{unmark all } v \in V \\ t \leftarrow 0 \\ \text{for each } v \in V \\ \quad \text{if } v \text{ is unmarked} \\ \qquad t \leftarrow \text{Explore}(G,v,t) \\ \quad \text{end if} \\ \text{end for} \end{array} & \textbf{Explore}(G,v,t): \begin{array}{l} \text{mark } v \\ t \leftarrow t+1 \\ d[v] \leftarrow t \\ \text{for each } (v,w) \in E \\ \quad \text{if } w \text{ is unmarked} \\ \qquad t \leftarrow \text{Explore}(G,w,t) \\ \quad \text{end if} \\ \text{end for} \\ t \leftarrow t+1 \\ f[v] \leftarrow t \\ \text{return } t \end{array} \\ \hline \end{array}

Suppose that the input directed graph G(V,E)G(V, E) is a directed acyclic graph (DAG).
For an edge (u,v)∈E(u, v) \in E, which of the following options will NEVER be correct?

Select all correct options
View worked solution

Answer: B;D

To determine which options can never be correct for a directed edge (u,v)∈E(u, v) \in E in a Directed Acyclic Graph (DAG) GG, we analyze the relationship between the discovery interval [d[x],f[x]][d[x], f[x]] and finishing interval of vertices uu and vv during Depth-First Search (DFS).

1. The Parenthesis Theorem

For any two vertices uu and vv in a graph, their active intervals [d[u],f[u]][d[u], f[u]] and [d[v],f[v]][d[v], f[v]] must satisfy exactly one of the following conditions:

  • Disjoint: The intervals are entirely separate, i.e., d[u]<f[u]<d[v]<f[v]d[u] < f[u] < d[v] < f[v] or d[v]<f[v]<d[u]<f[u]d[v] < f[v] < d[u] < f[u].
  • Nested: One interval is entirely contained within the other, i.e., d[u]<d[v]<f[v]<f[u]d[u] < d[v] < f[v] < f[u] or d[v]<d[u]<f[u]<f[v]d[v] < d[u] < f[u] < f[v].

Intervals can never partially overlap. Therefore, the relation d[u]<d[v]<f[u]<f[v]d[u] < d[v] < f[u] < f[v] is impossible for any pair of vertices in any graph. This makes Option D impossible.

2. Classification of Edges in a DAG

For a directed edge (u,v)∈E(u, v) \in E, the type of edge determines the interval relationship:

  • Tree / Forward Edge: uu is an ancestor of vv in the DFS forest. This corresponds to the nested interval d[u]<d[v]<f[v]<f[u]d[u] < d[v] < f[v] < f[u]. This matches Option A, which is possible.
  • Cross Edge: vv is visited and finished before uu is discovered. This corresponds to the disjoint interval d[v]<f[v]<d[u]<f[u]d[v] < f[v] < d[u] < f[u]. This matches Option C, which is possible.
  • Back Edge: vv is an ancestor of uu in the DFS forest. This corresponds to the nested interval d[v]<d[u]<f[u]<f[v]d[v] < d[u] < f[u] < f[v]. However, a back edge (u,v)(u, v) implies a directed cycle in the graph. Since GG is a DAG (Directed Acyclic Graph), it cannot contain any back edges. Thus, this relation is impossible, making Option B impossible.

Conclusion

The options that can NEVER be correct are B and D.

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

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