1. Gyanastra
  2. Algorithms
  3. Graph Traversal, DFS Applications, Topological Sort and SCC
  4. Depth-First Search: Timestamps, Parenthesis and White-Path Theorems
Free lesson, no account needed 11 min read 10 practice questions after it
Asked in past papers3 times
  • GATE CSE20252007
  • UGC NET2015

Algorithms · Graph Traversal, DFS Applications, Topological Sort and SCC

Depth-First Search: Timestamps, Parenthesis and White-Path Theorems

4.1 The recursive algorithm with timestamps

time = 0
DFS(G):
  for each v in V: colour[v] = WHITE; pi[v] = NIL
  for each v in V:
      if colour[v] == WHITE: DFS-VISIT(v)

DFS-VISIT(u):
  time = time + 1;  d[u] = time          # discovery time
  colour[u] = GRAY
  for each v in Adj[u]:
      if colour[v] == WHITE:
          pi[v] = u
          DFS-VISIT(v)
  colour[u] = BLACK
  time = time + 1;  f[u] = time          # finish time
  • d[u]d[u] = discovery time, f[u]f[u] = finish time. Both are integers in 1..2n1..2n; all 2n2n timestamps are distinct.
  • The interval [d[u],f[u]][d[u], f[u]] is the active interval of uu.

4.2 Worked trace on 7 vertices

Use the same graph as §3.2, adjacency lists sorted ascending, and start the outer loop at vertex 1.

1 -- 2 -- 3
|    |    |
4 -- 5    6
     |
     7

Trace (time counter in the last column):

Eventtimedd / ff
discover 11d[1]=1d[1]=1
discover 2 (from 1)2d[2]=2d[2]=2
discover 3 (from 2)3d[3]=3d[3]=3
discover 6 (from 3)4d[6]=4d[6]=4
6 has no white neighbour; finish 65f[6]=5f[6]=5
finish 36f[3]=6f[3]=6
discover 5 (from 2)7d[5]=7d[5]=7
discover 4 (from 5)8d[4]=8d[4]=8
4's neighbours: 1 (GRAY), 5 (GRAY) — none white; finish 49f[4]=9f[4]=9
discover 7 (from 5)10d[7]=10d[7]=10
finish 711f[7]=11f[7]=11
finish 512f[5]=12f[5]=12
finish 213f[2]=13f[2]=13
finish 114f[1]=14f[1]=14

Final table:

vv1234567
d[v]d[v]12387410
f[v]f[v]14136912511

Check: every d<fd<f; all 14 timestamps used exactly once; [d[1],f[1]]=[1,14][d[1],f[1]]=[1,14] contains everything (root); [d[2],f[2]]=[2,13][d[2],f[2]]=[2,13] contains [3,6],[7,12][3,6],[7,12]; [d[5],f[5]]=[7,12][d[5],f[5]]=[7,12] contains [8,9],[10,11][8,9],[10,11]. Consistent.

DFS forest (parent pointers):

1
|
2
├── 3
│   └── 6
└── 5
    ├── 4
    └── 7

4.3 Complexity

Each vertex is discovered once and finished once; each adjacency list is scanned once when its vertex is explored. With lists: Θ(V+E)\Theta(V+E). With a matrix: Θ(V2)\Theta(V^2).

Stack depth. The recursion stack holds at most one vertex per level of the current DFS path. Worst case (a path graph 1−2−3−⋯−n1-2-3-\dots-n): depth nn. Best case (star graph): depth 2 (root + one leaf). So the stack uses O(V)O(V) space in the worst case.

6.1 Parenthesis theorem

For any two vertices u,vu,v in a DFS of a (directed or undirected) graph, exactly one of the following holds:

  1. [d[u],f[u]][d[u],f[u]] and [d[v],f[v]][d[v],f[v]] are disjoint — neither is an ancestor of the other.
  2. [d[u],f[u]][d[u],f[u]] is entirely inside [d[v],f[v]][d[v],f[v]] — uu is a descendant of vv.
  3. [d[v],f[v]][d[v],f[v]] is entirely inside [d[u],f[u]][d[u],f[u]] — vv is a descendant of uu.

The intervals never partially overlap. This is why DFS timestamps behave like balanced parentheses: ( ) ( ( ) ) etc.

Use in GATE: given a set of (d,f)(d,f) pairs, check that no two intervals partially overlap. If they do, the timestamps are impossible.

6.2 White-path theorem

vv is a descendant of uu in the DFS forest if and only if at time d[u]d[u], there is a path from uu to vv consisting entirely of WHITE vertices.

Use: proves that the DFS tree captures reachability; also underlies the correctness of cycle detection (a back edge exists iff a directed cycle exists).

6.3 Cycle detection

  • Directed graph: has a cycle iff DFS produces at least one back edge.
  • Undirected graph: has a cycle iff DFS produces at least one back edge (equivalently, an edge to an already-visited vertex that is not the parent).
Pages 1–2 of 2