Skip to content

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.

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

  • knotkit/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: Compares vectors, probability distributions, and sets and exposes each rule’s mathematical guarantees
  • knotkit/prng: Generates seeded pseudorandom numbers and unbiased integers for reproducible simulations and tests
{"v":1,"box":[236.0399,94.1597,1043,437.3555],"x":{"s":"linear","lim":[-0.609491179472,14.1537396122],"inv":false,"g":0,"t":"Tour length above the shortest, 6,859 units (%); tick height is the share of seeds","tk":[[0,"0"],[5,"5"],[10,"10"],[13.5442484327,"13.5"]]},"y":{"s":"linear","lim":[-0.5,3.5],"inv":false,"g":0,"tk":[[3,"Held–Karp (exact), 12.7 ms"],[2,"Farthest insertion, 2.29 µs"],[1,"Closest insertion, 2.25 µs"],[0,"Tree walk (MST), 2.63 µs"]]},"ti":"On ulysses16, farthest insertion is optimal from 45 of 200 seeds","a":[{"k":"line","c":"#fffff8","s":"x","d":[0,2.55,0,3.45],"p":[269.35,175.67,269.35,98.45],"ds":"post"},{"k":"line","c":"#5b6b7c","s":"x","d":[0,2.55,0,3.45],"p":[269.35,175.67,269.35,98.45],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[0,1.89875,0,2.10125],"p":[269.35,231.55,269.35,214.17],"ds":"post"},{"k":"line","c":"#1d5fa6","s":"x","d":[0,1.89875,0,2.10125],"p":[269.35,231.55,269.35,214.17],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[1.16635078,1.973,1.16635078,2.027],"p":[333.11,225.17,333.11,220.54],"ds":"post"},{"k":"line","c":"#1d5fa6","s":"x","d":[1.16635078,1.973,1.16635078,2.027],"p":[333.11,225.17,333.11,220.54],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[3.25120279924,1.964,3.25120279924,2.036],"p":[447.07,225.95,447.07,219.77],"ds":"post"},{"k":"line","c":"#1d5fa6","s":"x","d":[3.25120279924,1.964,3.25120279924,2.036],"p":[447.07,225.95,447.07,219.77],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[1.866161248,1.95725,1.866161248,2.04275],"p":[371.36,226.53,371.36,219.19],"ds":"post"},{"k":"line","c":"#1d5fa6","s":"x","d":[1.866161248,1.95725,1.866161248,2.04275],"p":[371.36,226.53,371.36,219.19],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[1.03513631725,1.96625,1.03513631725,2.03375],"p":[325.94,225.75,325.94,219.96],"ds":"post"},{"k":"line","c":"#1d5fa6","s":"x","d":[1.03513631725,1.96625,1.03513631725,2.03375],"p":[325.94,225.75,325.94,219.96],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[3.03251202799,1.9415,3.03251202799,2.0585],"p":[435.11,227.88,435.11,217.84],"ds":"post"},{"k":"line","c":"#1d5fa6","s":"x","d":[3.03251202799,1.9415,3.03251202799,2.0585],"p":[435.11,227.88,435.11,217.84],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[2.50765417699,1.96625,2.50765417699,2.03375],"p":[406.42,225.75,406.42,219.96],"ds":"post"},{"k":"line","c":"#1d5fa6","s":"x","d":[2.50765417699,1.96625,2.50765417699,2.03375],"p":[406.42,225.75,406.42,219.96],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[0.685231083248,1.97075,0.685231083248,2.02925],"p":[306.81,225.37,306.81,220.35],"ds":"post"},{"k":"line","c":"#1d5fa6","s":"x","d":[0.685231083248,1.97075,0.685231083248,2.02925],"p":[306.81,225.37,306.81,220.35],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[0.0874763084998,1.94375,0.0874763084998,2.05625],"p":[274.14,227.68,274.14,218.03],"ds":"post"},{"k":"line","c":"#1d5fa6","s":"x","d":[0.0874763084998,1.94375,0.0874763084998,2.05625],"p":[274.14,227.68,274.14,218.03],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[2.79924187199,1.9685,2.79924187199,2.0315],"p":[422.36,225.56,422.36,220.16],"ds":"post"},{"k":"line","c":"#1d5fa6","s":"x","d":[2.79924187199,1.9685,2.79924187199,2.0315],"p":[422.36,225.56,422.36,220.16],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[7.42090683773,0.95725,7.42090683773,1.04275],"p":[674.98,312.32,674.98,304.99],"ds":"post"},{"k":"line","c":"#c4501b","s":"x","d":[7.42090683773,0.95725,7.42090683773,1.04275],"p":[674.98,312.32,674.98,304.99],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[7.55212130048,0.9505,7.55212130048,1.0495],"p":[682.15,312.9,682.15,304.41],"ds":"post"},{"k":"line","c":"#c4501b","s":"x","d":[7.55212130048,0.9505,7.55212130048,1.0495],"p":[682.15,312.9,682.15,304.41],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[7.18763668173,0.8425,7.18763668173,1.1575],"p":[662.23,322.17,662.23,295.14],"ds":"post"},{"k":"line","c":"#c4501b","s":"x","d":[7.18763668173,0.8425,7.18763668173,1.1575],"p":[662.23,322.17,662.23,295.14],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[12.4362151917,0.865,12.4362151917,1.135],"p":[949.12,320.24,949.12,297.07],"ds":"post"},{"k":"line","c":"#c4501b","s":"x","d":[12.4362151917,0.865,12.4362151917,1.135],"p":[949.12,320.24,949.12,297.07],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[4.05306896049,0.964,4.05306896049,1.036],"p":[490.9,311.75,490.9,305.57],"ds":"post"},{"k":"line","c":"#c4501b","s":"x","d":[4.05306896049,0.964,4.05306896049,1.036],"p":[490.9,311.75,490.9,305.57],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[10.0014579385,0.97075,10.0014579385,1.02925],"p":[816.04,311.17,816.04,306.15],"ds":"post"},{"k":"line","c":"#c4501b","s":"x","d":[10.0014579385,0.97075,10.0014579385,1.02925],"p":[816.04,311.17,816.04,306.15],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[13.5442484327,-0.45,13.5442484327,0.45],"p":[1009.69,433.07,1009.69,355.85],"ds":"post"},{"k":"line","c":"#0b7f61","s":"x","d":[13.5442484327,-0.45,13.5442484327,0.45],"p":[1009.69,433.07,1009.69,355.85],"ds":"post"},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#5c5c57"}],"lg":[]} 0 5 10 13.5 Tour length above the shortest, 6,859 units (%); tick height is the share of seeds Held–Karp (exact), 12.7 ms Farthest insertion, 2.29 µs Closest insertion, 2.25 µs Tree walk (MST), 2.63 µs exact reference, the same tour for every seed no seed: one tour, +13.54% 45 at +0%, none above +3.25% Solver, median p50 solve time (Apple M1 Pro, Bun 1.4.4) On ulysses16, farthest insertion is optimal from 45 of 200 seeds
{"v":1,"box":[236.0399,122.3921,595,437.3555],"x":{"s":"linear","lim":[-0.609491179472,14.1537396122],"inv":false,"g":0,"t":"Tour length above the shortest, 6,859 units (%); tick height is the share of seeds","tk":[[0,"0"],[5,"5"],[10,"10"],[13.5442484327,"13.5"]]},"y":{"s":"linear","lim":[-0.5,3.5],"inv":false,"g":0,"tk":[[3,"Held–Karp (exact), 12.7 ms"],[2,"Farthest insertion, 2.29 µs"],[1,"Closest insertion, 2.25 µs"],[0,"Tree walk (MST), 2.63 µs"]]},"ti":"On ulysses16, farthest insertion is\noptimal from 45 of 200 seeds","a":[{"k":"line","c":"#fffff8","s":"x","d":[0,2.55,0,3.45],"p":[250.86,197.2,250.86,126.33],"ds":"post"},{"k":"line","c":"#5b6b7c","s":"x","d":[0,2.55,0,3.45],"p":[250.86,197.2,250.86,126.33],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[0,1.89875,0,2.10125],"p":[250.86,248.48,250.86,232.53],"ds":"post"},{"k":"line","c":"#1d5fa6","s":"x","d":[0,1.89875,0,2.10125],"p":[250.86,248.48,250.86,232.53],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[1.16635078,1.973,1.16635078,2.027],"p":[279.22,242.63,279.22,238.38],"ds":"post"},{"k":"line","c":"#1d5fa6","s":"x","d":[1.16635078,1.973,1.16635078,2.027],"p":[279.22,242.63,279.22,238.38],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[3.25120279924,1.964,3.25120279924,2.036],"p":[329.91,243.34,329.91,237.67],"ds":"post"},{"k":"line","c":"#1d5fa6","s":"x","d":[3.25120279924,1.964,3.25120279924,2.036],"p":[329.91,243.34,329.91,237.67],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[1.866161248,1.95725,1.866161248,2.04275],"p":[296.23,243.87,296.23,237.14],"ds":"post"},{"k":"line","c":"#1d5fa6","s":"x","d":[1.866161248,1.95725,1.866161248,2.04275],"p":[296.23,243.87,296.23,237.14],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[1.03513631725,1.96625,1.03513631725,2.03375],"p":[276.03,243.16,276.03,237.85],"ds":"post"},{"k":"line","c":"#1d5fa6","s":"x","d":[1.03513631725,1.96625,1.03513631725,2.03375],"p":[276.03,243.16,276.03,237.85],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[3.03251202799,1.9415,3.03251202799,2.0585],"p":[324.59,245.11,324.59,235.9],"ds":"post"},{"k":"line","c":"#1d5fa6","s":"x","d":[3.03251202799,1.9415,3.03251202799,2.0585],"p":[324.59,245.11,324.59,235.9],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[2.50765417699,1.96625,2.50765417699,2.03375],"p":[311.83,243.16,311.83,237.85],"ds":"post"},{"k":"line","c":"#1d5fa6","s":"x","d":[2.50765417699,1.96625,2.50765417699,2.03375],"p":[311.83,243.16,311.83,237.85],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[0.685231083248,1.97075,0.685231083248,2.02925],"p":[267.52,242.81,267.52,238.2],"ds":"post"},{"k":"line","c":"#1d5fa6","s":"x","d":[0.685231083248,1.97075,0.685231083248,2.02925],"p":[267.52,242.81,267.52,238.2],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[0.0874763084998,1.94375,0.0874763084998,2.05625],"p":[252.99,244.93,252.99,236.07],"ds":"post"},{"k":"line","c":"#1d5fa6","s":"x","d":[0.0874763084998,1.94375,0.0874763084998,2.05625],"p":[252.99,244.93,252.99,236.07],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[2.79924187199,1.9685,2.79924187199,2.0315],"p":[318.92,242.98,318.92,238.02],"ds":"post"},{"k":"line","c":"#1d5fa6","s":"x","d":[2.79924187199,1.9685,2.79924187199,2.0315],"p":[318.92,242.98,318.92,238.02],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[7.42090683773,0.95725,7.42090683773,1.04275],"p":[431.29,322.61,431.29,315.88],"ds":"post"},{"k":"line","c":"#c4501b","s":"x","d":[7.42090683773,0.95725,7.42090683773,1.04275],"p":[431.29,322.61,431.29,315.88],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[7.55212130048,0.9505,7.55212130048,1.0495],"p":[434.49,323.14,434.49,315.35],"ds":"post"},{"k":"line","c":"#c4501b","s":"x","d":[7.55212130048,0.9505,7.55212130048,1.0495],"p":[434.49,323.14,434.49,315.35],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[7.18763668173,0.8425,7.18763668173,1.1575],"p":[425.62,331.65,425.62,306.84],"ds":"post"},{"k":"line","c":"#c4501b","s":"x","d":[7.18763668173,0.8425,7.18763668173,1.1575],"p":[425.62,331.65,425.62,306.84],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[12.4362151917,0.865,12.4362151917,1.135],"p":[553.24,329.87,553.24,308.61],"ds":"post"},{"k":"line","c":"#c4501b","s":"x","d":[12.4362151917,0.865,12.4362151917,1.135],"p":[553.24,329.87,553.24,308.61],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[4.05306896049,0.964,4.05306896049,1.036],"p":[349.41,322.08,349.41,316.41],"ds":"post"},{"k":"line","c":"#c4501b","s":"x","d":[4.05306896049,0.964,4.05306896049,1.036],"p":[349.41,322.08,349.41,316.41],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[10.0014579385,0.97075,10.0014579385,1.02925],"p":[494.04,321.55,494.04,316.94],"ds":"post"},{"k":"line","c":"#c4501b","s":"x","d":[10.0014579385,0.97075,10.0014579385,1.02925],"p":[494.04,321.55,494.04,316.94],"ds":"post"},{"k":"line","c":"#fffff8","s":"x","d":[13.5442484327,-0.45,13.5442484327,0.45],"p":[580.18,433.42,580.18,362.55],"ds":"post"},{"k":"line","c":"#0b7f61","s":"x","d":[13.5442484327,-0.45,13.5442484327,0.45],"p":[580.18,433.42,580.18,362.55],"ds":"post"},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#5c5c57"}],"lg":[]} 0 5 10 13.5 Tour length above the shortest, 6,859 units (%); tick height is the share of seeds Held–Karp (exact), 12.7 ms Farthest insertion, 2.29 µs Closest insertion, 2.25 µs Tree walk (MST), 2.63 µs exact reference, the same tour for every seed no seed: one tour, +13.54% 45 at +0%, none above +3.25% Solver, median p50 solve time (Apple M1 Pro, Bun 1.4.4) On ulysses16, farthest insertion is optimal from 45 of 200 seeds
  • 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 at build time, from the fixtures the benchmark uses; it reproduces the README figure.

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

problems/metric-tsp/README.md states the fixture, the seed, and the result table.