Hungarian Algorithm
The Hungarian algorithm is a primal–dual combinatorial optimization algorithm that solves the minimum-cost or maximum-weight bipartite assignment problem in O(V³) strongly polynomial time.
Hungarian algorithm (also known as the Kuhn–Munkres algorithm or Munkres assignment algorithm) is a primal–dual combinatorial optimization procedure in computation and algorithms that solves the linear assignment problem—finding a minimum-cost (or maximum-weight) perfect matching in an edge-weighted bipartite graph—in O(V3) strongly polynomial time. Developed and published by Harold W. Kuhn in 1955 and refined by James Munkres in 1957, the algorithm was named by Kuhn in honor of the Hungarian mathematicians Dénes Kőnig and Jenő Egerváry, whose 1931 theorems on bipartite vertex covers and weighted matrix reductions provided its mathematical foundation; posthumous archival discoveries in 2006 revealed that Carl Gustav Jacob Jacobi had formulated an equivalent method in Latin manuscripts published in 1890.
As the historical prototype of the primal–dual method in linear programming and computational complexity theory, the Hungarian algorithm maintains a feasible dual solution consisting of vertex potentials ui and vj alongside a partial primal matching M restricted to zero-reduced-cost tight edges satisfying ui + vj = ci,j. In each phase, it either augments the matching M inside the tight equality subgraph or adjusts the vertex potentials along an alternating tree to bring new tight edges into the equality subgraph until a perfect matching of size n is achieved.
Mathematical Formulation and Primal–Dual Duality
The Linear Assignment Problem and Birkhoff Polytope
Given an n × n real cost matrix C = (ci,j) representing the cost of assigning worker (left vertex) i ∈ {1, …, n} to task (right vertex) j ∈ {1, …, n}, the minimum-cost assignment problem seeks a bijection (permutation) π : {1, …, n} → {1, …, n} minimizing total cost ∑i=1n ci,π(i). Formulated as a linear program over binary decision variables xi,j ∈ {0, 1}, the primal linear program is:
subject to:
- ∑j=1n xi,j = 1 for all i ∈ {1, …, n} (each row assigned once),
- ∑i=1n xi,j = 1 for all j ∈ {1, …, n} (each column assigned once),
- xi,j ≥ 0 for all i, j ∈ {1, …, n}.
Because the constraint matrix of a bipartite graph is totally unimodular, the Birkhoff–von Neumann Theorem guarantees that the feasible polytope of doubly stochastic matrices has integral extreme points (permutation matrices).
Dual Vertex Potentials and Complementary Slackness
Introducing unconstrained real dual variables (called vertex potentials or labels) u1, …, un ∈ ℝ for the rows and v1, …, vn ∈ ℝ for the columns yields the dual linear program:
subject to the dual feasibility constraints:
Define the non-negative reduced cost (or slack) of edge (i, j) as c̄i,j = ci,j − ui − vj ≥ 0. An edge (i, j) is called tight (or admissible) if c̄i,j = 0, meaning ui + vj = ci,j. The equality subgraph Gu,v = (U ∪ W, Eu,v) consists of all tight edges:
By linear programming complementary slackness, if (u, v) is a feasible dual potential and M is a perfect matching (n edges) contained inside the equality subgraph Eu,v, then M is a minimum-cost assignment:
Because weak duality states that every assignment has cost at least ∑ ui + ∑ vj, equality proves optimality of both M and (u, v).
Algorithmic Procedure and O(V³) Implementation
Matrix vs. Graph-Theoretic Primal–Dual Formulation
While early textbook presentations described row and column zero-covering steps on the matrix C (O(n4) naive time), Jack Edmonds and Richard Karp (1972) and independently Dinic and Kronrod (1969) formulated the modern O(n3) graph-theoretic algorithm by maintaining minimum column slacks:
- Dual Initialization: Set ui ← minj ci,j for each row i and vj ← mini (ci,j − ui) for each column j. Start with an empty matching M = ∅ inside Gu,v.
- Phase Loop (n Phases): While |M| < n, select an unmatched left vertex r ∈ U and grow a Hungarian alternating tree T rooted at r inside Gu,v. Let S ⊆ U be the set of left vertices currently in T (initially S = {r}) and TW ⊆ W be the set of right vertices currently in T (initially TW = ∅).
- Slack Array Maintenance: For every column j ∈ W \ TW, maintain its minimum slack to the current tree S:
together with the minimizing parent vertex argmini ∈ S.
- Dual Potential Update and Tree Growth: Compute the step size Δ = minj ∈ W \ TW slack[j] > 0 (or Δ = 0 if a tight edge to W \ TW already exists). Update potentials and slacks in O(n) time:
- For all i ∈ S: ui ← ui + Δ
- For all j ∈ TW: vj ← vj − Δ
- For all j ∈ W \ TW: slack[j] ← slack[j] − Δ
Note that edges inside S × TW remain tight ((ui + Δ) + (vj − Δ) = ui + vj), while at least one column j* ∈ W \ TW reaches slack[j*] = 0 and becomes connected to S by a new tight edge.
- Tree Extension or Augmentation:
- If j* is unmatched, an augmenting path from r to j* in Gu,v has been found! Flip the matching along this path (M ← M ⊕ P), increasing |M| by 1, and end the phase.
- If j* is already matched to some row i* ∈ U \ S, add j* to TW and i* to S, and update slack[j] ← min(slack[j], ci*,j − ui* − vj) for all j ∈ W \ TW in O(n) time.
Because each of the n phases adds at most n vertices to S and TW, and each vertex addition updates the slack array in O(n) time, each phase takes O(n2) time. Across all n phases, the total runtime is strongly polynomial O(n3) (or O(V E + V2 log V) on sparse bipartite graphs when implemented as successive shortest augmenting paths via potential-reweighted Dijkstra's algorithm with Fibonacci heaps, Fredman and Tarjan 1987).
Comparative Complexity of Assignment and Weighted Matching Solvers
| Algorithm / Solver | Graph & Weight Regime | Worst-Case Time Complexity | Primary Algorithmic Mechanism |
|---|---|---|---|
| Kuhn–Munkres Naive Matrix (1955–1957) | Dense n × n Bipartite | O(n4) | Iterative line-covering of matrix zeros |
| Hungarian Slack-Array (Dinic–Kronrod / Jonker–Volgenant) | Dense n × n Bipartite | O(n3) | Alternating tree + O(n) column slack updates |
| Edmonds–Karp / Tomizawa Successive Shortest Path (1971–1972) | Sparse Bipartite (V, E) | O(V E + V2 log V) | Reduced-cost Dijkstra's algorithm per augmentation |
| Gabow–Tarjan Cost-Scaling (1989) | Integer Weights ≤ C | O(E √V log(V C)) | Bit-scaling + Hopcroft–Karp algorithm phases |
| Edmonds Weighted Blossom (1965 / Gabow 1990) | General Non-Bipartite | O(V(E + V log V)) | Primal–dual odd-set blossom potentials |
Scientific and Engineering Applications
- Multi-Object Visual Tracking (Simple Online and Realtime Tracking / DeepSORT): Autonomous vehicle perception and cellular microscopy tracking pipelines use the Hungarian algorithm at every video frame to match Kalman-predicted bounding boxes against newly detected objects using Intersection-over-Union (IoU) and Mahalanobis appearance distances.
- Single-Cell Transcriptomics and Spatial Alignment: Computational biology tools align cell clusters across multi-omic batches and map single-cell RNA-seq profiles to spatial transcriptomic landmarks via optimal transport and minimum-cost bipartite matching.
- Quantum Chemistry and Orbit Localization: Molecular orbital correspondence across geometry optimization steps and atom-to-atom reaction mapping in chemoinformatics rely on the Hungarian algorithm to minimize root-mean-square coordinate displacement.
Frequently Asked Questions
How is the Hungarian algorithm adapted for maximum-weight bipartite matching?
To maximize ∑ wi,j xi,j, either negate all weights (ci,j = Wmax − wi,j where Wmax = maxi,j wi,j) and solve for minimum cost, or reverse the dual inequality to ui + vj ≥ wi,j and minimize ∑ ui + ∑ vj.
Why are vertex potentials used instead of running the Bellman–Ford algorithm at every step?
After the initial potentials satisfy ci,j − ui − vj ≥ 0, all reduced edge weights in the residual network are non-negative. This allows shortest augmenting paths to be found in O(V2) (or O(E + V log V)) time via Dijkstra's algorithm rather than O(V E) time via the Bellman–Ford algorithm.
References
- Kuhn, H. W. (1955). "The Hungarian method for the assignment problem." Naval Research Logistics Quarterly, 2(1–2), 83–97. https://doi.org/10.1002/nav.3800020109
- Munkres, J. (1957). "Algorithms for the assignment and transportation problems." Journal of the Society for Industrial and Applied Mathematics, 5(1), 32–38. https://doi.org/10.1137/0105003
- Edmonds, J., & Karp, R. M. (1972). "Theoretical improvements in algorithmic efficiency for network flow problems." Journal of the ACM, 19(2), 248–264. https://doi.org/10.1145/321694.321699
- Jonker, R., & Volgenant, A. (1987). "A shortest augmenting path algorithm for dense and sparse linear assignment problems." Computing, 38(4), 325–340. https://doi.org/10.1007/BF02278710