Sub topic 15
Strongly Connected Components
This chapter assumes you already know recursive DFS, finish order (post-order), and topological sort from earlier chapters. You should also be comfortable with directed graphs stored as adjacency-list dictionaries.
What a strongly connected component is
In a directed graph, a strongly connected component (SCC) is a largest set of vertices in which every vertex can reach every other vertex by following edge directions. A vertex with no cycle through it forms an SCC by itself.
If you shrink each SCC to a single node, the result is always a directed acyclic graph (DAG), called the condensation. So SCCs let you treat a messy cyclic graph as a DAG of tightly linked groups. Common uses include finding mutually recursive modules, solving 2-SAT, and simplifying dependency graphs before a topological sort.
Kosaraju's algorithm
Kosaraju's algorithm runs DFS twice:
- Run a full DFS on the graph and record each vertex when it finishes.
- Build the reversed graph by flipping every edge.
- Go through the vertices in decreasing finish time. Each time you reach an unassigned vertex, run DFS from it in the reversed graph. Every vertex that search reaches belongs to one SCC.
This works because the vertex that finishes last sits in a "source" SCC of the condensation. Reversing the edges turns that source SCC into a sink, so the second DFS cannot leak out of it into other components. Each later search starts in the next source SCC, and every edge that would leave it points to a component that has already been assigned.
def kosaraju(graph):
visited = set()
order = []
def visit(node):
visited.add(node)
for neighbor in graph[node]:
if neighbor not in visited:
visit(neighbor)
order.append(node) # finish order
for node in graph:
if node not in visited:
visit(node)
reverse = {node: [] for node in graph}
for node in graph:
for neighbor in graph[node]:
reverse[neighbor].append(node)
assigned = set()
components = []
def collect(node, component):
assigned.add(node)
component.append(node)
for neighbor in reverse[node]:
if neighbor not in assigned:
collect(neighbor, component)
for node in reversed(order):
if node not in assigned:
component = []
collect(node, component)
components.append(component)
return components
graph = {"a": ["b"], "b": ["c"], "c": ["a", "d"],
"d": ["e"], "e": ["d", "f"], "f": []}
print(kosaraju(graph)) # [['a', 'c', 'b'], ['d', 'e'], ['f']]Both passes visit each vertex and edge once, and building the reversed graph does the same, so the total cost is O(V + E).
Tarjan's algorithm
Tarjan's algorithm finds SCCs in a single DFS. Each vertex gets a discovery index and a low-link value. The low-link value is the smallest index the vertex can reach through its DFS subtree plus at most one back edge to a vertex that is still on a stack. When a vertex finishes and its low-link equals its own index, that vertex is the root of an SCC. You then pop the stack down to that vertex, and the popped vertices form the component.
| Property | Kosaraju | Tarjan |
|---|---|---|
| DFS passes | 2 | 1 |
| Needs reversed graph | Yes | No |
| Extra bookkeeping | Finish order | Index, low-link, stack |
| Order of output SCCs | Topological order of the condensation | Reverse topological order |
| Ease of getting it right | Simpler | More error-prone |
Common errors
- Running the second pass on the original graph instead of the reversed one. Searches then spill across components.
- Using discovery order instead of finish order in pass one.
- In Tarjan, updating low-link from a visited vertex that is no longer on the stack. That vertex belongs to an SCC that is already finished.
Exercise
In the example graph, add the edge f -> a. What does kosaraju(graph) return now, and why?
Show answer
It returns a single component with all six vertices. With f -> a, every vertex can reach a, and a can reach every vertex through c -> d -> e -> f, so they are all mutually reachable.
Exercise
Why does the second pass of Kosaraju's algorithm need the reversed graph instead of the original one?
Show answer
The vertex with the latest finish time lies in a source SCC. In the original graph, a DFS from it could follow outgoing edges into other SCCs. In the reversed graph that SCC becomes a sink, so the search stays inside it and collects exactly one component.