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:

51 | 96 | 50 | 31 |

91 | 12 | 10 | 71 |

64 | 41 | 3 | 84 |

93 | 78 | 55 | 92 |

**Subtract row minima**

We subtract the row minimum from each row:

20 | 65 | 19 | 0 | (-31) |

81 | 2 | 0 | 61 | (-10) |

61 | 38 | 0 | 81 | (-3) |

38 | 23 | 0 | 37 | (-55) |

**Subtract column minima**

We subtract the column minimum from each column:

0 | 63 | 19 | 0 |

61 | 0 | 0 | 61 |

41 | 36 | 0 | 81 |

18 | 21 | 0 | 37 |

(-20) | (-2) |

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

There are 3 lines required to cover all zeros:

0 | 63 | 19 | 0 | x |

61 | 0 | 0 | 61 | x |

41 | 36 | 0 | 81 | |

18 | 21 | 0 | 37 | |

x |

**Create additional zeros**

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

0 | 63 | 37 | 0 |

61 | 0 | 18 | 61 |

23 | 18 | 0 | 63 |

0 | 3 | 0 | 19 |

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

There are 4 lines required to cover all zeros:

0 | 63 | 37 | 0 | x |

61 | 0 | 18 | 61 | x |

23 | 18 | 0 | 63 | x |

0 | 3 | 0 | 19 | x |

**The optimal assignment**

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

0 | 63 | 37 | 0 |

61 | 0 | 18 | 61 |

23 | 18 | 0 | 63 |

0 | 3 | 0 | 19 |

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

51 | 96 | 50 | 31 |

91 | 12 | 10 | 71 |

64 | 41 | 3 | 84 |

93 | 78 | 55 | 92 |

The optimal value equals 139.

HungarianAlgorithm.com © 2013-2020