# 霍普克罗夫特-卡普算法

霍普克罗夫特-卡普算法由约翰·E·霍普克罗夫特（John E. Hopcroft）与理查德·M·卡普（Richard M. Karp）于1973年首次发表，它在一个有n个顶点、m条边的二分图中构造最大基数匹配，需要O(m√n)步——即运行时间与(m + n)√n成正比。<sup>[1](https://epubs.siam.org/doi/10.1137/0202019)</sup> 它改进了朴素的增广路径方法——后者耗时\( O(mn) \)——改为在单趟中找出多条增广路径，而非每次只找一条。<sup>[2](https://arxiv.org/abs/2312.12584)</sup>

| 关键事实 | 详情 |
|---|---|
| 发表信息 | 《SIAM计算杂志》第2卷第4期，第225–231页，1973年12月；1972年11月14日投稿<sup>[1](https://epubs.siam.org/doi/10.1137/0202019)</sup> |
| 解决的问题 | 二分图中的最大基数匹配，等价于单位顶点容量网络中的最大流<sup>[3](https://arxiv.org/abs/1105.1569)</sup> |
| 运行时间 | \( O((m+n)\sqrt{n}) \)，或 \( O(\sqrt{\kappa}\,m) \)，其中κ为最大匹配的大小<sup>[1](https://epubs.siam.org/doi/10.1137/0202019)</sup><sup> • </sup><sup>[3](https://arxiv.org/abs/1105.1569)</sup> |
| 阶段数 | 至多 \( 2\sqrt{n} \) 次迭代，每次先做一次BFS再做一次DFS，每次耗时 \( O(m) \)<sup>[4](https://algorithms.discrete.ma.tum.de/graph-algorithms/matchings-hopcroft-karp/index_en.html)</sup> |
| 正确性基础 | 贝尔热引理：一个匹配是最大匹配当且仅当不存在增广路径<sup>[4](https://algorithms.discrete.ma.tum.de/graph-algorithms/matchings-hopcroft-karp/index_en.html)</sup> |
| 软件实现 | 在SciPy的`maximum_bipartite_matching`和NetworkX的`hopcroft_karp_matching`中实现<sup>[5](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csgraph.maximum_bipartite_matching.html)</sup><sup> • </sup><sup>[6](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.bipartite.matching.hopcroft_karp_matching.html)</sup> |

## 所解决的问题

二分图的顶点划分成两个不相交的集合，边只存在于两个集合之间。匹配是一组互不共享顶点的边，最大匹配则是边数尽可能多的匹配。原论文用一个矩形阵列中的已占据格子来引出这个问题：一个独立集中的任意两个格子都不在同一行或同一列；一种解读是，行代表男孩，列代表女孩，已占据的格子表示一对彼此契合的男女，目标是尽可能多地配成契合的伴侣。<sup>[7](https://web.eecs.umich.edu/~pettie/matching/Hopcroft-Karp-bipartite-matching.pdf)</sup>

该问题等价于在一个关联的简单二分网络上的最大流问题，因此任何最大流算法都能解决它；同一个网络还可用于二分图的顶点覆盖与稳定集问题。<sup>[3](https://arxiv.org/abs/1105.1569)</sup>

## 算法的工作原理

朴素的增广路径算法反复寻找单条增广路径——即一条交替路径，起点和终点都是未匹配（自由）的顶点，若翻转其边，匹配便会扩大。根据贝尔热引理，一个匹配是最大匹配，恰好当不再存在增广路径，因此算法终止时得到的答案是正确的。<sup>[4](https://algorithms.discrete.ma.tum.de/graph-algorithms/matchings-hopcroft-karp/index_en.html)</sup> 它的弱点在于：它可能先探索较长的增广路径，并且每做一次增广就要付出一次全图搜索的代价。<sup>[8](https://tamnd.github.io/brain/programming/algorithms/12/12.10/)</sup>

霍普克罗夫特-卡普算法修正了这一浪费之处：在一个阶段中处理多条最短增广路径。<sup>[9](https://www.cs.cornell.edu/courses/cs6820/2023fa/handouts/matchings.pdf)</sup> 每个阶段分两步：

1. **广度优先搜索。** 从二分图一侧的所有自由顶点出发，BFS对每个顶点计算到达它的最短增广路径的长度，并将边分类为前进边或后退边。前进边构成一个分层图，所有最短增广路径都位于这个分层图之中。<sup>[9](https://www.cs.cornell.edu/courses/cs6820/2023fa/handouts/matchings.pdf)</sup>
2. **深度优先搜索。** 在分层图中做DFS，找出一个由顶点互不相交的最短增广路径构成的极大集合——这与阻塞流相对应——然后沿着所有这些路径同时增广匹配。<sup>[9](https://www.cs.cornell.edu/courses/cs6820/2023fa/handouts/matchings.pdf)</sup><sup> • </sup><sup>[4](https://algorithms.discrete.ma.tum.de/graph-algorithms/matchings-hopcroft-karp/index_en.html)</sup>

分层图的构造正是让所有最短增广路径得以同时被找到的关键；在最坏情况下，每条边被检查一次，以确定这组路径。<sup>[4](https://algorithms.discrete.ma.tum.de/graph-algorithms/matchings-hopcroft-karp/index_en.html)</sup>

## 为什么运行时间是O(m√n)

两个事实给出这个界。第一，最短增广路径的长度从一个阶段到下一个阶段严格递增，因此在√n个阶段之后，任何剩余的增广路径长度至少为√n。第二，若最短增广路径的长度为l，则最优匹配比当前匹配至多超出n/(l+1)，因为剩余的顶点不相交增广路径每条至少使用n个顶点中的l+1个。一旦l达到√n，至多还需要√n次增广，因此总共至多有2√n个阶段。<sup>[4](https://algorithms.discrete.ma.tum.de/graph-algorithms/matchings-hopcroft-karp/index_en.html)</sup> 每个阶段耗时O(m)，因为BFS和DFS各自将每条边考虑一次，所以总耗时为O(m√n)。<sup>[4](https://algorithms.discrete.ma.tum.de/graph-algorithms/matchings-hopcroft-karp/index_en.html)</sup> 用κ表示最大匹配的大小，它以二分图中较小一侧的顶点数n₁为上界，同一个界可写作O(√κ·m)。<sup>[3](https://arxiv.org/abs/1105.1569)</sup>

## 应用与地位

该算法是实践中的标准工具。SciPy的`maximum_bipartite_matching`实现了霍普克罗夫特-卡普算法，时间为\( O(|E|\sqrt{|V|}) \)，空间与行数成线性关系，并指出当输入的列数多于行数时，转置输入会有所帮助；根据柯尼希定理，它返回的匹配大小也等于一个最小顶点覆盖的大小。<sup>[5](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csgraph.maximum_bipartite_matching.html)</sup> NetworkX以`hopcroft_karp_matching`的名字提供同一算法，它是`maximum_matching`的别名，以字典形式返回匹配。<sup>[6](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.bipartite.matching.hopcroft_karp_matching.html)</sup>

在对八种基于增广路径的二分匹配算法进行的实证比较中，采用达夫-维贝格修正的霍普克罗夫特-卡普算法与采用公平策略的波滕-范算法是表现最好的两种方法，但在所有输入上没有单一的胜出者；贪心初始化，尤其是卡普-西普瑟或最小度初始化，对性能有显著影响。<sup>[10](https://www.osti.gov/servlets/purl/1118557)</sup> 后来的工作表明，伪流算法的思想可以移植到霍普克罗夫特-卡普算法中，同时保持相同的复杂度。<sup>[3](https://arxiv.org/abs/1105.1569)</sup>

## 后来的更快算法

几十年来，霍普克罗夫特-卡普算法一直是二分匹配最快的算法，只有极稠密的图例外——在那种图上，快速矩阵乘法可给出\( O(n^{2.371}) \)的时间。<sup>[2](https://arxiv.org/abs/2312.12584)</sup> 2013年，蒙德里（Mądry）引入了一种连续方法，在稀疏图上快得多；这条路线在2022年陈（Chen）及其合作者的结果中达到顶峰，他们实现了\( m^{1+o(1)} \)时间的最大二分匹配，更一般地，也实现了最小费用流。<sup>[2](https://arxiv.org/abs/2312.12584)</sup> 2023年的一项纯组合算法运行时间为\( \widetilde{O}(m^{1/3}n^{5/3}) \)，在中等稠密图上同时优于霍普克罗夫特-卡普算法和矩阵乘法方法。<sup>[2](https://arxiv.org/abs/2312.12584)</sup>

## References

1. [John E. Hopcroft and Richard M. Karp (1973). An n^{5/2} Algorithm for Maximum Matchings in Bipartite Graphs. SIAM Journal on Computing 2(4):225–231.](https://epubs.siam.org/doi/10.1137/0202019)
2. [A Faster Combinatorial Algorithm for Maximum Bipartite Matching. arXiv:2312.12584.](https://arxiv.org/abs/2312.12584)
3. [Practical and theoretical improvements for bipartite matching using the pseudoflow algorithm. arXiv:1105.1569.](https://arxiv.org/abs/1105.1569)
4. [The Hopcroft-Karp Algorithm, TU Munich algorithms visualization site.](https://algorithms.discrete.ma.tum.de/graph-algorithms/matchings-hopcroft-karp/index_en.html)
5. [scipy.sparse.csgraph.maximum_bipartite_matching, SciPy v1.18.0 Manual.](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csgraph.maximum_bipartite_matching.html)
6. [hopcroft_karp_matching, NetworkX 3.7 documentation.](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.bipartite.matching.hopcroft_karp_matching.html)
7. [Hopcroft–Karp original paper PDF, SIAM J. Comput. 2(4), 1973.](https://web.eecs.umich.edu/~pettie/matching/Hopcroft-Karp-bipartite-matching.pdf)
8. [12.10 Hopcroft-Karp Algorithm, brain (tamnd).](https://tamnd.github.io/brain/programming/algorithms/12/12.10/)
9. [1.4 The Hopcroft-Karp algorithm, Cornell CS 6820 lecture notes (2023).](https://www.cs.cornell.edu/courses/cs6820/2023fa/handouts/matchings.pdf)
10. [Multithreaded Algorithms for Maximum Matching in Bipartite Graphs, OSTI.](https://www.osti.gov/servlets/purl/1118557)

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

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

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/hopcroft-karp-algorithm

English original: https://www.edgepedia.org/hopcroft-karp-algorithm
