Lin–Kernighan heuristic

From Wikipedia, the free encyclopedia

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.


For a given instance 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 and are looking for new tour such that the symmetric difference is not too large and the length of the new tour is less than the length of the current tour. Since is typically much smaller than and , it is convenient to consider the quantity

— the gain of using when switching from

since : how much longer the current tour is than the new tour . Naively -opt can be regarded as examining all with exactly elements ( in but not in , and another in but not in ) such that is again a tour, looking for such a set which has . It is however easier to do those tests in the opposite order: first search for plausible with positive gain, and only second check if is in fact a tour.

Define a trail in to be alternating (with respect to ) if its edges are alternatingly in and not in , respectively. Because the subgraphs and are -regular, the subgraph will have vertices of degree , , and only, and at each vertex there are as many incident edges from as there are from . Hence (essentially by Hierholzer's algorithm for finding Eulerian circuits) the graph decomposes into closed alternating trails. Sets that may satisfy for some tour may thus be found by enumerating closed alternating trails in , even if not every closed alternating trail makes into a tour; it could alternatively turn out to be a disconnected -regular subgraph.

Key idea[edit]

Alternating trails (closed or open) are built by extending a shorter alternating trail, so when exploring the neighbourhood of the current tour , 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 . This does not prevent finding every closed trail with positive gain, thanks to the following lemma.

Lemma. If are numbers such that , then there is a cyclic permutation of these numbers such that all partial sums are positive as well, i.e., there is some such that

for all .

For a closed alternating trail , one may define if and if ; the sum is then the gain . Here the lemma implies that there for every closed alternating trail with positive gain exists at least one starting vertex for which all the gains of the partial trails are positive as well, so will be found when the search explores the branch of alternating trails starting at . (Prior to that the search may have considered other subtrails of 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 , where is a vertex, is the current number of edges in the trail, and is the current trail gain.
  1. For all , push onto the stack.
  2. While the stack is nonempty:
    1. Pop off the stack and let . The current alternating trail is now .
    2. If is even then:
      For each such that (there are at most two of these), push onto the stack.
    3. If instead is odd then:
      1. If then report as a closed alternating trail with gain .
      2. For each such that and (there may be of these, or there could be none), push 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 of positive gain, it additionally requires that 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 there is no longer any unpicked edge remaining in , but that is far beyond what is practical to explore.
  • In most iterations one wishes to quickly find a better tour , 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[edit]

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 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 .
  • The infeasibility depth 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 .

Because there are alternating trails of length , 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 (standard value ) as a lower bound on the exponent of the algorithm complexity. Lin & Kernighan report as an empirical exponent of 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 of the travelling salesman problem, and a tour
Output: a locally optimal tour
a stack of triples , where is a vertex, is the current number of edges in the trail, and is the current trail gain,
the sequence of vertices in the current alternating trail,
the best set of exchange edges found for current tour, and its corresponding gain .
Initialise the stack to being empty.
Set and .
For all , push onto the stack.
While the stack is nonempty:
Pop off the stack and let .
If is even then
for each such that ,
push onto the stack if: , or and is a tour (Hamiltonicity check)
else ( is odd):
If , , and is a tour (Hamiltonicity check) then let and .
For each such that and , push onto the stack.
End if.
Let be the top element on the stack (peek, not pop). If then
if then
set (update current tour) and clear the stack.
else if then
pop all elements off the stack that have
end if
end if
end while
until .

The length of the alternating trails considered are thus not explicitly bounded, but beyond the backtracking depth 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.


The closed alternating trails found by the above method are all connected, but the symmetric difference of two tours need not be, so in general this method of alternating trails cannot explore the full neighbourhood of a trail . 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[edit]

The Lin–Kernighan heuristic checks the validity of tour candidates 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 . Concretely, at larger depths in the search a vertex is only appended to the alternating trail if is a tour. By design that set of edges constitutes a 2-factor in , 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 edges as input, one ends up with 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 edges from . 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 . Note that after picking edges from , the remaining subgraph consists of paths. The outcome of the Hamiltonicity test done when considering the th edge depends only on in which of these paths that resides and whether is before or after . Hence it would be sufficient to examine different cases as part of performing step 2.3.2 for ; as far as is concerned, the outcome of this test can be inherited information rather than something that has to be computed fresh.


  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.

External links[edit]