fastlap solves the linear assignment problem — also known as the maximum weight matching in bipartite graphs — at high speed from Python. It ships six production-grade algorithms (LAPJV, Hungarian, LAPMOD, Dantzig, Auction, Subgradient) behind a single solve_lap() call, with optional parallel batch solving and weighted cost support.
If you work with object tracking, task scheduling, resource allocation, matching algorithms, or combinatorial optimisation, fastlap gives you an drop-in Rust accelerator for the core assignment step.
Why fastlap?
| fastlap (Rust) | scipy.optimize | lapjv (Python) | |
|---|---|---|---|
| Speed | Sub-ms on 100×100 | ~ms | ~ms |
| Algorithms | 6 | 1 | 1 |
| Batch parallel | solve_lap_batch |
manual | manual |
| Weighted costs | built-in | no | no |
| Rectangular matrices | yes | yes | yes |
| Input validation | NaN/Inf/empty guard | basic | none |
| Dependencies | numpy | numpy+scipy | numpy+cython |
Installation
# From source (requires Rust toolchain)
git clone https://github.com/LakoreAI/fastlap.git
cd fastlap
pip install maturin && maturin develop
# Or via pip (once published)
pip install fastlap
Requirements: Python ≥ 3.9, NumPy ≥ 1.26.
Quick Start
import fastlap
cost_matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]
total_cost, row_assign, col_assign = fastlap.solve_lap(cost_matrix, algorithm="lapjv")
print(total_cost) # 15.0
print(row_assign) # [0, 1, 2]
print(col_assign) # [0, 1, 2]
solve_lap accepts plain Python lists, NumPy arrays, or SciPy CSR sparse matrices. Unassigned entries return None:
import numpy as np
# Rectangular 2×3 matrix — one column is unassigned
cost, rows, cols = fastlap.solve_lap(
np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64), algorithm="lapjv"
)
print(cols) # [0, 1, None] — column 2 unassigned
Six Algorithms
| Algorithm | Time Complexity | Optimal? | Square-only | Best for |
|---|---|---|---|---|
| LAPJV | O(n³) | Yes | No | General-purpose default |
| Hungarian | O(n³) | Yes | No | Classical / academic use |
| LAPMOD | O(n³) | Yes | No | Sparse-aware formulations |
| Dantzig | O(n³) | Yes | No | Simplex-based workflows |
| Auction | O(n²·k) | ε-optimal | Yes | Large sparse cost matrices |
| Subgradient | O(n³ + iters·n²) | Yes | Yes | Dual-based warm-up |
>>> fastlap.get_supported_algorithms()
['lapjv', 'hungarian', 'lapmod', 'subgradient', 'auction', 'dantzig']
Select with the algorithm parameter — all return the same format: (cost, row_assign, col_assign).
Batch Solving (Parallel)
Solve hundreds of independent assignment problems across all CPU cores:
import numpy as np
import fastlap
matrices = [np.random.rand(50, 50) for _ in range(500)]
results = fastlap.solve_lap_batch(matrices, algorithm="lapjv")
# Each result is (cost, row_assign, col_assign)
costs = [r[0] for r in results]
Uses Rayon internally — linear speedup with core count.
Weighted Costs
Multiply each entry by a per-element weight before solving (useful in tracking pipelines where confidence scores gate assignment costs):
import numpy as np
import fastlap
cost = np.array([[1, 2], [3, 4]], dtype=np.float64)
weights = np.array([[1, 0.5], [0.5, 1]], dtype=np.float64)
total, rows, cols = fastlap.solve_lap_weighted(cost, weights, algorithm="lapjv")
The returned total_cost is computed from the original (unweighted) matrix.
Input Validation
fastlap rejects invalid inputs with clear error messages:
import fastlap, numpy as np
# NaN
fastlap.solve_lap(np.array([[1, float("nan")], [3, 4]]), "lapjv")
# ValueError: Matrix contains NaN at position [0, 1]
# Inf
fastlap.solve_lap(np.array([[1, float("inf")], [3, 4]]), "lapjv")
# ValueError: Matrix contains infinite value at position [0, 1]
# Empty
fastlap.solve_lap(np.array([]), "lapjv")
# ValueError: Matrix must not be empty
Use Cases
- Object tracking — frame-to-frame data association (Hungarian tracker, SORT, DeepSORT)
- Task scheduling — assign jobs to machines minimising total cost
- Resource allocation — match supply to demand in logistics
- Graph matching — bipartite matching in network analysis
- Experimental design — optimal matching in causal inference
- Robotics — multi-robot task allocation
Performance Benchmarks
Run the built-in benchmark yourself:
uv run pytest tests/test_correctness.py -k benchmark -v
Or use the comparison script:
uv run python examples/examples.py
Citation
If you use fastlap in research, please cite:
@software{fastlap2025,
author = {Le Duc Minh},
title = {fastlap: A High-Performance Python LAP Solver Powered by Rust},
year = {2025},
publisher = {GitHub},
url = {https://github.com/LakoreAI/fastlap},
note = {Python-Rust implementation of LAPJV, Hungarian, LAPMOD, Dantzig, Auction, and Subgradient algorithms}
}
FAQ
What is the linear assignment problem?
The linear assignment problem (LAP) is a combinatorial optimisation problem: given an n×n cost matrix, find a one-to-one mapping (permutation) between rows and columns that minimises the total cost. It is polynomially solvable (unlike the travelling salesman problem) and appears in many applied contexts.
How do I choose an algorithm?
Use LAPJV (the default) unless you have a specific reason not to. For large sparse matrices where ε-optimal solutions are acceptable, try Auction. For rectangular matrices, use LAPJV, Hungarian, LAPMOD, or Dantzig — Auction and Subgradient fall back to padded SAP internally.
Does fastlap support GPU acceleration?
Not yet. All computation runs on the CPU via Rust. GPU support is on the roadmap.
How does fastlap compare to scipy.optimize.linear_sum_assignment?
fastlap is typically 2–10× faster for matrices up to 1000×1000, offers six algorithms (SciPy only implements one), supports parallel batch solving, and provides input validation. SciPy is a better choice if you already depend on it and performance is not critical.
Can I use fastlap with PyTorch/TensorFlow tensors?
Convert to NumPy first: fastlap.solve_lap(tensor.numpy(), algorithm="lapjv").
Contributing
See CONTRIBUTING.md for development setup, testing, and how to add a new algorithm.
License
MIT — see LICENSE.
Contact
Open an issue at github.com/LakoreAI/fastlap/issues.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastlap-0.2.1-cp312-cp312-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: fastlap-0.2.1-cp312-cp312-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 376.4 kB
- Tags: CPython 3.12, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df5a3c89a3bdcf3b6958bc35ac1298e90a21df87f44cfb45d528d7da134e0db9
|
|
| MD5 |
000c252100a2bb4feae7f302c980f0f7
|
|
| BLAKE2b-256 |
0f45c96eadb2c274ad6329d4ce5c3aca0f208db68d140863b103ebbf26cbe151
|