312. 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.

312.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

312.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

312.3. Complexity

312.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.

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

Heuristic Move type Quality
2-opt reverse one segment  5% above optimal
3-opt 3-edge swap, multiple reconnections  3% above optimal
Lin-Kernighan variable-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.

312.6. Practical tips

312.7. See also