Sub topic 7
Metric TSP: From Spanning Trees to Tours
This chapter assumes you know minimum spanning trees (MSTs) and Prim's algorithm, Euler tours, and the lower-bounding technique from earlier chapters.
The Problem and the Metric Assumption
In the Traveling Salesman Problem (TSP), you get n cities and a distance between every pair. You want the shortest tour that visits every city once and returns to the start. TSP is NP-hard, so the goal here is a provably short tour found in polynomial time.
This chapter covers metric TSP, where distances satisfy the triangle inequality: d(u, w) ≤ d(u, v) + d(v, w). A direct trip is never longer than a detour. Both guarantees below rely on this assumption, and the next chapter shows why it is needed.
The Lower Bound: MST ≤ OPT
Take an optimal tour and delete any one edge. What remains is a path that visits every vertex, and a path is a spanning tree. Edge weights are non-negative, so:
MST weight ≤ OPT − (deleted edge) ≤ OPT
The MST can be computed in polynomial time, and it gives a bound on the unknown optimum.
The Double-Tree Algorithm
- Build an MST.
- Walk around the tree with a depth-first traversal. The walk uses every tree edge twice, so its cost is 2 · MST.
- Shortcut: list the vertices in preorder and skip any vertex already visited.
Shortcutting replaces a stretch of the walk u → … → w with the direct edge u → w. The triangle inequality guarantees this never increases the length. So:
tour ≤ 2 · MST ≤ 2 · OPT
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]
def tour_length(dist, tour):
return sum(dist[a][b] for a, b in zip(tour, tour[1:]))
D = [[0, 2, 3, 4],
[2, 0, 2, 3],
[3, 2, 0, 2],
[4, 3, 2, 0]]
t = tsp_double_tree(D)
print(t, tour_length(D, t)) # [0, 1, 2, 3, 0] 10The MST here is the path 0–1–2–3, with weight 6. The tour has length 10, which is within the 2 · 6 = 12 bound and is also optimal for this instance.
Christofides: Improving to 3/2
The double-tree algorithm is wasteful because it doubles every edge just to make every vertex degree even, which an Euler tour needs. Christofides' algorithm (1976) fixes only the vertices that need it:
- Build an MST T.
- Let O be the set of vertices with odd degree in T. A graph always has an even number of odd-degree vertices, so |O| is even.
- Find a minimum-weight perfect matching M on O.
- T ∪ M has all degrees even. Take an Euler tour of it and shortcut.
Why M ≤ OPT/2: Shortcut the optimal tour so that it visits only the vertices of O. By the triangle inequality, this shorter cycle costs at most OPT. Its edges split into two alternating perfect matchings on O. The cheaper of the two costs at most OPT/2, and M is no more expensive than either.
So tour ≤ MST + M ≤ OPT + OPT/2 = 1.5 · OPT.
| Algorithm | Extra structure | Ratio | Main cost |
|---|---|---|---|
| Double tree | Duplicate all MST edges | 2 | O(n²) with Prim |
| Christofides | Min-weight matching on odd vertices | 3/2 | O(n³) for matching |
Exercise
In the double-tree analysis, which step uses the triangle inequality, and which step uses only the fact that edge weights are non-negative?
Show answer
Shortcutting the doubled walk uses the triangle inequality, because replacing a detour with a direct edge must not increase the cost. The bound MST ≤ OPT uses only non-negativity: deleting an edge from the optimal tour leaves a spanning path that costs no more than OPT.
Exercise
Why does Christofides match only the odd-degree vertices of the MST instead of all vertices?
Show answer
An Euler tour requires every vertex to have even degree. Only the odd-degree vertices break this, so matching them is enough. Matching on a smaller vertex set costs at most OPT/2, which keeps the total at most 1.5 · OPT.