# How does the 3-opt algorithm for TSP work?

I understand that the 3-Opt Heuristic for solving the Traveling Salesman problem involves removing three edges from a graph and adding three more to recomplete the tour. However, I've seen many papers that mention that when three edges are removed, there remain only 2 possible ways to recombine the tour - this doesn't make sense to me.

For example, I found a paper [1] that says:

The 3-opt algorithm works in a similar fashion, but instead of removing two edges we remove three. This means that we have two ways of reconnecting the three paths into a valid tour1 (ﬁgure 2 and ﬁgure 3). A 3-opt move can actually be seen as two or three 2-opt moves.

However, I count 3 different ways to reconnect the tour. What am I missing here?

Also, can someone link me to an algorithm for 3-opt if possible? I'm just trying to understand it, but I haven't come across any clear algorithms yet: all resources I find simply say "remove three edges, reconnect them". That's it, which is sort of vague.

Here are the 3 tours that seem to me to be 3-opt moves after removing three edges.

1. Heuristics for the Traveling Salesman Problem by C. Nilsson
• The paper you link is written in a very informal style and gives a nonstandard definition of TSP in the very first sentence; it also doesn't appear to have been published anywhere. Of course, that doesn't affect the validity of your question but there are probably better explanations of 3-OPT than the one you found. Jan 19, 2014 at 11:45
• Papers at this level probably assume that interested readers don't have trouble translating a more or less definite description into code. That's common on (T)CS; if that's a hindrance for you, consider reading more basic stuff first in order to get more training.
– Raphael
Jan 19, 2014 at 15:14
• As a high school student, I find none of this stuff basic, haha. Anyways, I'm fairly certain there are more than 2 ways (3 to be exact) to reconnect the tour such that the move wouldn't be identical to a two-opt move... so I was wondering why this paper states there are only two. I've looked for other papers in search for a definitive algorithm I could easily implement but haven't found any, so I tried implementing it (in Java) based on what I know and it didn't really work well, since I'm still confused about how many different ways to reconnect there are.
– u3l
Jan 19, 2014 at 15:58
• for 2d euclidean TSP a tour with intersecting lines can always lead to another tour with no intersecting lines and a shorter tour, ie only case #1 you list (after the initial config) can be considered. maybe some of that info is being left out in the writeup(s) you cite. ie they are not considering/excluding the guaranteed nonoptimal intersecting cases. ps it would be helpful/clearer if you labeled each graph. take it you mean: initial, tour1, tour2, tour3.
– vzn
Jan 20, 2014 at 15:59
• try starting at wikipedia 3opt entry for refs. several are decades old but electronic copies are available.
– vzn
Jan 20, 2014 at 16:05

You missed the footnote — these ways are "not including the connections being identical to a single 2-opt move". Indeed, there are only two permutations in $S_3$ without fixed points (also known as derangements), namely $(123)$ and $(132)$. More generally, for a $k$-opt move it is enough to consider permutations without fixed points, since those with $t$ fixed points are $(k-t)$-moves.

The algorithm for $k$-opt local search is as follows. Start with some initial solution, say the one produced by Christofides's algorithm. Repeatedly try to improve it by performing a $k$-opt: choose $k$ edges and reconnect them in a different way (this time it's OK if the move is also an $\ell$-move for some $\ell < k$) that results in a shorter tour.

The way this is implemented is by going over all sets of $k$ edges and over all ways of reconnecting the edges, perhaps in some intelligent order, and computing the difference in length (there is no need to recompute the length of the entire tour, just the different of length; that takes $O(k)$ instead of $O(n)$); if an improvement is found, we make the switch and repeat from the beginning. We continue like that until we get stuck.

A different variant is to always try all possibilities, and use the one that results in the best improvement. There are also other variants which you can probably find in the literature.

• Okay... I missed that some of those permutations are just single two opt moves now. However, I still count 3 different permutations, excluding the one before the move, which have 0 fixed points. (i'll edit an image into my post in a few minutes)
– u3l
Jan 19, 2014 at 9:22

Lets say we have 3 points A, B and C. First we swap(A,B) and then we only can swap(B,C) or swap(A,C). This way we only have two different possibilities.

• This definitely needs more explanation. Mar 3, 2014 at 20:38
• Well... maybe i'm wrong. Correct me please. 2-opt: We have A-B-C-D cities. We swap two and we get A-C-B-D. Only two possibilities, original and one new - ABCD and ACBD. 3-opt: A-B-C-D cities. We swap(1,2) and get BACD. We swap the third city with either first or second and get BCAD - swap(2,3) or CABD by swap(1,3). Three possibilities, original and two new - ABCD, BACD and BCAD. What about k-opt (4,5,6...)? Mar 6, 2014 at 2:18

I could find 4 3-opt moves (which are not 2-opt): If i gave the hexagon tour a numbering of 123456 (starting from the top left vertex), the other tours would have numbers of 125634, 124365, 126534 and 125643, which are a subset of the derangement of 12[3456] (where 3456 is being deranged).

• Welcome to the site, but I'm not sure how this answers the question. On Stack Exchange sites, we're looking for the answer to the question at the top of the page, not for "here's some related information." Sep 26, 2016 at 23:28