Skip to main content

GATE CSE 2026Algorithms › Graph AlgorithmsVerified

GATE CSE 2026 PYQ: Consider the following pseudocode for depth-first search (DFS) algorithm which takes a directed graph G ( V…

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?

  1. A
    d[u]<d[v]<f[v]<f[u]d[u] < d[v] < f[v] < f[u]
  2. B
    d[v]<d[u]<f[u]<f[v]d[v] < d[u] < f[u]< f[v]
    Correct answer
  3. C
    d[v]<f[v]<d[u]<f[u]d[v] < f[v] < d[u] < f[u]
  4. D
    d[u]<d[v]<f[u]<f[v]d[u] < d[v] < f[u] < f[v]
    Correct answer

Answer

(B, D) d [ v ] < d [ u ] < f [ u ] < f [ v ] d[v] < d[u] < f[u]< f[v] d [ v ] < d [ u ] < f [ u ] < f [ v ]; d [ u ] < d [ v ] < f [ u ] < f [ v ] d[u] < d[v] < f[u] < f[v] d [ u ] < d [ v ] < f [ u ] < f [ v ]

Approach

DFS assigns each vertex a time interval [d[v], f[v]]. For any two vertices the intervals are either strictly nested (ancestor/descendant) or completely disjoint — they can never partially overlap. In a DAG there are no back edges, which additionally rules out an ancestor relation that would create a cycle.

For an edge (u,v) in a DAG, only the nested-interval case (A, tree/forward edge) and the disjoint-interval case (C, cross edge) are realizable. Option B would require a back edge, which a DAG cannot have, and option D would require partially overlapping intervals, which no DFS can produce. Thus B and D will never be correct.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.