The Hungarian method is a combinatorial optimization algorithm that solves the assignment problem in polynomial time and which anticipated later primal-dual methods. It was developed and published in 1955 by Harold Kuhn, who gave the name "Hungarian method" because the algorithm was largely based on the earlier works of two Hungarian mathematicians: Dénes Kőnig and Jenő Egerváry.
James Munkres reviewed the algorithm in 1957 and observed that it is (strongly) polynomial .[3] Since then the algorithm has been known also as the Kuhn–Munkres algorithm or Munkres assignment algorithm.
The Munkres module provides an implementation of the Munkres algorithm (also called the Hungarian algorithm or the Kuhn-Munkres algorithm), useful for solving the Assignment Problem.
In [46]:
from munkres import Munkres, make_cost_matrix, print_matrix
In [43]:
matrix = [[12, 4, 2],
[8, 7, 6],
[7, 5, 2]]
print_matrix(matrix, msg='Profit Matrix')
In [44]:
cost_matrix = make_cost_matrix(matrix)
print_matrix(cost_matrix, msg='Cost Matrix')
In [45]:
m = Munkres()
indexes = m.compute(cost_matrix)
total = 0
for row, column in indexes:
value = matrix[row][column]
total += value
print('(%d, %d) -> %d' % (row, column, value))
print('Total profit=%d' % total)
In [ ]: