Sub topic 2
Reversing Every Edge: The Transpose Graph
This chapter assumes you know recursive depth-first search (DFS), adjacency lists, DFS finish times, and the condensation DAG from the previous chapter. The examples use this graph, which has the SCCs {0,1,2}, {3,4,5}, and {6,7}:
adj = [[1], [2], [0, 3], [4], [5], [3], [5, 7], [6]]The Transpose Graph
The transpose of G is the graph you get by reversing every edge. If G has the edge u → v, the transpose has v → u. Reversing edges keeps cycles intact, so the transpose has exactly the same SCCs as G. Its condensation, however, points the opposite way.
Kosaraju's Algorithm: Two Passes of DFS
- Run DFS on G and record each vertex when it finishes.
- Build the transpose.
- Go through the vertices in decreasing finish time. For each vertex that has no label yet, run DFS on the transpose. Every vertex this DFS reaches belongs to one SCC.
def kosaraju(n, adj):
visited = [False] * n
order = []
def dfs1(u):
visited[u] = True
for v in adj[u]:
if not visited[v]:
dfs1(v)
order.append(u)
for u in range(n):
if not visited[u]:
dfs1(u)
radj = [[] for _ in range(n)]
for u in range(n):
for v in adj[u]:
radj[v].append(u)
comp = [-1] * n
def dfs2(u, c):
comp[u] = c
for v in radj[u]:
if comp[v] == -1:
dfs2(v, c)
c = 0
for u in reversed(order):
if comp[u] == -1:
dfs2(u, c)
c += 1
return comp
adj = [[1], [2], [0, 3], [4], [5], [3], [5, 7], [6]]
print(kosaraju(8, adj)) # [1, 1, 1, 2, 2, 2, 0, 0]Why Kosaraju's Algorithm Works
Suppose the condensation has an edge from component C to component D. Then the latest finish time in C is greater than the latest finish time in D. So the vertex with the highest finish time belongs to a source component of the condensation. In the transpose, a source component becomes a sink, which means the second DFS cannot leave it. Each later DFS can only reach components that already have labels. As a result, the components are numbered in topological order.
Low-Link Values and Component Roots
Tarjan's algorithm needs only one DFS. Each vertex v gets a discovery index, index[v]. It also gets a low-link value, low[v], which is the smallest index v can reach through its DFS subtree using at most one edge to a vertex that is still on the stack. A vertex is the root of its SCC exactly when low[v] == index[v].
Tarjan's Algorithm: One Pass with a Stack
def tarjan(n, adj):
index = [None] * n
low = [0] * n
on_stack = [False] * n
stack, sccs = [], []
counter = 0
def strongconnect(v):
nonlocal counter
index[v] = low[v] = counter
counter += 1
stack.append(v)
on_stack[v] = True
for w in adj[v]:
if index[w] is None:
strongconnect(w)
low[v] = min(low[v], low[w])
elif on_stack[w]:
low[v] = min(low[v], index[w])
if low[v] == index[v]:
comp = []
while True:
w = stack.pop()
on_stack[w] = False
comp.append(w)
if w == v:
break
sccs.append(comp)
for v in range(n):
if index[v] is None:
strongconnect(v)
return sccs
adj = [[1], [2], [0, 3], [4], [5], [3], [5, 7], [6]]
print(tarjan(8, adj)) # [[5, 4, 3], [2, 1, 0], [7, 6]]Exercise
Tarjan's algorithm outputs the SCCs in what order relative to the condensation DAG, and why?
Show answer
Reverse topological order. A root is popped only after DFS has finished every component reachable from it, so sink components come out first.