npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

munkres

v2.1.1

Published

A lightweight and efficient implementation of the Munkres (Hungarian) algorithm for optimal assignment in square and rectangular matrices.

Readme

Munkres

Optimally pair up two sets (workers to jobs, drivers to riders, bids to items) to minimize total cost.

Given a cost matrix where matrix[y][x] is the cost of pairing item y with item x, munkres returns the one-to-one assignment with the lowest possible total cost. It's a fast, dependency-free implementation of the Munkres (Hungarian) algorithm.

Reach for it whenever you have a table of pairing costs (or profits) and need the optimal matching rather than a greedy guess.

Version JSR License codecov npm bundle size

Features

  • Flexible: number or bigint costs; square (NxN) or rectangular (MxN) matrices; any MatrixLike input (arrays, typed arrays, custom indexables).
  • Fast: O(M²N) time when M ≤ N (O(MN²) otherwise) and only O(M + N) extra memory. Benchmarks below.
  • Typed & dependency-free: first-class TypeScript types, zero runtime dependencies, ships both ESM and CommonJS.
  • Robust: force an assignment with -Infinity or avoid one with Infinity; built-in helpers for building and transforming matrices.

Install

npm install munkres

yarn add munkres

pnpm add munkres

jsr add @munkres/munkres

Usage

import { munkres } from "munkres";

// matrix[y][x] = cost of assigning worker y to job x
const costMatrix = [
  [1, 2, 3],
  [2, 4, 6],
  [3, 6, 9],
];

const assignments = munkres(costMatrix);
// → [[0, 2], [1, 1], [2, 0]]   (worker 0 → job 2, worker 1 → job 1, worker 2 → job 0)

Each result is a [y, x] pair (row, then column). The result has min(rows, cols) pairs; if there are more workers than jobs (or vice versa), the unmatched ones are simply absent.

Maximizing instead of minimizing? Convert your profit matrix to a cost matrix first:

import { munkres, copyMatrix, invertMatrix } from "munkres";

const profitMatrix = [
  [9, 8, 7],
  [8, 6, 4],
  [7, 4, 1],
];

const costMatrix = copyMatrix(profitMatrix);
invertMatrix(costMatrix); // flip profits into costs
const assignments = munkres(costMatrix);
// → [[0, 2], [1, 1], [2, 0]]

API

munkres(costMatrix, options?)

Runs the algorithm and returns a set of optimal [y, x] assignment pairs. If several optimal assignments exist, one is returned.

  • costMatrix: a MatrixLike<number> or MatrixLike<bigint> where costMatrix[y][x] is the cost of assigning worker y to job x. Costs are minimized, so use -Infinity to force an assignment (chosen whenever possible) and Infinity to avoid one (used only as a last resort, when no finite alternative exists).
  • options.finite (boolean, default false): promise the matrix is all-finite and in-range, skipping input validation for a small speedup on large matrices. If the promise is broken, the result is undefined (it won't throw). See MunkresOptions.

Returns Pair<number>[], an array of [y, x] pairs, length min(rows, cols).

Throws

  • TypeError: a number matrix contains NaN. (Use Infinity to avoid an assignment instead.)
  • RangeError: a number matrix's value range (max - min) exceeds Number.MAX_VALUE / 2 and could overflow. Scale the matrix down, or use bigint.

Precision: the number path uses 64-bit floats. For integer costs that need an exact optimum (especially near or beyond Number.MAX_SAFE_INTEGER), use a bigint matrix for arbitrary precision.

Types

Helpers

Utilities for building and transforming cost matrices:

| Helper | Description | | --- | --- | | copyMatrix(matrix) | Copy a matrix. | | createMatrix(workers, jobs, callbackFn) | Build a matrix from worker/job lists and a cost function. | | genMatrix(numRows, numCols, callbackFn) | Build a matrix from dimensions and a callback. | | getMatrixMax(matrix) / getMatrixMin(matrix) | Find the max / min value. | | invertMatrix(matrix, bigVal?) | Invert values (max → min); converts between minimizing and maximizing. Uses the matrix max if bigVal is omitted. | | negateMatrix(matrix) | Negate all values; another way to swap minimizing and maximizing. |

Benchmarks

Performance is tracked automatically on every release and every commit to main:

As a ballpark (Apple M2, v2.1.1): | Matrix Size | number | bigint | | --- | --- | --- | | 64x64 | ~0.07ms | ~0.17ms | | 256x256 | ~1.2ms | ~3.7ms | | 1024x1024 | ~26ms | ~110ms | | 4096x4096 | ~0.73s | ~3.8s |

Contributing

Contributions are welcome. See the contributing guide for local setup, tests, and the style guide.

License

MIT © Michael Rojas