Held–Karp algorithm explained

The Held–Karp algorithm, also called the Bellman–Held–Karp algorithm, is a dynamic programming algorithm proposed in 1962 independently by Bellman[1] and by Held and Karp[2] to solve the traveling salesman problem (TSP), in which the input is a distance matrix between a set of cities, and the goal is to find a minimum-length tour that visits each city exactly once before returning to the starting point. It finds the exact solution to this problem, and to several related problems including the Hamiltonian cycle problem, in exponential time.

Algorithm description and motivation

Number the cities

1,2,\ldots,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\subseteq\{2,\ldots,n\}

and every city

e1

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(\emptyset,e)

is simply

d(1,e)

, and

g(\{2\},3)

is just the length of

123

. Likewise,

g(\{2,3\},4)

is the length of either

1234

or

1324

, 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

1234

is shorter than

1324

, then

12345

must be shorter than

13245

, and the length of

13245

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

14325

, and the shortest path from

1

through

\{2,3,4,5\}

to

6

ends with the edge

56

, then the whole path from

1

to

6

must be

143256

, and not any of the other five paths created by visiting

\{2,3,4\}

in a different order.

More generally, suppose

S=\{s1,\ldots,sk\}

is a set of

k

cities. For every integer

1\leqi\leqk

, write

Si=S\setminus\{si\}=\{s1,\ldots,si-1,si+1,\ldots,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)=min1g(Si,si)+d(si,e)

.

This stage of the algorithm finishes when

g(\{2,\ldots,i-1,i+1,\ldots,n\},i)

is known for every integer

2\leqi\leqn

, 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.

Algorithmic complexity

The Held–Karp algorithm has exponential time complexity

\Theta(2nn2)

, significantly better than the superexponential performance

\Theta(n!)

of a brute-force algorithm. Held–Karp, however, requires

\Theta(n2n)

space to hold all computed values of the function

g(S,e)

, while brute force needs only

\Theta(n2)

space to store the graph itself.

Time

Computing one value of

g(S,e)

for a

k

-element subset

S

of

\{2,\ldots,n\}

requires finding the shortest of

k

possible paths, each found by adding a known value of

g

and an edge length from the original graph; that is, it requires time proportional to

k

. There are \binom

k

-element subsets of

\{2,\ldots,n\}

; and each subset gives

n-k-1

possible values of

e

. Computing all values of

g(S,e)

where

|S|=k

thus requires time k (n-k-1) \binom = (n-1) (n-2) \binom, for a total time across all subset sizes (n-1)(n-2) \sum_^ \binom = (n-1) (n-2) 2^ = \Theta(n^2 2^n). The second stage of the algorithm, finding a complete cycle from

n-1

candidates, takes

\Theta(n)

time and does not affect asymptotic performance.

For undirected graphs, the algorithm can be stopped early after the k = \lfloor\frac\rfloor step, and finding the minimum g(S, e) + g(S^c\setminus\, e) for every \, where S^c is the complement set of S. This is analogous to a bidirectional search starting at 1 and meeting at midpoint e. However, this is a constant factor improvement and does not affect asymptotic performance.

Space

Storing all values of

g(S,e)

for subsets of size

k

requires keeping (n-k-1) \binom = (n-1) \binom values. A complete table of values of

g(S,e)

thus requires space \sum_^ (n-1) \binom = (n-1) 2^n = \Theta(n 2^n). This assumes that n is sufficiently small enough such that S can be stored as a bitmask of constant multiple of machine words, rather than an explicit k-tuple.

If only the length of the shortest cycle is needed, not the cycle itself, then space complexity can be improved somewhat by noting that calculating

g(S,e)

for a

S

of size

k

requires only values of

g

for subsets of size

k-1

. Keeping only the (n-1) \left[\binom{n-2}{k-1} + \binom{n-2}{k} \right] values of

g(S,e)

where

S

has size either

k-1

or

k

reduces the algorithm's maximum space requirements, attained when k = \left\lfloor \frac \right\rfloor, to

\Theta(\sqrt{n}2n)

.

Pseudocode

Source:[3]

function algorithm TSP (G, n) is for k := 2 to n do g(k) := d(1, k) end for for s := 2 to n−1 do for all S ⊆, |S| = s do for all k ∈ S do g(S, k) := minm≠k,m∈S [g(S\{k}, m) + d(m, k)] end for end for end for opt := mink≠1 [g({2, 3, ..., n}, k) + d(k, 1)] return (opt) end function

Related algorithms

Exact algorithms for solving the TSP

Besides Dynamic Programming, Linear programming and Branch and bound are design patterns also used for exact solutions to the TSP. Linear programming applies the cutting plane method in integer programming, i.e. solving the LP formed by two constraints in the model and then seeking the cutting plane by adding inequality constraints to gradually converge at an optimal solution. When people apply this method to find a cutting plane, they often depend on experience, so this method is seldom used as a general method.

The term branch and bound was first used in 1963 in a paper published by Little et al. on the TSP, describing a technique of combining smaller search spaces and establishing lower bounds to enlarge the practical range of application for an exact solution. The technique is useful for expanding the number of cities able to be considered computationally, but still breaks down in large-scale data sets.

It controls the searching process through applying restrictive boundaries, allowing a search for the optimal solution branch from the space state tree to find an optimal solution as quickly as possible. The pivotal component of this algorithm is the selection of the restrictive boundary. Different restrictive boundaries may form different branch-bound algorithms.

Approximate algorithms for solving the TSP

As the application of precise algorithm to solve problem is very limited, we often use approximate algorithm or heuristic algorithm. The result of the algorithm can be assessed by C / C* ≤ ε . C is the total travelling distance generated from approximate algorithm; C* is the optimal travelling distance; ε is the upper limit for the ratio of the total travelling distance of approximate solution to optimal solution under the worst condition. The value of ε >1.0. The more it closes to 1.0, the better the algorithm is. These algorithms include: Interpolation algorithm, Nearest neighbour algorithm, Clark & Wright algorithm, Double spanning tree algorithm, Christofides algorithm, Hybrid algorithm, Probabilistic algorithm (such as Simulated annealing).

Notes and References

  1. ‘Dynamic programming treatment of the travelling salesman problem’, Richard Bellman, Journal of Assoc. Computing Mach. 9. 1962.
  2. 'A dynamic programming approach to sequencing problems’, Michael Held and Richard M. Karp, Journal for the Society for Industrial and Applied Mathematics 1:10. 1962
  3. Web site: Dynamic programming . January 2020 . https://web.archive.org/web/20150208031521/http://www.cs.upc.edu/~mjserna/docencia/algofib/P07/dynprog.pdf . 2015-02-08.