# Kosaraju 算法

**Kosaraju 算法**是一种两遍法，用于计算有向图的强连通分量（SCC）：先对图做一次深度优先搜索，记录各顶点的完成时间；再在边反向的（转置）图上做第二次深度优先搜索，按完成时间递减的顺序依次从各顶点出发，第二次搜索得到的每一棵树即构成一个强连通分量。对于含 \( V \) 个顶点、\( E \) 条边的图，其运行时间为 \( O(V + E) \)，常被称为 Kosaraju–Sharir 算法，因为 S. Rao Kosaraju 于 1978 年前后提出该方法却未发表，而 Micha Sharir 于 1981 年发表了等价的方法。<sup>[1](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/graph/strongly-connected-components.md)</sup><sup> • </sup><sup>[2](https://jeffe.cs.illinois.edu/teaching/algorithms/book/Algorithms-JeffE-2up.pdf)</sup><sup> • </sup><sup>[3](https://cris.tau.ac.il/en/publications/a-strong-connectivity-algorithm-and-its-applications-in-data-flow/)</sup>

| 关键事实 | 说明 |
|---|---|
| 解决的问题 | 将有向图的顶点划分为强连通分量，即由有向路径相互可达的极大顶点集合<sup>[1](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/graph/strongly-connected-components.md)</sup> |
| 方法 | 两次深度优先搜索：一次在 \( G \) 上记录完成时间，一次在转置图 \( G^T \) 上按完成时间递减的顺序进行；第二遍的每棵 DFS 树即一个强连通分量<sup>[1](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/graph/strongly-connected-components.md)</sup><sup> • </sup><sup>[4](https://cs.uwaterloo.ca/~eschost/publications/CS341-lec03-Eric.pdf)</sup>  |
| 运行时间 | \( O(n + m) \)，含构建转置图的时间<sup>[1](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/graph/strongly-connected-components.md)</sup><sup> • </sup><sup>[4](https://cs.uwaterloo.ca/~eschost/publications/CS341-lec03-Eric.pdf)</sup>  |
| 归属 | 由 S. Rao Kosaraju 于 1978 年前后提出（未发表）；Micha Sharir 独立提出，并于 1981 年发表<sup>[1](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/graph/strongly-connected-components.md)</sup><sup> • </sup><sup>[2](https://jeffe.cs.illinois.edu/teaching/algorithms/book/Algorithms-JeffE-2up.pdf)</sup><sup> • </sup><sup>[3](https://cris.tau.ac.il/en/publications/a-strong-connectivity-algorithm-and-its-applications-in-data-flow/)</sup>  |
| 输出顺序 | 各分量按凝聚图拓扑排序所对应的顺序输出<sup>[1](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/graph/strongly-connected-components.md)</sup> |
| 主要替代方案 | Tarjan 1972 年的算法在单次 DFS 中、无需转置图即可求出强连通分量，同样为线性时间<sup>[5](https://epubs.siam.org/doi/10.1137/0201010)</sup> |

## 算法步骤

有向图 \( G = (V, E) \) 的强连通分量，是这样一个极大顶点集合：其中任意一对顶点 \( u \) 与 \( v \)，都存在从 \( u \) 到 \( v \) 的路径，也存在从 \( v \) 到 \( u \) 的路径。转置图 \( G^T \) 拥有相同的顶点，但每条边的方向都反转。<sup>[4](https://cs.uwaterloo.ca/~eschost/publications/CS341-lec03-Eric.pdf)</sup>

整个过程分为三步：<sup>[1](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/graph/strongly-connected-components.md)</sup><sup> • </sup><sup>[4](https://cs.uwaterloo.ca/~eschost/publications/CS341-lec03-Eric.pdf)</sup>

1. 在 \( G \) 上依次进行深度优先搜索，直到所有顶点都被访问，并为每个顶点 \( v \) 记录其退出（完成）时间 \( t_{\text{out}}[v] \)，即从 \( v \) 可达的所有顶点都已探索完毕、搜索从 \( v \) 回溯的那一刻。各次搜索之间不重置时间戳计数器。
2. 构建转置图 \( G^T \)，在其上依次进行深度优先搜索，每次搜索都从剩余完成时间最大的未访问顶点出发。
3. 第二遍搜索所得 DFS 森林中的每一棵树，即作为一个强连通分量输出。

总运行时间为 \( O(n + m) \)，因为深度优先搜索执行了两次，构建转置图也是线性的；滑铁卢大学的讲义还提醒，不要忘记反转图所需的开销。<sup>[1](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/graph/strongly-connected-components.md)</sup><sup> • </sup><sup>[4](https://cs.uwaterloo.ca/~eschost/publications/CS341-lec03-Eric.pdf)</sup>

## 正确性

正确性建立在凝聚图的结构之上。凝聚图以 \( G \) 的各强连通分量为节点，当 \( G \) 中有一条边从分量 \( C \) 的某顶点指向分量 \( C' \) 的某顶点时，就有一条从 \( C \) 指向 \( C' \) 的弧。凝聚图必定无环，因为若各分量构成一个环，环上的顶点便相互可达，就会合并成一个更大的分量。<sup>[6](https://wiki.algo.informatik.tu-darmstadt.de/Kosaraju)</sup>

关键引理与完成时间有关：若凝聚图中存在一条从分量 \( C \) 指向分量 \( C' \) 的边，则 \( t_{\text{out}}[C] > t_{\text{out}}[C'] \)，其中分量的完成时间取其各顶点完成时间的最大值。证明按第一遍搜索先到达哪个分量分情况讨论；无论哪种情况，后到达的分量完成得都不更晚，因此凝聚图的每条边都从完成时间较大的分量指向完成时间较小的分量。<sup>[1](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/graph/strongly-connected-components.md)</sup>

由此可得两个推论，论证便告完成。其一，完成时间最大的顶点位于凝聚图的某个源分量中，即没有入边的分量。其二，\( G \) 与 \( G^T \) 的强连通分量完全相同，且 \( G^T \) 的凝聚图正是 \( G \) 的凝聚图的转置。在 \( G^T \) 中，该源分量没有指向未访问分量的边，因此从其中最后完成的顶点出发做深度优先搜索，恰好只访问它自身所在的分量，不会触及别的分量。删去这些顶点后重复此过程，便可逐个剥离出各分量。<sup>[1](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/graph/strongly-connected-components.md)</sup>

这种排序还有一个有用的副产品：第二遍搜索发现各分量的顺序，恰好对应凝聚图的一个拓扑排序，这正是依赖解析类应用所需要的。<sup>[1](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/graph/strongly-connected-components.md)</sup><sup> • </sup><sup>[7](https://learngraphtheory.org/articles/strongly-connected-components.html)</sup>

## 历史

S. Rao Kosaraju 是约翰斯·霍普金斯大学（Johns Hopkins University）的计算机科学家，1969 年获宾夕法尼亚大学（University of Pennsylvania）博士学位，他于 1978 年前后提出该方法，却从未发表，只作为未出版的课程材料流传。<sup>[8](https://www.cs.jhu.edu/~kosaraju/)</sup><sup> • </sup><sup>[2](https://jeffe.cs.illinois.edu/teaching/algorithms/book/Algorithms-JeffE-2up.pdf)</sup> 当时在纽约大学（New York University）的 Micha Sharir 于 1981 年在《计算机与数学应用》（*Computers and Mathematics with Applications*）第 7 卷第 1 期第 67–72 页发表了《一种强连通性算法及其在数据流分析中的应用》（"A strong-connectivity algorithm and its applications in data flow analysis"），给出构造全部强连通分量的线性算法，并将其用于数据流分析问题的迭代求解，改进了 Hecht–Ullman 算法。<sup>[3](https://cris.tau.ac.il/en/publications/a-strong-connectivity-algorithm-and-its-applications-in-data-flow/)</sup> 因此该方法同时归功于两人，在较为严谨的文献中称为 Kosaraju–Sharir 算法。<sup>[1](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/graph/strongly-connected-components.md)</sup> Aho、Hopcroft 与 Ullman 的教科书叙述也收录了该算法，因此 Boost 的文档将其变体称为 CLR / Aho–Hopcroft–Ullman 形式。<sup>[9](https://www.boost.org/doc/libs/develop/libs/graph/doc/html/graph/algorithms/connected_components/strong_components.html)</sup>

## 与 Tarjan 算法的比较

Robert Tarjan 1972 年的论文《深度优先搜索与线性图算法》（"Depth-first search and linear graph algorithms"，《SIAM 计算杂志》1(2):146–160）在单次深度优先搜索中求出强连通分量，维护一个低链值（low-link 值）和一个顶点栈，无需反向图，时间与空间均以 \( V \) 和 \( E \) 的线性为界。<sup>[5](https://epubs.siam.org/doi/10.1137/0201010)</sup> 在邻接表表示下，两种算法都以 \( O(n + m) \) 时间运行；Kosaraju 算法需要转置图，Tarjan 算法则不需要。<sup>[7](https://learngraphtheory.org/articles/strongly-connected-components.html)</sup>

二者的差别在于所呈现的信息。Kosaraju 算法的输出已经是凝聚图的一个拓扑序，而 Tarjan 算法按逆拓扑序输出各分量，汇点在前，因此将其输出反转即得相同顺序。<sup>[7](https://learngraphtheory.org/articles/strongly-connected-components.html)</sup> 从教学角度看，Kosaraju 算法的正确性论证更简短，仅依赖完成时间引理；而在大图的工业级代码中，以迭代方式编写的单遍算法可以省去第二份弧的副本。<sup>[7](https://learngraphtheory.org/articles/strongly-connected-components.html)</sup>

## 实现与实践要点

各库的支持情况并不统一。NetworkX 提供 `kosaraju_strongly_connected_components(G, source=None)`，将每个分量作为节点集合产出，对无向图会抛出 `NetworkXNotImplemented`；但其默认的 `strongly_connected_components` 采用的是 Tarjan 算法的迭代版本，并吸收了 Tarjan 与 Zwick 2024 年综述中的改进。<sup>[10](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.components.kosaraju_strongly_connected_components.html)</sup><sup> • </sup><sup>[11](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.components.strongly_connected_components.html)</sup> Boost.Graph 提供 `kosaraju_strong_components` 作为备选实现，文档称其比 Tarjan 算法慢一个常数因子、占用更多内存，并且由于内部要对图做转置，要求图模型满足可变图（Mutable Graph）概念。<sup>[9](https://www.boost.org/doc/libs/develop/libs/graph/doc/html/graph/algorithms/connected_components/strong_components.html)</sup>

文献中反复出现两点实现上的告诫。递归写法在长路径上可能导致调用栈溢出，因此无论使用何种语言，超大图都应以显式栈处理。<sup>[7](https://learngraphtheory.org/articles/strongly-connected-components.html)</sup> 此外，深度优先搜索本身难以并行化：John H. Reif 于 1985 年证明，计算字典序 DFS 序是 P-完全的，这有力说明不存在快速的并行算法，从而促使人们研究并行的强连通分量方法，例如 Lisa K. Fleischer、Bruce Hendrickson 与 Ali Pinar 于 2000 年提出的分治方法：选取一个枢轴点，求该点可达的集合与可达该点的集合之交，再对剩余部分递归处理。<sup>[7](https://learngraphtheory.org/articles/strongly-connected-components.html)</sup>

## References

1. [Strongly connected components, cp-algorithms](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/graph/strongly-connected-components.md)
2. [Jeff Erickson, Algorithms, section 6.6, Strong Components in Linear Time](https://jeffe.cs.illinois.edu/teaching/algorithms/book/Algorithms-JeffE-2up.pdf)
3. [M. Sharir (1981). A strong-connectivity algorithm and its applications in data flow analysis. Computers and Mathematics with Applications 7(1):67-72, Tel Aviv University research portal](https://cris.tau.ac.il/en/publications/a-strong-connectivity-algorithm-and-its-applications-in-data-flow/)
4. [CS 341: Algorithms, University of Waterloo lecture notes on Kosaraju's algorithm](https://cs.uwaterloo.ca/~eschost/publications/CS341-lec03-Eric.pdf)
5. [Robert 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)
6. [Kosaraju, Algowiki, TU Darmstadt](https://wiki.algo.informatik.tu-darmstadt.de/Kosaraju)
7. [Strongly Connected Components: Tarjan's and Kosaraju's Algorithms, learngraphtheory.org](https://learngraphtheory.org/articles/strongly-connected-components.html)
8. [S. Rao Kosaraju, home page, Johns Hopkins University](https://www.cs.jhu.edu/~kosaraju/)
9. [Boost.Graph strong_components documentation](https://www.boost.org/doc/libs/develop/libs/graph/doc/html/graph/algorithms/connected_components/strong_components.html)
10. [kosaraju_strongly_connected_components, NetworkX 3.7 documentation](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.components.kosaraju_strongly_connected_components.html)
11. [strongly_connected_components, NetworkX 3.7 documentation](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.components.strongly_connected_components.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 19, 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/kosarajus-algorithm

English original: https://www.edgepedia.org/kosarajus-algorithm
