2-Opt TSP Solver

Solve a traveling salesman problem step by step. Build a first tour with the nearest neighbor or cheapest link heuristic, then watch 2-opt remove crossings and shorten it, one exchange at a time. On small problems the result is compared with the exact optimum.

  • Draw cities or type a distance table
  • Every 2-opt exchange explained
  • Compared with the exact optimum
Random cities:14 cities · max 100
1234567891011121314

Click to add a city, drag a city to move it, double-click a city to remove it.

  • tour
  • new edge
  • other candidates
  • added by 2-opt
  • removed by 2-opt
  • start city
Step 21 of 21

No exchange of two edges shortens the tour any more: it is 2-optimal (a local optimum). Length: 239.4. Held-Karp confirms that this tour is optimal.

Method

2-opt rule

Best improvement makes the exchange that saves the most; first improvement makes the first one it finds.

Result

First tourNearest neighbor
317.3
After 2-opt2-opt saved 24.6% · 2-opt exchanges: 5
239.4
Optimal tour (Held-Karp)Optimal
239.4

Step-by-step solution

Tip: use the ← and → keys to move between steps.

How the 2-opt algorithm works

The traveling salesman problem

Given a list of cities and the distance between every pair, the traveling salesman problem (TSP) asks for the shortest tour that visits each city exactly once and returns to the start. It is NP-hard: the number of tours grows like (n − 1)!/2, so exact methods such as Held-Karp dynamic programming only work for small instances. Larger problems are solved with heuristics that find a good tour quickly.

Building a first tour

Nearest neighbor: start at a city and always go to the closest city not yet visited, then return to the start. It is fast and intuitive, but the last steps often have to cover long distances. Repetitive nearest neighbor runs it from every start city and keeps the shortest tour.

Cheapest link (sorted edges): sort all the edges from shortest to longest and keep an edge unless it would give a city a third edge or close a cycle before every city is included. The result is a tour built from short edges, often better than nearest neighbor.

Improving it with 2-opt

A 2-opt exchange removes two edges of the tour, a–b and c–d, and reconnects the two pieces with a–c and b–d, which reverses the path between b and c. The exchange is kept if the two new edges are shorter together than the two old ones. 2-opt repeats this until no exchange helps; the tour is then 2-optimal, a local optimum.

With distances on a plane, two crossing edges can always be replaced by two shorter ones, so a 2-optimal tour never crosses itself. That is why 2-opt is often described as "untangling" the tour. Best improvement applies the exchange that saves the most at each step; first improvement applies the first improving exchange it finds.

How good is the result?

A 2-optimal tour is not always optimal, but on random cities it is typically within about 5% of the optimum. This solver computes the exact optimum with Held-Karp for up to 16 cities so that you can see the gap. Stronger local searches such as 3-opt and Lin–Kernighan exchange more edges at once and get closer still.

Frequently asked questions

What is the 2-opt algorithm?

A local search for the traveling salesman problem, proposed by Croes in 1958. It repeatedly removes two edges of the tour and reconnects the tour the other way when that makes it shorter, until no such exchange is left.

Is the nearest neighbor algorithm optimal?

No. It is a greedy heuristic: each step takes the closest unvisited city, which can force long edges at the end. Its tours are often 20–25% longer than the optimum. Running it from every start city (repetitive nearest neighbor) or following it with 2-opt improves the result.

Does 2-opt always find the shortest tour?

No. It stops at a local optimum: a tour that no single exchange of two edges can improve. For cities in convex position (for example on a circle) that local optimum is always the optimal tour, but in general a shorter tour may still exist.

Can I enter my own distance table?

Yes. Choose "Distance table", set the number of cities (up to 10) and type the distances. The table is symmetric, so each distance is typed once. Cities placed on the map can go up to 100.

How is the exact optimum computed?

With the Held-Karp dynamic programming algorithm, which examines subsets of cities instead of every tour. Its running time grows like n²·2ⁿ, so it is used for up to 16 cities.

Related on LearnGraphTheory