Proofs: Hungarian algorithm
These are the human proofs of Hungarian algorithm, at full length.
The Hungarian algorithm returns a least-cost assignment full
This proof shows: correctness, termination.
Let be an matrix with and finite entries. (The implementation replaces a missing entry by a large finite cost first; the last part of this proof covers that step.) Rows are , columns are , and column 0 is a dummy. For a row and a column , the reduced cost is . A pair is tight if . Column is matched if , and free otherwise.
Round invariant (R). Before the round of row (line L3):
- restricted to the columns is injective on its nonzero values, and its nonzero values are exactly the rows ;
- for each row and each column ;
- for each column , and for each free column;
- each matched pair is tight;
- for each row .
Line L1 makes (R) true for : there are no matched columns and no rows .
Pass invariant (P). Fix a round . In this round, call a column used when its mark is true, and call the rows of the used columns the tree rows; , so row is a tree row as soon as column 0 is used. At the end of each pass (after line L12):
- every used column other than 0 is matched, and the tree rows are distinct;
- for each row and each column ;
- for each column that is not used, , and is a used column with ;
- for each used column , the pairs and are tight, and was used before ;
- (R3) and (R4) hold, and the column chosen at line L9 satisfies .
Proof that (P) holds after each pass. Consider a pass. At line L5, column becomes used and its row becomes a tree row. In the first pass, and . In a later pass, is the column of the pass before, which is matched, since otherwise line L14 would have ended the loop; so is a row that is not yet a tree row, because is injective. This gives (P1).
Lines L6 to L8 lower to when that is smaller, and record . So, before the shift, is the least reduced cost from a tree row to , for each column that is not used, and names a column whose row reaches it. Line L9 sets and to a column that reaches it. There is at least one column that is not used, because the used columns other than 0 are matched and there are at most matched columns. So is finite.
Lines L10 to L12 add to for each tree row and subtract from for each used column . Look at the effect on for a row and a column :
- a tree row and used: does not change.
- a tree row and not used: drops by . Before the shift , so after it . Line L12 lowers by too, so (P3) stays true, and .
- not a tree row and used: grows by . In the first pass no column is used, so this case does not occur. In a later pass, , because by (P2) and (P3) of the pass before, every is at least 0 before line L6 and line L6 only takes minima with reduced costs that are at least 0 by (P2). So stays at least 0.
- not a tree row and not used: does not change.
Before the first pass, (R2) gives for . Row is a tree row from the first pass on, so the second case covers it. This proves (P2).
(R3): only used columns change . A used column was used in a pass after the first, where , so only goes down, and stays true. A free column is never used, by (P1), so its stays 0. (R4): a matched pair has both ends in the tree or both outside, because the tree rows are exactly the rows of used columns, and row is not matched. So its reduced cost does not change, and it stays tight.
(P4): when a column becomes used, it was in the pass before, so the pair became tight at that time, and is tight by (R4). Later shifts do not change pairs with both ends in the tree, and line L8 does not change for a used column.
Each round ends. Each pass makes one more column used. By (P1), the used columns other than 0 are matched, and at most columns are matched. So after at most passes, the column of line L13 is free and line L14 ends the loop.
The augmentation keeps (R). When the loop ends, is a free column with , and is tight by (P3). Line L15 follows the chain , which reaches 0, because each value is a column used strictly before (P4), and sets for each column on the chain other than 0. So each row on the chain moves from its column to the next column ; row moves to the first column after 0; and the free column gets a row. The new pairs are the pairs , which are tight by (P3) and (P4). After the round, the matched rows are , is still injective, and (R2) to (R4) hold for by (P2) and (P5). Row was not a tree row, so . So (R) holds before the round of row .
Theorem. HUNGARIAN stops, and the pairs of line L16 form an assignment of all rows to distinct columns of least total cost.
Proof. Each of the rounds ends, and by (R) after the last round, every row has its own column. Let be the result and any assignment of rows to distinct columns. By (R2), for each , so
where the second step uses for the columns that does not use (R3). For both steps are equalities: each pair is tight by (R4), and the columns that does not use are free, with by (R3). So .
Missing entries. Let be the set of finite entries, with least value and largest value , and let replace each missing entry. An assignment that uses only finite entries costs at most . An assignment that uses at least one replaced entry costs at least . So if some assignment avoids the missing entries, every optimal assignment of the new matrix avoids them too, and it is optimal for the old matrix. If no assignment avoids them, the optimal assignment of the new matrix uses one, and the implementation reports that there is none.