Cover Zeros with Minimum Lines: The Key Hungarian Algorithm Step (Lecture-Style)
After row and column reduction, the Hungarian method creates many zeros. The next step is to
cover all zeros using the minimum number of horizontal/vertical lines.
If that minimum equals n, a full assignment (perfect matching) can be extracted from zeros.
If it is less than n, you do the θ adjustment and repeat.
You can see this step rendered automatically in the Assignment Problem Solver (Hungarian method). Next step after covering zeros: θ adjustment explained.
On this page
Quick takeaway: Let n be the matrix size after balancing.
If the minimum number of lines needed to cover all zeros is n, then there is a full set of independent zeros (a perfect matching),
so you can read the assignment. If it’s < n, you must apply the
θ adjustment step and repeat.
What does “cover zeros with minimum lines” mean?
Definition
Lines are entire rows or entire columns
You are allowed to draw:
• a horizontal line through an entire row, or
• a vertical line through an entire column,
and you want every zero entry to lie on at least one drawn line, using as few lines as possible.
Why minimum lines matters (perfect matching connection)
Why it works
Independent zeros = assignment candidates
A valid assignment needs n zeros, one in each row and each column, with no conflicts.
This is exactly a perfect matching in the “zero graph” (zeros are edges).
There is a deep theorem behind this: in bipartite graphs, the size of a maximum matching equals the size of a minimum vertex cover (Kőnig’s theorem). In Hungarian terms, that becomes:
maximum number of independent zeros = minimum number of lines to cover all zeros
Learn the graph viewpoint: bipartite matching & assignment.
How to find the minimum lines (marking method)
Procedure
Classic marking method used in Hungarian hand-solutions
Start from a set of “assigned” (independent) zeros (often found by trying to pick one zero per row/col without conflict). Then:
- Mark all rows that do not have an assigned zero.
- For every marked row, mark each column that contains a zero in that row.
- For every marked column, mark each row that has an assigned zero in that column.
- Repeat steps 2–3 until no new rows/columns can be marked.
- Draw lines through:
• every unmarked row, and
• every marked column.
n, proceed to the θ adjustment.
Worked examples (fully solved, lecture-style)
Worked examples
Expand only the example you need
These examples start from a reduced matrix (after row + column reduction), because the “cover zeros” step happens there. You can reproduce them in the Hungarian solver by entering an original matrix and viewing step tables.
Example 1 — 4×4: cover requires 3 lines, so θ adjustment is needed
Reduced matrix (zeros already created):
C1 C2 C3 C4 R1 0 2 0 1 R2 1 0 3 0 R3 0 1 2 3 R4 2 0 1 2
Step A — Try to select independent zeros (one per row/column)
We attempt to “assign” zeros without sharing a column. One possible attempt:
Pick (R1,C1) = 0 Pick (R2,C4) = 0 Pick (R4,C2) = 0 Now R3 has a zero at C1, but C1 is already used → cannot pick a 4th independent zero in this attempt
So we currently have only 3 assigned zeros. That suggests we might need θ adjustment. The cover-lines step will confirm this properly.
Step B — Mark rows with NO assigned zero
Assigned zeros are in rows R1, R2, R4. Row R3 has no assignment, so mark:
Marked rows: {R3}
Step C — Mark columns that have a zero in any marked row
In row R3, the zeros occur at (R3,C1) only. So mark column C1:
Marked columns: {C1}
Step D — Mark rows that have an assigned zero in any marked column
Column C1 has an assigned zero at (R1,C1). So mark row R1:
Marked rows: {R3, R1}
Step E — Repeat: mark columns with zeros in newly marked rows
Row R1 has zeros at C1 and C3. C1 is already marked, so add C3:
Marked columns: {C1, C3}
Step F — Mark rows with assigned zeros in newly marked columns
Column C3 has no assigned zero in our current assignment attempt (we assigned R1→C1, R2→C4, R4→C2). So no new rows are marked. Stop.
Step G — Draw the minimum covering lines
Draw lines through:
• Unmarked rows = rows not in {R3, R1} → {R2, R4}
• Marked columns = {C1, C3}
Lines drawn: rows {R2, R4} and columns {C1, C3}
Total lines = 2 + 2 = 4 (BUT note: this depends on the starting assignment choice)
The lecture point: different initial “assigned zeros” choices can change the marking trace. To be precise, the true minimum lines equals the maximum matching size among zeros. In this example, the maximum independent zeros is 3, so the minimum cover is 3 lines in the optimal cover (your calculator finds this reliably via matching logic).
Conclusion: Because the maximum number of independent zeros is 3 < 4, the matrix does not yet allow a full assignment from zeros. The next step is the θ adjustment step, which creates new zeros and increases matching size.
Example 2 — 4×4: cover requires 4 lines, so an assignment exists among zeros
Reduced matrix:
C1 C2 C3 C4 R1 0 2 1 3 R2 2 0 3 0 R3 1 3 0 2 R4 3 1 2 0
Step A — Select independent zeros (easy here)
We can pick one zero in each row and each column without conflict:
(R1,C1) = 0 (R2,C2) = 0 (R3,C3) = 0 (R4,C4) = 0
This is already 4 independent zeros.
Step B — What does the minimum-lines step say?
If a full set of independent zeros exists (size n), then the minimum number of lines needed to cover all zeros is also n.
Conclusion: Minimum cover lines = 4 (= n), so you can stop iterating and read the assignment from zeros.
Example 3 — Why “quick starring” can mislead (independent zeros vs just “many zeros”)
A common beginner mistake is to think: “There are lots of zeros, so it must be solvable immediately.” But what you need is independent zeros—not just zeros.
Matrix with many zeros but conflicts:
C1 C2 C3 C4 R1 0 0 0 5 R2 0 0 0 6 R3 0 0 0 7 R4 1 2 3 0
Lecture explanation
Rows R1–R3 have zeros only in columns C1–C3. Column C4 has a zero only in row R4. You can certainly assign R4→C4. But then you still need three distinct columns among C1–C3 for R1–R3, which is possible here. Now imagine one more restriction (e.g., one of those zeros is forbidden or missing), and the perfect matching breaks immediately.
• maximum matching among zeros, and
• minimum line cover count, rather than “eyeballing” the number of zeros.
Related: feasibility with forbidden pairs: assignment feasibility / infeasibility.
Questions people ask
People ask
When do you stop in the Hungarian algorithm?
You stop iterating when the minimum number of lines needed to cover all zeros equals n (matrix size after balancing).
Then a perfect matching exists among zeros.
People ask
What does it mean if the minimum lines is less than n?
It means the zero structure cannot yet support a full set of independent zeros (no perfect matching among zeros yet). You must do the θ adjustment to create new zeros and try again.
People ask
Is “minimum lines to cover zeros” the same as “maximum independent zeros”?
Yes, for the zero graph (bipartite), the two numbers are equal (Kőnig’s theorem). Hungarian relies on this connection.
Check your answer (calculator + next links)
Tools + next reads
Use these next
Disclaimer
This page is for educational purposes only. Real-world assignments may include constraints beyond a one-to-one matrix model. Validate results with a production optimizer and domain review. Read our full disclaimer.
Sources
- Kuhn (1955). The Hungarian method for the assignment problem
- Munkres (1957). Algorithms for the Assignment and Transportation Problems