Strongly connected
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.
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.
Pass 1: finish order
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.
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.
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.
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.
Pass 2: flipped arrows
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.
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.
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.
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.
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.
What happens
;; step 1/11 · graph
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→E
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. 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.