Interactive Graph Theory Learning
Interactive Graph Theory Learning
Guest User
Using app without sign in
Online traveling salesman solver
Finds shortest tour visiting all vertices exactly once
Select an algorithm and generate steps to begin visualization
The Traveling Salesman Problem (TSP) asks for the shortest tour that visits every city exactly once and returns to the start. It is the most famous NP-hard problem in combinatorial optimization, simple to state yet exponentially hard to solve exactly.
The Held-Karp dynamic programming solution stores, for every subset of cities and every ending city, the cheapest way to visit that subset. Each state is extended by one unvisited city at a time, giving O(n squared times 2 to the n) time, exact but only feasible for roughly 20 cities. Larger instances rely on heuristics such as nearest neighbor and 2-opt, or metaheuristics and branch-and-bound solvers that reach near-optimal tours for thousands of cities.
TSP models delivery route planning, warehouse order picking, drilling circuit boards, DNA sequencing assembly, and telescope observation scheduling. It anchors the study of NP-completeness and approximation algorithms, and interviewers use it to probe understanding of complexity classes and dynamic programming over bitmasks.
There is no fast exact algorithm for the travelling salesman problem, so the practical answer is a two-stage one: build a decent tour quickly, then improve it locally until it stops improving.
// Stage 1: nearest neighbour, builds a tour in O(n squared)
tour = [start]
while some city is unvisited:
next = unvisited city closest to tour.last
tour.append(next)
tour.append(start) // close the loop
// Stage 2: 2-opt, removes crossings until no move helps
repeat until no improvement:
for each pair of edges (a,b) and (c,d) in tour:
if dist(a,c) + dist(b,d) < dist(a,b) + dist(c,d):
reverse the tour segment between b and c
// Exact, for small n: Held-Karp dynamic programming
dp[S][j] = min over k in S\{j} of dp[S\{j}][k] + dist(k, j)The 2-opt move is worth understanding geometrically. If two edges of a tour cross, swapping their endpoints and reversing the segment between them always shortens the tour by the triangle inequality. So 2-opt is, quite literally, the operation of pulling the knots out of a loop of string.
Run nearest neighbour then 2-opt on four cities placed at the corners of a rectangle, where the greedy choice provably goes wrong.
Example graph: Cities A(0,0), B(0,3), C(4,3), D(4,0). Distances: A-B 3, B-C 4, C-D 3, A-D 4, and the diagonals A-C and B-D both 5.
The optimal tour is the rectangle perimeter at 14, not either diagonal-crossing tour at 18. This is the whole story of TSP heuristics in miniature: a fast constructive pass gets close, and local search removes the crossings that the greedy choice introduced.
Time: O(n squared) heuristic, O(n squared times 2^n) exact · Space: O(n squared) heuristic, O(n times 2^n) exact
Nearest neighbour scans all remaining cities at each of n steps, giving O(n squared). Each 2-opt sweep tests all O(n squared) pairs of edges and repeats until no improvement, which is fast in practice but has no useful worst-case bound. Held-Karp is exact and fills a table indexed by subset and endpoint: there are 2^n subsets times n endpoints, and each entry takes O(n) to compute, hence O(n squared times 2^n) time and O(n times 2^n) memory. That is a hard wall around n = 20 to 25, since 2^25 times 25 already exceeds a billion table entries. Brute force over all permutations is far worse at O(n!).
The right method depends almost entirely on how many cities you have and whether you need a provable optimum.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Held-Karp exact DP | Fewer than about 20 cities and you need a guaranteed optimal tour. | O(n^2 · 2^n) |
| Christofides | Distances obey the triangle inequality and you want a proven bound: never worse than 1.5 times optimal. | O(n^3) |
| Nearest neighbour plus 2-opt | Hundreds to thousands of cities and a tour within a few percent of optimal is good enough. | O(n^2) per pass |
| Lin-Kernighan | Large instances where quality matters more than implementation effort. The practical state of the art. | near O(n^2.2) |
| Vehicle routing solvers | The real problem has multiple vehicles, capacities or time windows. Then it is not TSP at all. | varies |
Read the full article: The Traveling Salesperson Problem Explained
Related algorithms: Hamiltonian Path, Fleet Dispatching (mTSP), Capacitated Vehicle Routing (CVRP)