315. TSP 2opt

A simple but effective local search heuristic for the TSP. Iteratively replaces pairs of edges with a cheaper pair until no improvement is possible.

315.1. The 2-opt move

Given a tour 𝐴𝐵𝐶𝐷, the 2-opt move reverses the segment 𝐵𝐶, giving 𝐴𝐶𝐵𝐷.

Geometrically: it removes two non-adjacent edges (𝐴,𝐵) and (𝐶,𝐷), and adds (𝐴,𝐶) and (𝐵,𝐷)uncrossing the tour.

Improvement condition: do the swap iff

𝑐(𝐴,𝐶)+𝑐(𝐵,𝐷)<𝑐(𝐴,𝐵)+𝑐(𝐶,𝐷)

315.2. Algorithm

T ← initial tour (e.g., nearest neighbor)
Repeat:
    For each pair of non-adjacent edges (A,B), (C,D) in T:
        If c(A,C) + c(B,D) < c(A,B) + c(C,D):
            Perform 2-opt swap
            Break (or continue to next pair)
Until no improvement found
Return T

315.3. Complexity

315.4. Why it works

Each successful swap strictly decreases tour length. Since there are finitely many tours and the objective is bounded below, the process terminates.

315.5. 2-opt vs 3-opt vs Lin-Kernighan

HeuristicMove typeQuality
2-optreverse one segment 5% above optimal
3-opt3-edge swap, multiple reconnections 3% above optimal
Lin-Kernighanvariable-depth swaps 1% above optimal — gold standard

Lin-Kernighan dynamically tries 2-opt, 3-opt, …, 𝑘-opt moves with backtracking. LKH-3 (Helsgaun’s variant) is the state-of-the-art heuristic.

315.6. Practical tips

315.7. See also