Sub topic 3
DFS Discovery and Finish Times
This chapter assumes you already know recursive depth-first search (DFS), adjacency lists, and basic Python. It adds one idea to plain DFS: recording when the search enters each vertex and when it leaves. Both Kosaraju's and Tarjan's algorithms rely on these timestamps.
Two timestamps per vertex
A DFS keeps a global clock that ticks once per event. Each vertex u gets two values:
- Discovery time
d[u]: the tick when DFS first reachesuand starts exploring its edges. - Finish time
f[u]: the tick when DFS has explored every edge out ofuand returns from it.
The interval [d[u], f[u]] covers the whole time u is on the recursion stack. Any vertex discovered during that interval is a descendant of u in the DFS tree.
Computing the timestamps
def dfs_times(n, adj):
disc = [0] * n
fin = [0] * n
time = 0
def visit(u):
nonlocal time
time += 1
disc[u] = time # entering u
for v in adj[u]:
if disc[v] == 0: # 0 means not yet discovered
visit(v)
time += 1
fin[u] = time # leaving u
for u in range(n):
if disc[u] == 0:
visit(u)
return disc, fin
# SCCs of this graph: {0,1,2}, {3,4,5}, {6,7}
adj = [[1], [2], [0, 3], [4], [5], [3], [5, 7], [6]]
disc, fin = dfs_times(8, adj)
print(disc) # [1, 2, 3, 4, 5, 6, 13, 14]
print(fin) # [12, 11, 10, 9, 8, 7, 16, 15]The outer loop matters. A single DFS from vertex 0 never reaches 6 or 7, so the loop starts a second tree at vertex 6. Every vertex gets timestamps whether or not it is reachable from 0.
The parenthesis property
For any two vertices u and v, their intervals are either nested or disjoint. They never partially overlap. Write each discovery as "(" and each finish as ")", and the sequence is always properly balanced. Here, [4, 9] for vertex 3 contains [6, 7] for vertex 5, because 5 is a descendant of 3. Vertex 6's interval [13, 16] is disjoint from vertex 0's interval, because they belong to different DFS trees.
A related fact, the white-path theorem, says that v becomes a descendant of u exactly when, at time d[u], there is a path from u to v made only of undiscovered vertices.
Why finish times matter for SCCs
The vertex with the largest finish time always lies in a component that no other component has an edge into. In the example, vertex 6 finishes last (16), and nothing outside {6, 7} points into that component. Kosaraju's algorithm is built on this fact. A later chapter proves it.
A common mistake is to assume that a smaller discovery time means "earlier in the graph." Discovery order depends on the order of vertices in the outer loop and in each adjacency list. Finish times carry the structural guarantee. Discovery times do not.
Exercise
Run dfs_times on adj = [[1], [2, 3], [], [2]] with n = 4. Give disc and fin, and name the vertex that finishes last.
Show answer
disc = [1, 2, 3, 5] and fin = [8, 7, 4, 6]. DFS goes 0 → 1 → 2, then 2 finishes at 4. Next it visits 3 at time 5, sees that 2 is already discovered, and 3 finishes at 6. Then 1 finishes at 7 and 0 at 8, so vertex 0 finishes last.
Exercise
Can one DFS of a graph give vertex a the interval [2, 6] and vertex b the interval [4, 9]?
Show answer
No. The intervals partly overlap, which breaks the parenthesis property. Once b is discovered at time 4, during a's interval, b is a descendant of a and must finish before a does, at a time below 6.