Skip to main content

Maximum and minimum cost assignments using the Munkres/Hungarian algorithm for n × m cost matrices

Project description

Munk

Repository PyPi License

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

munk-0.0.2.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

munk-0.0.2-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file munk-0.0.2.tar.gz.

File metadata

  • Download URL: munk-0.0.2.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for munk-0.0.2.tar.gz
Algorithm Hash digest
SHA256 392475df944a85f7d86ed9c76d5e464911e58d1f2a6eb00fd4484b9f33bc433c
MD5 91a463aa02057945cd9d44a10d016ed8
BLAKE2b-256 a1ef94b22c5025e26bc5602ef4c02116e0302cad216e3d1cd41113f3f03eca33

See more details on using hashes here.

File details

Details for the file munk-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: munk-0.0.2-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.13

File hashes

Hashes for munk-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ca2d21b2d283b46c1af1f3972b6b2a4e22254ea2c2d270c2c780cbb3b4fdb8d2
MD5 f84e32dd85fadfb75cc333a53e00bedb
BLAKE2b-256 461cf069009c2b2b05233c0f1e13100d35076a48487d2001e48b689aecc08abf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page