[Verse 1] Graph's a maze of arrows, nodes connected tight Strongly connected components hiding from sight Kosaraju cracked the code with two-pass design First pass forward, second pass rewind DFS from every vertex, mark what you can reach Stack them up in finish order, that's the wisdom each Transpose the graph, flip every edge around Now the magic happens, secrets will be found [Chorus] Two DFS sweeps, that's the Kosaraju way Forward then reverse, components on display Stack the finish times, transpose and go again Linear time complexity, O of V plus E my friend Forward stack reverse, remember this refrain Kosaraju's double vision breaks the cycle chain [Verse 2] First traversal builds a tower, latest finish on top When recursion can't go further, that's where timestamps stop Take that stack of ordered vertices, flip the graph's direction Now each DFS from stack-top finds one component section What was reachable before becomes unreachable now Transpose cuts the bridges, shows you which nodes bow To which component family, isolated and complete Kosaraju's theorem makes the algorithm neat [Chorus] Two DFS sweeps, that's the Kosaraju way Forward then reverse, components on display Stack the finish times, transpose and go again Linear time complexity, O of V plus E my friend Forward stack reverse, remember this refrain Kosaraju's double vision breaks the cycle chain [Bridge] Why does transposition work this algorithmic spell? If A can reach B forward, then B reaches A when reversed But only within components, not across the well That separates the families, boundaries unrehearsed Stack gives post-order, highest finish first to pop Ensures we start each component from its topmost stop [Verse 3] Implementation's elegant, two functions side by side First DFS fills the stack with finish times as guide Second DFS on transpose pops and explores each tree Colors mark the territories, components running free Applications everywhere, web crawling, circuit design Social network clustering, recommendation engines shine Kosaraju's legacy lives in graphs both large and small Two-pass revelation conquers connectivity's call [Chorus] Two DFS sweeps, that's the Kosaraju way Forward then reverse, components on display Stack the finish times, transpose and go again Linear time complexity, O of V plus E my friend Forward stack reverse, remember this refrain Kosaraju's double vision breaks the cycle chain
← Tarjan's algorithm (strongly connected components) | Prim's algorithm →