Hungarian Algorithm
You have 4 programmers and 4 tasks. Each programmer takes a different amount of time on each task. You want to assign exactly one task to each programmer so the total time is as small as possible. That's the assignment problem.
Bipartite matching finds any maximum matching. The Hungarian algorithm finds the cheapest one. It runs in \(O(n^3)\) — astronomically better than trying all n! assignments.
The Core Insight: Row and Column Reduction
Here's the elegant key: if you subtract a constant from every entry in a row, you shift every possible assignment's total cost by the same amount. The relative order of assignments doesn't change — the optimal one is still optimal.
Same for columns. So:
- Subtract the minimum from each row (every row now has at least one zero).
- Subtract the minimum from each column (every column now has at least one zero).
Now look for an assignment using only zero-cost cells. If we find a perfect matching of zeros (one per row, one per column), that's our optimal assignment — it costs zero after reduction, which means it's minimal in the original matrix.
When Zeros Don't Cover Everything
The initial zeros might not support a perfect matching. When that happens, we "create" more zeros without destroying the existing ones:
- Find the minimum number of lines (rows or columns) needed to cover all current zeros — call this k.
- If k = n, we have a perfect matching of zeros — done!
- Otherwise, find the smallest uncovered value, subtract it from all uncovered rows, and add it to all covered columns. This creates at least one new zero while maintaining all existing ones.
- Repeat from step 1.
The Augmenting Path View
Internally, finding a perfect matching of zeros is a bipartite matching problem. The algorithm uses augmenting paths (similar to max flow) through zero-cost edges. When stuck, the matrix update (step 3 above) creates new zero-cost edges to enable progress. This always terminates after at most n phases.
Why It's Correct: LP Duality
The row and column subtraction values act as dual variables for the linear programming relaxation of the assignment problem. The algorithm maintains dual feasibility throughout (no negative reduced costs) and achieves primal feasibility (a perfect matching) when done. LP strong duality then guarantees the solution is optimal.