Sub topic 1
Directed Graphs and Mutual Reachability
This chapter covers the vocabulary used by both SCC algorithms: reachability, components, DFS timestamps, edge types, and the condensation graph.
Prerequisites
This chapter assumes you know:
- Directed graphs stored as adjacency lists, where
adj[u]lists everyvwith an edgeu → v. - Recursive depth-first search (DFS).
- How to read Python code.
Reachability in One Direction
In a directed graph, vertex v is reachable from u if a path of edges leads from u to v. Reachability is not symmetric. If u reaches v, v may still have no path back to u. Two vertices are mutually reachable when each one reaches the other.
Strongly Connected Components
A strongly connected component (SCC) is a maximal set of vertices in which every pair is mutually reachable. "Maximal" means you cannot add another vertex without breaking that property. Every vertex belongs to exactly one SCC. A vertex that lies on no cycle is an SCC by itself.
The running example uses adj = [[1], [2], [0, 3], [4], [5], [3], [5, 7], [6]]. Its SCCs are {0,1,2}, {3,4,5}, and {6,7}.
DFS Discovery and Finish Times
DFS keeps one clock. It stamps a vertex with a discovery time when it first visits that vertex, and with a finish time after it has explored all of the vertex's outgoing edges.
def dfs_times(n, adj):
disc = [-1] * n
fin = [-1] * n
t = 0
def dfs(u):
nonlocal t
disc[u] = t
t += 1
for v in adj[u]:
if disc[v] == -1:
dfs(v)
fin[u] = t
t += 1
for u in range(n):
if disc[u] == -1:
dfs(u)
return disc, fin
adj = [[1], [2], [0, 3], [4], [5], [3], [5, 7], [6]]
print(dfs_times(8, adj))
# ([0, 1, 2, 3, 4, 5, 12, 13], [11, 10, 9, 8, 7, 6, 15, 14])Tree, Back, Forward, and Cross Edges
When DFS examines an edge u → v, the state of v decides the edge's type:
| Type | Condition when `u → v` is examined |
|---|---|
| Tree | `v` is not yet discovered |
| Back | `v` is discovered but not finished (it is an ancestor of `u`) |
| Forward | `v` is finished and `disc[u] < disc[v]` |
| Cross | `v` is finished and `disc[v] < disc[u]` |
Back edges create cycles, and cycles are what join vertices into one SCC. In the example graph, 2 → 0, 5 → 3, and 7 → 6 are back edges, and 6 → 5 is a cross edge.
The Condensation DAG
If you collapse each SCC into a single node, you get the condensation. An edge joins two component nodes when any original edge crosses between them. The condensation is always acyclic: a cycle between components would make them mutually reachable, so they would be one SCC.
Exercise
In the example DFS, how is the edge 2 → 3 classified, and does it join 2 and 3 into one SCC?
Show answer
It is a tree edge, because 3 is undiscovered when DFS examines the edge. It does not join them, because no path leads from 3 back to 2.