A Bit of History
Harold Kuhn published "The Hungarian Method for the Assignment Problem" in 1955, and deliberately chose that name to credit the two Hungarian mathematicians whose earlier theoretical work made his algorithm possible: Dénes Kőnig (whose 1931 theorem was already met in Part 16) and Jenő Egerváry, whose 1931 refinement of König's ideas supplied the key combinatorial structure Kuhn's method exploits directly. Two years later, in 1957, James Munkres revisited the method and proved it runs in strictly polynomial time — \(O(n^3)\) — which is why the algorithm is also frequently called the Kuhn-Munkres algorithm in more careful references.
Working Principle
The assignment problem: given an \(n \times n\) cost matrix \(C\) (worker \(i\) costs \(C[i][j]\) to assign to task \(j\)), find a perfect matching minimizing total cost. The Hungarian algorithm works directly on the cost matrix, in four repeated steps:
- Row reduction: subtract each row's minimum from every entry in that row.
- Column reduction: subtract each column's minimum from every entry in that column.
- Cover all zeros with the minimum number of horizontal/vertical lines. If the number of lines equals \(n\), an optimal assignment exists among the zeros — stop.
- Otherwise, adjust: find the smallest uncovered value, subtract it from every uncovered entry, and add it to every entry covered twice (where a horizontal and vertical line cross) — then return to step 3.
Key Insight
Every reduction step is cost-preserving for the optimal assignment: subtracting a constant from an entire row (or column) shifts every possible assignment's total cost by exactly the same amount, so it can never change which assignment is cheapest — only the numbers used to describe how cheap it is. The algorithm's entire strategy is to keep creating "free" zeros this way until enough exist to read off a complete zero-cost assignment directly.
Worked Example
A 3×3 cost matrix (workers A, B, C; tasks 1, 2, 3):
| Task 1 | Task 2 | Task 3 | |
|---|---|---|---|
| A | 9 | 11 | 14 |
| B | 6 | 15 | 13 |
| C | 12 | 13 | 6 |
Row reduction (subtract row minimums 9, 6, 6): rows become \([0,2,5]\), \([0,9,7]\), \([6,7,0]\). Column reduction (column minimums are now 0, 2, 0 — only column 2 needs adjustment): column 2 becomes \([0,7,5]\). Covering zeros: \((A,1)\), \((B,1)\)... but two zeros share column 1, so only 2 lines are needed to cover all zeros, not 3 — another adjustment round follows (details omitted for brevity), eventually yielding the optimal assignment \(A{\to}1, B{\to}\text{(unused zero path)}, C{\to}3\), landing on the true minimum-cost assignment \(A{\to}2, B{\to}1, C{\to}3\) with total cost \(11+6+6=23\) — matching what a brute-force check of all \(3! = 6\) possible assignments would confirm.
Why Reduction Preserves the Optimum
Formally: if you subtract a constant \(u_i\) from every entry in row \(i\) and \(v_j\) from every entry in column \(j\), any perfect matching's total cost changes by exactly \(\sum_i u_i + \sum_j v_j\) — the same amount for every possible assignment, since a perfect matching uses each row and column exactly once. So the relative ordering of assignments by cost is completely unchanged, and an assignment using only zero-cost entries in the reduced matrix must be optimal in the original matrix too — precisely the reduction-and-cover strategy the algorithm exploits, connecting back to König's theorem's matching-cover duality from Part 16 to guarantee that "enough zeros for a full assignment" is always eventually reachable.
Complexity Analysis
Munkres' 1957 analysis established:
$$\text{Time: } O(n^3) \qquad \text{Space: } O(n^2)$$
where \(n\) is the number of workers/tasks — polynomial, and (unlike Ford-Fulkerson) independent of the actual cost values involved.
Implementation
Real-World Applications
Multi-Object Tracking in Computer Vision
Video-tracking systems that follow multiple moving objects frame to frame must decide, every frame, which detected object in the new frame corresponds to which tracked object from the previous frame — exactly an assignment problem, with cost typically based on position and appearance similarity. The Hungarian algorithm is the standard solution used in production tracking pipelines (sports analytics, autonomous vehicle perception, surveillance systems) precisely because it guarantees the globally optimal frame-to-frame correspondence, not just a locally greedy guess.
Exercises
- Complete the worked example by hand through all reduction rounds, confirming the final assignment and its total cost of 23.
- Explain why subtracting a row's minimum from every entry in that row never changes which assignment is optimal, using the "shared constant shift" argument from the correctness section.
- Modify a cost matrix to maximize total value instead of minimizing cost (hint: negate every entry, or subtract every entry from a large constant), and verify the Hungarian algorithm still produces the correct optimal assignment.
- Challenge: Handle an unbalanced assignment problem (more workers than tasks, or vice versa) by padding the cost matrix with dummy rows/columns of cost 0, and verify the algorithm still produces a sensible partial assignment.
Limitations
Requires a Complete, Square Cost Matrix
The classical Hungarian algorithm assumes a full \(n \times n\) cost matrix (every worker can theoretically do every task, even if at very high cost) — unbalanced or sparse assignment problems need padding with dummy entries first, and \(O(n^3)\) can become genuinely slow once \(n\) reaches the tens of thousands, where more specialized auction-based or network-simplex algorithms are typically preferred in production systems.