Exact TSP solver using the Held-Karp bitmask DP algorithm — Rust-powered Python bindings
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
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 held_karp-0.1.1.tar.gz.
File metadata
- Download URL: held_karp-0.1.1.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5813926f289b27bc2a0b74ae348412fe738d26c44bbc38cb9c37e1629f273042
|
|
| MD5 |
739041eff581cb5d180d890221df7c1d
|
|
| BLAKE2b-256 |
0c924e5e7411a6eba3643f6599f0701e0ac10e0ddc8d34c055a63fd8161f1211
|
File details
Details for the file held_karp-0.1.1-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: held_karp-0.1.1-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 107.6 kB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
558eb48979a3876f77bace38c2dc899fb25c951f0f485f4b5617c548186cae83
|
|
| MD5 |
918cebd2c5c9bbdc704209189a9b6175
|
|
| BLAKE2b-256 |
4d5d61c038ce0796a12c21c3cb161c964da6bdd7ca6378dc7ebd36c2e21b6cfd
|
File details
Details for the file held_karp-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: held_karp-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 240.9 kB
- Tags: CPython 3.13, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f1adc5164756262b90aeb293a6b1fd42f39fe9dd8426d8a980e9eb824ce48f5
|
|
| MD5 |
7745a818de02d2fb573a632cbc4b5e7c
|
|
| BLAKE2b-256 |
c22860f7b5d98349ddd9aaa3dda97ba7399b414d8aba060952b1bd6394dc5be6
|
File details
Details for the file held_karp-0.1.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: held_karp-0.1.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 237.6 kB
- Tags: CPython 3.13, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
29e3d086e2fd9c94b36f36cdd96c0d6af544acaa6d9ee3be60549fb9fd7d5eda
|
|
| MD5 |
df9bdde2f456f324b1b856319b92fa7b
|
|
| BLAKE2b-256 |
fe1fa7f95743cf202f45876ac159b816b4fc731c948b4f2453d07e3f10d11501
|
File details
Details for the file held_karp-0.1.1-cp313-cp313-macosx_11_0_arm64.whl.
File metadata
- Download URL: held_karp-0.1.1-cp313-cp313-macosx_11_0_arm64.whl
- Upload date:
- Size: 210.1 kB
- Tags: CPython 3.13, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11b484b8c756b4f7b37fea7c8fe48d7eaab008cd34ac054a79b53b44c96494d3
|
|
| MD5 |
a71a0c9bc6787bb41974fe7a65cd1847
|
|
| BLAKE2b-256 |
5edbd31a1faa7efef20c744bb07f1e7a9a84d487c1b0be6afcf6927c6c1da9d8
|
File details
Details for the file held_karp-0.1.1-cp313-cp313-macosx_10_12_x86_64.whl.
File metadata
- Download URL: held_karp-0.1.1-cp313-cp313-macosx_10_12_x86_64.whl
- Upload date:
- Size: 214.0 kB
- Tags: CPython 3.13, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5206b36b696b617604b308ba121f299552da065b354d86cc610272b2a05d3072
|
|
| MD5 |
86a3583e6765029f0b26e7d00af9b0a8
|
|
| BLAKE2b-256 |
014ba39d56720b25b2edac90909b4d039649bc96098c4be150e43a60385efd0b
|
File details
Details for the file held_karp-0.1.1-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: held_karp-0.1.1-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 107.4 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cfb9062b2bc612f9315e971b4f79d3072663984bcd296f91d56b1c01a62e0bb6
|
|
| MD5 |
54225ecff4452fcf85fec3a3afab5b05
|
|
| BLAKE2b-256 |
f418d3ae191b764a2b0b83d6cb4f9164f6f6e71654829db47b314a0386ff6c33
|
File details
Details for the file held_karp-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: held_karp-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 240.9 kB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
422b265f82f5bcac4526cd47c1e733d4f513e2c7abdf1fd7a5b0cce3ecaea9cf
|
|
| MD5 |
3ccc124129aa9c1ef9bec8b79fd072c4
|
|
| BLAKE2b-256 |
7b82e55c7f54dfc02cdacc8d88d0d0daee6998c0f167e099f3de761e9e7956d9
|
File details
Details for the file held_karp-0.1.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: held_karp-0.1.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 237.7 kB
- Tags: CPython 3.12, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e6cdb188c5772deb00747adc0de8ff4c3148b056a10aa2cdd49ecc31ad2d4ca
|
|
| MD5 |
f23d9fc5f28997cf170ba912389a80af
|
|
| BLAKE2b-256 |
aca031352bd7d28fca909b5468011bb417d617a0fb03476ec4b81d3f83277eeb
|
File details
Details for the file held_karp-0.1.1-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: held_karp-0.1.1-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 209.8 kB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
331a698c57899d4d76fba7f75d81ada4bdce749770b61704d69b41dc27c28ada
|
|
| MD5 |
bfae77801926877de10a603feefdf8ce
|
|
| BLAKE2b-256 |
9e1b1e31368dff6f69f0a770fe47e40581268028eb3f4e38853f9ecd806172e3
|
File details
Details for the file held_karp-0.1.1-cp312-cp312-macosx_10_12_x86_64.whl.
File metadata
- Download URL: held_karp-0.1.1-cp312-cp312-macosx_10_12_x86_64.whl
- Upload date:
- Size: 213.9 kB
- Tags: CPython 3.12, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b84f77cc4975c34e368c52fc78fa57103bcbfd3ce566d553a93a114ec6a2f025
|
|
| MD5 |
650fc8602296b6b69257cab5a6aa1728
|
|
| BLAKE2b-256 |
2ae3a564bd14601cf26752ecdd6b4de0193361b592eae2835b59f900365ea5c4
|
File details
Details for the file held_karp-0.1.1-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: held_karp-0.1.1-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 107.7 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
937097b9a5cc6709c15eae22723992ed92720e04104c0fd80e3102e26723bbd2
|
|
| MD5 |
378085b2a2e228613cac1bc218103b36
|
|
| BLAKE2b-256 |
4ff1b2f36300e90cb4c99451f8d2a5a883f3295e07abe71b717cfde61a24e8d2
|
File details
Details for the file held_karp-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: held_karp-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 241.0 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c70f15a05d13eeb41ff529b543da37b56c72c3189468c509a31aff553f2ea2f0
|
|
| MD5 |
51c2ff12f21fe2de97ee039f4362c393
|
|
| BLAKE2b-256 |
a4b33be110656730d7a5a8b6a3648b4c6d838bd41e1362fc8a21059de84c15b8
|
File details
Details for the file held_karp-0.1.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: held_karp-0.1.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 237.9 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a45f73d7fa14a3e3a7cd0ee21d689b687847fcec38d8a609bc4afac4ebe472a5
|
|
| MD5 |
8907e808eced6605881fb77f51392a8d
|
|
| BLAKE2b-256 |
a0ceb030651fd4cbffb6a17dac688b8ecd7cbddc0a4690813b61c42af1c475b3
|
File details
Details for the file held_karp-0.1.1-cp311-cp311-macosx_11_0_arm64.whl.
File metadata
- Download URL: held_karp-0.1.1-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 213.2 kB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
589c61f3b975769134ee09fed229cefe86f1ff1fcf523a1dcda61c6b7126db4a
|
|
| MD5 |
1fbdbe0e34b931bbf25957b2825b0eec
|
|
| BLAKE2b-256 |
233b664898e1620aeb33914a04d82aa1efcca9460b68137694d28d05d9342191
|
File details
Details for the file held_karp-0.1.1-cp311-cp311-macosx_10_12_x86_64.whl.
File metadata
- Download URL: held_karp-0.1.1-cp311-cp311-macosx_10_12_x86_64.whl
- Upload date:
- Size: 217.1 kB
- Tags: CPython 3.11, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23a34327551195ea62b27ef01e6b3150547b672d5e62555fa17d5e06f1e39d48
|
|
| MD5 |
cf0dae51728899136fa28a98eca0bb87
|
|
| BLAKE2b-256 |
bbe83dca7e16ca96e0e9b16442602a061ddea58b38e7f62e3744448887229755
|
File details
Details for the file held_karp-0.1.1-cp310-cp310-win_amd64.whl.
File metadata
- Download URL: held_karp-0.1.1-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 107.9 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f0cb0d4928e2752f895524f51f3fa60b633ccf1bbe56b771aa5febed76305cdd
|
|
| MD5 |
ff5206505bedf3c6351a7adffb9216df
|
|
| BLAKE2b-256 |
83880b4996b06ffecdc4427550ee058283ebb14de95e1fa8c3758643b0b62aed
|
File details
Details for the file held_karp-0.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: held_karp-0.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 241.2 kB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c5c9ab8b95fc5dcd484130aefca2a0357294d71cf9df3e9eea3883b3dc66d624
|
|
| MD5 |
51a51400faa79d5aae4d2c82fdca6b1d
|
|
| BLAKE2b-256 |
4b0af75582db0eb6969acbfabed8bd1bbf955e65fe279bb1c3ce4465c0c4ee66
|
File details
Details for the file held_karp-0.1.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: held_karp-0.1.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 238.2 kB
- Tags: CPython 3.10, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c716a411ecc3e86ac4784dd7b1d837f9b5e30cb25ca9e44688d48c130ccd5dd2
|
|
| MD5 |
a0a8d7b0e6e3c99ba727b1e1c57dc38b
|
|
| BLAKE2b-256 |
3696b36319fc51ffa6eefd1a279bd351227051b49238c9d65cd51c11b1b472f6
|
File details
Details for the file held_karp-0.1.1-cp310-cp310-macosx_11_0_arm64.whl.
File metadata
- Download URL: held_karp-0.1.1-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 213.6 kB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
41eaa86f130c01f604c55cb4ebedd98fc450d69294c9b62b38c9cbd6791077f4
|
|
| MD5 |
6f0a8eb925e1f793873e1cebaa5cd3bf
|
|
| BLAKE2b-256 |
110f75a7cf180239927ba542da188896f82d900730d8966c19e5f421c092b4da
|
File details
Details for the file held_karp-0.1.1-cp310-cp310-macosx_10_12_x86_64.whl.
File metadata
- Download URL: held_karp-0.1.1-cp310-cp310-macosx_10_12_x86_64.whl
- Upload date:
- Size: 217.4 kB
- Tags: CPython 3.10, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b8caed986babf679b6d2e6a95f29b2294a80c0740df90a9fd233f6d15a6aea8
|
|
| MD5 |
dccdf2736280be4ca4f07046e03360d8
|
|
| BLAKE2b-256 |
ec3e926fb41507dca5c5520c74e31a9d345d7676edff1ccef0ef12161756ccc3
|