Strong Components  ·  Kosaraju–Sharir

Two depth-first passes find the strongly connected components of a digraph. Phase 1 runs DFS on the reverse graph Gᴿ and records the finish order. Phase 2 runs DFS on G, taking sources in reverse postorder of that first pass — and every DFS tree it grows is exactly one strong component. Format: Algs4 Digraph (V, then E, then directed edges v w).

650ms
Reverse graph Gᴿ
unseen
active (on stack)
finished
G tree edge
Gᴿ tree edge
Algorithm
Phase 1
DFS on Gᴿ → finish order
Phase 2
DFS on G in reverse postorder
Status
Press  Step  or  Play  to begin.
Finish order of Gᴿ
building…
Vertices appear here as they finish in the reverse-graph DFS.
Active path · the stack
empty
Strong components
found in Phase 2
Edit / paste a digraph
Format (Sedgewick & Wayne Digraph): vertex count V, then edge count E, then E directed edges v w read as v→w. The reverse graph Gᴿ is built automatically for Phase 1. Phase 1 and Phase 2 each sweep vertices 0 … V−1 as roots; Phase 2 visits them in the reverse postorder produced by Phase 1.

Optional fixed layout: after the edges, add exactly V more lines of x y — one pair per vertex, in order — to pin down your own layout instead of the automatic one. Any numeric scale works; it's auto-centred and scaled to fit, preserving relative position and aspect ratio. Loading a graph always fills the box back in with its current layout's coordinates so you can nudge them; tick the box above to see the plain format instead.

Mixed-format tolerant: each edge occupies exactly one line. Pasting a weighted graph here (an extra third number per edge line) just ignores that weight, since this page is unweighted — no need to strip it out by hand.