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.3.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.3-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: munk-0.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 7354a96a3ae5167ac2344743f415b92e706f0b1f154d9fe7bd5a90ca70524084
MD5 aa9ebf40d53399a117976e85d291c3a0
BLAKE2b-256 9c942001a8df1e02a1c3e94ce1b1c002d44eb0b39cdd273fca6e868a4a1dc517

See more details on using hashes here.

File details

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

File metadata

  • Download URL: munk-0.0.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8cce178e9785539621974a8d3a7bb50f255f87a7bb75981609910d747c73239d
MD5 647c1e2659beb33b17f28751d3c027aa
BLAKE2b-256 1e5bd10ec85e88890c4597320961b1918d0ccead94056e4568d4ca68cc61b965

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