A Bit of History
The Traveling Salesman Problem (TSP) introduced in Part 14 is NP-hard. In 1958, Georges A. Croes introduced the 2-Opt local search heuristic to untangle self-crossing routes in Euclidean TSP instances. In 1965, S. Lin expanded this idea to 3-Opt, which later led to the world-famous Lin-Kernighan (LK / LKH) heuristic in 1973 (developed by Shen Lin and Brian Kernighan). Today, LKH is widely considered the most effective heuristic for solving symmetric TSP instances up to hundreds of thousands of cities within 1% of the proven optimal tour.
Nearest Neighbor Constructive Heuristic
The Nearest Neighbor (NN) algorithm is a simple greedy constructive heuristic:
- Start at an arbitrary initial city $v_0$. Mark it visited.
- While unvisited cities remain: find the unvisited city $u$ closest to the current city, add edge $(\text{current}, u)$ to the tour, mark $u$ visited, and set $\text{current} = u$.
- Return to the starting city $v_0$ to complete the tour.
Although NN runs in $O(N^2)$ time, it suffers from "greedy myopia": early choices are cheap, but the final remaining unvisited cities may force extremely long, crossing "disaster edges" back to the start.
2-Opt Local Search
2-Opt is a local search improvement heuristic designed to eliminate edge crossings. It repeatedly takes two non-adjacent edges $(A, B)$ and $(C, D)$ in a tour and replaces them with $(A, C)$ and $(B, D)$ if that replacement reduces total tour length:
2-Opt Swap Rule
Given a tour represented as an ordered array of cities, replacing edges between indices $i$ and $i+1$ and $j$ and $j+1$ ($i < j$) is equivalent to reversing the sub-segment of the tour array from index $i+1$ to $j$!
The change in distance ($\Delta \text{length}$) is calculated in $O(1)$ time:
$$\Delta = d(A, C) + d(B, D) - \left( d(A, B) + d(C, D) \right)$$
If $\Delta < 0$, the swap is performed. The algorithm repeats until no 2-edge swap improves the tour (a 2-optimal state).
3-Opt Local Search
3-Opt removes three edges $(A,B)$, $(C,D)$, and $(E,F)$ from the tour, breaking it into 3 paths. Re-connecting these 3 paths without creating subtours offers 8 possible configurations (including the original). By checking all 7 valid non-original reconnections, 3-Opt escapes local minima that 2-Opt gets stuck in.
Worked Example
Consider a tour on 4 2D points $(0,0), (0,1), (1,0), (1,1)$ where the current tour crosses itself: $A(0,0) \to D(1,1) \to B(0,1) \to C(1,0) \to A(0,0)$ with total length $1.414 + 1.0 + 1.414 + 1.0 = 4.828$.
- Select edges $(A, D)$ and $(B, C)$.
- Try swapping them with $(A, B)$ and $(D, C)$.
- New tour: $A(0,0) \to B(0,1) \to D(1,1) \to C(1,0) \to A(0,0)$.
- New length: $1.0 + 1.0 + 1.0 + 1.0 = 4.0$. Delta $\Delta = -0.828 < 0$. The crossing is untangled!
Complexity & Performance
| Algorithm | Type | Time Complexity per Pass | Tour Quality vs. Optimal |
|---|---|---|---|
| Nearest Neighbor | Constructive Greedy | \(O(N^2)\) | 15% – 25% above OPT |
| 2-Opt Local Search | Iterative Local Search | \(O(N^2)\) per pass | 3% – 7% above OPT |
| 3-Opt Local Search | Iterative Local Search | \(O(N^3)\) per pass | 1% – 3% above OPT |
| Lin-Kernighan (LKH) | Variable $k$-Opt | \(O(N^{2.2})\) empirical | < 0.1% above OPT |
Implementation
Real-World Applications
Logistics Fleet Dispatch & PCB Drilling
Delivery services (UPS, FedEx) and rideshare routing servers run 2-Opt and 3-Opt local search on top of initial greedy tours to optimize driver routes in real time. In industrial manufacturing, printed circuit board (PCB) drill heads use 2-Opt / Lin-Kernighan heuristics to minimize drill motion between millions of hole coordinates per day.
Exercises
- Construct a 4-point counterexample where Nearest Neighbor generates a tour that is strictly worse than the optimal tour.
- Show that 2-Opt swap always removes at least one edge crossing when executed on a 2D Euclidean TSP instance.
- Write out the 8 possible path reconnection choices for 3-Opt, and identify which ones correspond to simple 2-Opt swaps.
- Challenge: Implement Simulated Annealing on top of 2-Opt swaps to allow probabilistic acceptance of bad moves ($\Delta > 0$) and escape local minima.
Limitations
Local Minima & Worst-Case Guarantees
2-Opt and 3-Opt are local search heuristics: they guarantee convergence to a locally optimal tour with respect to 2-swaps or 3-swaps, but do not offer worst-case approximation ratios on non-metric graphs. For guaranteed 3/2-approximation on metric TSP, use Christofides Algorithm.