held-karp
Exact TSP solver using the Held-Karp bitmask dynamic programming algorithm, implemented in Rust with Python bindings.
Features
- Exact optimal solution — guaranteed shortest round-trip tour
- Fast — Rust implementation, 50-300× faster than pure Python
- Deterministic — same input always produces the same output
- Asymmetric support — works with non-symmetric distance matrices
- Dual interface — use from Rust (crates.io) or Python (PyPI)
Complexity
| Time | O(n² · 2ⁿ) |
| Space | O(n · 2ⁿ) |
| Max nodes | 20 |
For typical use cases (n=4–8), runs in microseconds.
Installation
Python
pip install held-karp
Rust
[dependencies]
held-karp-core = "0.1"
Usage
Python
import held_karp
# From a 2D distance matrix
matrix = [
[0, 10, 15, 20],
[10, 0, 35, 25],
[15, 35, 0, 30],
[20, 25, 30, 0],
]
path, cost = held_karp.solve(matrix)
print(f"Path: {path}, Cost: {cost}")
# Path: [0, 1, 3, 2], Cost: 80.0
# From a flat numpy array (faster, avoids nested list conversion)
import numpy as np
dist = np.array(matrix, dtype=float)
path, cost = held_karp.solve_flat(dist.ravel().tolist(), len(dist))
Rust
use held_karp_core::solve;
let dist = vec![
0.0, 10.0, 15.0, 20.0,
10.0, 0.0, 35.0, 25.0,
15.0, 35.0, 0.0, 30.0,
20.0, 25.0, 30.0, 0.0,
];
let (path, cost) = solve(&dist, 4).unwrap();
assert_eq!(path, vec![0, 1, 3, 2]);
assert_eq!(cost, 80.0);
API Reference
Python
held_karp.solve(distance_matrix) -> (list[int], float)
Solve TSP from a list of lists (n × n).
Args:
distance_matrix: Square matrix where[i][j]is the distance from nodeito nodej
Returns: (path, cost) — optimal node order (starting from 0) and total round-trip distance
held_karp.solve_flat(flat_matrix, n) -> (list[int], float)
Solve TSP from a flat row-major array of n * n floats. Useful with numpy arrays.
Rust
held_karp_core::solve(dist, n) -> Result<(Vec<usize>, f64), SolveError>
Solve from a flat row-major slice.
held_karp_core::solve_matrix(matrix) -> Result<(Vec<usize>, f64), SolveError>
Solve from a 2D Vec.
Performance
Benchmarked against a pure Python Held-Karp implementation:
| n (cities) | Python | Rust | Speedup |
|---|---|---|---|
| 4 | 12 µs | 0.23 µs | 52× |
| 6 | 155 µs | 1.3 µs | 119× |
| 8 | 2.4 ms | 8.1 µs | 296× |
License
MIT
Metadata
Release files for held-karp 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| held_karp-0.1.1.tar.gz | 7.1 kB | Without provenance | Details for held_karp-0.1.1.tar.gz |
Built distributions (wheels)
Total release size: 4.1 MB
Release files / held_karp-0.1.1.tar.gz
| Download URL | held_karp-0.1.1.tar.gz |
|---|---|
| Size | 7.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5813926f289b27bc2a0b74ae348412fe738d26c44bbc38cb9c37e1629f273042
|
|
BLAKE2b-256 checksum How to use checksums |
0c924e5e7411a6eba3643f6599f0701e0ac10e0ddc8d34c055a63fd8161f1211
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Release files / held_karp-0.1.1-cp313-cp313-win_amd64.whl
| Download URL | held_karp-0.1.1-cp313-cp313-win_amd64.whl |
|---|---|
| Size | 107.6 kB |
| Tags | CPython 3.13 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
558eb48979a3876f77bace38c2dc899fb25c951f0f485f4b5617c548186cae83
|
|
BLAKE2b-256 checksum How to use checksums |
4d5d61c038ce0796a12c21c3cb161c964da6bdd7ca6378dc7ebd36c2e21b6cfd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Release files / held_karp-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | held_karp-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 240.9 kB |
| Tags | CPython 3.13 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
5f1adc5164756262b90aeb293a6b1fd42f39fe9dd8426d8a980e9eb824ce48f5
|
|
BLAKE2b-256 checksum How to use checksums |
c22860f7b5d98349ddd9aaa3dda97ba7399b414d8aba060952b1bd6394dc5be6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Release files / held_karp-0.1.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | held_karp-0.1.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 237.6 kB |
| Tags | CPython 3.13 Linux glibc 2.17+ ARM64 |
|
SHA-256 checksum How to use checksums |
29e3d086e2fd9c94b36f36cdd96c0d6af544acaa6d9ee3be60549fb9fd7d5eda
|
|
BLAKE2b-256 checksum How to use checksums |
fe1fa7f95743cf202f45876ac159b816b4fc731c948b4f2453d07e3f10d11501
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Release files / held_karp-0.1.1-cp313-cp313-macosx_11_0_arm64.whl
| Download URL | held_karp-0.1.1-cp313-cp313-macosx_11_0_arm64.whl |
|---|---|
| Size | 210.1 kB |
| Tags | CPython 3.13 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
11b484b8c756b4f7b37fea7c8fe48d7eaab008cd34ac054a79b53b44c96494d3
|
|
BLAKE2b-256 checksum How to use checksums |
5edbd31a1faa7efef20c744bb07f1e7a9a84d487c1b0be6afcf6927c6c1da9d8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Release files / held_karp-0.1.1-cp313-cp313-macosx_10_12_x86_64.whl
| Download URL | held_karp-0.1.1-cp313-cp313-macosx_10_12_x86_64.whl |
|---|---|
| Size | 214.0 kB |
| Tags | CPython 3.13 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
5206b36b696b617604b308ba121f299552da065b354d86cc610272b2a05d3072
|
|
BLAKE2b-256 checksum How to use checksums |
014ba39d56720b25b2edac90909b4d039649bc96098c4be150e43a60385efd0b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Release files / held_karp-0.1.1-cp312-cp312-win_amd64.whl
| Download URL | held_karp-0.1.1-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 107.4 kB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
cfb9062b2bc612f9315e971b4f79d3072663984bcd296f91d56b1c01a62e0bb6
|
|
BLAKE2b-256 checksum How to use checksums |
f418d3ae191b764a2b0b83d6cb4f9164f6f6e71654829db47b314a0386ff6c33
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Release files / held_karp-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | held_karp-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 240.9 kB |
| Tags | CPython 3.12 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
422b265f82f5bcac4526cd47c1e733d4f513e2c7abdf1fd7a5b0cce3ecaea9cf
|
|
BLAKE2b-256 checksum How to use checksums |
7b82e55c7f54dfc02cdacc8d88d0d0daee6998c0f167e099f3de761e9e7956d9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Release files / held_karp-0.1.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | held_karp-0.1.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 237.7 kB |
| Tags | CPython 3.12 Linux glibc 2.17+ ARM64 |
|
SHA-256 checksum How to use checksums |
5e6cdb188c5772deb00747adc0de8ff4c3148b056a10aa2cdd49ecc31ad2d4ca
|
|
BLAKE2b-256 checksum How to use checksums |
aca031352bd7d28fca909b5468011bb417d617a0fb03476ec4b81d3f83277eeb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Release files / held_karp-0.1.1-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | held_karp-0.1.1-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 209.8 kB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
331a698c57899d4d76fba7f75d81ada4bdce749770b61704d69b41dc27c28ada
|
|
BLAKE2b-256 checksum How to use checksums |
9e1b1e31368dff6f69f0a770fe47e40581268028eb3f4e38853f9ecd806172e3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Release files / held_karp-0.1.1-cp312-cp312-macosx_10_12_x86_64.whl
| Download URL | held_karp-0.1.1-cp312-cp312-macosx_10_12_x86_64.whl |
|---|---|
| Size | 213.9 kB |
| Tags | CPython 3.12 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
b84f77cc4975c34e368c52fc78fa57103bcbfd3ce566d553a93a114ec6a2f025
|
|
BLAKE2b-256 checksum How to use checksums |
2ae3a564bd14601cf26752ecdd6b4de0193361b592eae2835b59f900365ea5c4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Release files / held_karp-0.1.1-cp311-cp311-win_amd64.whl
| Download URL | held_karp-0.1.1-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 107.7 kB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
937097b9a5cc6709c15eae22723992ed92720e04104c0fd80e3102e26723bbd2
|
|
BLAKE2b-256 checksum How to use checksums |
4ff1b2f36300e90cb4c99451f8d2a5a883f3295e07abe71b717cfde61a24e8d2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Release files / held_karp-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | held_karp-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 241.0 kB |
| Tags | CPython 3.11 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
c70f15a05d13eeb41ff529b543da37b56c72c3189468c509a31aff553f2ea2f0
|
|
BLAKE2b-256 checksum How to use checksums |
a4b33be110656730d7a5a8b6a3648b4c6d838bd41e1362fc8a21059de84c15b8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Release files / held_karp-0.1.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | held_karp-0.1.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 237.9 kB |
| Tags | CPython 3.11 Linux glibc 2.17+ ARM64 |
|
SHA-256 checksum How to use checksums |
a45f73d7fa14a3e3a7cd0ee21d689b687847fcec38d8a609bc4afac4ebe472a5
|
|
BLAKE2b-256 checksum How to use checksums |
a0ceb030651fd4cbffb6a17dac688b8ecd7cbddc0a4690813b61c42af1c475b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Release files / held_karp-0.1.1-cp311-cp311-macosx_11_0_arm64.whl
| Download URL | held_karp-0.1.1-cp311-cp311-macosx_11_0_arm64.whl |
|---|---|
| Size | 213.2 kB |
| Tags | CPython 3.11 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
589c61f3b975769134ee09fed229cefe86f1ff1fcf523a1dcda61c6b7126db4a
|
|
BLAKE2b-256 checksum How to use checksums |
233b664898e1620aeb33914a04d82aa1efcca9460b68137694d28d05d9342191
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Release files / held_karp-0.1.1-cp311-cp311-macosx_10_12_x86_64.whl
| Download URL | held_karp-0.1.1-cp311-cp311-macosx_10_12_x86_64.whl |
|---|---|
| Size | 217.1 kB |
| Tags | CPython 3.11 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
23a34327551195ea62b27ef01e6b3150547b672d5e62555fa17d5e06f1e39d48
|
|
BLAKE2b-256 checksum How to use checksums |
bbe83dca7e16ca96e0e9b16442602a061ddea58b38e7f62e3744448887229755
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Release files / held_karp-0.1.1-cp310-cp310-win_amd64.whl
| Download URL | held_karp-0.1.1-cp310-cp310-win_amd64.whl |
|---|---|
| Size | 107.9 kB |
| Tags | CPython 3.10 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
f0cb0d4928e2752f895524f51f3fa60b633ccf1bbe56b771aa5febed76305cdd
|
|
BLAKE2b-256 checksum How to use checksums |
83880b4996b06ffecdc4427550ee058283ebb14de95e1fa8c3758643b0b62aed
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Release files / held_karp-0.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | held_karp-0.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 241.2 kB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
c5c9ab8b95fc5dcd484130aefca2a0357294d71cf9df3e9eea3883b3dc66d624
|
|
BLAKE2b-256 checksum How to use checksums |
4b0af75582db0eb6969acbfabed8bd1bbf955e65fe279bb1c3ce4465c0c4ee66
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Release files / held_karp-0.1.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | held_karp-0.1.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 238.2 kB |
| Tags | CPython 3.10 Linux glibc 2.17+ ARM64 |
|
SHA-256 checksum How to use checksums |
c716a411ecc3e86ac4784dd7b1d837f9b5e30cb25ca9e44688d48c130ccd5dd2
|
|
BLAKE2b-256 checksum How to use checksums |
3696b36319fc51ffa6eefd1a279bd351227051b49238c9d65cd51c11b1b472f6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Release files / held_karp-0.1.1-cp310-cp310-macosx_11_0_arm64.whl
| Download URL | held_karp-0.1.1-cp310-cp310-macosx_11_0_arm64.whl |
|---|---|
| Size | 213.6 kB |
| Tags | CPython 3.10 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
41eaa86f130c01f604c55cb4ebedd98fc450d69294c9b62b38c9cbd6791077f4
|
|
BLAKE2b-256 checksum How to use checksums |
110f75a7cf180239927ba542da188896f82d900730d8966c19e5f421c092b4da
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Release files / held_karp-0.1.1-cp310-cp310-macosx_10_12_x86_64.whl
| Download URL | held_karp-0.1.1-cp310-cp310-macosx_10_12_x86_64.whl |
|---|---|
| Size | 217.4 kB |
| Tags | CPython 3.10 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
0b8caed986babf679b6d2e6a95f29b2294a80c0740df90a9fd233f6d15a6aea8
|
|
BLAKE2b-256 checksum How to use checksums |
ec3e926fb41507dca5c5520c74e31a9d345d7676edff1ccef0ef12161756ccc3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.13.1
|
Security for held-karp 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.