Skip to main content

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 node i to node j

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

held_karp-0.1.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

held_karp-0.1.0-cp313-cp313-win_amd64.whl (107.6 kB view details)

Uploaded CPython 3.13Windows x86-64

held_karp-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (240.9 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

held_karp-0.1.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (237.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

held_karp-0.1.0-cp313-cp313-macosx_11_0_arm64.whl (210.1 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

held_karp-0.1.0-cp313-cp313-macosx_10_12_x86_64.whl (214.0 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

held_karp-0.1.0-cp312-cp312-win_amd64.whl (107.4 kB view details)

Uploaded CPython 3.12Windows x86-64

held_karp-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (240.9 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

held_karp-0.1.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (237.7 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

held_karp-0.1.0-cp312-cp312-macosx_11_0_arm64.whl (209.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

held_karp-0.1.0-cp312-cp312-macosx_10_12_x86_64.whl (213.9 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

held_karp-0.1.0-cp311-cp311-win_amd64.whl (107.7 kB view details)

Uploaded CPython 3.11Windows x86-64

held_karp-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (241.0 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

held_karp-0.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (237.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

held_karp-0.1.0-cp311-cp311-macosx_11_0_arm64.whl (213.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

held_karp-0.1.0-cp311-cp311-macosx_10_12_x86_64.whl (217.1 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

held_karp-0.1.0-cp310-cp310-win_amd64.whl (107.8 kB view details)

Uploaded CPython 3.10Windows x86-64

held_karp-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (241.2 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

held_karp-0.1.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (238.2 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ ARM64

held_karp-0.1.0-cp310-cp310-macosx_11_0_arm64.whl (213.7 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

held_karp-0.1.0-cp310-cp310-macosx_10_12_x86_64.whl (217.4 kB view details)

Uploaded CPython 3.10macOS 10.12+ x86-64

File details

Details for the file held_karp-0.1.0.tar.gz.

File metadata

  • Download URL: held_karp-0.1.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.1

File hashes

Hashes for held_karp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8c92b2126e7a89a0428843fc276c4f0d130089054ddc4bb70dee9626b7106b50
MD5 c3eac96da3b0d21af96a912d182107b7
BLAKE2b-256 db330ace0f1db1def38e096bf5201ed537758303eec8768038c2e053a6ceaa3c

See more details on using hashes here.

File details

Details for the file held_karp-0.1.0-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for held_karp-0.1.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 7ea3e049860c40d78a3f15c09e8bfc491c59382e1f1034ac783200d5852805aa
MD5 edc9f1b58f9ae2ccd635a44a15824649
BLAKE2b-256 1ba835e128d8b457b49aa839cd45612c7d67f75f5fcecda5ae83d04d3b34cc97

See more details on using hashes here.

File details

Details for the file held_karp-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for held_karp-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fc36ffc73ac857fd31dfabf8e2a946ad1b72f0c363e5a2ac651248fad519ac7a
MD5 f3410456f0c378665b2bd1071b20beb4
BLAKE2b-256 43ea5533b78211d5018079f183f5f29a587149608e40262d247bc26897a37ef1

See more details on using hashes here.

File details

Details for the file held_karp-0.1.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for held_karp-0.1.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 405a898b365f99ccda16764ed71f0d1d060e4fa05d3d07826140b1e0a930a14f
MD5 20c07d915878ded61564254247a3eafc
BLAKE2b-256 899aef4c3252d3bee7f26ccb6f1ebc734bdd650abc0afc782fd78ebb8b5a6cf7

See more details on using hashes here.

File details

Details for the file held_karp-0.1.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for held_karp-0.1.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9f308876b2c4a5830392cfeb036092582be9d122e04a1b15a6627480b9117eae
MD5 52af2aa5b3d950a87eb1b23729482d41
BLAKE2b-256 3f61f7993550c2c5688c2a3072327207f09838958cd77182913906d000f4bb1c

See more details on using hashes here.

File details

Details for the file held_karp-0.1.0-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for held_karp-0.1.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a0f4560b32275da9d5d0c57f574deb817a0d7691819c53da3704ba810b05ea19
MD5 90f3bc3b723b14a0ee7a6e31007273cb
BLAKE2b-256 2d7930fc84c38a3b7fddf50d6d45c4933cc7683ebd6636740fbd2132e98819ff

See more details on using hashes here.

File details

Details for the file held_karp-0.1.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for held_karp-0.1.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 555f55cfa67974475296138fc4cef9e0b689969548a46b7e9ee7a21c7f3977ac
MD5 9c6a1e04b61457384761b44a97705e6b
BLAKE2b-256 c9cdfab36435c6d6f8137a40f23eb1af2324a3cf15729d00d43db5deac0498c4

See more details on using hashes here.

File details

Details for the file held_karp-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for held_karp-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9eb635558d60920d8638540f1be4f90acbd493c8a0bfe4ec960064150e8d9eff
MD5 9149f7481bf72f65d6654feb866fb7d9
BLAKE2b-256 9917317bb709b4153c52e7f36e221f7f8b8e8370acb6e683326b3415ea8c5bcc

See more details on using hashes here.

File details

Details for the file held_karp-0.1.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for held_karp-0.1.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ff25f4cdc9a0f17c601a5533a30da297e4ee98bd19ba6c81906e3635ff605f6a
MD5 b0f022fa54f973fe2fbb6bb63de1d469
BLAKE2b-256 d6dc549daa0d3ae800e506fac7b9229e940e4fb2afe676c82cf38d4a73a2b491

See more details on using hashes here.

File details

Details for the file held_karp-0.1.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for held_karp-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0a20e0e4cabdeebdb3001e429a9099e777aa9cddd844a957221a0e051fc6e47a
MD5 ecbd377d686ad7c59847475eb224c686
BLAKE2b-256 d0ac10c454a74c40bdf26c33857cfd0481ca42c5c57b5c0980597d8cf593f876

See more details on using hashes here.

File details

Details for the file held_karp-0.1.0-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for held_karp-0.1.0-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 95ba59c32a3658c047a8b5dc6ecbafced2db51630e3acfb2037c0595a8d531c9
MD5 cc71346a3c77313e6e72980e7109ad05
BLAKE2b-256 f057191cb97f8aeddc3f288300e7e79a14b96f803ef7cd4cf90fcfea8ef74b9c

See more details on using hashes here.

File details

Details for the file held_karp-0.1.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for held_karp-0.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 cc79282e24de2cc108e7de3423db1a6b1feb15e0b3a7f446a8a04a605c0b1c7a
MD5 a5b5143eb7c56faa21b3d39352d4e438
BLAKE2b-256 2c8584d98faafea1f9e7308595b178edbab1c277dd054ddac8a3ba135e1a2b7b

See more details on using hashes here.

File details

Details for the file held_karp-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for held_karp-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a658c35e5307af3ea62681dbed563cd4b56aefbffafe542ac665e14b631c1219
MD5 df9570b595718a001c042d95adf2ca59
BLAKE2b-256 ff0b903bc78d148129e4badb0cd4e0581b284f8f528cb7c80e7416b862eacf0c

See more details on using hashes here.

File details

Details for the file held_karp-0.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for held_karp-0.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e3b24dda2465d4295f277e9e585e11c1894677ac38eb81ee5f21baf647361c8b
MD5 3ce1464134ec8248ff5c029f50dbf232
BLAKE2b-256 8a6eefe335e148c64b53585ba2d1b75fd76f6eb1a8a6cad1348602845f9a26d5

See more details on using hashes here.

File details

Details for the file held_karp-0.1.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for held_karp-0.1.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c05e6fcfc80ee048b0288df65679bcbb323e1e6e7768aa328387ad54ffebfbb5
MD5 9532c915a2b803fc651cffbdb1e3124a
BLAKE2b-256 82587bcb41eb4ac149e69b876276ed9d1c0134cd836130c96107b80c7ef659e8

See more details on using hashes here.

File details

Details for the file held_karp-0.1.0-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for held_karp-0.1.0-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 cd9c4dc1e99d1cbb80137bd2d5fb97024d9329ef214f09998e1ecb86ea947506
MD5 969a5a42694d14cbb84a89f59d844e49
BLAKE2b-256 bb16257282fcaca2ea8158bc92626b00845565c9599fd73ccc26acbbbc63bbb7

See more details on using hashes here.

File details

Details for the file held_karp-0.1.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for held_karp-0.1.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 30b8f746ac8aaf976eb96d2227f4d4a541d7d6c9c64ad1f998abbb31d99b53ca
MD5 41f5a5783c3d9d3e5b366e7689ebe9f5
BLAKE2b-256 266a0052576fdb57513f42e14af3006f680a6130bf1b4c386922ef443ebec5f6

See more details on using hashes here.

File details

Details for the file held_karp-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for held_karp-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 73a95766424b1d842bdb3ba4949d214513eda3b5f85356f81d0b1d0ed0cab9d0
MD5 57280941fa657badc0f941fb58af6574
BLAKE2b-256 623bb2498b735beeea6b7f74e7ea993139a4731acb4228da5b88571757bc6847

See more details on using hashes here.

File details

Details for the file held_karp-0.1.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for held_karp-0.1.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 41c59ed17bda0a87cd5eddf39a24dd4a16122079f6d574b5e2e76045dd64ce0c
MD5 ef9841a185fcbb6dd4cc80ba5a051443
BLAKE2b-256 eb15e672d64329635b04dd4ed7150ca4dd19b9673128a0284bf4b48b6fc2d9f5

See more details on using hashes here.

File details

Details for the file held_karp-0.1.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for held_karp-0.1.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 43b1183bd81a17463816539da2ac27e04053637378f1405d3c6ecca0851d90be
MD5 66fd52bc4f106117653a8f548afcd12c
BLAKE2b-256 63987e4eeb1287f811cadddff55725de7588948d2325f11497445dbf4c98226a

See more details on using hashes here.

File details

Details for the file held_karp-0.1.0-cp310-cp310-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for held_karp-0.1.0-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 2113dba5d4095fe28f4f947d6d03d1f3af1aafa994dc028159bd6b23c24e7675
MD5 ec19bf92c72246c506e4488c3b9d58b3
BLAKE2b-256 ffab1c0d014e8e7186d40202eba18fd17e7267c64c8bac093cf67aa3381bbfcb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page