ArticleslgStudy

computer science

2-opt

2-opt is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand 2-opt rather than just read about it. In short: In optimization, 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, although the basic move had already been suggested by Flood.

2-opt — main illustration
2-opt — illustration

Key takeaways

  • 2-opt belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect 2-opt to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of 2-opt from memory before moving on to harder problems.

Reference excerpt

In optimization, 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, although the basic move had already been suggested by Flood. The main idea behind it is to take a route that crosses over itself and reorder it so that it does not. A complete 2-opt local search will compare every possible valid combination of the swapping mechanism. This technique can be applied to the traveling salesman problem as well as many related problems. These include the vehicle routing problem (VRP) as well as the capacitated VRP, which require minor modification of the algorithm.

Pseudocode Visually, one swap looks like:

- A B - - A - B - × ==> - C D - - C - D -

In pseudocode, the mechanism by which the 2-opt swap manipulates a given route is as follows. Here v1 and v2 are the first vertices of the edges that are to be swapped when traversing through the route:

procedure 2optSwap(route, v1, v2) { 1. take route[start] to route[v1] and add them in order to new_route 2. take route[v1+1] to route[v2] and add them in reverse order to new_route 3. take route[v2+1] to route[start] and add them in order to new_route return new_route; }

Here is an example of the above with arbitrary input:

Example route: A → B → E → D → C → F → G → H → A Example parameters: v1=1, v2=4 (assuming starting index is 0) Contents of new_route by step: (A → B) A → B → (C → D → E) A → B → C → D → E → (F → G → H → A) This is the complete 2-opt swap making use of the above mechanism:

repeat until no improvement is made { best_distance = calculateTotalDistance(existing_route) start_again: for (i = 0; i <= number of nodes eligible to be swapped - 1; i++) { for (j = i + 1; j <= number of nodes eligible to be swapped; j++) { new_route = 2optSwap(existing_route, i, j) new_distance = calculateTotalDistance(new_route) if (new_distance < best_distance) { existing_route = new_route best_distance = new_distance goto start_again } } } }

The particular nodes or depots that are at the start and at the end of the path should be removed from the search as an eligible candidates for swapping, as reversing the order would cause an invalid path. For example, with depot at A:

A → B → C → D → A

Swapping using node[0] and node[2] would yield

C → B → A → D → A

which is not valid (does not leave from A, the depot).

Efficient implementation Building the new route and calculating the distance of the new route can be a very expensive operation, usually O(n) where n is the number of vertices in the route. In a symmetric case (where the distance between A and B is the same as between B and A), this can be skipped by performing a O(1) operation. Since a 2-opt operation involves removing 2 edges and adding 2 different edges we can subtract and add the distances of only those edges.

lengthDelta = - dist(route[v1], route[v1+1]) - dist(route[v2], route[v2+1]) + dist(route[v1+1], route[v2+1]) + dist(route[v1], route[v2])

If lengthDelta is negative that would mean that the new distance after the swap would be smaller. Once it is known that lengthDelta is negative, then we perform a 2-opt swap. This saves us a lot of computation.

C++ code

Output

Visualization

See also 3-opt local search (optimization) Lin–Kernighan heuristic

References

G. A. CROES (1958). A method for solving traveling salesman problems. Operations Res. 6 (1958), pp., 791-812. M. M. FLOOD (1956). The traveling-salesman problem. Operations Res. 4 (1956), pp., 61-75.

External links The Traveling Salesman Problem: A Case Study in Local Optimization Improving Solutions: 2-opt Exchanges Archived 2015-05-11 at the Wayback Machine

Illustrations

2-opt: 2-opt
2-opt
2-opt: 2-opt Swap Path Visualization
2-opt Swap Path Visualization

Worked examples

Example 1 — a first encounter with 2-opt

Start with the simplest possible case. Write down what 2-opt claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to 2-opt before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about 2-opt ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of 2-opt

In research
2-opt appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses 2-opt in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
2-opt is common in secondary-school and first-year university syllabi. It links to neighbouring topics Heuristic algorithms, Travelling salesman problem, so understanding it makes those chapters shorter.
In everyday life
Look for 2-opt outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “2-opt” →

How to study 2-opt in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what 2-opt means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain 2-opt out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is 2-opt in simple terms?

In optimization, 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, although the basic move had already been suggested by Flood.

Why does 2-opt matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study 2-opt?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on 2-opt.

Tags

  • Heuristic algorithms
  • Travelling salesman problem

Keep exploring