Lably
Skip to content

Held–Karp algorithm

Solution of the traveling salesman problem

  • 4 min read
  • 3 chapters

Related on Lably

Through Graph (discrete mathematics) and Word (computer architecture)

Algorithm description and motivation

Number the cities 1,2,…,n, with 1 designated arbitrarily as a "starting" city (since the solution to TSP is a Hamiltonian cycle, the choice of starting city doesn't matter). The Held–Karp algorithm begins by calculating, for each set of cities S⊆{2,…,n} and every city e≠1 not contained in S, the shortest one-way path from 1 to e that passes through every city in S in some order (but not through any other cities). Denote this distance g(S,e), and write d(u,v) for the length of the direct edge from u to v. We'll compute values of g(S,e) starting with the smallest sets S and finishing with the largest.

When S has two or fewer elements, then calculating g(S,e) requires looking at one or two possible shortest paths. For example, g(∅,e) is simply d(1,e), and g({2},3) is just the length of 1→2→3. Likewise, g({2,3},4) is the length of either 1→2→3→4 or 1→3→2→4, whichever is shorter.

Once S contains three or more cities, the number of paths through S rises quickly, but only a few such paths need to be examined to find the shortest. For instance, if 1→2→3→4 is shorter than 1→3→2→4, then 1→2→3→4→5 must be shorter than 1→3→2→4→5, and the length of 1→3→2→4→5 is not a possible value of g({2,3,4},5). Similarly, if the shortest path from 1 through {2,3,4} to 5 is 1→4→3→2→5, and the shortest path from 1 through {2,3,4,5} to 6 ends with the edge 5→6, then the whole path from 1 to 6 must be 1→4→3→2→5→6, and not any of the other five paths created by visiting {2,3,4} in a different order.

More generally, suppose S={s1,…,sk} is a set of k cities. For every integer 1≤i≤k, write Si=S∖{si}={s1,…,si−1,si+1,…,sk} for the set created by removing si from S. Then if the shortest path from 1 through S to e has si as its second-to-last city, then removing the final edge from this path must give the shortest path from 1 to si through Si. This means there are only k possible shortest paths from 1 to e through S, one for each possible second-to-last city si with length g(Si,si)+d(si,e), and g(S,e)=min1≤i≤kg(Si,si)+d(si,e).

This stage of the algorithm finishes when g({2,…,i−1,i+1,…,n},i) is known for every integer 2≤i≤n, giving the shortest distance from city 1 to city i that passes through every other city. The much shorter second stage adds these distances to the edge lengths d(i,1) to give n−1 possible shortest cycles, and then finds the shortest.

The shortest path itself (and not just its length), finally, may be reconstructed by storing alongside g(S,e) the label of the second-to-last city on the path from 1 to e through S, raising space requirements by only a constant factor.

Sign in to keep reading

2 more chapters are free with a Lably account.

More in Discrete mathematics