Consider the following pseudocode for depth-first search (DFS) algorithm which…

2026

Consider the following pseudocode for depth-first search (DFS) algorithm which takes a directed graph 𝐺(𝑉,𝐸) as input, where 𝑑[𝑣] and 𝑓[𝑣] are the discovery time and finishing time, respectively, of the vertex 𝑣 ∈𝑉.

image.png

Suppose that the input directed graph 𝐺(𝑉,𝐸) is a directed acyclic graph (DAG). For an edge (𝑢,𝑣)∈𝐸, which of the following options will NEVER be correct?

  1. A.

    𝑑[𝑢]<𝑑[𝑣]<𝑓[𝑣]<𝑓[𝑢]

  2. B.

    𝑑[𝑣]<𝑑[𝑢]<𝑓[𝑢]<𝑓[𝑣]

  3. C.

    𝑑[𝑣]<𝑓[𝑣]<𝑑[𝑢]<𝑓[𝑢]

  4. D.

    𝑑[𝑢]<𝑑[𝑣]<𝑓[𝑢]<𝑓[𝑣]

Show answer & explanation

Correct answer: B, D

In Depth-First Search (DFS), the discovery time $d[v]$ and finishing time $f[v]$ for any vertex $v$ satisfy $d[v] < f[v]$. The relationship between intervals $[d[u], f[u]]$ and $[d[v], f[v]]$ for any two vertices is governed by the Parenthesis Theorem: the intervals are either disjoint or one is contained within the other.

Analysis of Edge Types

For an edge $(u, v) \in E$ in a DAG:

  • Tree/Forward Edge: $v$ is a descendant of $u$. Interval of $v$ is inside $u$. Order: $d[u] < d[v] < f[v] < f[u]$. (Option A is possible)

  • Back Edge: $u$ is a descendant of $v$. This implies a cycle. Order: $d[v] < d[u] < f[u] < f[v]$. (Option B is impossible in a DAG)

  • Cross Edge: $u$ and $v$ are in different subtrees. Intervals are disjoint. Order: $d[v] < f[v] < d[u] < f[u]$ (if $v$ finished first). (Option C is possible)

Why Option D is Impossible

Option D suggests $d[u] < d[v] < f[u] < f[v]$. This implies that the interval $[d[u], f[u]]$ partially overlaps with $[d[v], f[v]]$. The Parenthesis Theorem states that DFS intervals cannot partially overlap; they must be either completely disjoint or completely nested. Therefore, this ordering is structurally impossible in any DFS traversal.

Thus, the options that will NEVER be correct are B and D.

Explore the full course: Coding For Placement