Munkres (Hungarian) Algorithm
The `munkres` library (version 1.1.4) provides a Python implementation of the Munkres (also known as the Hungarian) algorithm for solving the assignment problem, which aims to find a minimum-cost bipartite matching. It typically runs in O(n^3) time. While its original GitHub repository (linked on PyPI) is marked as unmaintained, the PyPI package remains available and provides a robust solution for assignment problems.
Warnings
- breaking As of version 1.1.0, the `munkres` library dropped support for Python 2. Users on Python 2 must use an older version (pre-1.1.0).
- gotcha The Munkres algorithm natively solves minimization problems. To solve maximization problems (e.g., maximizing profit), the cost matrix must be transformed. A common approach is to subtract all matrix elements from a sufficiently large constant (e.g., the maximum value in the matrix).
- gotcha The `munkres` module automatically pads rectangular cost matrices with zeros to make them square, as the algorithm requires a square matrix. However, this operation works on a *copy* of the input matrix, so the caller's original matrix is not modified.
- gotcha Using the `DISALLOWED` constant correctly marks an assignment as impossible. However, if using `DISALLOWED` results in a scenario where a row or column has no possible valid assignments, the `compute` method will raise an `UnsolvableMatrix` exception, indicating that no complete assignment can be found under the given constraints.
- gotcha The utility function `munkres.print_matrix()` might raise a `ValueError` (e.g., `math.log10(0)`) if the matrix contains zero values, especially in older versions or specific Python environments. This function is for display and does not affect the core algorithm.
Install
-
pip install munkres
Imports
- Munkres
from munkres import Munkres
- DISALLOWED
from munkres import Munkres, DISALLOWED
Quickstart
from munkres import Munkres, DISALLOWED
def create_cost_matrix():
# Example cost matrix: 3 workers, 3 tasks
# cost[i][j] is cost of worker i doing task j
return [
[5, 9, 1],
[10, 3, DISALLOWED],
[8, 7, 4]
]
m = Munkres()
matrix = create_cost_matrix()
# The algorithm operates on a copy of the matrix,
# so the original 'matrix' is not modified.
indexes = m.compute(matrix)
total_cost = 0
print('Assignments:')
for row, column in indexes:
value = matrix[row][column]
total_cost += value
print(f' Worker {row} assigned to task {column} with cost {value}')
print(f'Total cost: {total_cost}')