Tarjan's algorithm (strongly connected components)

hip-hop, educational · 2:41

Listen on 93

Lyrics

[Verse 1]
Walking through the graph with purpose and a plan
Every node gets numbered by my steady hand
Discovery time first, then low-link comes to play
Stack is growing tall as I traverse this way
DFS is running deep into the core
Marking every vertex that I haven't seen before
Building up a forest from the roots I find
Tarjan's got the method to reveal what's intertwined

[Chorus]
Stack it up, number down, low-link all around
Strongly connected pieces waiting to be found
When discovery equals low-link at the top
Pop until you're back and let the cycle stop
Tarjan's algorithm, one pass is all you need
Linear time complexity, maximum speed
Components in the graph, circles in the flow
Stack and recurse, that's how the strong ones show

[Verse 2]
Low-link holds the minimum that I can reach
From this vertex going down, that's what I teach
If there's a back edge to a node that's on the stack
Update low-link value, keep the connection track
But if it's a cross edge to a finished part
Don't update the low-link, keep them apart
The magic happens when we're backing out
Discovery equals low-link, time to shout

[Chorus]
Stack it up, number down, low-link all around
Strongly connected pieces waiting to be found
When discovery equals low-link at the top
Pop until you're back and let the cycle stop
Tarjan's algorithm, one pass is all you need
Linear time complexity, maximum speed
Components in the graph, circles in the flow
Stack and recurse, that's how the strong ones show

[Bridge]
Root of a component has the special sign
Discovery time and low-link align
Pop the stack until you reach that root
Every node between them forms the group
Mutual reachability is the key
If you can get there, you can get back free
That's the definition of the strongest bond
Tarjan found the way to look beyond

[Verse 3]
Kosaraju needs two passes, we just need one
DFS with bookkeeping until we're done
Applications everywhere from web page rank
To finding deadlocks where the processes sank
Social network clusters, circuit analysis too
Strongly connected components guide us through
Time complexity linear, space is just the same
Tarjan wrote his name in algorithmic fame

[Chorus]
Stack it up, number down, low-link all around
Strongly connected pieces waiting to be found
When discovery equals low-link at the top
Pop until you're back and let the cycle stop
Tarjan's algorithm, one pass is all you need
Linear time complexity, maximum speed
Components in the graph, circles in the flow
Stack and recurse, that's how the strong ones show

[Outro]
When the graph is calling and you need to know
Which nodes stick together in the data flow
Remember Tarjan's wisdom, let the stack grow high
One DFS traversal and the truth won't lie

← Topological sort | Kosaraju's algorithm →