Sub topic 7
Metric TSP: From Spanning Trees to Tours
The Traveling Salesperson Problem (TSP) gives you n cities and a distance between every pair. You want the shortest closed tour that visits every city exactly once. TSP is NP-hard, and the next chapter shows that with arbitrary distances it cannot be approximated within any constant factor unless P = NP. This chapter covers the case where approximation does work: metric TSP, where distances satisfy the triangle inequality.
This chapter assumes you know minimum spanning trees (MSTs) and Prim's algorithm, Euler tours (a closed walk that uses every edge exactly once, which exists when every vertex has even degree in a connected multigraph), and the lower-bounding technique from earlier chapters.
The Metric Assumption
An instance is metric when distances are symmetric, nonnegative, and obey the triangle inequality:
- d(u, v) = d(v, u) and d(u, u) = 0
- d(u, w) ≤ d(u, v) + d(v, w) for all u, v, w
The triangle inequality is what lets the algorithm take shortcuts. If a walk goes u → v → w and v has already been visited, jumping straight from u to w costs no more. Every algorithm in this chapter builds a walk that may repeat vertices and then shortcuts it into a tour. Without the metric property, shortcutting can make the tour arbitrarily longer, and the guarantee disappears.
Many real inputs are metric. Euclidean distances are metric, and so are shortest-path distances in a road network with nonnegative edge lengths.
The Lower Bound: MST ≤ OPT
To prove a ratio, we need a lower bound on the optimal tour length OPT that we can compute in polynomial time. The MST provides one.
Take an optimal tour and delete any one edge. The result is a path that visits every vertex. A path is a spanning tree, so its weight is at least the MST weight. Edge weights are nonnegative, so deleting an edge cannot increase the cost:
MST ≤ OPT − (deleted edge) ≤ OPT.
The Double-Tree Algorithm
The algorithm turns the MST into a tour in three steps:
- Compute an MST T.
- Double every edge of T. Every vertex now has even degree, so an Euler tour exists. Its cost is exactly 2 · w(T).
- Walk the Euler tour and skip any vertex already visited. This shortcutting gives a Hamiltonian cycle.
Walking the doubled tree with depth-first search and recording each vertex the first time you reach it produces the preorder of T. So step 3 amounts to listing T's vertices in preorder and returning to the start.
Implementation
This version builds the MST with the O(n²) array form of Prim's algorithm, which fits dense distance matrices, and then outputs the preorder with an explicit stack.
def tsp_double_tree(dist):
n = len(dist)
in_tree = [False] * n
parent = [None] * n
key = [float("inf")] * n
key[0] = 0
children = [[] for _ in range(n)]
for _ in range(n):
u = min((i for i in range(n) if not in_tree[i]), key=lambda i: key[i])
in_tree[u] = True
if parent[u] is not None:
children[parent[u]].append(u)
for v in range(n):
if not in_tree[v] and dist[u][v] < key[v]:
key[v] = dist[u][v]
parent[v] = u
tour, stack = [], [0]
while stack:
u = stack.pop()
tour.append(u)
stack.extend(reversed(children[u]))
return tour + [0]
D = [[0, 2, 9, 10], [2, 0, 6, 4], [9, 6, 0, 3], [10, 4, 3, 0]]
print(tsp_double_tree(D))The output is [0, 1, 3, 2, 0]. Prim's algorithm builds the tree 0–1, 1–3, 3–2 with weight 9, and the preorder walks down that path.
The Proof
Let T be the MST and C the returned tour.
- The doubled tree's Euler walk costs 2 · w(T).
- Each shortcut replaces a stretch u → x₁ → … → xₖ → w with the single edge u → w. Applying the triangle inequality repeatedly shows d(u, w) ≤ d(u, x₁) + … + d(xₖ, w). So shortcutting never increases cost, and w(C) ≤ 2 · w(T).
- From the lower bound, w(T) ≤ OPT.
Therefore w(C) ≤ 2 · OPT, and the running time is O(n²).
Check the Input Before Trusting the Bound
The sample matrix D above is not metric: d(0, 3) = 10, but d(0, 1) + d(1, 3) = 6. The code runs anyway, because nothing in it checks the triangle inequality. Only the proof uses it, so the factor-2 guarantee does not hold for this input.
If your distances come from a graph, replace them with their metric closure, the matrix of all-pairs shortest-path distances. The closure is metric. Each edge of a tour in the closure stands for a shortest path in the original graph, so you can expand the tour into a walk of the same cost. Be aware that this walk may revisit vertices, so it fits problems that allow repeated visits, such as delivery routes on a road network.
Append this to the same script:
def is_metric(dist):
n = len(dist)
return all(dist[i][j] <= dist[i][k] + dist[k][j]
for i in range(n) for j in range(n) for k in range(n))
def metric_closure(dist):
n = len(dist)
d = [row[:] for row in dist]
for k in range(n):
for i in range(n):
for j in range(n):
if d[i][k] + d[k][j] < d[i][j]:
d[i][j] = d[i][k] + d[k][j]
return d
def tour_length(dist, tour):
return sum(dist[a][b] for a, b in zip(tour, tour[1:]))
print(is_metric(D)) # False
M = metric_closure(D)
print(M) # [[0, 2, 8, 6], [2, 0, 6, 4], [8, 6, 0, 3], [6, 4, 3, 0]]
print(is_metric(M)) # True
t = tsp_double_tree(M)
print(t, tour_length(M, t)) # [0, 1, 3, 2, 0] 17On M, the MST weight is 9, so the bound guarantees a tour of length at most 18. The tour found has length 17, which is optimal for this small instance. On larger inputs the double-tree tour is usually well above optimal, and the factor 2 is asymptotically tight for this algorithm.
Christofides: Improving the Ratio to 3/2
Doubling every edge is wasteful. An Euler tour only needs every vertex to have even degree, and in T only the odd-degree vertices need fixing. Christofides' algorithm (1976; Serdyukov found it independently) adds only enough edges to make those degrees even:
- Compute an MST T.
- Let O be the set of vertices with odd degree in T. |O| is even, because the degrees in any graph sum to an even number.
- Compute a minimum-weight perfect matching M on O, using the metric distances.
- T ∪ M is connected and every vertex has even degree. Find an Euler tour of it.
- Shortcut repeated vertices.
The proof needs one more lower bound: w(M) ≤ OPT / 2. Take an optimal tour and shortcut it to visit only the vertices of O. By the triangle inequality, the resulting cycle costs at most OPT. The cycle has an even number of edges. Taking every other edge splits it into two perfect matchings on O, so the cheaper one costs at most OPT / 2. M is a minimum matching, so it costs no more than that.
The total is w(T) + w(M) ≤ OPT + OPT/2 = 1.5 · OPT. Minimum-weight perfect matching (Edmonds' blossom algorithm) takes O(n³) time, which dominates the running time.
Comparison and Current Limits
| Algorithm | Ratio | Time | Key ingredient |
|---|---|---|---|
| Double tree | 2 | O(n²) dense | MST ≤ OPT, doubling |
| Christofides | 3/2 | O(n³) | MST plus matching ≤ OPT/2 |
| Karlin, Klein, Oveis Gharan (2020) | 3/2 − 10⁻³⁶ | polynomial, impractical | Randomized trees from an LP |
| Arora / Mitchell PTAS | 1 + ε | n·(log n)^O(1/ε) | Applies only to Euclidean inputs |
For more than 40 years, 3/2 was the best known ratio for general metric TSP, and the 2020 improvement is tiny. On the hardness side, metric TSP is NP-hard to approximate within 123/122. The true threshold lies somewhere between these bounds. In practice, people often run a heuristic such as 2-opt or Lin–Kernighan on top of a Christofides or double-tree tour. The starting tour carries the guarantee, and the heuristic usually shortens it a lot.
Common Errors
- Running on non-metric data. The code still returns a tour, but with no guarantee. Check the triangle inequality or use the metric closure.
- Asymmetric distances. If d(u, v) ≠ d(v, u), the problem is asymmetric TSP, and both MST-based proofs fail. That problem needs different methods.
- Forgetting the return edge. A tour returns to its start. Leaving out the final edge measures a Hamiltonian path instead of a tour.
- MST of a sparse graph. The algorithms assume a complete graph. If some edges are missing, compute the metric closure first.
- Assuming the ratio describes typical results. The 2 and 3/2 bounds are worst-case. Measure your actual tour against the lower bound w(T) to get a ratio for that instance.
Exercise
Why does the double-tree proof break if some distances are negative, even when the triangle inequality holds?
Show answer
The lower bound MST ≤ OPT comes from deleting one edge of the optimal tour. That step assumes the deleted edge has nonnegative weight. With a negative edge, the remaining path can cost more than OPT, so w(T) ≤ OPT can fail.
Exercise
In Christofides' algorithm, suppose the MST is a star: one center connected to 5 leaves (6 vertices in total). Which vertices are in O, and how many edges does M contain?
Show answer
Every leaf has degree 1, which is odd, and the center has degree 5, which is also odd. So O contains all 6 vertices, and M is a perfect matching on them with 3 edges.
Exercise
For the metric closure M in this chapter, the MST weight is 9. Without running any code, give an interval that must contain OPT, using only the double-tree tour of length 17 and the MST bound.
Show answer
MST ≤ OPT gives OPT ≥ 9. Any tour gives an upper bound, so OPT ≤ 17. OPT therefore lies in [9, 17]. Checking all three distinct tours shows OPT = 17.
Exercise
Show that shortcutting an optimal tour down to the vertex set O gives a cycle that splits into two perfect matchings on O.
Show answer
Since |O| is even, the shortcut cycle on O has an even number of edges, |O| of them. Number the edges around the cycle. The odd-numbered edges cover each vertex of O exactly once, and so do the even-numbered edges. So each set is a perfect matching on O. Their costs add up to the cycle's cost, which is at most OPT, so the cheaper matching costs at most OPT/2.