Kosaraju's algorithm

symphonic, cinematic, dramatic, orchestral · 4:12

Listen on 93

Lyrics

[Verse 1]
When you got a directed graph that's strongly connected
Need to find components that are closely related
Kosaraju steps up with a two-pass solution
First we dive deep then we flip for resolution
Start with any vertex run that DFS complete
Mark the finish times as each node we meet
Stack them up in order when the search is done
Finishing time order is phase number one

[Chorus]
Two DFS passes that's the Kosaraju way
First pass forward then transpose and replay
Finish times matter in the order we stack
Transpose the graph then we're running it back
Strong components found when we trace every path
Linear time complexity doing the math
Kosaraju Kosaraju two passes we need
DFS twice and we're guaranteed to succeed

[Verse 2]
Phase one complete now we got our stack ready
Vertices ordered by finish times steady
Take that graph and flip every single edge
What pointed left now points right on the ledge
This transpose graph holds the same components
But now we can find them with properponents
Pop from the stack run DFS again
Each tree we discover is a component my friend

[Chorus]
Two DFS passes that's the Kosaraju way
First pass forward then transpose and replay
Finish times matter in the order we stack
Transpose the graph then we're running it back
Strong components found when we trace every path
Linear time complexity doing the math
Kosaraju Kosaraju two passes we need
DFS twice and we're guaranteed to succeed

[Bridge]
Why does it work let me break it down clear
Strongly connected means paths everywhere
Forward and backward between every pair
First DFS gives us the finishing layer
Transpose reveals what's truly connected
Components emerge just as we expected
Two simple passes but the logic runs deep
Graph theory wisdom that we always keep

[Verse 3]
Implementation clean with recursive calls
Visit every vertex as the algorithm crawls
Mark each node visited keep the stack tight
Finishing order gonna guide us right
Transpose creation just reverse every arc
Second DFS lights up each component spark
Linear time bound keeps it running fast
Kosaraju algorithm built to last

[Chorus]
Two DFS passes that's the Kosaraju way
First pass forward then transpose and replay
Finish times matter in the order we stack
Transpose the graph then we're running it back
Strong components found when we trace every path
Linear time complexity doing the math
Kosaraju Kosaraju two passes we need
DFS twice and we're guaranteed to succeed

[Outro]
When graphs get complex and connections run wild
Kosaraju breaks it down simple and styled
Two-pass solution with that DFS flow
Strong components identified now you know

← Tarjan's algorithm (strongly connected components) | Prim's algorithm →