{"id":265,"date":"2026-11-13T12:36:21","date_gmt":"2026-11-13T04:36:21","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-tarjan-finds-strongly-connected-components\/"},"modified":"2026-11-13T21:54:00","modified_gmt":"2026-11-13T13:54:00","slug":"how-tarjan-finds-strongly-connected-components","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-tarjan-finds-strongly-connected-components\/","title":{"rendered":"How Tarjan finds strongly connected components"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Tarjan&#8217;s strongly connected components<\/strong> algorithm is one depth-first search. Each vertex carries a discovery index and a <strong>low-link<\/strong>, the smallest index reachable from its subtree by tree edges and at most one back edge. Vertices sit on a stack until their component is emitted. When a vertex&#8217;s low-link equals its own index, it is the root of a component, and the stack down to that vertex is the component. The time is <strong>\u0398(V+E)<\/strong>.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Two vertices are strongly connected when each can reach the other by directed paths. The strongly connected components are the maximal such sets. Condensing each component to a vertex leaves a DAG. The algorithmic problem is to find the sets in one pass, without building the transpose graph.<\/p>\n\n\n<p class=\"wp-block-paragraph\">A depth-first search already knows which vertices are on the path from the root of the current tree. A back edge can reach one of them. The low-link summarizes the oldest discovery index the subtree can see. A vertex that cannot see anything older than itself is the first vertex of its component in this search.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The algorithm assigns each vertex to exactly one component and runs in \u0398(V+E) time and \u0398(V) extra memory, including the recursion stack. The components are emitted in reverse topological order of the condensation DAG: an edge between components points from a later-emitted component toward an earlier one, or the implementation&#8217;s stack order produces the reverse, and a caller who needs a topological order of the condensation can reverse the emission list. Within a component the order of names is the stack order, not a special order.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The low-link update uses a successor that is on the stack, not every gray or black vertex. A cross edge to a vertex already assigned to a finished component must not pull the low-link down. That restriction is the usual bug.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Initialize index = 0, an empty stack, and every vertex unvisited.<\/li>\n<li>From each unvisited vertex, recurse. On entry, set both index and low-link to the clock, increment the clock, and push the vertex.<\/li>\n<li>For each successor, recurse if it is unvisited, then set low-link to the minimum of itself and the successor&#8217;s low-link. If the successor is already on the stack, set low-link to the minimum of itself and the successor&#8217;s index.<\/li>\n<li>When the successor list is done and low-link equals index, pop the stack through this vertex. Those popped vertices are one strongly connected component.<\/li>\n<li>A vertex popped in an earlier component is not on the stack. An edge to it does not update low-link.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The single pass needs the stack test. Kosaraju&#8217;s algorithm computes the same partition with two depth-first searches and an explicit transpose, which is easier to see and uses the same linear time. Neither algorithm orders vertices inside a component. The condensation is the DAG that a later topological sort can schedule.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when low-link follows a finished component<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Minimize low-link over every visited successor, including popped ones<\/td>\n<td>A cross edge into an earlier component pulls the low-link below the true root. Distinct components merge.<\/td>\n<\/tr>\n<tr>\n<td>Pop only the root and call it the component<\/td>\n<td>The component is the contiguous stack segment from the root to the top. Members sit above the root.<\/td>\n<\/tr>\n<tr>\n<td>Omit the outer loop over unvisited vertices<\/td>\n<td>A vertex the first tree cannot reach never joins a component.<\/td>\n<\/tr>\n<tr>\n<td>Use low-link equality with the parent index instead of the vertex&#8217;s own index<\/td>\n<td>The root test is low-link = discovery index of this vertex. The parent&#8217;s index is a different number.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Two passes and a transpose are Kosaraju. The DAG of components, once known, is what topological sort orders. Bridges and articulation points of an undirected graph use a related but different low value and are not this algorithm.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment computes Tarjan&#8217;s low-link. A root of a strongly connected component is a node whose low-link equals its discovery index. The graph 0\u21921\u21922\u21920 and 2\u21923 has components {0, 1, 2} and {3}. It prints <code>2 1<\/code>.<\/p>\n\n<pre><code class=\"language-python\">adj = [[1], [2], [0, 3], []]\ndisc = [-1] * 4\nlow = [-1] * 4\nstack, on = [], [False] * 4\ntime = 0\ncomps = []\n\ndef dfs(u):\n    global time\n    disc[u] = low[u] = time\n    time += 1\n    stack.append(u)\n    on[u] = True\n    for v in adj[u]:\n        if disc[v] &lt; 0:\n            dfs(v)\n            low[u] = min(low[u], low[v])\n        elif on[v]:\n            low[u] = min(low[u], disc[v])\n    if low[u] == disc[u]:\n        comp = []\n        while True:\n            v = stack.pop()\n            on[v] = False\n            comp.append(v)\n            if v == u:\n                break\n        comps.append(comp)\n\nfor u in range(4):\n    if disc[u] &lt; 0:\n        dfs(u)\nsizes = sorted(len(c) for c in comps)\nassert sizes == [1, 3]\nprint(*sizes)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Tarjan&#8217;s strongly connected components condense a call graph or a dependency graph, and they are the components a 2-SAT solver needs. Compilers use this condensation. Kosaraju&#8217;s algorithm computes it with a transpose pass instead of one depth-first search.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop Tarjan&#8217;s one-pass structure if a transpose and two simpler searches are easier to audit. Kosaraju returns the same components. Keep Tarjan when the graph is huge, the transpose is inconvenient, or one recursion must emit the components.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for Tarjan&#8217;s strongly connected components returns the low-link root test and the rule that finished components stay off the stack. <strong>Tarjan&#8217;s algorithm finds strongly connected components in one depth-first search by popping the stack when a vertex&#8217;s low-link equals its discovery index.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Tarjan&#8217;s strongly connected components algorithm is one depth-first search. Each vertex carries a discovery index and a low-link, the smallest index reachable from its subtree by tree edges and at most one back edge. Vertices sit on a stack until their component is emitted. When a vertex&#8217;s&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,32],"tags":[],"class_list":["post-265","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-graphs"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=265"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/265\/revisions"}],"predecessor-version":[{"id":664,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/265\/revisions\/664"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}