Fill in the cost matrix of an assignment problem and click on 'Solve'. The optimal assignment will be determined and a step by step explanation of the hungarian algorithm will be given.

Fill in the cost matrix (random cost matrix):

Size: 3x3 4x4 5x5 6x6 7x7 8x8 9x9 10x10

This is the original cost matrix:

75 | 46 | 43 | 8 |

90 | 45 | 95 | 6 |

92 | 59 | 27 | 2 |

18 | 25 | 69 | 41 |

**Subtract row minima**

We subtract the row minimum from each row:

67 | 38 | 35 | 0 | (-8) |

84 | 39 | 89 | 0 | (-6) |

90 | 57 | 25 | 0 | (-2) |

0 | 7 | 51 | 23 | (-18) |

**Subtract column minima**

We subtract the column minimum from each column:

67 | 31 | 10 | 0 |

84 | 32 | 64 | 0 |

90 | 50 | 0 | 0 |

0 | 0 | 26 | 23 |

(-7) | (-25) |

**Cover all zeros with a minimum number of lines**

There are 3 lines required to cover all zeros:

67 | 31 | 10 | 0 | |

84 | 32 | 64 | 0 | |

90 | 50 | 0 | 0 | x |

0 | 0 | 26 | 23 | x |

x |

**Create additional zeros**

The number of lines is smaller than 4. The smallest uncovered number is 10. We subtract this number from all uncovered elements and add it to all elements that are covered twice:

57 | 21 | 0 | 0 |

74 | 22 | 54 | 0 |

90 | 50 | 0 | 10 |

0 | 0 | 26 | 33 |

**Cover all zeros with a minimum number of lines**

There are 3 lines required to cover all zeros:

57 | 21 | 0 | 0 | |

74 | 22 | 54 | 0 | |

90 | 50 | 0 | 10 | |

0 | 0 | 26 | 33 | x |

x | x |

**Create additional zeros**

The number of lines is smaller than 4. The smallest uncovered number is 21. We subtract this number from all uncovered elements and add it to all elements that are covered twice:

36 | 0 | 0 | 0 |

53 | 1 | 54 | 0 |

69 | 29 | 0 | 10 |

0 | 0 | 47 | 54 |

**Cover all zeros with a minimum number of lines**

There are 4 lines required to cover all zeros:

36 | 0 | 0 | 0 | x |

53 | 1 | 54 | 0 | x |

69 | 29 | 0 | 10 | x |

0 | 0 | 47 | 54 | x |

**The optimal assignment**

Because there are 4 lines required, the zeros cover an optimal assignment:

36 | 0 | 0 | 0 |

53 | 1 | 54 | 0 |

69 | 29 | 0 | 10 |

0 | 0 | 47 | 54 |

This corresponds to the following optimal assignment in the original cost matrix:

75 | 46 | 43 | 8 |

90 | 45 | 95 | 6 |

92 | 59 | 27 | 2 |

18 | 25 | 69 | 41 |

The optimal value equals 97.

HungarianAlgorithm.com © 2013-2019