Sub topic 2
What Is a Strongly Connected Component?
This chapter assumes you can read Python, represent a directed graph as an adjacency list (adj[u] lists the vertices that u has edges to), and run a breadth-first or depth-first search from one vertex. It also builds on the previous chapter's notion of reachability: vertex v is reachable from u when there is a directed path from u to v, written u ⇝ v.
The Definition
Two vertices u and v are mutually reachable when u ⇝ v and v ⇝ u. A strongly connected component (SCC) is a maximal set of vertices in which every pair is mutually reachable. "Maximal" means you cannot add any other vertex to the set without breaking that property.
A graph is strongly connected when the whole vertex set forms a single SCC, so every vertex can reach every other vertex.
Every vertex reaches itself through a path of length zero. So a vertex with no cycle through it still belongs to an SCC: the singleton set containing only that vertex.
Why SCCs Partition the Graph
Mutual reachability is an equivalence relation, which is why SCCs never overlap:
- Reflexive.
u ⇝ uholds through the empty path, souis mutually reachable with itself. - Symmetric. The definition is symmetric in
uandv. - Transitive. If
uandvare mutually reachable, andvandware mutually reachable, then joining the pathu ⇝ vtov ⇝ wgivesu ⇝ w. Joiningw ⇝ vtov ⇝ ugivesw ⇝ u.
An equivalence relation splits its domain into disjoint equivalence classes, and the SCCs are exactly those classes. As a result, every vertex belongs to exactly one SCC, and the SCC of u equals the set of all vertices mutually reachable with u. This also explains why maximality comes for free: an equivalence class already contains everything related to its members.
A Worked Example
The example graph below has 8 vertices and is used throughout the tutorial:
adj = [[1], [2], [0, 3], [4], [5], [3], [5, 7], [6]]Its edges are 0→1, 1→2, 2→0, 2→3, 3→4, 4→5, 5→3, 6→5, 6→7, 7→6.
Finding the components by hand
A reliable manual method is to pick an unassigned vertex u, compute the set of vertices u can reach (its forward set), then compute the set of vertices that can reach u (its backward set). The SCC of u is the intersection of the two sets.
| Start `u` | Forward set (reached by `u`) | Backward set (reaches `u`) | SCC = intersection |
|---|---|---|---|
| 0 | {0, 1, 2, 3, 4, 5} | {0, 1, 2} | {0, 1, 2} |
| 3 | {3, 4, 5} | {0, 1, 2, 3, 4, 5, 6, 7} | {3, 4, 5} |
| 6 | {3, 4, 5, 6, 7} | {6, 7} | {6, 7} |
Look at vertex 3. Every vertex in the graph can reach it, but 3 can only reach {3, 4, 5}. One-way reachability does not put two vertices in the same SCC. The edge 2→3 connects the first two components, yet no path leads back from 3 to 2.
Structural Facts Worth Knowing
These properties come up again in the correctness proofs of both algorithms.
- Cycles stay inside one SCC. All vertices on a directed cycle are mutually reachable, so a cycle can never cross component boundaries. Conversely, two distinct vertices share an SCC exactly when some closed walk passes through both.
- Paths between members stay inside. Suppose
uandvare in SCCCandwlies on some path fromutov. Thenu ⇝ w, andw ⇝ v ⇝ u, sowis also inC. A path can leave a component only if it never returns. - Each SCC with two or more vertices is strongly connected as an induced subgraph. Because the paths between members stay inside the component, you can verify strong connectivity using only the internal edges.
- Between-component edges point one way. If an edge
C₁ → C₂existed alongside a pathC₂ ⇝ C₁, the two components would merge. This is the reason the component graph is acyclic.
Edge Cases
| Graph shape | Number of SCCs |
|---|---|
| Single directed cycle through all `n` vertices | 1 |
| Directed acyclic graph (DAG) with `n` vertices | `n` (all singletons) |
| `n` isolated vertices, no edges | `n` |
| Two vertices with edges both ways | 1 |
| Directed path `0→1→…→n-1` | `n` |
A few details catch people out:
- Self-loops (
u→u) do not change the partition, becauseuis already in its own SCC. Some applications care whether a singleton has a self-loop. For example, cycle detection treats{u}with a self-loop as a cycle. The SCC algorithms ignore self-loops. - Parallel edges have no effect on reachability, so they do not change the components either.
- Disconnected graphs need an outer loop over all vertices. A search started from one vertex only finds what that vertex can reach.
A Brute-Force Reference Implementation
The definition gives an algorithm directly. Compute reachability from every vertex, then group the vertices that are mutually reachable. This takes O(V · (V + E)) time and O(V²) memory, which is far slower than the linear-time algorithms in later chapters. It is short and obviously correct, though, so it works well as a test oracle for checking a faster implementation on small random graphs.
from collections import deque
def reachable_from(s, adj):
"""Return a boolean list: seen[v] is True when s reaches v."""
seen = [False] * len(adj)
seen[s] = True
queue = deque([s])
while queue:
u = queue.popleft()
for v in adj[u]:
if not seen[v]:
seen[v] = True
queue.append(v)
return seen
def scc_brute_force(adj):
n = len(adj)
reach = [reachable_from(s, adj) for s in range(n)]
comp = [-1] * n
sccs = []
for u in range(n):
if comp[u] != -1:
continue
members = [v for v in range(n) if reach[u][v] and reach[v][u]]
for v in members:
comp[v] = len(sccs)
sccs.append(members)
return sccs, comp
adj = [[1], [2], [0, 3], [4], [5], [3], [5, 7], [6]]
sccs, comp = scc_brute_force(adj)
print(sccs) # [[0, 1, 2], [3, 4, 5], [6, 7]]
print(comp) # [0, 0, 0, 1, 1, 1, 2, 2]The two outputs are the two common representations of the result. sccs is a list of components, and comp maps each vertex to a component label. Kosaraju's algorithm naturally produces comp, and Tarjan's algorithm naturally produces sccs. To convert one into the other, take a single pass over the vertices.
Common Misconceptions
- "An edge connects two vertices, so they are in the same component." That rule applies only to undirected graphs. In a directed graph you need paths in both directions.
- "Each pair in an SCC needs edges both ways." You only need paths. In the 3-cycle
0→1→2→0, vertices 0 and 2 have only the direct edge2→0, but0→1→2closes the loop. - "One DFS tree is one SCC." A single DFS from vertex 0 in the example visits {0, 1, 2, 3, 4, 5}, which spans two components. Separating them requires the extra machinery of finish times or low-link values.
- "SCCs and weakly connected components are the same." Weakly connected components ignore edge directions. The example graph is one weakly connected component but has three SCCs.
Exercises
Exercise
A directed graph has vertices 0–5 and edges 0→1, 1→0, 1→2, 2→3, 3→4, 4→2, 5→4. List its strongly connected components.
Show answer
{0, 1}, {2, 3, 4}, and {5}. Vertices 0 and 1 form a 2-cycle. Vertices 2, 3, 4 form the cycle 2→3→4→2. Vertex 5 reaches 4, but nothing reaches 5, so it is a singleton. The edge 1→2 is one-way, so {0, 1} and {2, 3, 4} stay separate.
Exercise
You add the edge 3→1 to the graph in the previous exercise. What are the SCCs now, and why?
Show answer
{0, 1, 2, 3, 4} and {5}. The new path 2→3→1 together with 1→2 makes vertex 1 mutually reachable with 2, 3, and 4. Vertex 0 is mutually reachable with 1, so transitivity merges all five vertices. Vertex 5 still has no incoming edges.
Exercise
Prove that if u and v lie in the same SCC C, every vertex on every directed path from u to v also lies in C.
Show answer
Let w be on a path from u to v. The prefix of that path gives u ⇝ w. The suffix gives w ⇝ v, and because u and v share an SCC, v ⇝ u, so w ⇝ u. Then u and w are mutually reachable, and w belongs to the SCC of u, which is C.
Exercise
What is the maximum number of SCCs a directed graph with n vertices and at least one edge can have, and what is the minimum number of edges for a graph with n ≥ 2 vertices to have exactly one SCC?
Show answer
The maximum is n. Any DAG works, for example a single edge 0→1 with the other vertices isolated. The minimum is n edges, achieved by a single directed cycle through all vertices. With fewer than n edges, some vertex has no incoming edge or no outgoing edge, so it cannot be mutually reachable with any other vertex.