Given an cost matrix, find a perfect matching (one-to-one assignment of agents to tasks) with minimum total cost.
The problem is modeled as a weighted bipartite graph, with agents on one side and tasks on the other. The goal is to find a perfect matching that minimizes the sum of weights (costs).
Original Cost Matrix
The original cost matrix:

Can be represented mathematically as :
Where indexes rows (agents), and indexes columns (tasks).
Step 1: Row Reduction (Initialize )
Initialize potentials , and subtract from each row to ensure every row has at least one zero. This builds a feasible dual solution and begins construction of the equality graph, which consists of zero-weight edges.
For each row , find the minimum element and subtract it from all entries in that row:
This ensures every row has at least one zero, reducing the matrix while preserving the relative cost differences between assignments. Additionally, this subtraction initialises a feasible labelling for .
This transforms the cost matrix into one where each row has a zero, making it easier to build an equality graph, which contains only zero-weight edges.

Step 2: Column Reduction
Initialize and subtract from each column to ensure at least one zero per column. After this, we update our equality graph again—edges in this graph are where cost equals (i.e., zero entries).
Now for each column , subtract the minimum value in that column from all entries in the column:
This ensures each column also has at least one zero. Now, both rows and columns are guaranteed to have zeros, increasing the density of the equality graph and improving the chances of finding a perfect matching.
The resulting zero entries define edges in the equality graph. Only edges with zero reduced cost are part of this graph, and we attempt to find a matching within it.
At this point we have a zero-reduced matrix , and our current dual variables are:
We will now build a bipartite graph with an edge if . This is our equality graph: the set of edges that satisfy .

Step 3: Initial Assignment (Matching)/Find A Maximum Matching In Equality Graph
Step 3.1 Row Based Assignment
Assign as many agents to tasks as feasible.
So scan each row. If there is exactly one zero, assign that agent to the corresponding task. Cross out all other zeros in the same column.
For each row I, if cell (I, j) is the only 0 valued cell available for assignment in that row, then assign xi,j = 1 (i.e. Agent I is matched to Task j for this iteration), and cross out all other 0 cells in column j.
If multiple 0s are available in any row, ignore that row
This greedily assigns unique matches and prevents conflicts. It reduces the size of the problem by locking in confident assignments.
We look for rows that contain exactly one zero (i.e., unique possible assignment) and tentatively assign:
- Row 1 (): only zero is at
- Assign
- Cross out all other ‘s at column as column is assigned
- Row 2: only zero is at
- Assign
- Row 3: multiple zeros, ignore.
- Row 4: only zero is at , but already taken so skip
- Row 5: only zero is at , but already taken so skip
Tentative matching results in:
model the xij matrix here as well in math syntax

Now, scan each column. If there’s only one zero in a column, assign the task to that agent and cross out other zeros in the same row. This picks up more assignments missed in 3.1 and helps us get closer to a perfect matching.
Now apply the column-based assignment rule: For each column , if there is exactly one zero (i.e., one possible assignment), assign that task to the corresponding agent.
step 3.2 –repeat step 3.1, but now for columns, instead of rows: Assign as many tasks to agents as feasible: For each column j, if cell (I, j) is the only 0 valued cell available in that column, then assign xi,j = 1 (i.e. Task j is matched to Agent I for this iteration), and cross out all other 0 cells in row I. If multiple 0s are available, ignore that column.
After both steps, we may still have unmatched rows or columns. If , we are done.
Only consider rows/columns not already assigned or eliminated. From the reduced cost matrix:
With current assignments:
- Column 1 (): only zero is at row
- Assign
- Cross out all other ‘s at row
- Column 2: Already assigned, skip
- Column 3: Already assigned, skip
- Column 4: Only one zero at row , but already taken so skip
- Column 5: Only one zer oat but already taken so skip

Caveat About Steps 3.1 And Steps 3.2
- After completion of step 3.2, a situation may arise that not ALL 0 valued cells in the matrix are crossed out or assigned.
- This indicates potentially multiple possible optimal matchings for the given problem.
- To ensure all zeros are crossed out or assigned after step 3.2, an approach is to break the impasse by
- choosing an aribitrary assignment among the uncrossed 0s,
- and repeating steps 3.1 and 3.2, until all 0 cells are crossed out or assigned.
Step 3.3
If , we must improve the labeling to create more zero entries.
- Mark all unassigned rows.
These rows indicate agents not yet matched. We begin our marking here.

- If row is marked, for any 0 cell in that row, mark its corresponding column.
This explores the equality graph to find alternate paths or augmenting paths for matching.

- For any marked column with an assignment (assigned zero), mark its corresponding row of that assignment.
This helps propagate markings to potentially find alternating paths or build towards the minimum vertex cover.

Repeat steps 3.3.2 and 3.3.3 until no new rows/columns can be marked.
- Cover all unmarked rows and marked columns.
The number of covered row and columns represent the number of assignments in the reduced cost matrix.
This set of lines gives a minimum vertex cover in the equality graph, according to Kőnig’s theorem. The size of this cover equals the size of the maximum matching.

Step 4: Update Labeling & Cost Matrix
If |M| = N, (i.e. perfect matching reached), terminate the algorithm.
- The current M will be the optimal assignment, and current PN I=1 ui + PN j=1 vj value will be the min cost
else if |M| < N (i.e. perfect matching not yet reached), then
- Identify the minimum number among cells not covered.
- For every uncovered row, increase .
- For every covered column, decrease .
These updates preserve feasibility and increase the number of zero entries in the matrix, creating new edges in the equality graph.

Return to Step 3 and repeat until (i.e., a perfect matching is found).
Step 5: Repeat Matching
Once a perfect matching is found extract the assignment from the final matching and compute the total cost:
Or equivalently, since complementary slackness holds: