Fact-checked by Grok 2 weeks ago

2-opt

The 2-opt algorithm is a local search designed to approximate solutions to the traveling salesman problem (TSP), which seeks the shortest possible route visiting a set of cities and returning to the origin. It operates by starting with an initial tour and iteratively improving it through 2-opt moves, where two non-adjacent edges are removed and replaced by two new edges that reconnect the tour segments in a reversed order, provided the change reduces the total tour length. The process continues until no further improvements are possible, yielding a locally optimal tour under the 2-opt criterion. First proposed by G. A. Croes in 1958, the algorithm provides a mechanized approach to TSP that applies to both symmetric and asymmetric distance metrics, emphasizing efficiency over exhaustive search. Unlike exact methods, which become computationally infeasible for large instances due to TSP's NP-hard nature, 2-opt offers a practical that can be terminated early for approximate solutions of desired accuracy. In practice, it is often combined with initial tour construction heuristics, such as the nearest neighbor method, to enhance starting quality before applying the local search. For the metric TSP—where distances satisfy the triangle inequality—the 2-opt algorithm has a worst-case approximation ratio of \sqrt{\frac{n}{2}}, meaning the length of the resulting tour is at most \sqrt{\frac{n}{2}} times the optimal tour length, where n is the number of cities, regardless of the initial tour. This bound is tight, as demonstrated by constructed instances where the 2-optimal tour achieves exactly \sqrt{\frac{n}{2}} of the optimum. Theoretical analyses also reveal probabilistic performance insights, such as expected convergence speeds under random initial tours, underscoring its reliability for large-scale problems in operations research and logistics. Despite its simplicity, 2-opt remains a foundational component in more advanced TSP solvers, including extensions like k-opt and hybrid metaheuristics.

Introduction

Definition and Purpose

The traveling salesman problem (TSP) is a fundamental optimization challenge in and , seeking the shortest possible that visits each of a given set of cities exactly once and returns to the starting point. Formally, for a with n vertices representing cities and edge weights denoting distances or costs, the TSP requires identifying a minimum-weight spanning all vertices. The 2-opt algorithm is a local search designed to approximate solutions to the TSP by iteratively refining an initial to reduce its total length. Introduced as a method for improving suboptimal tours, it begins with any feasible initial Hamiltonian cycle—such as one generated by a simple nearest-neighbor —and repeatedly applies targeted modifications until no further reductions in length are possible. This process yields a locally optimal , though it does not guarantee the global optimum due to the NP-hard nature of the TSP. At its core, a 2-opt move involves selecting two non-adjacent edges in the current tour and reconnecting the endpoints in an alternative configuration that eliminates potential crossings and shortens the path. By focusing on pairwise edge exchanges, the algorithm efficiently explores the neighborhood of possible tours, making it particularly valuable for large-scale TSP instances where exact methods like dynamic programming or branch-and-bound become computationally infeasible. Its simplicity and empirical effectiveness have established 2-opt as a foundational improvement , often serving as a building block for more advanced techniques.

Historical Development

The 2-opt method was invented by G. A. Croes in as an early approach to solving the traveling salesman problem (TSP), marking one of the first systematic local search techniques in . Croes proposed the algorithm in his paper published in , where it served as a practical improvement procedure for generating near-optimal tours by iteratively swapping edges to reduce total distance, initially implemented using computational tools available at the time. This innovation emerged during the nascent phase of , where manual and semi-automated methods were transitioning to programmed solutions for complex routing problems. In the , the 2-opt gained broader recognition through advancements in TSP research, including early computer implementations and integrations with s. It became a standard tool in solvers as digital computing enabled testing on larger instances, paving the way for extensions like 3-opt introduced by Shen Lin in 1965. The method evolved from these early computational experiments in to more robust algorithmic tools in the , coinciding with increased access to digital computers that enabled exhaustive searches over larger instances. Initial applications often involved manual oversight for small-scale problems, but by the mid-1970s, automated iterations became feasible, allowing repeated 2-opt improvements on tours of dozens to hundreds of cities. A pivotal milestone occurred in 1973 with the Lin-Kernighan heuristic, which incorporated 2-opt as a foundational building block for generalized k-opt methods, dynamically adjusting the scope of swaps to achieve superior performance on symmetric TSP instances. This extension solidified 2-opt's role in heuristic design, influencing subsequent developments in local search. The enduring impact of 2-opt extends to modern metaheuristics, where it functions as a standard local search operator in frameworks like genetic algorithms and for TSP and related optimization problems, enhancing global search with efficient neighborhood exploration.

Core Mechanism

The 2-opt Swap Operation

The 2-opt swap operation is a fundamental local improvement step in methods for the traveling salesman problem (TSP), where a tour is represented as a cyclic sequence of cities numbered 1 to n, with edges connecting consecutive cities in the order (including the closing edge from n to 1). To perform a swap, two non-adjacent edges are selected, denoted as (i, j) and (k, l), where i, j, k, l are distinct cities in the tour such that the endpoints form a configuration (i connected to j, and k to l, with no other direct connections between them). The swap reconnects these endpoints by removing the original edges and adding new ones: (i, k) and (j, l), while reversing the path segment between j and k to maintain a valid Hamiltonian cycle. Geometrically, this operation addresses inefficiencies in the , particularly in TSP instances where edge crossings indicate suboptimal paths. By swapping the edges, the operation effectively uncrosses the in the formed by the four endpoints, reversing the order of the intermediate cities between the two edges to create a smoother, non-intersecting path that typically shortens the overall distance traveled. This reversal straightens the route, eliminating the detour caused by the crossing and aligning the path more directly between the key points. A 2-opt swap is only applied if it results in an improvement, specifically when the sum of the lengths of the new edges is less than that of the removed edges: d(i, k) + d(j, l) < d(i, j) + d(k, l), where d(\cdot, \cdot) denotes the distance between cities. This condition ensures the total tour length decreases, providing a measurable gain in solution quality. While a single 2-opt swap produces a new tour that avoids self-intersections in the selected quadrilateral, it may alter the global structure, potentially creating fresh opportunities for additional swaps elsewhere in the tour without guaranteeing overall optimality.

Tour Improvement Process

The 2-opt tour improvement process begins with an initial valid tour of the cities, which can be generated using a simple heuristic such as the nearest neighbor method, where the tour starts at an arbitrary city and repeatedly visits the closest unvisited city until all are included, closing back to the start. This initialization provides a feasible but typically suboptimal Hamiltonian cycle as the starting point for refinement. The core of the process involves an iterative loop that scans all possible pairs of non-adjacent edges in the current tour. For each pair, it checks if replacing them with an alternative pair of edges would reduce the total tour length; if an improving swap is identified, it is applied, and the scan restarts from the beginning to re-evaluate the updated tour. Two main variants exist: the first-improvement strategy, which applies the first improving swap encountered during the scan and immediately restarts, promoting faster but potentially less thorough progress; and the best-improvement strategy, which evaluates all possible pairs in a full pass to select and apply the swap that yields the maximum length reduction before restarting. These variants balance computational efficiency against solution quality, with first-improvement often used in practice for its speed. To maintain the tour as a single cycle after each swap, the segment of the path between the two selected edges is reversed, ensuring connectivity without creating disjoint cycles or self-intersections in the representation. The process terminates when a complete scan reveals no improving swaps, indicating that the tour is locally optimal with respect to 2-opt moves—no single swap can further reduce the length. In terms of convergence, the algorithm typically requires a small number of iterations relative to the problem size, often achieving local optimality after a sublinear number of swaps per city—for instance, around 0.2 to 3.5 moves per city on average for large instances (n up to 10^6), depending on the initial tour quality—though it may settle into suboptimal local minima that are not globally optimal. This rapid convergence makes 2-opt effective for initial refinement, but escaping local minima often requires hybridization with other techniques.

Algorithmic Details

Pseudocode

The 2-opt algorithm accepts as input an initial tour, represented as an array of city indices (e.g., a permutation of cities 0 to n-1), and a distance matrix or function computing the cost between any pair of cities; it outputs the improved tour order and its total length after local optimization. The procedure initializes the tour length by summing distances along the cyclic path and repeatedly scans for improving 2-opt swaps until no further reductions are found, treating the tour as circular where the last city connects back to the first. For edge cases such as tours with n \leq 3 cities, no non-adjacent edges exist for swapping, so the algorithm terminates immediately without changes. A standard first-improvement variant of the 2-opt algorithm, which applies the first beneficial swap encountered, is presented below in pseudocode. This version assumes a 0-indexed tour array and handles the cyclic nature by using modular indexing for distances involving the tour's end (e.g., the edge from the last to the first city). Note that the loops consider only non-wrapping segments; for full coverage of all possible 2-opt moves in the cyclic tour, implementations often use advanced data structures like doubly linked lists or tour duplication to handle wrapping reversals efficiently. An optional best-improvement variant tracks the minimum delta cost across all candidate pairs in each iteration and applies only the optimal swap if any improvement exists.
function twoOpt(tour, dist):
    n = length(tour)
    if n <= 3:
        return tour, computeTourLength(tour, dist)
    
    current_length = computeTourLength(tour, dist)
    improved = true
    
    while improved:
        improved = false
        for i = 0 to n-2:
            for j = i+2 to n-1:
                # Compute delta for swapping edges (tour[i], tour[i+1]) and (tour[j], tour[(j+1) mod n])
                cityA = tour[i]
                cityB = tour[(i+1) mod n]
                cityC = tour[j]
                cityD = tour[(j+1) mod n]
                
                old_cost = dist[cityA][cityB] + dist[cityC][cityD]
                new_cost = dist[cityA][cityC] + dist[cityB][cityD]
                delta = new_cost - old_cost
                
                if delta < 0:
                    # Perform reversal of segment from i+1 to j
                    reverse(tour, i+1, j)
                    current_length += delta
                    improved = true
                    break  # First-improvement: stop inner loop on first gain
            if improved:
                break  # Restart outer scan after improvement
    
    return tour, current_length

function computeTourLength(tour, dist):
    n = length(tour)
    length = 0
    for i = 0 to n-1:
        length += dist[tour[i]][tour[(i+1) mod n]]
    return length

function reverse(tour, start, end):
    while start < end:
        swap(tour[start], tour[end])
        start += 1
        end -= 1
This pseudocode focuses on the core swap operation, where a negative delta indicates an improvement by reconnecting the tour segments without crossings. For the best-improvement variant, replace the inner loop with tracking the pair yielding the smallest delta and apply the reversal only after scanning all pairs if the minimum delta is negative.

Complexity Analysis

The 2-opt algorithm's time complexity is dominated by the process of identifying improving swaps in each iteration. In a naive implementation, every possible pair of non-adjacent edges in the current tour of n cities must be evaluated, leading to O(n²) candidate swaps; evaluating the gain for each swap takes constant time if distances are precomputed. Thus, each iteration requires O(n²) time. The total running time depends on the number of iterations until a local optimum is reached, where no further improving swaps exist. In the worst case, this can require an exponential number of iterations, as there exist tour instances where the algorithm follows exponentially long paths in the state space before terminating, such as paths of length 2^{n+3} - 14 for Euclidean instances with 8n vertices. Assuming a bounded number of iterations, such as O(n), the overall time complexity simplifies to O(n³); however, this bound does not hold universally due to the potential for exponential steps. In practice, the algorithm converges much faster, often requiring only a subquadratic number of iterations on real-world instances, making it efficient for problem sizes up to several thousand cities—for example, solving a 442-city instance in under 12 seconds on 1990s hardware. For larger instances in the millions, performance degrades without optimizations, as the O(n²) per-iteration cost accumulates. The primary bottleneck is the enumeration of edge pairs, which accounts for the bulk of computation in unoptimized scans. Regarding space complexity, the algorithm uses O(n) space to represent and manipulate the current tour as a sequence or cycle. If a distance matrix is precomputed for efficient swap evaluations, this adds O(n²) space; otherwise, distances can be computed on-the-fly at additional time cost. In comparison to exact methods for the traveling salesman problem, such as the Held-Karp dynamic programming algorithm with O(2^n n²) time, 2-opt offers a heuristic approach that runs in polynomial time on typical instances despite its worst-case exponential behavior, providing good approximations without optimality guarantees.

Practical Implementation

Data Structures for Efficiency

Efficient implementations of the 2-opt algorithm rely on specialized data structures to minimize the time required for tour modifications and edge evaluations, addressing the inherent O(n^2) complexity of scanning potential swaps. A common approach uses arrays to represent the tour, where cities are stored in a permutation array, and predecessor and successor information is maintained via modular indexing for quick access to neighbors. This array-based structure allows O(1) lookups for adjacent cities but requires O(n) time to reverse segments during a swap, as the entire subsequence must be explicitly reversed. Alternatively, doubly linked lists provide a more flexible representation, with each city node containing pointers to its previous and next neighbors in the tour, enabling segment reversals in O(1) time by simply adjusting four pointers at the swap endpoints. For larger instances, multi-level linked structures, such as two-level trees combining doubly linked lists, further optimize traversal and update times to O(log n) per operation while preserving the benefits of fast modifications. To facilitate rapid distance calculations during swap evaluations, distances between all pairs of cities are precomputed and stored in a symmetric matrix, allowing O(1) lookups assuming Euclidean or predefined metrics. This precomputation, typically O(n^2) space and time upfront, eliminates repeated computations of edge costs, which would otherwise dominate runtime in dense graphs. For the core operation, implementations skip invalid edge pairs—such as adjacent edges where i+1 = j or j+1 = i—to avoid trivial or non-improving swaps, reducing the effective search space from O(n^2) to approximately O(n(n-3)/2) without altering the asymptotic complexity. Advanced variants employ bitmasks or neighbor lists to further prune candidates, focusing only on promising pairs based on geometric proximity, though these are more common in extensions like . Incremental updates enhance efficiency by maintaining the current tour length as a single variable, updated solely by the delta cost of a potential swap rather than recomputing the entire tour sum. The delta for a 2-opt move between edges (i, i+1) and (j, j+1) is calculated as Δ = d(P, P) + d(P[i+1], P[j+1]) - d(P, P[i+1]) - d(P, P[j+1]), where d denotes the precomputed distance and P the tour permutation, enabling O(1) acceptance decisions post-lookup. This avoids O(n) full evaluations per iteration, significantly speeding up repeated local searches. Memory trade-offs are inherent in these choices: array representations are compact, using O(n) space for the permutation and inverse arrays, making them suitable for instances up to n ≈ 1,000 where random access is frequent. In contrast, doubly linked lists or tree-based structures consume O(n) additional space for pointers but offer superior modification speeds for large tours (n > 10,000), trading memory for reduced traversal costs in swap-heavy algorithms. The independent nature of edge pair evaluations in 2-opt lends itself to parallelization, where multiple processors or GPU threads can assess disjoint subsets of potential swaps concurrently, achieving proportional to the number of cores for the neighborhood scan phase. For instance, data decomposition assigns sub-tours to threads, enabling simultaneous 2-opt checks on non-overlapping edges while using doubly linked lists to synchronize updates, with a reported speedup of approximately 2.6× for instances such as fi10639 (10,639 cities). This approach preserves solution quality equivalent to sequential 2-opt but scales to multi-core environments without requiring tour-wide locks during evaluation.

Sample Code in C++

This section presents a complete, self-contained C++ implementation of the 2-opt algorithm for solving the traveling salesman problem (TSP), employing a first-improvement strategy to iteratively apply swaps until no local enhancement is possible in a scanning pass. The code precomputes a for O(1) edge cost lookups, representing the tour as a vector of city indices for straightforward segment reversal, and uses the standard library's std::reverse for efficiency in modifying the path. This approach aligns with the core 2-opt method introduced by Croes.
cpp
#include <bits/stdc++.h>

using namespace std;

struct Point {
    double x, y;
};

double distance(const Point& a, const Point& b) {
    return hypot(a.x - b.x, a.y - b.y);
}

int main() {
    int n;
    cin >> n;
    vector<Point> points(n);
    for (int i = 0; i < n; ++i) {
        cin >> points[i].x >> points[i].y;
    }

    // Precompute distance matrix for O(1) lookups (O(n^2) space and time, efficient for moderate n)
    vector<vector<double>> dist_matrix(n, vector<double>(n));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            dist_matrix[i][j] = distance(points[i], points[j]);
        }
    }

    // Initialize tour as sequential order (0 to n-1)
    vector<int> tour(n);
    iota(tour.begin(), tour.end(), 0);

    // 2-opt with first-improvement: scan for first improving swap, apply, restart scan
    bool improved = true;
    while (improved) {
        improved = false;
        for (int i = 0; i < n - 1; ++i) {
            bool inner_improved = false;
            for (int j = i + 2; j < n; ++j) {
                // Skip adjacent edges in wrapping case
                if (i == 0 && j == n - 1) continue;
                // Identify edges for potential 2-opt swap
                int a = tour[i];
                int b = tour[i + 1];
                int c = tour[j];
                int d = tour[(j + 1) % n];

                // Delta: new edges a-c and b-d vs. old a-b and c-d
                double old_cost = dist_matrix[a][b] + dist_matrix[c][d];
                double new_cost = dist_matrix[a][c] + dist_matrix[b][d];
                double delta = new_cost - old_cost;

                if (delta < 0) {  // Improvement found
                    // Reverse segment from i+1 to j (non-wrapping for simplicity)
                    reverse(tour.begin() + i + 1, tour.begin() + j + 1);
                    improved = true;
                    inner_improved = true;
                    break;  // First-improvement: apply and stop inner scan
                }
            }
            if (inner_improved) {
                break;  // Restart outer scan from beginning
            }
        }
    }

    // Compute and output final tour length
    double total_length = 0.0;
    for (int i = 0; i < n; ++i) {
        total_length += dist_matrix[tour[i]][tour[(i + 1) % n]];
    }

    // Output: tour indices (space-separated), followed by length
    for (int i = 0; i < n; ++i) {
        if (i > 0) cout << " ";
        cout << tour[i];
    }
    cout << endl << fixed << setprecision(6) << total_length << endl;

    return 0;
}
The program expects input from standard input in the format: the first line contains the integer n (number of cities, 3 ≤ n ≤ 1000 for practicality), followed by n lines each with two double values representing the x and y coordinates of each city (0-based indexing). It outputs the improved tour as n space-separated integers on one line, followed by the total tour length on the next line, rounded to six decimal places. The implementation avoids wrapping reversals across the tour's end for coding simplicity, which covers most local improvements effectively in practice, though full coverage could require rotating the tour start. This code is standard C++11-compliant and can be compiled with g++ using the command g++ -std=c++11 -O2 -o twopt twopt.cpp, requiring no external libraries beyond the C++ standard library (the <bits/stdc++.h> header includes all necessary ones for brevity). It runs in O(n^2) time per improvement iteration, with the number of iterations typically small for local search. For verification, compile and run the program with the following test input representing 5 cities forming a near-square with one offset point (initial sequential tour length ≈ 16.064, expected improved length ≈ 14.472 after 2-opt):
5
0.0 0.0
4.0 0.0
4.0 3.0
0.0 3.0
2.0 1.0
Sample output:
0 3 2 1 4
14.472136
This demonstrates the algorithm reducing the tour by swapping to route through the central point efficiently.

Examples and Visualization

Step-by-Step Example

Consider a small instance of the traveling salesman problem (TSP) with five cities labeled A, B, C, D, and E, having coordinates A(0,0), B(1,3), C(4,1), D(2,4), and E(3,2). The initial is A-B-C-D-E-A. The distances between consecutive cities in this tour, computed using the formula \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2} and rounded to one decimal place, are A-B: 3.2, B-C: 3.6, C-D: 3.6, D-E: 2.2, and E-A: 3.6, yielding a total tour length of 16.2. In the first iteration, consider the non-adjacent edges B-C and D-E for a potential 2-opt swap. The change in tour length, or delta, is calculated as \Delta = d(B, D) + d(C, E) - d(B, C) - d(D, E), where d(\cdot, \cdot) denotes the distance between cities. Substituting the values gives \Delta = 1.4 + 1.4 - 3.6 - 2.2 = -3.0. Since \Delta < 0, the swap improves the tour. The new tour is obtained by reversing the segment between C and D, resulting in A-B-D-C-E-A. The updated edges are A-B: 3.2, B-D: 1.4, D-C: 3.6, C-E: 1.4, and E-A: 3.6, for a total length of 13.2. In the second iteration, scan the remaining pairs of non-adjacent edges in the current tour A-B-D-C-E-A and identify D-C and E-A as candidates. The delta is \Delta = d(D, E) + d(C, A) - d(D, C) - d(E, A) = 2.2 + 4.1 - 3.6 - 3.6 = -0.9. Since \Delta < 0, apply the swap by reversing the segment between C and E, yielding the new tour A-B-D-E-C-A. The edges are now A-B: 3.2, B-D: 1.4, D-E: 2.2, E-C: 1.4, and C-A: 4.1, with a total length of 12.3. Further iterations reveal no additional swaps with negative delta, so the algorithm converges to this local optimum tour A-B-D-E-C-A of length 12.3.

Visual Representation

Visual representations of the 2-opt typically employ before-and-after diagrams to illustrate the removal of crossings in a plotted on a two-dimensional , demonstrating how swapping two non-adjacent edges shortens the path by reconnecting the segments in a non-crossing manner. These static figures often depict an initial suboptimal with intersecting edges highlighted in red, followed by the improved where the swapped edges eliminate the , resulting in a smoother polygonal path connecting the cities. Animations provide a dynamic view of tour evolution across multiple iterations, sequentially applying 2-opt swaps while color-coding the affected edges—such as using dashed red lines for the removed edges and solid green lines for the newly formed ones—to track progressive improvements in tour length. This sequence highlights how the algorithm iteratively refines the path on a of city coordinates, often starting from a random or nearest-neighbor initial tour and converging toward a local minimum. Such visualizations can be created using tools like for static graph layouts of TSP tours, in for plotting and animating paths on coordinate planes, or interactive TSP software compatible with TSPLIB instances, such as the TSP which supports real-time algorithm execution. Key visual elements include directional arrows along the tour to indicate the sequence of city visits, dashed lines representing the edges targeted for removal, solid lines for the replacement edges, and a distance scale to quantify the reduction in tour length after each swap. These elements aid in understanding the geometric behind the , emphasizing how 2-opt prioritizes uncrossing paths to approximate optimality. Visualizations of common pitfalls, such as local optima, depict a stabilized tour where no single 2-opt swap yields improvement—shown as a non-crossing but suboptimal —yet a globally better tour exists that requires multiple simultaneous changes beyond the algorithm's scope. For instance, in the example instance detailed earlier, an might freeze at such a local minimum, illustrating the need for extensions like repeated restarts to escape it.

Applications and Limitations

Use in TSP Solvers

The 2-opt algorithm serves as a fundamental local search in prominent TSP solvers, enhancing initial through iterative edge swaps to reduce total distance. In the TSP solver, 2-opt principles are integrated within the Lin-Kernighan () heuristic, a k-opt variant that systematically explores edge exchanges to generate high-quality upper bounds for exact branch-and-cut optimization, enabling solutions for instances up to 85,900 cities. Similarly, OR-Tools employs 2-opt as a core neighborhood operator in its solver for TSP, where it performs edge reversals and swaps—often combined with 3-opt moves and follow-up 2-opt iterations—to refine solutions under metaheuristics like guided local search, supporting efficient handling of practical routing problems. Hybrid approaches frequently incorporate 2-opt to improve upon constructive heuristics and metaheuristics for TSP. For instance, starting with a nearest neighbor initial tour and applying 2-opt local search can significantly reduce tour length by resolving crossings and suboptimal segments, achieving better approximations than standalone methods. When combined with optimization (ACO), 2-opt acts as a post-processing step to locally optimize pheromone-guided tours, as seen in hybrids like AC2OptGA, which blend ACO's global exploration with 2-opt's refinement for multi-vehicle TSP variants, yielding near-optimal results on benchmark instances. In industrial applications, 2-opt enhances efficiency in routing, such as scheduling, where it minimizes distances in dynamic environments akin to those optimized by systems like UPS's , which relies on local improvements for daily route adjustments across thousands of drivers. For (PCB) drilling, 2-opt-based evolution strategies optimize hole-drilling sequences modeled as TSP, reducing machine time and by iteratively improving paths on boards with hundreds of points. In genome sequencing alignments, 2-opt facilitates DNA fragment assembly by treating overlap graphs as TSP instances, repositioning fragments to minimize reconstruction errors in de novo sequencing pipelines. Empirical performance of 2-opt demonstrates its practicality, routinely achieving tours within 5% of optimal (i.e., 95-100% optimality) on TSP instances with up to 10,000 cities when applied iteratively after simple constructors. Open-source libraries further democratize its use; Python's NetworkX supports TSP approximations where users implement 2-opt for tour visualization and refinement on graph-based instances, while Java's JGAP integrates 2-opt as a local search mutation in genetic algorithms for TSP, enabling customizable evolutionary solvers for tasks.

Extensions and Comparisons

The 2-opt serves as the foundational case in the family of k-opt methods for improving tours in the traveling salesman problem (TSP), where k represents the number of edges removed and reconnected. In 3-opt, three edges are removed from the current tour, creating three subpaths that are reconnected in one of up to eight possible ways (excluding those that simply reverse segments), potentially yielding better solutions than 2-opt by exploring a larger neighborhood. However, 3-opt requires evaluating O(n^3) possible moves per iteration, making it computationally more intensive than the O(n^2) complexity of 2-opt, though optimized implementations can mitigate this to practical levels for moderate instance sizes. The Lin-Kernighan generalizes k-opt by allowing variable k, starting with a 2-opt-like exchange and iteratively adding subsequent exchanges (up to a dynamically determined k) as long as they increase the total gain, while ensuring the structure remains a valid tour through disjoint link sets and feasibility checks. This approach often produces superior results to fixed-k methods like 3-opt, as demonstrated on benchmark instances up to 110 cities where it achieved optimal or near-optimal tours faster than exhaustive 3-opt searches. Compared to 2-opt, Lin-Kernighan explores deeper improvements but at higher average cost per move, with empirical run times scaling roughly as O(n^2) due to selective gain criteria rather than full enumeration. In contrast to edge-swapping in k-opt methods, Or-opt and related relocate moves focus on intra-tour optimizations by extracting a consecutive segment of 1, 2, or 3 cities and reinserting it elsewhere in the tour, preserving orientation to minimize disruptions. Proposed by Or in , Or-opt requires O(n^2) evaluations per , similar to 2-opt, but targets relocation rather than reversal, making it complementary for refining where segment shifts yield gains without edge reversals. Simulated annealing differs from 2-opt by incorporating probabilistic acceptance of worse moves to escape local optima, using temperature-based perturbations often drawn from 2-opt neighborhoods, which allows broader exploration but increases runtime variability compared to 2-opt's deterministic hill-climbing. While 2-opt converges quickly to good local solutions, can achieve 5-10% better approximations on larger instances by avoiding premature stagnation, though it demands parameter tuning for effectiveness. Modern variants integrate 2-opt as an adaptive operator in genetic algorithms for TSP, where edge swaps serve as mutation steps to diversify populations or as post-crossover local search to refine offspring tours, enhancing convergence on large-scale problems by combining global search with local improvements. For instance, applying 2-opt mutations after recombination expands the search space and mitigates premature convergence, outperforming standard genetic algorithms in solution quality for instances beyond 100 cities. 2-opt is preferred for rapid initial improvements on easy or time-constrained instances due to its simplicity and O(n^2) efficiency, while escalating to 3-opt or Lin-Kernighan is advisable for harder problems where deeper neighborhoods justify the O(n^3) or variable cost, as higher k beyond 4 often yields in solution quality relative to added computation.

References

  1. [1]
    A Method for Solving Traveling-Salesman Problems - PubsOnLine
    G. A. Croes, (1958) A Method for Solving Traveling-Salesman Problems. Operations Research 6(6):791-812. https://doi.org/10.1287/opre.6.6.791 · PDF download ...Missing: PDF | Show results with:PDF
  2. [2]
    New Results on the Old k-opt Algorithm for the Traveling Salesman ...
    This paper develops several results, some worst-case and some probabilistic, on the performance of 2- and k-opt local search for the traveling salesman problem ...
  3. [3]
    The approximation ratio of the 2-Opt Heuristic for the metric ...
    Abstract. The 2-Opt heuristic is one of the simplest algorithms for finding good solutions to the metric Traveling Salesman Problem. It is the key ingredient ...
  4. [4]
    [PDF] Traveling salesman problem
    The traveling salesman problem asks for the least costly route to visit each of m cities exactly once and return to the home city.
  5. [5]
    [PDF] An Effective Heuristic Algorithm for the Traveling-Salesman Problem
    A. CROES, "A Method for Solving Traveling-Salesman Problems," Opns. Res. 5, 791-. 812 (1958).
  6. [6]
    Worst case and probabilistic analysis of the 2-Opt algorithm for the ...
    2-Opt is probably the most basic and widely used local search heuristic for the TSP. This heuristic achieves amazingly good results on “real world” Euclidean in ...
  7. [7]
    [PDF] Algorithmic strategies for finding the best TSP 2-OPT move in ... - arXiv
    Mar 28, 2024 · Abstract. We describe an exact algorithm for finding the best 2-OPT move which, experimentally, was observed.Missing: ijkl | Show results with:ijkl
  8. [8]
    [PDF] The Traveling Salesman Problem: A Case Study in Local Optimization
    The traveling salesman problem (TSP) has been an early proving ground for many approaches to combinatorial optimization, including clas- sical local ...
  9. [9]
    [PDF] The Approximation Ratio of the 2-Opt Heuristic for the Euclidean ...
    The 2-Opt heuristic is a simple improvement heuristic for the Traveling Salesman Problem. It starts with an arbitrary tour and then repeatedly replaces two ...<|control11|><|separator|>
  10. [10]
    [PDF] Learning 2-opt Heuristics for the Traveling Salesman Problem via ...
    In this work, we propose a deep reinforcement learning algorithm trained via Policy. Gradient to learn improvement heuristics based on 2-opt moves. Our ...
  11. [11]
    [PDF] Average-Case Analysis of the 2-opt Heuristic for the TSP
    The 2-opt heuristic is a local search algorithm for solving the traveling salesman problem. We see the main idea of the 2-opt heuristic algorithm in figure 2.
  12. [12]
    [PDF] Different approaches to Travelling Salesman Problem
    In each step, 2-Opt Algorithm deletes two edges thus creating 2 subtours and ... One 2-Opt move has time complexity of O(n2). This is be- cause in one 2-Opt ...
  13. [13]
    [PDF] Topics in Local Search - UBC Computer Science
    Local Search in Combinatorial Optimization. John Wiley &. Sons, 1997. J.L. Bentley. Fast Algorithms for Geometric Traveling Salesman Problems. ORSA Journal ...<|separator|>
  14. [14]
    [PDF] Worst Case and Probabilistic Analysis of the 2-Opt Algorithm for the ...
    2-Opt is probably the most basic and widely used local search heuristic for the TSP. This heuristic achieves amazingly good results on “real world” Euclidean ...
  15. [15]
    [PDF] Travelling Salesman Problem - UDC
    If the new route is shorter, keep it, else, keep previous route. Eg: 1. Time complexity: Θ(n^2). 2. Does NOT promise the global optimal solution.
  16. [16]
    [PDF] Finding Tours in the TSP
    As an alternative, Lin and Kernighan [1973] developed an algorithm that is sometimes referred to as \variable -opt". The core of the algorithm is an effective ...
  17. [17]
    [PDF] Parallel 2-Opt Local Search on GPU - CORE
    DOUBLY LINKED LIST AS REPRESENTATION OF TSP. Most TSP tours are represented by using 1-dimension buffer memory or unidirectional list. A drawback of using. 1 ...
  18. [18]
    [PDF] Local Search for Traveling Salesman Problem
    Efficient implementations of 2-opt, 2H-opt and 3-opt local search. A. Neighborhood pruning (exact or heuristic). Fixed radius search + Candidate lists + DLB.
  19. [19]
    2-opt - Wikipedia
    2-opt is a simple local search algorithm for solving the traveling salesman problem. The 2-opt algorithm was first proposed by Croes in 1958.Missing: paper | Show results with:paper
  20. [20]
  21. [21]
  22. [22]
    11 Animated Algorithms for the Traveling Salesman Problem
    Dec 27, 2019 · 2-Opt is a local search tour improvement algorithm proposed by Croes in 1958. ... 2-opt will consider every possible 2-edge swap ...
  23. [23]
    Convex Hull | Traveling Salesman Problem Visualizer
    Interactive solver for the traveling salesman problem to visualize different algorithms. Includes various Heuristic and Exhaustive algorithms.<|control11|><|separator|>
  24. [24]
    Graph-Drawing / TSP-Route-Drawing in C++ with "known" coordinates
    Nov 1, 2010 · The nice thing about drawing multiple tours in a vehicle routing problem is that "not so bad" algorithms tend to discover nice non-overlapping ...
  25. [25]
    afourmy/pyTSP: A 2D/3D visualization of the Traveling ... - GitHub
    pyTSP uses various approaches to solve the TSP (linear programming, construction heuristics, optimization heuristics, genetic algorithm).
  26. [26]
    Finding the Best 3-OPT Move in Subcubic Time - MDPI
    Clearly, the complexity is O ( n 3 ) since there are O ( n 2 ) partial moves and the evaluation of each of them takes O ( n ) time. To show the lower bound Ω ( ...
  27. [27]
    [PDF] A k-Opt Based Constraint for the TSP - DROPS
    Most of the time, we notice that the use of 2-opt allows us to improve the solving times. For example, ali535 is improved by a factor of 3.5 in solving time and ...
  28. [28]
    [PDF] A Short History of the Traveling Salesman Problem - HEC Montréal
    Fully automated algorithms: 1976 paper: integrality reached first by branch and bound; subtour elimination constraints then introduced. (42 ≤ n ≤ 64).
  29. [29]
    [PDF] Comparative Analysis of Various Algorithms Used in Travelling ...
    The conclusion that is derived from this paper is for smaller size TSP (n<50), greedy 2-opt algorithm will perform well,i.e. high optimality vs. little ...
  30. [30]
    An Improved Genetic Algorithm with 2-Opt Local Search for the ...
    Apr 17, 2021 · In this paper, based on an novel crossover operator to expand the search space and 2-opt Local search, an improved genetic algorithm is proposed.