Sub topic 13
Strongly Connected Components as Cyclic Regions
This chapter assumes you know three-color DFS for directed graphs, iterative DFS with an explicit stack, and the fact that a directed graph has a topological order exactly when it has no cycle. Earlier chapters answered a yes-or-no question: is there a cycle? This chapter answers a broader one: which parts of the graph are cyclic?
What a Strongly Connected Component Is
A strongly connected component (SCC) is a maximal set of vertices in a directed graph in which every vertex can reach every other vertex. "Maximal" means that adding any other vertex would break that property. Every vertex belongs to exactly one SCC, so the SCCs partition the graph.
SCCs and cycles are linked directly. If two vertices u and v are in the same SCC, there is a path from u to v and a path back, and together those paths form a closed walk. A closed walk always contains a simple cycle. In the other direction, all the vertices on a cycle can reach each other, so they all sit in one SCC.
This gives a precise rule. An SCC contains a cycle if and only if it:
- has more than one vertex, or
- is a single vertex with a self-loop.
A single vertex with no self-loop is a trivial SCC and contains no cycle.
The Condensation Is Always Acyclic
If you shrink each SCC to a single node and keep the edges that run between different SCCs, you get the condensation. The condensation is always a DAG. If it had a cycle, the SCCs on that cycle could all reach each other, so they would be one SCC, which contradicts maximality.
This tells you what three-color DFS and Kahn's algorithm are really detecting. A graph is acyclic exactly when every SCC is trivial. When Kahn's algorithm stops early, the vertices it never removed include the vertices of every nontrivial SCC, along with the vertices downstream of them.
Finding SCCs with Kosaraju's Algorithm
Kosaraju's algorithm makes two passes and runs in O(V + E) time:
- Run DFS on the graph and record each vertex when it finishes.
- Reverse every edge. Then take vertices in decreasing finish order, and from each unassigned vertex, collect everything it can reach in the reversed graph. Each collection is one SCC.
Both passes below use explicit stacks, so large graphs do not hit Python's recursion limit.
def strongly_connected_components(adj):
n = len(adj)
visited = [False] * n
order = []
for s in range(n):
if visited[s]:
continue
visited[s] = True
stack = [(s, iter(adj[s]))]
while stack:
u, it = stack[-1]
v = next(it, None)
if v is None:
order.append(u)
stack.pop()
elif not visited[v]:
visited[v] = True
stack.append((v, iter(adj[v])))
radj = [[] for _ in range(n)]
for u in range(n):
for v in adj[u]:
radj[v].append(u)
comp = [-1] * n
components = []
for s in reversed(order):
if comp[s] != -1:
continue
cid = len(components)
comp[s] = cid
members = [s]
stack = [s]
while stack:
u = stack.pop()
for v in radj[u]:
if comp[v] == -1:
comp[v] = cid
members.append(v)
stack.append(v)
components.append(members)
return components
def cyclic_regions(adj):
return [c for c in strongly_connected_components(adj)
if len(c) > 1 or c[0] in adj[c[0]]]
adj = [[1], [2], [0, 3], [4], [3], [5], []]
print(strongly_connected_components(adj)) # [[6], [5], [0, 2, 1], [3, 4]]
print(cyclic_regions(adj)) # [[5], [0, 2, 1], [3, 4]]The check c[0] in adj[c[0]] scans one adjacency list, so the filter costs O(V + E) overall.
Common Errors
- Treating every SCC as cyclic. A single vertex is its own SCC. Without the self-loop check you will report cycles that do not exist.
- Forgetting to reverse edges. If the second pass walks the original graph, it can absorb vertices from downstream SCCs.
- Using discovery order in the second pass. The algorithm needs decreasing finish order. Discovery order merges SCCs incorrectly.
- Expecting one cycle per SCC. An SCC can contain many overlapping cycles. To get a concrete cycle, run the reconstruction method from the earlier chapter on that SCC's vertices.
When SCCs Are the Right Tool
Use SCCs when you need to report every cyclic region at once, such as every group of mutually dependent modules. Use them also when you want to collapse cycles and then work on the acyclic condensation. If you only need to know whether a cycle exists, three-color DFS or Kahn's algorithm is simpler and runs in the same O(V + E) time.
Exercise
In the graph adj = [[1], [0], [2], [4], []], which SCCs does cyclic_regions return, and why is the SCC containing vertex 3 excluded?
Show answer
It returns [0, 1] and [2] (the order may vary). Vertices 0 and 1 reach each other, and vertex 2 has a self-loop. Vertex 3 is a trivial SCC: its only edge goes to 4, which cannot reach back, and 3 has no self-loop.
Exercise
Why must the condensation of any directed graph be a DAG?
Show answer
If the condensation had a cycle, every SCC on that cycle could reach every other one. Their union would then be strongly connected, which contradicts the rule that each SCC is maximal.