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
- = discovery time, = finish time. Both are integers in ; all timestamps are distinct.
- The interval is the active interval of .
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):
| Event | time | / |
|---|---|---|
| discover 1 | 1 | |
| discover 2 (from 1) | 2 | |
| discover 3 (from 2) | 3 | |
| discover 6 (from 3) | 4 | |
| 6 has no white neighbour; finish 6 | 5 | |
| finish 3 | 6 | |
| discover 5 (from 2) | 7 | |
| discover 4 (from 5) | 8 | |
| 4's neighbours: 1 (GRAY), 5 (GRAY) — none white; finish 4 | 9 | |
| discover 7 (from 5) | 10 | |
| finish 7 | 11 | |
| finish 5 | 12 | |
| finish 2 | 13 | |
| finish 1 | 14 |
Final table:
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
|---|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 8 | 7 | 4 | 10 | |
| 14 | 13 | 6 | 9 | 12 | 5 | 11 |
Check: every ; all 14 timestamps used exactly once; contains everything (root); contains ; contains . 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: . With a matrix: .
Stack depth. The recursion stack holds at most one vertex per level of the current DFS path. Worst case (a path graph ): depth . Best case (star graph): depth 2 (root + one leaf). So the stack uses space in the worst case.
6.1 Parenthesis theorem
For any two vertices in a DFS of a (directed or undirected) graph, exactly one of the following holds:
- and are disjoint — neither is an ancestor of the other.
- is entirely inside — is a descendant of .
- is entirely inside — is a descendant of .
The intervals never partially overlap. This is why DFS timestamps behave like balanced parentheses: ( ) ( ( ) ) etc.
Use in GATE: given a set of pairs, check that no two intervals partially overlap. If they do, the timestamps are impossible.
6.2 White-path theorem
is a descendant of in the DFS forest if and only if at time , there is a path from to 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).