# 匈牙利算法

匈牙利算法是一种组合优化方法，用于求解线性求和分配问题：给定一个 n×n 的成本或评分矩阵，它找出将行与列一一对应、使总成本最小（或总评分最大）的分配方案。[哈罗德·W·库恩](https://www.edgepedia.org/zh/harold-w-kuhn)（Harold W. Kuhn）当时是[布林莫尔学院](https://www.edgepedia.org/zh/bryn-mawr-college)（Bryn Mawr College）的数学家，在普林斯顿参与海军研究办公室（ONR）后勤项目，于1955年3月在《海军研究后勤季刊》（*Naval Research Logistics Quarterly*）上以“匈牙利方法”之名发表了这一方法，因为它利用了两位匈牙利数学家[德内斯·柯尼希](https://www.edgepedia.org/zh/denes-konig)（Dénes Kőnig）和耶诺·埃格瓦里（Jenő Egerváry）工作中潜藏的思想，而这些思想比线性规划早了十五年以上。<sup>[1](https://www.math.utoronto.ca/mccann/1855/KuhnNRL55.pdf)</sup><sup> • </sup><sup>[2](https://www.bibsonomy.org/pow-challenge/?return=https%3A%2F%2Fwww.bibsonomy.org%2Fbibtex%2F20bbf339729b509a2836d225e7dd174bb%2Fgergie)</sup> 该算法也被称为库恩–蒙克雷斯算法或蒙克雷斯算法。<sup>[3](https://static.scipy.org/doc/scipy-1.2.3/scipy-ref-1.2.3.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 解决的问题 | 线性求和分配问题，等价于二分图中的最小权匹配：找出工人到工作的完整分配，使成本最小<sup>[4](https://scipy.github.io/devdocs/reference/generated/scipy.optimize.linear_sum_assignment.html)</sup> |
| 原始发表 | 哈罗德·W·库恩，《海军研究后勤季刊》2(1–2): 83–97，1955年3月<sup>[2](https://www.bibsonomy.org/pow-challenge/?return=https%3A%2F%2Fwww.bibsonomy.org%2Fbibtex%2F20bbf339729b509a2836d225e7dd174bb%2Fgergie)</sup> |
| 名称由来 | 建立在柯尼希和埃格瓦里的工作之上，比线性规划早十五年以上<sup>[1](https://www.math.utoronto.ca/mccann/1855/KuhnNRL55.pdf)</sup> |
| 时间复杂度 | 最初表述为 O(n⁴)；经埃德蒙兹与卡普以及富泽改进后为 O(n³)；对于 n×m 的矩形问题为 O(n²m)<sup>[5](https://sites.math.washington.edu/~thomas/teaching/m514_web/50YearsIP.pdf)</sup><sup> • </sup><sup>[6](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/graph/hungarian-algorithm.md)</sup> |
| 正确性 | 基于库恩–蒙克雷斯定理，是线性规划对偶的一个实例<sup>[7](https://algorithms.discrete.ma.tum.de/matching/hungarian-method/)</sup> |
| 现代实现 | SciPy 的 `linear_sum_assignment` 改用了一种改进的容克–福尔根南特算法<sup>[4](https://scipy.github.io/devdocs/reference/generated/scipy.optimize.linear_sum_assignment.html)</sup> |
| 著名的现代应用 | DETR 风格目标检测器中预测与真值之间的二分匹配<sup>[8](https://arxiv.org/abs/2005.12872)</sup> |

## 它所解决的问题

库恩将问题表述如下：n 个人中的每一个对 n 项工作中每一项的完成情况都有一个数值评分，任务是将人与工作配对，使 n 个评分的总和最大化。<sup>[1](https://www.math.utoronto.ca/mccann/1855/KuhnNRL55.pdf)</sup> 一个分配是整数 1 到 n 的一个排列：每个人恰好得到一项工作，且没有工作被分配给两个不同的人。以成本形式表示，目标是在布尔矩阵 X 上最小化 \( \sum_i \sum_j C_{i,j} X_{i,j} \)，其中每一行恰好分配给一列，每一列恰好分配给一行；该问题也被称为二分图中的最小权匹配。<sup>[4](https://scipy.github.io/devdocs/reference/generated/scipy.optimize.linear_sum_assignment.html)</sup>

这个问题很古老。卡尔·古斯塔夫·雅各比（Carl Gustav Jacobi）在19世纪试图界定一个微分方程组的次数时遇到了它，并找到了计算该界的一个好算法，库恩后来称这一进展完全现代、在其时代背景下显得格格不入。<sup>[9](https://www.math.utoronto.ca/mccann/1855/KuhnEJOR12.pdf)</sup> 分配问题可以表述为线性规划，这一点最早由 D. F. Votaw 和 A. Orden 在1952年关于人员分配问题的论文中指出。<sup>[9](https://www.math.utoronto.ca/mccann/1855/KuhnEJOR12.pdf)</sup>

## 方法如何运作

库恩所述目的是开发一种以特别有效的方式利用线性规划对偶的计算方法；丹齐格和冯·诺依曼等人曾强调将问题与其对偶规划一起处理的计算优势。<sup>[1](https://www.math.utoronto.ca/mccann/1855/KuhnNRL55.pdf)</sup> 对偶问题为每个人分配非负量 u、为每项工作分配非负量 v，使得对个人 i 和工作 j 的分配量之和不小于评分 r<sub>ij</sub>；任何此类充足预算的总和永远不会小于任何分配的评分总和，而值相重合的分配与预算同时解决两个问题。<sup>[1](https://www.math.utoronto.ca/mccann/1855/KuhnNRL55.pdf)</sup>

该算法分两层构建。简单分配问题使用仅含两个评分1和0的资格矩阵，表示工人是否有资格从事某项工作；库恩从柯尼希《图论》（*Theorie der Graphen*，1936）中的证明推导出这一部分。具有任意正整数评分的一般分配问题则通过一个库恩称之为计算上平凡的程序归约到这一特例，该程序源自埃格瓦里1931年的论文《矩阵的组合性质》（*Matrixok kombinatorius tulajdonságairól*）。<sup>[1](https://www.math.utoronto.ca/mccann/1855/KuhnNRL55.pdf)</sup> 具体而言，每个充足预算定义一个简单分配问题，其中个人 i 有资格从事工作 j，当且仅当 u<sub>i</sub> + v<sub>j</sub> = r<sub>ij</sub>；如果所有 n 个人都能如此分配，则该分配与预算联合最优，否则预算可减少一个正整数量，因此有限序列的简单分配问题终止于最优解。<sup>[1](https://www.math.utoronto.ca/mccann/1855/KuhnNRL55.pdf)</sup>

## 复杂度历史

库恩的原始表述运行时间为 \( O(n^{4}) \)。詹姆斯·蒙克雷斯（James Munkres）1957年的分析表明，该方法可以实现为在 n（顶点数）的多项式时间内运行，这一结果有助于确立多项式时间计算作为算法质量的标准。<sup>[5](https://sites.math.washington.edu/~thomas/teaching/m514_web/50YearsIP.pdf)</sup> [杰克·埃德蒙兹](https://www.edgepedia.org/zh/jack-edmonds)（Jack Edmonds）和理查德·卡普（Richard Karp），以及后来的富泽，观察到该算法可以修改以获得 \( O(n^{3}) \) 的运行时间。<sup>[10](https://ricerca.univaq.it/retrieve/e2188bfb-f6e0-f28d-e053-d805fe0ac2bb/Thesis_F.pdf)</sup> 在标准的 O(n³) 表述中，n 次迭代中的每一次以 O(n²) 时间处理一行，对于方阵问题总体为 O(n³)，对于矩形问题为 O(n²m)。<sup>[6](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/graph/hungarian-algorithm.md)</sup> 势更新步骤的朴素实现成本为 O(n⁴)，缓存相关最小值可将每步成本降至 O(n²)。<sup>[7](https://algorithms.discrete.ma.tum.de/matching/hungarian-method/)</sup>

## 后来的改进与竞争算法

匈牙利算法是导出网络问题一般原始–对偶方法的源算法。<sup>[11](https://gwern.net/doc/statistics/decision/1987-jonker.pdf)</sup> [阿姆斯特丹大学](https://www.edgepedia.org/zh/university-of-amsterdam)的罗伊·容克（Roy Jonker）和通·福尔根南特（Ton Volgenant）于1986年描述了对匈牙利分配算法的三项易于实现的改进，将计算时间降至此前的大约二分之一到三分之一以下，且收益随问题规模增大而增加，并表明匈牙利算法本质上等价于基于最短增广路的分配算法。<sup>[12](https://www.sciencedirect.com/science/article/pii/0167637786900738)</sup> 他们1987年发表在《计算》（*Computing*）38(4): 325–340上的最短增广路算法 LAPJV，在稠密和稀疏问题上平均计算时间均一致低于他们所测试的其他算法，对成本系数范围的敏感性较低。<sup>[11](https://gwern.net/doc/statistics/decision/1987-jonker.pdf)</sup> 在他们基准测试的稀疏实例上，LAPJVsp 明显优于劳勒风格的 O(n³) 匈牙利方法代码。<sup>[11](https://gwern.net/doc/statistics/decision/1987-jonker.pdf)</sup>

因此，广泛使用的库实现不再运行匈牙利方法本身：SciPy 1.2 中引入的 `scipy.optimize.linear_sum_assignment` 使用无初始化的改进容克–福尔根南特算法求解线性求和分配问题，并且还能处理矩形成本矩阵，其中并非每一行都需要一列。<sup>[4](https://scipy.github.io/devdocs/reference/generated/scipy.optimize.linear_sum_assignment.html)</sup><sup> • </sup><sup>[13](https://static.scipy.org/doc/scipy-1.2.0/scipy-ref-1.2.0.pdf)</sup> Git 版本控制系统同样基于容克–福尔根南特的论文实现了线性分配。<sup>[14](https://code.googlesource.com/git/%2B/5cdb38458ee5b53daada9cb767ae51a08650bd6a/linear-assignment.c)</sup>

## 现代应用

该算法仍然是现代计算机视觉系统的标准组件。2020年引入的 DETR（检测变换器，DEtection TRansformer）将目标检测视为直接的集合预测问题：其基于集合的全局损失在优化对象特定损失之前，产生预测对象与真值对象之间的最优二分匹配，这使模型能够舍弃非极大值抑制和锚点生成等手工设计的组件。<sup>[8](https://arxiv.org/abs/2005.12872)</sup> 匈牙利匹配步骤是 DETR 训练中公认的计算成本，后来的工作试图降低它，或者加速匹配本身，或者用学习到的对应关系替代显式匹配；一项2026年的提案报告将匹配延迟降低50%以上。<sup>[15](https://arxiv.org/abs/2603.08514)</sup> 分配表述也出现在多目标跟踪、资源分配中，并作为二次分配问题和旅行商问题等更难组合问题的松弛。<sup>[16](https://www.sciencedirect.com/science/article/abs/pii/S0377221798003610)</sup>

## 历史与命名

库恩以该方法的匈牙利渊源为其命名。柯尼希在研究最大匹配时发现了图是二分图时成立的对偶结果；埃格瓦里是矩阵理论专家，以构造性证明将柯尼希的结果从0–1矩阵推广到任意实矩阵。库恩在计算机革命之初借助线性规划对偶，意识到他们的工作中潜藏着一个可编程用于电子计算的良好分配问题算法。<sup>[9](https://www.math.utoronto.ca/mccann/1855/KuhnEJOR12.pdf)</sup> 库恩于1954年将埃格瓦里1931年的匈牙利文论文译为英文，题为“论矩阵的组合性质”，发表于乔治·华盛顿大学后勤论文集中。<sup>[9](https://www.math.utoronto.ca/mccann/1855/KuhnEJOR12.pdf)</sup> 1955年的论文获得了最佳论文奖，《海军研究后勤》于2005年在第52卷第7–21页为纪念该问题五十周年重新发表了它。<sup>[17](https://oamonitor.ireland.openaire.eu/rpo/galway/search/publication?pid=10.1002%2Fnav.20053)</sup> 库恩在2012年《欧洲运筹学杂志》的文章《三个时代的故事：匈牙利方法的发现与再发现》中重述了发现史，将雅各比、柯尼希、埃格瓦里和他自己跨越普鲁士君主制、匈牙利和战后美国联系起来。<sup>[9](https://www.math.utoronto.ca/mccann/1855/KuhnEJOR12.pdf)</sup>

## References

1. [Harold W. Kuhn (1955). The Hungarian method for the assignment problem. Naval Research Logistics Quarterly 2: 83–97.](https://www.math.utoronto.ca/mccann/1855/KuhnNRL55.pdf)
2. [BibSonomy bibliographic record for Kuhn 1955](https://www.bibsonomy.org/pow-challenge/?return=https%3A%2F%2Fwww.bibsonomy.org%2Fbibtex%2F20bbf339729b509a2836d225e7dd174bb%2Fgergie)
3. [SciPy Reference Guide 1.2.3, linear_sum_assignment](https://static.scipy.org/doc/scipy-1.2.3/scipy-ref-1.2.3.pdf)
4. [scipy.optimize.linear_sum_assignment, SciPy v2.0.0.dev Manual](https://scipy.github.io/devdocs/reference/generated/scipy.optimize.linear_sum_assignment.html)
5. [50 Years of Integer Programming 1958–2008 (includes analysis of Munkres 1957)](https://sites.math.washington.edu/~thomas/teaching/m514_web/50YearsIP.pdf)
6. [Hungarian algorithm, cp-algorithms](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/graph/hungarian-algorithm.md)
7. [The Hungarian Method, TUM Discrete Algorithms](https://algorithms.discrete.ma.tum.de/matching/hungarian-method/)
8. [Carion et al. (2020). End-to-End Object Detection with Transformers. arXiv:2005.12872.](https://arxiv.org/abs/2005.12872)
9. [H.W. Kuhn (2012). A tale of three eras: The discovery and rediscovery of the Hungarian Method. European Journal of Operational Research 219: 641–651.](https://www.math.utoronto.ca/mccann/1855/KuhnEJOR12.pdf)
10. [Thesis, Università degli Studi dell'Aquila (complexity history of the Hungarian method)](https://ricerca.univaq.it/retrieve/e2188bfb-f6e0-f28d-e053-d805fe0ac2bb/Thesis_F.pdf)
11. [Jonker & Volgenant (1987). A shortest augmenting path algorithm for dense and sparse linear assignment problems. Computing 38(4): 325–340.](https://gwern.net/doc/statistics/decision/1987-jonker.pdf)
12. [Jonker & Volgenant (1986). Improving the Hungarian assignment algorithm. Operations Research Letters 5(4): 171–175.](https://www.sciencedirect.com/science/article/pii/0167637786900738)
13. [SciPy Reference Guide 1.2.0 (introduction of linear_sum_assignment)](https://static.scipy.org/doc/scipy-1.2.0/scipy-ref-1.2.0.pdf)
14. [Git linear-assignment.c, based on Jonker & Volgenant 1987](https://code.googlesource.com/git/%2B/5cdb38458ee5b53daada9cb767ae51a08650bd6a/linear-assignment.c)
15. [Qiu, Li & Long (2026). Beyond Hungarian: Match-Free Supervision for End-to-End Object Detection. arXiv:2603.08514.](https://arxiv.org/abs/2603.08514)
16. [Linear assignment procedures, European Journal of Operational Research](https://www.sciencedirect.com/science/article/abs/pii/S0377221798003610)
17. [The Hungarian method for the assignment problem, OpenAIRE record (NRL vol. 52, pp. 7–21; Best Paper Award)](https://oamonitor.ireland.openaire.eu/rpo/galway/search/publication?pid=10.1002%2Fnav.20053)

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

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Graph and network algorithms › Matching and assignment

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/hungarian-algorithm

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