Maximum and minimum cost assignments using the Munkres/Hungarian algorithm for n × m cost matrices
Project description
Munk
Maximum and minimum cost assignments using the Hungarian/Munkres algorithm for n × m cost matrices.
Install
pip install munk
Looking for a JavaScript implementation? Check out @alg/munkres on JSR.
Examples
Munk proves min/max cost and assignment functions.
The cost functions return the cost of a maximum or minimum assignment:
from munk import min_cost, max_cost
cost_matrix = [
[8, 4, 7],
[5, 2, 3],
[9, 4, 8],
];
print(min_cost(cost_matrix)); # 15
print(max_cost(cost_matrix)); # 18
Often, people refer to "minimising cost matrices" and "maximising profit
matrices", Munk makes no distinction between profits and costs—a matrix of
values is provided and the values in an assignment are either minimised
(min_cost) or maximised (max_cost).
The assignment functions return the indices in the cost matrix for a minimum or maximum assignment:
from munk import min_assignment, max_assignment
cost_matrix = [
[8, 4, 7],
[5, 2, 3],
[9, 4, 8],
];
print(min_assignment(cost_matrix)); # [(0, 0), (1, 2), (2, 1)]
print(max_assignment(cost_matrix)); # [(0, 0), (1, 1), (2, 2)]
All cost and assignment functions handle any n × m matrices:
from munk import min_assignment, max_assignment
wide = [
[4, 5, 8, 7],
[2, 12, 6, 5],
[7, 8, 3, 9],
];
long = [
[11, 13, 13],
[7, 21, 13],
[10, 7, 15],
[17, 11, 13],
[10, 13, 14],
];
print(min_assignment(wide)) # [(0, 1), (1, 0), (2, 2)]
print(max_assignment(long)) # [(1, 1), (2, 2), (3, 0)]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file munk-0.0.1.tar.gz.
File metadata
- Download URL: munk-0.0.1.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98d0f6271dc84601b3cbddf7f6ca4a24cfeba1ab5620046e28352678a4a90e9b
|
|
| MD5 |
c7ec4fb49e396228fe8abe688a693094
|
|
| BLAKE2b-256 |
706c934e8c536aa7122e8255754db9578f4f7a236e0a089264f682f9ecbb5bd7
|
File details
Details for the file munk-0.0.1-py3-none-any.whl.
File metadata
- Download URL: munk-0.0.1-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5206d83291975364b17b97fc7889fd6dfb78c7340f411cfa663ca3f370e3c2d1
|
|
| MD5 |
63b49be3c54136548cdfd8f21cfbaf78
|
|
| BLAKE2b-256 |
7e67374f47c08aa35f4ba96a822b5043b832659c3b08b1f928a3276183143d09
|