Hungarian Algorithm finding minimum number of lines to cover zeroes?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In the Hungarian algorithm, the step about covering all zeros with the minimum number of horizontal and vertical lines is not a side puzzle. It is the test that tells you whether the current reduced cost matrix already contains enough structure to extract an optimal assignment.
Why Covering Zeros Matters
After row reduction and column reduction, the assignment problem is transformed so that good candidate assignments sit on zero entries. The next question is whether you can choose one zero per row and one zero per column without conflict.
The Hungarian algorithm answers that indirectly:
- if the minimum number of lines needed to cover all zeros equals
nfor ann x nmatrix, you are ready to build an optimal assignment - if fewer than
nlines are enough, you must adjust the matrix and continue
This works because of the connection between zero structure, bipartite matching, and minimum vertex cover.
The Graph Interpretation
A reduced matrix can be turned into a bipartite graph:
- each row becomes a node on the left
- each column becomes a node on the right
- every zero entry creates an edge between its row and column
Now the question "minimum number of lines covering all zeros" becomes "minimum number of row and column vertices covering all zero edges."
By Kőnig's theorem, in a bipartite graph the size of a minimum vertex cover equals the size of a maximum matching. That is the theoretical reason the Hungarian algorithm can use zero covering as an optimality test.
Small Example
Suppose the reduced matrix is:
The zero positions are:
- row 0, column 0
- row 0, column 2
- row 1, column 0
- row 2, column 1
- row 2, column 2
That gives a bipartite graph where rows connect to columns only at those zero positions. If the maximum matching size is 3, then the minimum number of covering lines is also 3, and the matrix is ready for the assignment step. If the maximum matching size is only 2, then you can cover all zeros with two lines, which means the matrix still needs another adjustment.
Practical Hungarian-Algorithm Procedure
Textbook descriptions often explain the line-covering step through marking rules rather than graph theory. A common practical procedure is:
- find a maximum set of independent zeros
- mark all rows that do not contain an assigned zero
- mark every column containing a zero in a marked row
- mark every row containing an assigned zero in a marked column
- repeat until no new rows or columns can be marked
- draw lines through all unmarked rows and all marked columns
The total number of lines you draw is the minimum number needed to cover all zeros.
This marking process is just another way to compute a minimum vertex cover from a maximum matching.
Computing It Programmatically
A clean implementation is to build the zero graph and compute a maximum matching, then derive the cover. The following Python example shows the matching part for a square matrix of zeros and nonzeros.
If matched == n, then the minimum number of covering lines is also n, so the zero pattern is sufficient for an optimal assignment. If matched < n, you continue with the Hungarian adjustment step: subtract the smallest uncovered value from all uncovered elements and add it at intersections of covering lines.
What This Means Inside the Algorithm
The line-cover test does not itself produce the final assignment every time. Its role is to answer whether the current zero structure is rich enough. If not, the matrix must be transformed again to create more useful zeros without changing the true optimum of the original cost problem.
That is why the Hungarian algorithm alternates between:
- creating or exposing zeros
- checking whether the zeros support a full assignment
Common Pitfalls
- Thinking the minimum-line step is separate from matching theory when it is really the same bipartite-cover problem.
- Covering zeros greedily by sight and assuming the result is always minimal.
- Forgetting that the number of minimum covering lines is compared with
n, the matrix dimension. - Confusing "all zeros are covered" with "an optimal assignment is already found."
- Adjusting the matrix before computing a true minimum cover.
Summary
- In the Hungarian algorithm, the minimum number of lines covering all zeros is a key optimality test.
- The zero matrix can be viewed as a bipartite graph of rows, columns, and zero edges.
- By Kőnig's theorem, the minimum zero cover size equals the maximum matching size.
- If the minimum cover size is
n, the matrix is ready for an optimal assignment. - If it is smaller than
n, adjust the matrix and repeat the process.
Related reading
- Hungarian algorithm multiple jobs per worker
- I am looking for a radio advertising scheduling algorithm / example / experience
- I do not understand the concept of Non Deterministic Turing Machine
- I have a Python list of the prime factors of a number. How do I pythonically find all the factors?
- Hyperparameter optimization for Deep Learning Structures using Bayesian Optimization
- Hyperparameter optimization for Deep Learning Structures using Bayesian Optimization
- I need a fast 96-bit on 64-bit specific division algorithm for a fixed-point math library
- I need an optimal algorithm to find the largest divisor of a number N. Preferably in C or C

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.