Registry / ai-ml / lap
library0.5.13pypypi✓ verified 22d ago

The 'lap' library provides a fast Python solver for the Linear Assignment Problem (LAP) using the Jonker-Volgenant algorithm for both dense (LAPJV) and sparse (LAPMOD) cost matrices. It's implemented from scratch based on original research papers. Currently at version 0.5.13, it sees active but infrequent releases, usually a few per year, ensuring ongoing maintenance and compatibility updates.

pip install lap
INSTALL
IMPORT
SIG · LAP
L
lap
ai-mlpythonv0.5.13
Install
3.7s avg
Import
254ms
Disk
92MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.5.13 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.242s · 91.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.7s · import 0.266s · 88MB
92MB installed
● package 92MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

lapjv
from lap import lapjv
import lap
lapmod
from lap import lapmod
import lap
LARGE
from lap import LARGE
import lap

This quickstart demonstrates how to use `lap.lapjv` for dense cost matrices and `lap.lapmod` for sparse matrices. `lapjv` can handle non-square matrices directly with `extend_cost=True`. `lapmod` is typically optimized for large, sparse *square* matrices; for non-square sparse problems, additional pre-processing or wrapper libraries might be needed.

import lap import numpy as np # Example for LAPJV (dense matrix) # C is the cost matrix, e.g., representing costs for assigning rows to columns C_dense = np.random.rand(4, 5) # 4 rows, 5 columns cost, x, y = lap.lapjv(C_dense, extend_cost=True) print(f"LAPJV Cost: {cost}\nRow assignments (x): {x}\nColumn assignments (y): {y}") # Example for LAPMOD (sparse matrix) - typically faster for larger, sparse matrices # For simplicity, using a dense matrix here, but 'lapmod' is optimized for sparse inputs. # Note: lapmod primarily expects square matrices for direct use, consider extensions for non-square. C_sparse_example = np.array([ [1, np.inf, 3, np.inf], [np.inf, 2, np.inf, 4], [5, np.inf, 6, np.inf], [np.inf, 7, np.inf, 8] ]) cost_mod, x_mod, y_mod = lap.lapmod(C_sparse_example) print(f"\nLAPMOD Cost: {cost_mod}\nRow assignments (x): {x_mod}\nColumn assignments (y): {y_mod}")
Debug
Known issues
gotchaWhen building `lap` from source (e.g., if pre-built wheels are unavailable or a custom environment requires it), a C++ compiler (like Microsoft Visual C++ 14.0 or greater on Windows) is required.
fix
Ensure a compatible C++ compiler is installed and properly configured in your environment. For Windows, install 'Microsoft C++ Build Tools' from Visual Studio.
affects: <0.5.13, potentially all versions for source build
gotchaThe `lapmod` solver is generally faster than `lapjv` for very large matrices (side > ~5000) that are also sparse (<50% finite coefficients). For smaller or denser matrices, `lapjv` might be preferred or perform similarly.
fix
Consider the characteristics of your cost matrix (size, density) when choosing between `lap.lapjv` and `lap.lapmod` to optimize performance.
affects: All versions
gotchaThe `lap.lapmod` function directly supports only square matrices. For non-square assignment problems, it's crucial to transform the cost matrix into a square form (e.g., by padding with infinite costs or using `extend_cost=True` with `lapjv`).
fix
For non-square matrices, either use `lap.lapjv(C, extend_cost=True)` or manually pad your sparse matrix to make it square before passing it to `lap.lapmod`. Libraries like `pylapy` can provide wrappers for more flexible sparse matrix handling.
affects: All versions
gotchaThe `lap` library expects a cost matrix where `np.inf` or large numbers represent prohibitive costs for assignments. Incorrectly using `0` or negative numbers for non-assignment costs can lead to unexpected results or incorrect optimal assignments.
fix
Ensure that non-assignable pairs have a cost of `np.inf` (or a sufficiently large number) in your cost matrix, and that valid assignment costs are non-negative.
affects: All versions
Upgrade
Version history
0.5.13latest on PyPI · released Feb 23, 2026
Audit
Dependencies
numpyrequiredRequired for numerical operations, specifically for representing cost matrices. `lap` v0.5.13 supports both NumPy 1.x and 2.x for Python 3.8-3.14.
Agent activity
3 hits · last 30 days
node
2
Resources
lap — pip install lap · libregistry