V

Algorithm Visualizer

ZHEN
Share on WeiboShare on XGitHub repositoryAuthor website
Learning ToolsAlgorithm Complexity ReferenceAlgorithm Learning Paths
Data StructuresArrayLinked ListStackQueue and DequeBinary Search TreeBinary HeapHash TableGraphTrieDisjoint Set UnionLRU CacheSkip ListSegment TreeB+ TreeBloom FilterFenwick Tree
SortingBubble SortCocktail Shaker SortBitonic SortSelection SortInsertion SortBinary Insertion SortShell SortMerge SortTop-Down Merge SortQuick SortThree-Way Quick SortDual-Pivot Quick SortHeap SortCounting SortRadix SortBucket Sort
Graph AlgorithmsDijkstra's Shortest PathKruskal's Minimum Spanning TreePrim's Minimum Spanning TreeBellman-Ford Shortest PathsTopological SortFloyd-WarshallStrongly Connected Components2-SATMaximum FlowBipartite MatchingLowest Common AncestorEulerian Path
Dynamic ProgrammingEdit Distance0/1 KnapsackUnbounded KnapsackLongest Common SubsequenceLongest Increasing SubsequenceCoin ChangeStone MergingTraveling Salesperson DPTree Dynamic ProgrammingDigit DPRerooting DP
Backtracking and SearchN-QueensSubsetsPermutationsCombination SumMaze Solving with DFSNumber of IslandsWord SearchSudoku SolverA* Search
StringsKMP String MatchingRabin-Karp String MatchingBoyer-Moore String MatchingManacher's Longest Palindromic SubstringSuffix ArrayLCP ArrayAho-Corasick AutomatonZ Function
Math and Number TheorySieve of EratosthenesLinear SieveEuclidean AlgorithmBinary ExponentiationExtended Euclidean AlgorithmChinese Remainder TheoremEuler's Totient FunctionMiller-Rabin Primality TestFast Fourier TransformPollard's Rho Factorization
Computational GeometryConvex HullRotating CalipersClosest Pair of PointsLine Segment IntersectionBentley-Ottmann Sweep Line
SearchingBinary SearchLower and Upper BoundSearch in a Rotated Sorted ArrayBinary Search on the AnswerTernary Search

Strongly Connected Components

Trace Tarjan discovery and low-link values as one DFS stack emits maximal mutually reachable components.

Core idea

Tarjan's DFS gives each vertex a discovery index and the earliest stacked vertex reachable from its subtree. When those two values match, the vertex is the root of one complete component.

Read the visualization

Badges show discovery and low-link values, a ring marks vertices still on the Tarjan stack, and each pop assigns one permanent component color.

00/012345

Discover this vertex, assign its discovery and low-link values, and push it on the stack.

1function tarjan(n: number, adj: number[][]): number[][] {
2 const dfn = Array(n).fill(-1), low = Array(n).fill(-1);
3 const onStk = Array(n).fill(false), stk: number[] = [];
4 const sccs: number[][] = []; let idx = 0;
5 const dfs = (u: number) => {
6 dfn[u] = low[u] = idx++; // enter u: dfn=low=enter between enter
7 stk.push(u); onStk[u] = true; // enter stack
8 for (const v of adj[u]) {
9 if (dfn[v] === -1) { // tree edge: v tree visit
10 dfs(v);
11 low[u] = Math.min(low[u], low[v]); // subtree return tree
12 } else if (onStk[v]) { // return edge: v tree stack middle
13 low[u] = Math.min(low[u], dfn[v]);
14 }
15 }
16 if (low[u] === dfn[u]) { // u back SCC root
17 const comp: number[] = []; let w;
18 do { w = stk.pop()!; onStk[w] = false; comp.push(w); }
19 while (w !== u); // scc stack to u → one SCC
20 sccs.push(comp);
21 }
22 };
23 for (let i = 0; i < n; i++) if (dfn[i] === -1) dfs(i);
24 return sccs;
25}
node / edge6 point / 7 directed edge
current node0(dfn=0, low=0)
stack[0]
searched SCC0
1 / 17

Complexity and tradeoffs

Time: O(V+E). Space: O(V). Each vertex enters and leaves the Tarjan stack once, and every directed edge is inspected once.

Invariant: A stacked vertex belongs to an unfinished DFS component, and its low-link is the earliest discovery reachable without leaving the stack.

Where it fits

SCC condensation turns any directed graph into a DAG. It supports cycle grouping, dependency analysis, reachability preprocessing, and the satisfiability test used by 2-SAT.