Strongly Connected Components (SCC) — Kosaraju's algorithm, Tarjan's algorithm
What exactly is an SCC?

Kosaraju's algorithm — two passes of DFS
WHY does this work? (derive it, don't memorize)
Key fact about finish times. Suppose SCCs and exist with an edge in the condensation (so comes "before" ). Then: i.e. the SCC that is "earlier" in the DAG finishes later.
Proof sketch (Why this is true):
- Case 1: DFS first enters . Since can reach , DFS will explore all of and before returning, so the first node touched in finishes after everything in . ✓
- Case 2: DFS first enters . Because the condensation is a DAG and edge goes , there is no path . So 's DFS finishes entirely without touching . Later DFS starts in → finishes after . ✓
Now reverse the graph. In , the edge becomes . We start the second DFS from the vertex of highest finish time — that vertex lies in a "source" SCC of the original DAG (no SCC finishes later). In that SCC is a sink: its edges to other SCCs are reversed and now point into it, so the DFS cannot escape to another SCC. Hence one DFS tree = exactly one SCC. Removing it, repeat. ∎
Tarjan's algorithm — one pass of DFS
WHY low[u] == disc[u] marks a root
- We keep nodes of the current DFS path/SCC-candidates on an explicit stack, with an
onStack[]flag. low[u]says "the earliest-discovered node can loop back to without leaving the current SCC."- If can reach an earlier node (), then isn't the entry of its SCC — some ancestor is.
- If cannot climb above itself (), is the highest node of its SCC = its root. Pop the stack down to ; those vertices are one SCC.
Recall Feynman: explain to a 12-year-old
Imagine cities connected by one-way roads. A "club" is a group of cities where you can drive from any city to any other and come back — a round trip is always possible. That club is an SCC. Kosaraju explores every city twice: first to note the order people finish their tour, then it flips all the road directions and re-tours starting from whoever finished last — each new tour fences off exactly one club. Tarjan is the smart traveler who does it in one go: he keeps a stack of "cities I might still loop back to," and the moment he realizes a city can't reach anyone older than itself, he knows that city is the leader of a club and scoops everyone above it off the stack.
Flashcards
What is an SCC in a directed graph?
What is the condensation of a graph?
Kosaraju's three steps?
Why does the highest-finish-time vertex matter in Kosaraju?
In Tarjan, what does low[u] mean?
Tarjan condition for an SCC root?
low[u] == disc[u]; pop the stack down to and including u to form the SCC.Tarjan: when to use low[v] vs disc[v]?
low[v] for tree edges (recursive child); disc[v] only when onStack[v] for back/cross edges.Time complexity of both algorithms?
Why is onStack[] needed in Tarjan?
Difference between SCC and undirected connected component?
Connections
- Depth First Search — both algorithms are DFS skeletons.
- Topological Sort — the condensation DAG can be topologically ordered.
- Directed Acyclic Graph (DAG) — what you get after contracting SCCs.
- 2-SAT — solved by SCCs on the implication graph.
- Bridges and Articulation Points — Tarjan's
low-value idea reused on undirected graphs. - Union-Find — alternative connectivity tool (undirected only).
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, SCC ka matlab hai directed graph mein aise nodes ka group jahan har node se har doosre node tak ja sakte ho aur wapas bhi aa sakte ho. One-way roads waale shehron jaisa socho — agar ek club ke andar saare cities mein round-trip possible hai, toh wo ek SCC hai. SCC important kyun? Kyunki jab tum har SCC ko ek single node maan ke "shrink" karte ho, toh poora graph ek DAG ban jaata hai — saare cycles gaayab, aur DAG par kaam (topological order, longest path) bahut easy ho jaata hai.
Kosaraju ka funda simple hai: pehle normal DFS chalao aur har node ka finish time note karo (jab uske saare children khatam ho jaate hain). Phir saare edges ulta kar do (reverse graph), aur jis node ka finish time sabse zyada hai usse start karke DFS karo decreasing finish-time order mein. Har DFS tree exactly ek SCC dega. Yaad rakhna — ulta graph aur decreasing order, dono zaroori hain, warna answer galat aayega.
Tarjan smart hai — sirf ek DFS mein kaam ho jaata hai. Har node ko disc (discovery index) deta hai aur low track karta hai (sabse purana node jahan tak wapas pahunch sakte ho). Ek explicit stack rakhta hai with onStack flag. Jab kisi node par low[u] == disc[u] mile, samajh lo wo apne SCC ka leader hai — stack se us node tak sab pop kar do, wahi ek SCC hai. Important rule: tree edge ke liye low[v] lo, lekin back/cross edge ke liye sirf disc[v] (aur tabhi jab onStack[v] true ho) — warna alag SCCs galti se merge ho jaayenge.
Dono O(V+E) hain. Exam/interview mein: agar one-pass aur low-link concept chahiye toh Tarjan, agar simple "DFS + reverse" yaad rakhna easy lage toh Kosaraju. Donon ki backbone DFS hi hai.