# Metric TSP

> Metric traveling salesperson: one exact solver and three approximations over complete distance graphs built through `knotkit/graph`'s factories. Held-Karp returns the shortest tour, the MST walk returns a tour provably within twice it when the rounded distances satisfy the triangle inequality, and closest and farthest insertion return cheap tours with no bound.

Metric traveling salesperson: one exact solver and three approximations over complete distance graphs built through `knotkit/graph`’s factories. Held-Karp returns the shortest tour, the MST walk returns a tour provably within twice it when the rounded distances satisfy the triangle inequality, and closest and farthest insertion return cheap tours with no bound.

## Result

> MST walk at n = 442: the three graph layouts land within 5% (0.697–0.723 ms); Held-Karp solves burma14 in 2.61 ms

## Modules

* [`knotkit/graph`](/modules/graph/): Provides breadth-first search, Dijkstra, topological order, and connected components over integer-vertex graphs, stored as adjacency lists, compressed sparse rows, or an adjacency matrix
* [`knotkit/distance`](/modules/distance/): Compares vectors, probability distributions, and sets and exposes each rule’s mathematical guarantees
* [`knotkit/prng`](/modules/prng/): Generates seeded pseudorandom numbers and unbiased integers for reproducible simulations and tests

## Figures

* From seed 42, farthest insertion matches the shortest tour of 6,859 units about 5,500× faster.
* Across 200 seeds, farthest insertion finds it 45 times and is never more than 3.25% longer.
* Held–Karp takes O(n² · 2ⁿ) time. Insertion takes O(n²) and can give longer tours.

Hover or focus a chart for exact values. Drawn by [`demo.ts`](https://github.com/jkomyno/knotkit/blob/main/problems/metric-tsp/demo.ts) at build time, from the fixtures the benchmark uses; it reproduces the README figure.

## Reproduce

```bash
git clone https://github.com/jkomyno/knotkit.git && cd knotkit && bun install
bun run demo:problem:metric-tsp
```

[`problems/metric-tsp/README.md`](https://github.com/jkomyno/knotkit/blob/main/problems/metric-tsp/README.md) states the fixture, the seed, and the result table.
