# Lin–Kernighan heuristic

In combinatorial optimization, Lin–Kernighan is one of the best heuristics for solving the symmetric travelling salesman problem.[citation needed] It belongs to the class of local search algorithms, which take a tour (Hamiltonian cycle) as part of the input and attempt to improve it by searching in the neighbourhood of the given tour for one that is shorter, and upon finding one repeats the process from that new one, until encountering a local minimum. As in the case of the related 2-opt and 3-opt algorithms, the relevant measure of "distance" between two tours is the number of edges which are in one but not the other; new tours are built by reassembling pieces of the old tour in a different order, sometimes changing the direction in which a sub-tour is traversed. Lin–Kernighan is adaptive and has no fixed number of edges to replace at a step, but favours small numbers such as 2 or 3.

## Derivation

For a given instance ${\displaystyle (G,c)}$ of the travelling salesman problem, tours are uniquely determined by their sets of edges, so we may as well encode them as such. In the main loop of the local search, we have a current tour ${\displaystyle T\subset \mathrm {E} (G)}$ and are looking for new tour ${\displaystyle T'\subset \mathrm {E} (G)}$ such that the symmetric difference ${\displaystyle F=T\mathbin {\triangle } T'}$ is not too large and the length ${\displaystyle \sum _{e\in T'}c(e)}$ of the new tour is less than the length ${\displaystyle \sum _{e\in T}c(e)}$ of the current tour. Since ${\displaystyle F}$ is typically much smaller than ${\displaystyle T}$ and ${\displaystyle T'}$, it is convenient to consider the quantity

${\displaystyle g(F)=\sum _{e\in F\cap T}c(e)-\sum _{e\in F\setminus T}c(e)\quad }$ — the gain of using ${\displaystyle F\subseteq \mathrm {E} (G)}$ when switching from ${\displaystyle T}$

since ${\displaystyle g(T\mathbin {\triangle } T')=\sum _{e\in T}c(e)-\sum _{e\in T'}c(e)}$: how much longer the current tour ${\displaystyle T}$ is than the new tour ${\displaystyle T'}$. Naively ${\displaystyle k}$-opt can be regarded as examining all ${\displaystyle F\subseteq \mathrm {E} (G)}$ with exactly ${\displaystyle 2k}$ elements (${\displaystyle k}$ in ${\displaystyle T}$ but not in ${\displaystyle T'}$, and another ${\displaystyle k}$ in ${\displaystyle T'}$ but not in ${\displaystyle T}$) such that ${\displaystyle T\mathbin {\triangle } F}$ is again a tour, looking for such a set which has ${\displaystyle g(F)>0}$. It is however easier to do those tests in the opposite order: first search for plausible ${\displaystyle F}$ with positive gain, and only second check if ${\displaystyle T\mathbin {\triangle } F}$ is in fact a tour.

Define a trail in ${\displaystyle G}$ to be alternating (with respect to ${\displaystyle T}$) if its edges are alternatingly in ${\displaystyle T}$ and not in ${\displaystyle T}$, respectively. Because the subgraphs ${\displaystyle {\bigl (}\mathrm {V} (G),T{\bigr )}}$ and ${\displaystyle {\bigl (}\mathrm {V} (G),T'{\bigr )}}$ are ${\displaystyle 2}$-regular, the subgraph ${\displaystyle G[T\mathbin {\triangle } T']={\bigl (}\mathrm {V} (G),T\mathbin {\triangle } T'{\bigr )}}$ will have vertices of degree ${\displaystyle 0}$, ${\displaystyle 2}$, and ${\displaystyle 4}$ only, and at each vertex there are as many incident edges from ${\displaystyle T}$ as there are from ${\displaystyle T'}$. Hence (essentially by Hierholzer's algorithm for finding Eulerian circuits) the graph ${\displaystyle G[T\mathbin {\triangle } T']}$ decomposes into closed alternating trails. Sets ${\displaystyle F\subseteq \mathrm {E} (G)}$ that may satisfy ${\displaystyle F=T\mathbin {\triangle } T'}$ for some tour ${\displaystyle T'}$ may thus be found by enumerating closed alternating trails in ${\displaystyle G}$, even if not every closed alternating trail ${\displaystyle F}$ makes ${\displaystyle T\mathbin {\triangle } F}$ into a tour; it could alternatively turn out to be a disconnected ${\displaystyle 2}$-regular subgraph.

### Key idea

Alternating trails (closed or open) are built by extending a shorter alternating trail, so when exploring the neighbourhood of the current tour ${\displaystyle T}$, one is exploring a search tree of alternating trails. The key idea of the Lin–Kernighan algorithm is to remove from this tree all alternating trails which have gain ${\displaystyle \leq 0}$. This does not prevent finding every closed trail with positive gain, thanks to the following lemma.

Lemma. If ${\displaystyle a_{0},\dotsc ,a_{n-1}}$ are numbers such that ${\displaystyle \sum _{i=0}^{n-1}a_{i}>0}$, then there is a cyclic permutation of these numbers such that all partial sums are positive as well, i.e., there is some ${\displaystyle k}$ such that

${\displaystyle \sum _{i=0}^{r}a_{(k+i){\bmod {n}}}>0}$ for all ${\displaystyle r=0,1,\dotsc ,n-1}$.

For a closed alternating trail ${\displaystyle F=e_{0}\,e_{1}\,\dots \,e_{n-1}}$, one may define ${\displaystyle a_{i}=c(e_{i})}$ if ${\displaystyle e_{i}\in T}$ and ${\displaystyle a_{i}=-c(e_{i})}$ if ${\displaystyle e_{i}\notin T}$; the sum ${\displaystyle \sum \nolimits _{i=0}^{n-1}a_{i}}$ is then the gain ${\displaystyle g(F)}$. Here the lemma implies that there for every closed alternating trail with positive gain exists at least one starting vertex ${\displaystyle v_{0}}$ for which all the gains of the partial trails are positive as well, so ${\displaystyle F}$ will be found when the search explores the branch of alternating trails starting at ${\displaystyle v_{0}}$. (Prior to that the search may have considered other subtrails of ${\displaystyle F}$ starting at other vertices but backed out because some subtrail failed the positive gain constraint.) Reducing the number of branches to explore translates directly to a reduction in runtime, and the sooner a branch can be pruned, the better.

This yields the following algorithm for finding all closed, positive gain alternating trails in the graph.

State: a stack of triples ${\displaystyle (u,i,g)}$, where ${\displaystyle u\in \mathrm {V} (G)}$ is a vertex, ${\displaystyle i\geq 0}$ is the current number of edges in the trail, and ${\displaystyle g}$ is the current trail gain.
1. For all ${\displaystyle u\in \mathrm {V} (G)}$, push ${\displaystyle (u,0,0)}$ onto the stack.
2. While the stack is nonempty:
1. Pop ${\displaystyle (u,i,g)}$ off the stack and let ${\displaystyle v_{i}:=u}$. The current alternating trail is now ${\displaystyle F=\{v_{0}v_{1},v_{1}v_{2},\dotsc ,v_{i-1}v_{i}\}}$.
2. If ${\displaystyle i}$ is even then:
For each ${\displaystyle u\in \mathrm {V} (G)}$ such that ${\displaystyle v_{i}u\in T\setminus \{v_{0}v_{1},v_{1}v_{2},\dotsc ,v_{i-1}v_{i}\}}$ (there are at most two of these), push ${\displaystyle {\bigl (}u,i+1,g+c(v_{i}u){\bigr )}}$ onto the stack.
3. If instead ${\displaystyle i}$ is odd then:
1. If ${\displaystyle g>c(v_{i}v_{0})}$ then report ${\displaystyle \{v_{0}v_{1},v_{1}v_{2},\dotsc ,v_{i-1}v_{i},v_{i}v_{0}\}}$ as a closed alternating trail with gain ${\displaystyle g-c(v_{i}v_{0})>0}$.
2. For each ${\displaystyle u\in \mathrm {V} (G)}$ such that ${\displaystyle g>c(v_{i}u)}$ and ${\displaystyle v_{i}u\notin T\cup \{v_{0}v_{1},v_{1}v_{2},\dotsc ,v_{i-1}v_{i}\}}$ (there may be ${\displaystyle O(n)}$ of these, or there could be none), push ${\displaystyle {\bigl (}u,i+1,g-c(v_{i}u){\bigr )}}$ onto the stack.
3. Stop

As an enumeration algorithm this is slightly flawed, because it may report the same trail multiple times, with different starting points, but Lin–Kernighan does not care because it mostly aborts the enumeration after finding the first hit. It should however be remarked that:

• Lin–Kernighan is not satisfied with just having found a closed alternating trail ${\displaystyle F}$ of positive gain, it additionally requires that ${\displaystyle T\mathbin {\triangle } F}$ is a tour.
• Lin–Kernighan also restricts the search in various ways, most obviously regarding the search depth (but not only in that way). The above unrestricted search still terminates because at ${\displaystyle i=2n}$ there is no longer any unpicked edge remaining in ${\displaystyle T}$, but that is far beyond what is practical to explore.
• In most iterations one wishes to quickly find a better tour ${\displaystyle T'}$, so rather than actually listing all siblings in the search tree before exploring the first of them, one may wish to generate these siblings lazily.

### Basic Lin–Kernighan algorithm

The basic form of the Lin–Kernighan algorithm not only does a local search counterpart of the above enumeration, but it also introduces two parameters that narrow the search.

• The backtracking depth ${\displaystyle p_{1}}$ is an upper bound on the length of the alternating trail after backtracking; beyond this depth, the algorithm explores at most one way of extending the alternating trail. Standard value is that ${\displaystyle p_{1}=5}$.
• The infeasibility depth ${\displaystyle p_{2}}$ is an alternating path length beyond which it begins to be required that closing the current trail (regardless of the gain of doing so) yields an exchange to a new tour. Standard value is that ${\displaystyle p_{2}=2}$.

Because there are ${\displaystyle O(n^{\lfloor p_{1}/2\rfloor })}$ alternating trails of length ${\displaystyle p_{1}}$, and the final round of the algorithm may have to check all of them before concluding that the current tour is locally optimal, we get ${\displaystyle \lfloor p_{1}/2\rfloor }$ (standard value ${\displaystyle 2}$) as a lower bound on the exponent of the algorithm complexity. Lin & Kernighan report ${\displaystyle 2.2}$ as an empirical exponent of ${\displaystyle n}$ in the average overall running time for their algorithm, but other implementors have had trouble reproducing that result.[1] It appears unlikely that the worst-case running time is polynomial.[2]

In terms of a stack as above, the algorithm is:

Input: an instance ${\displaystyle (G,c)}$ of the travelling salesman problem, and a tour ${\displaystyle T\subset \mathrm {E} (G)}$
Output: a locally optimal tour
Variables:
a stack of triples ${\displaystyle (u,i,g)}$, where ${\displaystyle u\in \mathrm {V} (G)}$ is a vertex, ${\displaystyle i\geq 0}$ is the current number of edges in the trail, and ${\displaystyle g}$ is the current trail gain,
the sequence ${\displaystyle v_{0},v_{1},\dotsc }$ of vertices in the current alternating trail,
the best set ${\displaystyle F}$ of exchange edges found for current tour, and its corresponding gain ${\displaystyle g^{*}}$.
Initialise the stack to being empty.
Repeat
Set ${\displaystyle g^{*}:=0}$ and ${\displaystyle F:=\varnothing }$.
For all ${\displaystyle u\in \mathrm {V} (G)}$, push ${\displaystyle (u,0,0)}$ onto the stack.
While the stack is nonempty:
Pop ${\displaystyle (u,i,g)}$ off the stack and let ${\displaystyle v_{i}:=u}$.
If ${\displaystyle i}$ is even then
for each ${\displaystyle u\in \mathrm {V} (G)}$ such that ${\displaystyle v_{i}u\in T\setminus \{v_{0}v_{1},v_{1}v_{2},\dotsc ,v_{i-1}v_{i}\}}$,
push ${\displaystyle {\bigl (}u,i+1,g+c(v_{i}u){\bigr )}}$ onto the stack if: ${\displaystyle i\leq p_{2}}$, or ${\displaystyle uv_{0}\notin T\cup \{v_{0}v_{1},v_{1}v_{2},\dotsc ,v_{i-1}v_{i},v_{i}u\}}$ and ${\displaystyle T\mathbin {\triangle } \{v_{0}v_{1},v_{1}v_{2},\dotsc ,v_{i-1}v_{i},v_{i}u,uv_{0}\}}$ is a tour (Hamiltonicity check)
else (${\displaystyle i}$ is odd):
If ${\displaystyle g>c(v_{i}v_{0})}$, ${\displaystyle g-c(v_{i}v_{0})>g^{*}}$, and ${\displaystyle T\mathbin {\triangle } \{v_{0}v_{1},v_{1}v_{2},\dotsc ,v_{i-1}v_{i},v_{i}v_{0}\}}$ is a tour (Hamiltonicity check) then let ${\displaystyle F:=\{v_{0}v_{1},v_{1}v_{2},\dotsc ,v_{i-1}v_{i},v_{i}v_{0}\}}$ and ${\displaystyle g^{*}:=g-c(v_{i}v_{0})}$.
For each ${\displaystyle u\in \mathrm {V} (G)}$ such that ${\displaystyle g>c(v_{i}u)}$ and ${\displaystyle v_{i}u\notin T\cup \{v_{0}v_{1},v_{1}v_{2},\dotsc ,v_{i-1}v_{i}\}}$, push ${\displaystyle {\bigl (}u,i+1,g-c(v_{i}u){\bigr )}}$ onto the stack.
End if.
Let ${\displaystyle (u,j,g)}$ be the top element on the stack (peek, not pop). If ${\displaystyle j\leq i}$ then
if ${\displaystyle g^{*}>0}$ then
set ${\displaystyle T:=T\mathbin {\triangle } F}$ (update current tour) and clear the stack.
else if ${\displaystyle i>p_{1}}$ then
pop all elements ${\displaystyle (u,j,g)}$ off the stack that have ${\displaystyle j>p_{1}}$
end if
end if
end while
until ${\displaystyle g^{*}=0}$.
Return ${\displaystyle T}$

The length of the alternating trails considered are thus not explicitly bounded, but beyond the backtracking depth ${\displaystyle p_{1}}$ no more than one way of extending the current trail is considered, which in principle stops those explorations from raising the exponent in the runtime complexity.

### Limitations

The closed alternating trails found by the above method are all connected, but the symmetric difference ${\displaystyle T\mathbin {\triangle } T'}$ of two tours need not be, so in general this method of alternating trails cannot explore the full neighbourhood of a trail ${\displaystyle T}$. The literature on the Lin–Kernighan heuristic uses the term sequential exchanges for those that are described by a single alternating trail. The smallest non-sequential exchange would however replace 4 edges and consist of two cycles of 4 edges each (2 edges added, 2 removed), so it is long compared to the typical Lin–Kernighan exchange, and there are few of these compared to the full set of 4-edge exchanges.

In at least one implementation by Lin & Kernighan there was an extra final step considering such non-sequential exchanges of 4 edges before declaring a tour locally optimal, which would mean the tours produced are 4-opt unless one introduces further constraints on the search (which Lin and Kernighan in fact did). The literature is vague on exactly what is included in the Lin–Kernighan heuristic proper, and what constitutes further refinements.

For the asymmetric TSP, the idea of using positive gain alternating trails to find favourable exchanges is less useful, because there are fewer ways in which pieces of a tour can be rearranged to yield new tours when one may not reverse the orientation of a piece. Two pieces can only be patched together to reproduce the original tour. Three pieces can be patched together to form a different tour in one way only, and the corresponding alternating trail does not extend to a closed trail for rearranging four pieces into a new tour. To rearrange four pieces, one needs a non-sequential exchange.

## Checking Hamiltonicity

The Lin–Kernighan heuristic checks the validity of tour candidates ${\displaystyle T\mathbin {\triangle } F}$ at two points: obviously when deciding whether a better tour has been found, but also as a constraint to descending in the search tree, as controlled via the infeasibility depth ${\displaystyle p_{2}}$. Concretely, at larger depths in the search a vertex ${\displaystyle v_{2k+1}}$ is only appended to the alternating trail if ${\displaystyle T\mathbin {\triangle } \{v_{0}v_{1},v_{1}v_{2},\dotsc ,v_{2k}v_{2k+1},v_{2k+1}v_{0}\}}$ is a tour. By design that set of edges constitutes a 2-factor in ${\displaystyle G}$, so what needs to be determined is whether that 2-factor consists of a single Hamiltonian cycle, or instead is made up of several cycles.

If naively posing this subproblem as giving a subroutine the set of ${\displaystyle n}$ edges as input, one ends up with ${\displaystyle O(n)}$ as the time complexity for this check, since it is necessary to walk around the full tour before being able to determine that it is in fact a Hamiltonian cycle. That is too slow for the second usage of this test, which gets carried out for every alternating trail with more than ${\displaystyle 2}$ edges from ${\displaystyle T}$. If keeping track of more information, the test can instead be carried out in constant time.

A useful degree of freedom here is that one may choose the order in which step 2.3.2 iterates over all vertices; in particular, one may follow the known tour ${\displaystyle T}$. Note that after picking ${\displaystyle k}$ edges from ${\displaystyle T}$, the remaining subgraph ${\displaystyle {\bigl (}\mathrm {V} (G),T\setminus \{v_{0}v_{1},\dotsc ,v_{2k-2}v_{2k-1}\}{\bigr )}}$ consists of ${\displaystyle k}$ paths. The outcome of the Hamiltonicity test done when considering the ${\displaystyle (k+1)}$th edge ${\displaystyle v_{2k}v_{2k+1}}$ depends only on in which of these paths that ${\displaystyle v_{2k}}$ resides and whether ${\displaystyle v_{2k+1}}$ is before or after ${\displaystyle v_{2k}}$. Hence it would be sufficient to examine ${\displaystyle 2k}$ different cases as part of performing step 2.3.2 for ${\displaystyle v_{2k-1}}$; as far as ${\displaystyle v_{2k+1}}$ is concerned, the outcome of this test can be inherited information rather than something that has to be computed fresh.

## References

1. ^ Melamed, I. I.; Sergeev, S. I.; Sigal, I. Kh. (1989). "The traveling salesman problem. Approximate algorithms". Avtomatika i Telemekhanika. 11: 3–26.
2. ^ Papadimitriou, C. H. (1992). "The complexity of the Lin–Kernighan heuristic for the travelling salesman problem". SIAM Journal on Computing. 21 (3): 450–465. doi:10.1137/0221030.