Strongly connected components
About this lesson
In a graph with one-way arrows, some nodes can reach each other both ways and some cannot. A strongly connected component is a largest group of nodes that can all reach each other. Kosaraju's algorithm finds every component with two depth-first searches: one to record the order nodes finish in, and one on the graph with every arrow reversed. Watch it run on six nodes, step by step. The Software lessons "Graphs" and "Graph search: BFS and DFS" and the Maths lesson "Graph theory: vertices, edges and breadth-first search" cover the graph basics and depth-first search used here. Union-find groups nodes when edges go both ways; this lesson handles one-way arrows.
A map of one-way arrows
A directed graph has nodes joined by one-way arrows. In Python we store it as a dict: each node lists the nodes its arrows point to. This one has six nodes and seven arrows.
g = {
'A': ['B'],
'B': ['A', 'C'],
'C': ['D'],
'D': ['E'],
'E': ['C'],
'F': ['E'],
}
6 nodes, 7 arrows: A→B B→A B→C C→D D→E E→C F→ERead the lesson step by step
A map of one-way arrows
A directed graph has nodes joined by one-way arrows. In Python we store it as a dict: each node lists the nodes its arrows point to. This one has six nodes and seven arrows.
g = { 'A': ['B'], 'B': ['A', 'C'], 'C': ['D'], 'D': ['E'], 'E': ['C'], 'F': ['E'], } 6 nodes, 7 arrows: A→B B→A B→C C→D D→E E→C F→ENodes that reach each other
Two nodes are strongly connected when each can reach the other along arrows. C, D and E sit on a loop, so all three are. A strongly connected component (SCC) is a largest group like that.
u and v are strongly connected when there is a path u → … → v and a path v → … → u C → D → E → C one loop: C, D, E reach each other A → B → A A and B reach each other F → E but no path from E back to F every node is in exactly one SCCPass 1: search from A
Pass 1 is a depth-first search (DFS): follow an arrow to a new node, go as deep as you can, and back up only when stuck. From A it goes A, B, C, D, E. E only points at C, already seen.
pass 1: order = finish_order(g) start at A A → B new, go deeper B → A A already seen, skip B → C new, go deeper C → D new, go deeper D → E new, go deeper E → C C already seen, skipFinished nodes join a list
A node is finished when every arrow out of it has been tried. E finishes first. Backing up, D, C, B and A finish in turn. Each one joins the end of the order list as it finishes.
finish = every arrow out of the node has been tried E finished order = [E] D finished order = [E, D] C finished order = [E, D, C] B finished order = [E, D, C, B] A finished order = [E, D, C, B, A]F starts a second search
No arrow led to F, so the loop over all nodes starts a new search there. F's arrow points at E, already seen, so F finishes at once. The order list is now E, D, C, B, A, F.
for v in g: if v not in seen: dfs(v) A, B, C, D, E already seen F not seen: start again at F F → E E already seen, skip F finished order = ['E', 'D', 'C', 'B', 'A', 'F'] F finished lastWhy not search from F?
A search finds everything a node can reach. Searching from F on these arrows reaches F, E, C, D. But E cannot reach F, so that search mixes two components. We need searches that cannot leak out.
walk(g, 'F') on the original arrows reaches F, E, C, D F → E but no path E → … → F so F and E are in different SCCs: a plain search spills into C, D, EFlip every arrow
So reverse every arrow. Components stay the same, since a loop reversed is still a loop. F finished last, so no arrow came into its group. Flipped, no arrow leaves it: a search from F is trapped.
rg = reverse(g) rg['A'] = ['B'] rg['B'] = ['A'] rg['C'] = ['B', 'E'] rg['D'] = ['C'] rg['E'] = ['D', 'F'] rg['F'] = [] the node that finishes last lies in a group no outside arrow enters (CLRS, ch. 20)Pass 2: F alone
Pass 2 reads the order list from the end, so F goes first. Searching the flipped arrows, F has nowhere to go. The search finds only F: that is the first component.
for v in reversed(order): reversed(order) = F, A, B, C, D, E v = F walk(rg, 'F') finds [F] comps = [[F]]A and B together
Next comes A. Its flipped arrow reaches B, and B can only go back to A. The arrow from C now points into B, not out. So the search stops: A and B form the second component.
v = A walk(rg, 'A') A → B new B → A already seen finds [A, B] comps = [['F'], ['A', 'B']]The loop C, D, E
B is already in a component, so skip it. C starts the last search: it reaches E, then E reaches D. The arrows toward B and F lead to nodes already taken. Third component: C, E, D.
v = B already seen, skip v = C walk(rg, 'C') C → B already seen C → E new E → D new D → C already seen E → F already seen finds [C, E, D] v = D, v = E already seen scc(g) = [['F'], ['A', 'B'], ['C', 'E', 'D']]Two searches, linear time
Each pass visits every node once and tries every arrow once, and flipping touches each arrow once. With V nodes and E arrows the total work is O(V + E), the same as a single search.
pass 1 O(V + E) reverse O(V + E) pass 2 O(V + E) total O(V + E) here V = 6, E = 7 result: [['F'], ['A', 'B'], ['C', 'E', 'D']] ;; the full program (Python 3, runs as-is) def finish_order(g): order, seen = [], set() def dfs(v): seen.add(v) for w in g[v]: if w not in seen: dfs(w) order.append(v) # v is finished for v in g: if v not in seen: dfs(v) return order def reverse(g): rg = {v: [] for v in g} for v in g: for w in g[v]: rg[w].append(v) return rg def walk(g, v, seen): seen.add(v) found = [v] for w in g[v]: if w not in seen: found += walk(g, w, seen) return found def scc(g): order = finish_order(g) rg = reverse(g) seen, comps = set(), [] for v in reversed(order): if v not in seen: c = walk(rg, v, seen) comps.append(c) return comps g = {'A': ['B'], 'B': ['A', 'C'], 'C': ['D'], 'D': ['E'], 'E': ['C'], 'F': ['E']} print(finish_order(g)) print(scc(g))
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01In the lesson's graph (A→B, B→A, B→C, C→D, D→E, E→C, F→E), why are E and F in different components?
Strongly connected needs a path both ways. F → E exists, but no arrow ever leads back into F, so there is no path from E to F.
02Pass 1 finishes the nodes in the order E, D, C, B, A, F. Which node does pass 2 start from?
Pass 2 reads the order list from the end (for v in reversed(order)). The node that finished last lies in a component that no outside arrow enters, so it is the safe place to start.
03Why does pass 2 search the reversed graph instead of the original one?
No arrow enters F's component, but arrows can leave it. Reversed, no arrow leaves it, so the search from F stays inside. A component reversed is still the same component, since a loop reversed is still a loop.
04A graph has V nodes and E arrows. How long does Kosaraju's algorithm take?
Each of the two searches and the reversal touches every node and every arrow a constant number of times, so the total is O(V + E), the same as one depth-first search.
05A directed graph is a single loop of 5 nodes, 1 → 2 → 3 → 4 → 5 → 1. How many strongly connected components does it have?
Going round the loop, every node can reach every other node, so all 5 form one component.
Glossary
- Directed graph
- A graph whose edges are one-way arrows: an arrow from u to v lets you go from u to v, not back.
- Strongly connected
- Two nodes are strongly connected when each can reach the other by following arrows.
- Strongly connected component (SCC)
- A largest group of nodes that are all strongly connected to each other. Every node belongs to exactly one.
- Depth-first search (DFS)
- A search that follows an arrow to a new node and keeps going deeper, backing up only when it is stuck.
- Finished node
- A node whose every outgoing arrow has been tried by the depth-first search.
- Finish order
- The order in which a full depth-first search finishes the nodes. Pass 1 records it.
- Reversed graph
- The same nodes with every arrow pointing the other way. It has the same strongly connected components.
- Kosaraju's algorithm
- Find SCCs with two depth-first searches: one for the finish order, one on the reversed graph in reverse finish order.
Notes and sources
- The full program is real Python 3 (dicts keep insertion order since 3.7, so the searches visit nodes in the order shown). It prints ['E', 'D', 'C', 'B', 'A', 'F'] and then [['F'], ['A', 'B'], ['C', 'E', 'D']].
- This is Kosaraju's algorithm in the form CLRS gives it: finish order on the graph, then searches on the reversed graph. Sedgewick and Wayne run the mirror image (finish order on the reversed graph, then searches on the original). Both find the same components.
- Why it works: the node that finishes last in pass 1 lies in a component that no arrow from another component enters. After flipping, no arrow leaves that component, so the pass 2 search from it stays inside. Once that component is marked seen, the same holds for the next one.
- Both searches are recursive. Python stops deep recursion at a limit (see sys.getrecursionlimit), so for very large graphs write the searches with an explicit stack instead.
- Tarjan's algorithm finds the same components in a single depth-first search, also in O(V + E) time, by tracking the lowest discovery time each node can reach.
- Sedgewick and Wayne, Algorithms 4th ed., §4.2 Directed Graphs (strong connectivity, Kosaraju-Sharir)
- MIT 6.006 Introduction to Algorithms (Spring 2020), Lecture 10: Depth-First Search
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), chapter 20
- Python Standard Library: dict (insertion order)
- Python built-in functions: reversed()
- Python sys module: recursion limit
Quiz Unlocks after the last step.