Sub topic 2
What Is a Strongly Connected Component?
This chapter assumes you can read Python, know how a directed graph is stored as an adjacency list, and remember from the previous chapter that "v is reachable from u" means there is a directed path from u to v.
The definition
Two vertices u and v are mutually reachable if there is a path from u to v and a path from v to u. 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 to the set and keep that property.
Mutual reachability is an equivalence relation:
- Reflexive: every vertex reaches itself by the empty path.
- Symmetric: the definition treats u and v the same way.
- Transitive: if u and v are mutually reachable, and v and w are too, then joining the paths makes u and w mutually reachable.
Because it is an equivalence relation, the SCCs partition the vertices. Every vertex belongs to exactly one SCC, and no two SCCs overlap.
A worked example
The graph below is used throughout the tutorial.
Vertex 0 reaches 3 through the edge 2 → 3, but 3 cannot get back to 0, so 0 and 3 are in different components. The edges 2 → 3 and 6 → 5 run between components. Edges like these never lie on a cycle.
Checking the definition by brute force
The slowest correct method follows the definition directly. Compute what each vertex can reach, then put u and v together when each reaches the other.
def reachable(adj, s):
seen = {s}
stack = [s]
while stack:
u = stack.pop()
for v in adj[u]:
if v not in seen:
seen.add(v)
stack.append(v)
return seen
def brute_force_sccs(n, adj):
reach = [reachable(adj, u) for u in range(n)]
comp = [-1] * n
sccs = []
for u in range(n):
if comp[u] == -1:
members = [v for v in range(n) if v in reach[u] and u in reach[v]]
for v in members:
comp[v] = len(sccs)
sccs.append(members)
return sccs
adj = [[1], [2], [0, 3], [4], [5], [3], [5, 7], [6]]
print(brute_force_sccs(8, adj)) # [[0, 1, 2], [3, 4, 5], [6, 7]]This takes O(V · (V + E)) time. It is useful for testing faster algorithms on small graphs. Tarjan's and Kosaraju's algorithms produce the same partition in O(V + E).
Common mistakes
| Mistake | Correction |
|---|---|
| Treating a single edge u → v as enough | Both directions of reachability are required. |
| Forgetting single-vertex components | A vertex on no cycle is its own SCC. |
| Ignoring edge direction | Ignoring direction gives *weakly* connected components, a coarser partition. |
| Assuming a DAG has one big SCC | Every SCC of a DAG is a single vertex. |
Exercise
A graph has vertices 0–4 and edges 0→1, 1→2, 2→1, 2→3, 3→4. List its strongly connected components.
Show answer
{1, 2} is one component, because 1→2 and 2→1. Vertices 0, 3, and 4 are on no cycle, so each forms its own component: {0}, {3}, {4}. That makes four SCCs in total.
Exercise
Adding the edge 4→0 to the graph in the previous exercise changes how many SCCs there are. What is the new count?
Show answer
One. The path 0→1→2→3→4→0 is a cycle through all five vertices, so every vertex reaches every other and the whole graph is strongly connected.