{"id":47088062,"url":"https://github.com/havelessbemore/munkres","last_synced_at":"2026-03-12T08:55:12.310Z","repository":{"id":229929935,"uuid":"778050077","full_name":"havelessbemore/munkres","owner":"havelessbemore","description":"A lightweight and efficient implementation of the Munkres (Hungarian) algorithm for optimal assignment.","archived":false,"fork":false,"pushed_at":"2024-10-14T10:44:46.000Z","size":1724,"stargazers_count":2,"open_issues_count":7,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-10T23:53:04.520Z","etag":null,"topics":["assignment-problem","combinatorial-optimization","hungarian-algorithm","munkres-algorithm","optimization"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/havelessbemore.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":"AUTHORS","dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"havelessbemore"}},"created_at":"2024-03-27T01:25:50.000Z","updated_at":"2024-11-17T15:24:15.000Z","dependencies_parsed_at":"2024-04-15T15:41:05.023Z","dependency_job_id":"bc857e1f-c69b-4116-8b88-3eae6469099e","html_url":"https://github.com/havelessbemore/munkres","commit_stats":null,"previous_names":["havelessbemore/munkres"],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/havelessbemore/munkres","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/havelessbemore%2Fmunkres","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/havelessbemore%2Fmunkres/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/havelessbemore%2Fmunkres/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/havelessbemore%2Fmunkres/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/havelessbemore","download_url":"https://codeload.github.com/havelessbemore/munkres/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/havelessbemore%2Fmunkres/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30420528,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-12T07:30:13.030Z","status":"ssl_error","status_checked_at":"2026-03-12T07:29:54.885Z","response_time":114,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assignment-problem","combinatorial-optimization","hungarian-algorithm","munkres-algorithm","optimization"],"created_at":"2026-03-12T08:54:44.646Z","updated_at":"2026-03-12T08:55:05.214Z","avatar_url":"https://github.com/havelessbemore.png","language":"TypeScript","funding_links":["https://github.com/sponsors/havelessbemore"],"categories":[],"sub_categories":[],"readme":"# Munkres\n\nA lightweight and efficient implementation of the [Munkres (Hungarian) algorithm](https://en.wikipedia.org/wiki/Hungarian_algorithm) for optimal assignment.\n\n[![Version](https://img.shields.io/npm/v/munkres.svg)](https://www.npmjs.com/package/munkres)\n[![JSR](https://jsr.io/badges/@munkres/munkres)](https://jsr.io/@munkres/munkres)\n[![Maintenance](https://img.shields.io/maintenance/yes/2024.svg)](https://github.com/havelessbemore/munkres/graphs/commit-activity)\n[![License](https://img.shields.io/github/license/havelessbemore/munkres.svg)](https://github.com/havelessbemore/munkres/blob/master/LICENSE)\n[![codecov](https://codecov.io/gh/havelessbemore/munkres/graph/badge.svg?token=F362G7C9U0)](https://codecov.io/gh/havelessbemore/munkres)\n![npm bundle size](https://img.shields.io/bundlephobia/minzip/munkres)\n\n## Features\n\n1. Flexible\n\n   - Use `number` or `bigint` matrices.\n   - Use square (_NxN_) or rectangular (_MxN_) matrices.\n   - Works with any [MatrixLike](src/types/matrixLike.ts) input. Use arrays, typed arrays, custom objects, etc.\n\n1. Fast ([benchmarks](#results))\n\n   - _O(M\u003csup\u003e2\u003c/sup\u003eN)_ when _M \u003c= N_\n\n   - _O(MN\u003csup\u003e2\u003c/sup\u003e)_ when _M \u003e N_\n\n1. Efficient\n\n   - _O(M + N)_ memory\n\n1. Robust\n   - Supports `-Infinity` and `Infinity` values.\n   - [Helper methods](#helpers) provided for creating and modifying matrices.\n\n## Getting Started\n\n### Install\n\nNPM:\n\n```bash\nnpm install munkres\n```\n\nYarn:\n\n```bash\nyarn add munkres\n```\n\nJSR:\n\n```bash\njsr add @munkres/munkres\n```\n\n## Usage\n\nWith a cost matrix:\n\n```javascript\nimport munkres from \"munkres\";\n\n// Create a cost matrix. Cell [y, x] is the cost\n// of assigning the y-th worker to the x-th job.\nconst costMatrix = [\n  [1, 2, 3],\n  [2, 4, 6],\n  [3, 6, 9],\n];\n\n// Find a set of optimal assignments pairs (y, x).\nconst assignments = munkres(costMatrix);\n\nconsole.log(assignments);\n// Output: [[0, 2], [1, 1], [2, 0]]\n```\n\nWith a profit matrix:\n\n```javascript\nimport { munkres, copyMatrix, invertMatrix } from \"munkres\";\n\n// Create a profit matrix. Cell [y, x] is the\n// profit of assigning the y-th worker to the x-th job.\nconst profitMatrix = [\n  [9, 8, 7],\n  [8, 6, 4],\n  [7, 4, 1],\n];\n\n// Covert the profit matrix into a cost matrix.\nconst costMatrix = copyMatrix(profitMatrix);\ninvertMatrix(costMatrix);\n\n// Find a set of optimal assignments pairs (y, x).\nconst assignments = munkres(costMatrix);\n\nconsole.log(assignments);\n// Output: [[0, 2], [1, 1], [2, 0]]\n```\n\n## API\n\n- `munkres(costMatrix)`\n\n  Executes the Munkres algorithm on the given cost matrix and returns a set of optimal assignment pairs. Even if there are multiple optimal assignment sets, only one is returned.\n\n### Types\n\n- [`Matrix\u003cT\u003e`](src/types/matrix.ts): A generic 2D matrix (i.e. `T[][]`).\n\n- [`MatrixLike\u003cT\u003e`](src/types/matrixLike.ts): A generic read-only 2D matrix.\n\n  - Can be made from any `ArrayLike` objects (i.e. any indexable object with a numeric `length` property). This\n    allows for more flexibility, such as matrices made with typed arrays or objects.\n\n- [`Pair\u003cA, B = A\u003e`](src/types/pair.ts): A generic pair (i.e. `[A, B]`).\n\n### Helpers\n\n1. `copyMatrix(matrix)`: Creates a copy of the given matrix.\n\n1. `createMatrix(workers, jobs, callbackFn)`: Generates a matrix based on the given workers, jobs, and callback function.\n\n1. `genMatrix(numRows, numCols, callbackFn)`: Generates a matrix based on the given dimensions and a callback function.\n\n1. `getMatrixMax(matrix)`: Finds the maximum value in a given matrix.\n\n1. `getMatrixMin(matrix)`: Finds the minimum value in a given matrix.\n\n1. `invertMatrix(matrix, bigVal?)`: Inverts the values in the given matrix. Useful for converting between minimizing and maximizing problems. If `bigVal` is not given, the matrix's max value is used instead.\n\n1. `negateMatrix(matrix)`: Negates all values in the given matrix. Useful for converting between minimizing and maximizing problems.\n\n## Community and Support\n\nContributions are welcome!\n\n- **Questions / Dicussions**: Please contact us via [GitHub discussions](https://github.com/havelessbemore/munkres/discussions).\n\n- **Bug Reports**: Please use the [GitHub issue tracker](https://github.com/havelessbemore/munkres/issues) to report any bugs. Include a detailed description and any relevant code snippets or logs.\n\n- **Feature Requests**: Please submit feature requests as issues, clearly describing the feature and its potential benefits.\n\n- **Pull Requests**: Please ensure your code adheres to the existing style of the project and include any necessary tests and documentation.\n\nFor more information, check out the [contributor's guide](https://github.com/havelessbemore/munkres/CONTRIBUTING.md).\n\n## Build\n\n1. Clone the project from github\n\n```bash\ngit clone git@github.com:havelessbemore/munkres.git\ncd munkres\n```\n\n2. Install dependencies\n\n```bash\nnpm install\n```\n\n3. Build the project\n\n```bash\nnpm run build\n```\n\nThis will output ECMAScript (`.mjs`) and CommonJS (`.js`) modules in the `dist/` directory.\n\n## Format\n\nTo run the code linter:\n\n```bash\nnpm run lint\n```\n\nTo automatically fix linting issues, run:\n\n```bash\nnpm run format\n```\n\n## Test\n\nTo run tests:\n\n```bash\nnpm test\n```\n\nTo run tests with a coverage report:\n\n```bash\nnpm run test:coverage\n```\n\nA coverage report is generated at `./coverage/index.html`.\n\n## Benchmarks\n\n[Run benchmarks in your browser](https://jsbm.dev/QHoz2G2XHQknL).\n\nTo run locally:\n\n```bash\nnpm run bench\n```\n\n### CI / CD\n\nBenchmarks are integrated into our CI/CD pipeline and automatically run with each commit to the `main` branch. This helps monitor the performance impacts of development, preventing regressions and verifying changes maintain performance standards.\n\n[View historical results](https://havelessbemore.github.io/munkres/dev/bench/).\n\nSpecs:\n\n- Package version: latest\n- Runtime: NodeJS v20\n- Benchmarking Tool: tinybench v2.6.0\n- OS: [ubuntu-latest](https://github.com/actions/runner-images)\n\n### Results\n\nBelow are the latest results from running locally.\n\nSpecs:\n\n- Package version: v2.0.2\n- Runtime: NodeJS v20.12.2\n- Benchmarking Tool: tinybench v2.6.0\n- Machine:\n  - Model: MacBook Air\n  - Chip: Apple M2\n  - Memory: 8 GB\n  - OS: MacOS Sonoma\n\n#### `number[][]`\n\n| Dimensions | Min (ms)    | Max (ms)    | Avg (ms)    | Samples   |\n| ---------- | ----------- | ----------- | ----------- | --------- |\n| 2x2        | 0           | 0.92475     | 0.00014     | 3,611,437 |\n| 4x4        | 0.00013     | 0.38683     | 0.00041     | 1,227,622 |\n| 8x8        | 0.00062     | 0.2005      | 0.00134     | 372,763   |\n| 16x16      | 0.002       | 0.26346     | 0.00487     | 102,720   |\n| 32x32      | 0.009       | 0.06462     | 0.01874     | 26,678    |\n| 64x64      | 0.04483     | 0.28279     | 0.07837     | 6,381     |\n| 128x128    | 0.228       | 0.52058     | 0.34338     | 1,457     |\n| 256x256    | 1.07725     | 2.37567     | 1.59139     | 315       |\n| 512x512    | 5.86442     | 9.18546     | 7.71347     | 65        |\n| 1024x1024  | 31.82083    | 45.471      | 38.12961    | 50        |\n| 2048x2048  | 165.45792   | 245.93154   | 205.5154    | 50        |\n| 4096x4096  | 905.25592   | 1,384.78808 | 1,129.78629 | 50        |\n| 8192x8192  | 5,062.63533 | 7,561.39129 | 6,152.11964 | 50        |\n\n#### `bigint[][]`\n\n| Dimensions | Min (ms)     | Max (ms)     | Avg (ms)     | Samples   |\n| ---------- | ------------ | ------------ | ------------ | --------- |\n| 2x2        | 0.00004      | 1.45567      | 0.00018      | 2,763,520 |\n| 4x4        | 0.00029      | 0.61792      | 0.00058      | 860,516   |\n| 8x8        | 0.00121      | 0.63829      | 0.00246      | 203,435   |\n| 16x16      | 0.0045       | 0.639        | 0.00994      | 50,320    |\n| 32x32      | 0.02021      | 0.66221      | 0.04299      | 11,633    |\n| 64x64      | 0.11575      | 1.00283      | 0.19514      | 2,563     |\n| 128x128    | 0.53988      | 1.92654      | 0.9169       | 546       |\n| 256x256    | 3.32492      | 6.38088      | 4.51322      | 112       |\n| 512x512    | 15.75375     | 46.94746     | 25.27091     | 50        |\n| 1024x1024  | 107.30771    | 171.37271    | 129.81464    | 50        |\n| 2048x2048  | 546.04767    | 850.22892    | 689.95893    | 50        |\n| 4096x4096  | 2,835.29229  | 4,583.96688  | 3,697.85205  | 50        |\n| 8192x8192  | 18,069.79829 | 24,171.88267 | 21,107.47192 | 10        |\n\n---\n\nMade with ❤️ by [Michael Rojas](https://github.com/havelessbemore)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhavelessbemore%2Fmunkres","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhavelessbemore%2Fmunkres","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhavelessbemore%2Fmunkres/lists"}