# 塔扬强连通分量算法

**塔扬强连通分量算法**（Tarjan's strongly connected components algorithm）是罗伯特·恩德雷·塔扬（Robert Endre Tarjan）于1972年发表的一种深度优先搜索算法，能在一次遍历中以与顶点数和边数成线性关系的时间找出有向图的全部强连通分量。

在有向图中，强连通分量（SCC）是尽可能大的一个顶点集合，其中每个成员都能沿有向边到达其他任何一个成员；不在任何有向环上的顶点独自构成一个分量。1972年之前，已知的方法可能需要对每个顶点各做一次搜索，在稀疏图上给出二次时间。塔扬的成果发表在《SIAM计算杂志》第1卷第2期第146–160页（1971年8月30日投稿），表明只需一次搜索的代价就能找出所有分量，其空间和时间以 \( k_1 V + k_2 E + k_3 \) 为界，其中 \( k_1, k_2, k_3 \) 为常数，\( V \) 是顶点数，\( E \) 是边数。<sup>[1](https://epubs.siam.org/doi/10.1137/0201010)</sup><sup> • </sup><sup>[2](https://learngraphtheory.org/articles/strongly-connected-components.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 发表 | 罗伯特·恩德雷·塔扬，《深度优先搜索与线性图算法》，《SIAM计算杂志》1(2)：146–160，1972年6月；DOI 10.1137/0201010<sup>[1](https://epubs.siam.org/doi/10.1137/0201010)</sup> |
| 时间复杂度 | \( O(V + E) \)，这是最优的，因为任何算法都必须读取整个图<sup>[3](https://learngraphtheory.org/algorithm/tarjan-scc)</sup> |
| 遍历次数 | 对原图做一次深度优先搜索；不需要转置图<sup>[3](https://learngraphtheory.org/algorithm/tarjan-scc)</sup> |
| 输出顺序 | 各分量按凝聚图的逆拓扑序输出（汇点优先）<sup>[3](https://learngraphtheory.org/algorithm/tarjan-scc)</sup> |
| 最坏情况存储 | 在改进之前，字长为 \( w \) 比特时需要 \( v(2+5w) \) 比特<sup>[4](https://www.sciencedirect.com/science/article/abs/pii/S0020019015001532)</sup> |
| 主要替代方案 | 科萨拉朱–沙里尔算法（Kosaraju–Sharir，两遍遍历，需要转置图）和加博路径算法（Gabow，一遍遍历，两个栈）<sup>[3](https://learngraphtheory.org/algorithm/tarjan-scc)</sup><sup> • </sup><sup>[5](https://www.cs.princeton.edu/courses/archive/spr09/cos423/Lectures/bi-gabow.pdf)</sup> |
| 库支持情况 | NetworkX 使用经努蒂拉（Nuutila）修改的非递归塔扬算法；SciPy 的强连通模式引用了塔扬–兹维克（Tarjan–Zwick）2024年的综述<sup>[2](https://learngraphtheory.org/articles/strongly-connected-components.html)</sup><sup> • </sup><sup>[6](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csgraph.connected_components.html)</sup> |

## 算法工作原理

一次深度优先遍历驱动整个算法：每个顶点获得一个发现索引和一个低链值，后者定义为该顶点的子树通过至多一条回边到达仍在栈上的顶点所能触及的最小索引。顶点在被访问时压入栈中。若某顶点完成时其低链值等于自身的索引，该顶点即为分量的根，栈中位于它之上的顶点一并弹出，构成该分量。<sup>[3](https://learngraphtheory.org/algorithm/tarjan-scc)</sup>

在栈检查至关重要。如果一条边指向一个已访问且已被划入已完成分量的顶点，算法必须完全忽略它；跳过这一检查会让低链值在分量之间蔓延，当一条边指向已完成强连通分量中的顶点时可能产生错误结果，而指向仍在栈上的顶点的交叉边不会引发这种失败。<sup>[10](https://www.cs.cmu.edu/~15451-f20/Handouts/Sleator-strong-components.pdf)</sup> 每个被弹出归入分量的顶点还必须清除其在栈标志，否则后续的回边检查会对已不在栈上的顶点触发，悄然破坏后续分量。<sup>[3](https://learngraphtheory.org/algorithm/tarjan-scc)</sup>

每个顶点访问一次，每条边检查一次，每个顶点恰好压栈和弹栈各一次，因此时间为 \( O(V + E) \)，空间为 \( O(V) \)。<sup>[3](https://learngraphtheory.org/algorithm/tarjan-scc)</sup>

## 起源与背景

1972年的论文在提出SCC算法的同时，还给出了无向图双连通分量的算法，用以说明深度优先搜索（或称“回溯”）是解决图问题的一般性技术。<sup>[1](https://epubs.siam.org/doi/10.1137/0201010)</sup> 更早的工作是在传递闭包计算中遇到SCC的：珀多姆（Purdom，1970）和芒罗（Munro，1971）将环收缩，使可达性只需在更小的凝聚图上计算。塔扬的贡献在于从一次深度优先搜索中读出各分量，而非做更多次搜索。<sup>[2](https://learngraphtheory.org/articles/strongly-connected-components.html)</sup>

在搜索过程中收缩环的思想——路径类算法家族的基础——在珀多姆和芒罗的工作中已经存在；迪杰斯特拉（Dijkstra，1976）、切里扬与梅尔霍恩（Cheriyan and Mehlhorn，1996）以及加博（Gabow，2000）将其精炼为通常引用的形式。<sup>[2](https://learngraphtheory.org/articles/strongly-connected-components.html)</sup> 2024年，塔扬与乌里·兹维克（Uri Zwick）在《欧洲组合学杂志》（DOI 10.1016/j.ejc.2023.103815）发表了综述《用深度优先搜索寻找强分量》，将低链家族和路径家族与一种双向搜索方法并列讨论。<sup>[6](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csgraph.connected_components.html)</sup><sup> • </sup><sup>[2](https://learngraphtheory.org/articles/strongly-connected-components.html)</sup>

## 与其他线性时间算法的比较

由于强连通分量的三种线性时间算法具有相同的渐近代价，在它们之间选择取决于常数因子、内存使用以及保证实现正确的难易程度。<sup>[3](https://learngraphtheory.org/algorithm/tarjan-scc)</sup>

- **科萨拉朱–沙里尔算法**执行两次深度优先搜索，第二次按完成时间递减的顺序在反向（转置）图上进行，并按拓扑序报告分量。虽然它是在白板上推演起来最简单的算法，但需要额外的 O(V+E) 空间保存转置图的副本，否则必须在执行过程中构造它，这会使缓存流量翻倍。<sup>[2](https://learngraphtheory.org/articles/strongly-connected-components.html)</sup><sup> • </sup><sup>[7](https://unseel.com/cs/tarjan-scc)</sup>
- **塔扬算法**使用低链记账和单个分量栈执行一次遍历，避免任何反向图，在实践中通常被证明是最快的；对于能放入内存但超出缓存的大图，一遍完成所有操作相对于科萨拉朱算法在挂钟时间上大约有2倍的加速。<sup>[3](https://learngraphtheory.org/algorithm/tarjan-scc)</sup><sup> • </sup><sup>[7](https://unseel.com/cs/tarjan-scc)</sup>
- **加博路径算法**（《信息处理快报》74卷，2000年，第107–114页）是一种一遍算法，仅维护深度优先搜索路径的表示，使用两个栈而非整数低链字段。科罗拉多大学博尔德分校的哈罗德·N·加博（Harold N. Gabow）证明，仅用栈和数组，更简单的路径视角就足以实现线性时间；与基于树的算法逐行比较伪代码，显示出相近的底层资源使用。<sup>[5](https://www.cs.princeton.edu/courses/archive/spr09/cos423/Lectures/bi-gabow.pdf)</sup><sup> • </sup><sup>[7](https://unseel.com/cs/tarjan-scc)</sup>

若要凝聚图的拓扑序，科萨拉朱的输出可直接使用，或将塔扬的输出反转。<sup>[2](https://learngraphtheory.org/articles/strongly-connected-components.html)</sup>

## 空间优化与实现实践

在最坏情况下，塔扬算法需要 \( v(2+5w) \) 比特存储，其中 \( w \) 是机器的字长。努蒂拉与索伊萨隆-索伊宁（Nuutila and Soisalon-Soininen，1994）通过不存储最终为单顶点分量的顶点（这在稀疏图中很常见），将其降至 \( v(1+4w) \)。皮尔斯（Pearce，2016）进一步将最坏情况存储降至 \( v(1+3w) \) 比特，该算法被独立集成到 SciPy 库中。<sup>[4](https://www.sciencedirect.com/science/article/abs/pii/S0020019015001532)</sup><sup> • </sup><sup>[2](https://learngraphtheory.org/articles/strongly-connected-components.html)</sup>

递归深度是一个实际限制：塔扬算法天然是递归的，深度即最长路径的长度，因此在百万顶点的路径上递归搜索会溢出 C、Java 以及 Python 的默认调用栈。大图上的生产代码使用显式栈的迭代改写，其中完成的顶点将其低链值合并到父顶点的低链值中，这正是递归版本在每次调用返回后应用的树弧规则。<sup>[2](https://learngraphtheory.org/articles/strongly-connected-components.html)</sup>

## 并行计算

深度优先搜索难以并行化：赖夫（Reif）在1985年证明了寻找字典序DFS排序是P完全的，这强烈表明不存在针对它的高效并行算法，使得塔扬算法在并行环境中价值存疑。并行SCC实现转而建立在弗莱舍、亨德里克森与皮纳尔（Fleischer, Hendrickson, and Pinar，2000）的前向–后向方法之上：选取一个枢轴顶点，将它可达的集合与可达它的集合求交，得到枢轴所在的分量，然后对剩余三个部分独立递归；可达性并行化效果良好。<sup>[2](https://learngraphtheory.org/articles/strongly-connected-components.html)</sup><sup> • </sup><sup>[8](https://escholarship.org/uc/item/1hx5n2df)</sup>

在动态图中，情况又有所不同。用塔扬1972年的算法在静态图中计算SCC需要 \( O(m+n) \) 时间，但条件下界给出证据表明，完全动态的更新时间无法比每次更新后从头重算在多项式因子上有所改进；相反，在递减图上已取得实质性进展，翁茨基（Łącki）与罗迪蒂–兹维克（Roditty–Zwick）一系列工作最终形成了以近线性总更新时间维护SCC的算法。<sup>[9](https://arxiv.org/abs/1901.03615)</sup>

## 应用

塔扬算法有众多用途，常作为更大算法的子组件，例如传递闭包、编译器优化、程序分析和互模拟等价等算法。<sup>[4](https://www.sciencedirect.com/science/article/abs/pii/S0020019015001532)</sup> 一个应用是将有向图归约为其分量的有向无环图（DAG），这成为求解2-SAT的开局步骤；这些分量在分析调用图、在编译器中消除死代码、检测死锁、揭示包管理器和电子表格中的相互依赖，以及识别链接有向的社交网络中的社区结构方面也很有用。<sup>[3](https://learngraphtheory.org/algorithm/tarjan-scc)</sup> 在有限马尔可夫链中，强连通分量就是该链的互通类，而汇分量恰好是闭的常返类。<sup>[2](https://learngraphtheory.org/articles/strongly-connected-components.html)</sup>

## References

1. [Robert Endre Tarjan (1972). "Depth-first search and linear graph algorithms." SIAM Journal on Computing 1(2): 146–160.](https://epubs.siam.org/doi/10.1137/0201010)
2. [Strongly Connected Components: Tarjan & Kosaraju, learngraphtheory.org](https://learngraphtheory.org/articles/strongly-connected-components.html)
3. [Tarjan's SCC Finder: Strongly Connected Components, learngraphtheory.org](https://learngraphtheory.org/algorithm/tarjan-scc)
4. [Pearce (2016). "A space-efficient algorithm for finding strongly connected components." Information Processing Letters (ScienceDirect).](https://www.sciencedirect.com/science/article/abs/pii/S0020019015001532)
5. [Harold N. Gabow (2000). "Path-based depth-first search for strong and biconnected components." Information Processing Letters 74: 107–114.](https://www.cs.princeton.edu/courses/archive/spr09/cos423/Lectures/bi-gabow.pdf)
6. [scipy.sparse.csgraph.connected_components, SciPy v1.18.0 Manual](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csgraph.connected_components.html)
7. [Tarjan's Strongly Connected Components — How It Works & Code, Unseel](https://unseel.com/cs/tarjan-scc)
8. [A divide-and-conquer algorithm for identifying strongly connected components, eScholarship](https://escholarship.org/uc/item/1hx5n2df)
9. [Decremental Strongly-Connected Components and Single-Source Reachability in Near-Linear Time, arXiv:1901.03615](https://arxiv.org/abs/1901.03615)
10. [Sleator Strong Components.pdf, cs.cmu.edu](https://www.cs.cmu.edu/~15451-f20/Handouts/Sleator-strong-components.pdf)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Sep 30, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 逻辑与离散数学 › General discrete mathematics and discrete structures › Graph theory › Computational graph problems and algorithms › Connectivity and connected-component computation

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/tarjans-strongly-connected-components-algorithm

English original: https://www.edgepedia.org/tarjans-strongly-connected-components-algorithm
