Held–Karp algorithm

From HandWiki
Revision as of 16:46, 8 February 2024 by Rtexter1 (talk | contribs) (update)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Short description: Solution of the traveling salesman problem

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 [math]\displaystyle{ 1, 2, \ldots, n }[/math], with [math]\displaystyle{ 1 }[/math] designated arbitrarily as a "starting" city (since the solution to TSP is a cycle, the choice of starting city doesn't matter). The Held–Karp algorithm begins by calculating, for each set of cities [math]\displaystyle{ S \subseteq \{2, \ldots, n\} }[/math] and every city [math]\displaystyle{ e \neq 1 }[/math] not contained in [math]\displaystyle{ S }[/math], the shortest one-way path from [math]\displaystyle{ 1 }[/math] to [math]\displaystyle{ e }[/math] that passes through every city in [math]\displaystyle{ S }[/math] in some order (but not through any other cities). Denote this distance [math]\displaystyle{ g(S, e) }[/math], and write [math]\displaystyle{ d(u, v) }[/math] for the length of the direct edge from [math]\displaystyle{ u }[/math] to [math]\displaystyle{ v }[/math]. We'll compute values of [math]\displaystyle{ g(S, e) }[/math] starting with the smallest sets [math]\displaystyle{ S }[/math] and finishing with the largest.

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

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

More generally, suppose [math]\displaystyle{ S = \{s_1, \ldots, s_k\} }[/math] is a set of [math]\displaystyle{ k }[/math] cities. For every integer [math]\displaystyle{ 1 \leq i \leq k }[/math], write [math]\displaystyle{ S_i = S \setminus \{s_i\} = \{s_1, \ldots, s_{i-1}, s_{i+1}, \ldots, s_k\} }[/math] for the set created by removing [math]\displaystyle{ s_i }[/math] from [math]\displaystyle{ S }[/math]. Then if the shortest path from [math]\displaystyle{ 1 }[/math] through [math]\displaystyle{ S }[/math] to [math]\displaystyle{ e }[/math] has [math]\displaystyle{ s_i }[/math] as its second-to-last city, then removing the final edge from this path must give the shortest path from [math]\displaystyle{ 1 }[/math] to [math]\displaystyle{ s_i }[/math] through [math]\displaystyle{ S_i }[/math]. This means there are only [math]\displaystyle{ k }[/math] possible shortest paths from [math]\displaystyle{ 1 }[/math] to [math]\displaystyle{ e }[/math] through [math]\displaystyle{ S }[/math], one for each possible second-to-last city [math]\displaystyle{ s_i }[/math] with length [math]\displaystyle{ g(S_i, s_i) + d(s_i, e) }[/math], and [math]\displaystyle{ g(S, e) = \min_{1 \leq i \leq k} g(S_i, s_i) + d(s_i, e) }[/math].

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

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

Algorithmic complexity

The Held–Karp algorithm has exponential time complexity [math]\displaystyle{ \Theta(2^n n^2) }[/math], significantly better than the superexponential performance [math]\displaystyle{ \Theta(n!) }[/math] of a brute-force algorithm. Held–Karp, however, requires [math]\displaystyle{ \Theta(n 2^n) }[/math] space to hold all computed values of the function [math]\displaystyle{ g(S, e) }[/math], while brute force needs only [math]\displaystyle{ \Theta(n^2) }[/math] space to store the graph itself.

Time

Computing one value of [math]\displaystyle{ g(S, e) }[/math] for a [math]\displaystyle{ k }[/math]-element subset [math]\displaystyle{ S }[/math] of [math]\displaystyle{ \{2, \ldots, n\} }[/math] requires finding the shortest of [math]\displaystyle{ k }[/math] possible paths, each found by adding a known value of [math]\displaystyle{ g }[/math] and an edge length from the original graph; that is, it requires time proportional to [math]\displaystyle{ k }[/math]. There are [math]\displaystyle{ \binom{n-1}{k} }[/math] [math]\displaystyle{ k }[/math]-element subsets of [math]\displaystyle{ \{2, \ldots, n\} }[/math]; and each subset gives [math]\displaystyle{ n-k-1 }[/math] possible values of [math]\displaystyle{ e }[/math]. Computing all values of [math]\displaystyle{ g(S, e) }[/math] where [math]\displaystyle{ |S| = k }[/math] thus requires time [math]\displaystyle{ k (n-k-1) \binom{n-1}{k} = (n-1) (n-2) \binom{n-3}{k-1} }[/math], for a total time across all subset sizes [math]\displaystyle{ (n-1)(n-2) \sum_{k=1}^{n-2} \binom{n-3}{k-1} = (n-1) (n-2) 2^{n-3} = \Theta(n^2 2^n) }[/math]. The second stage of the algorithm, finding a complete cycle from [math]\displaystyle{ n-1 }[/math] candidates, takes [math]\displaystyle{ \Theta(n) }[/math] time and does not affect asymptotic performance.

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

Space

Storing all values of [math]\displaystyle{ g(S, e) }[/math] for subsets of size [math]\displaystyle{ k }[/math] requires keeping [math]\displaystyle{ (n-k-1) \binom{n-1}{k} = (n-1) \binom{n-2}{k} }[/math] values. A complete table of values of [math]\displaystyle{ g(S, e) }[/math] thus requires space [math]\displaystyle{ \sum_{k=0}^{n-2} (n-1) \binom{n-2}{k} = (n-1) 2^n = \Theta(n 2^n) }[/math]. This assumes that [math]\displaystyle{ n }[/math] is sufficiently small enough such that [math]\displaystyle{ S }[/math] 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 [math]\displaystyle{ g(S, e) }[/math] for a [math]\displaystyle{ S }[/math] of size [math]\displaystyle{ k }[/math] requires only values of [math]\displaystyle{ g }[/math] for subsets of size [math]\displaystyle{ k-1 }[/math]. Keeping only the [math]\displaystyle{ (n-1) \left[ \binom{n-2}{k-1} + \binom{n-2}{k} \right] }[/math] values of [math]\displaystyle{ g(S, e) }[/math] where [math]\displaystyle{ S }[/math] has size either [math]\displaystyle{ k-1 }[/math] or [math]\displaystyle{ k }[/math] reduces the algorithm's maximum space requirements, attained when [math]\displaystyle{ k = \left\lfloor \frac{n}{2} \right\rfloor }[/math], to [math]\displaystyle{ \Theta(\sqrt{n} 2^n) }[/math].

Example

Source:[3]

Distance matrix:

[math]\displaystyle{ C= \begin{pmatrix} 0 & 2 & 9 & 10 \\ 1 & 0 & 6 & 4 \\ 15 & 7 & 0 & 8 \\ 6 & 3 & 12 & 0 \end{pmatrix} }[/math]

Functions description:

  • g(x, S) - starting from 1, path min cost ends at vertex x, passing vertices in set S exactly once
  • cxy - edge cost ends at x from y
  • p(x, S) - the second-to-last vertex to x from set S. Used for constructing the TSP path back at the end.

k = 0, null set:

Set ∅:

       g(2, ∅) = c21 = 1 
       g(3, ∅) = c31 = 15
       g(4, ∅) = c41 = 6

k = 1, consider sets of 1 element:

Set {2}:

       g(3,{2}) = c32 + g(2, ∅ ) = c32 + c21 = 7 + 1 = 8       p(3,{2}) = 2
       g(4,{2}) = c42 + g(2, ∅ ) = c42 + c21 = 3 + 1 = 4       p(4,{2}) = 2

Set {3}:

       g(2,{3}) = c23 + g(3, ∅ ) = c23 + c31 = 6 + 15 = 21     p(2,{3}) = 3
       g(4,{3}) = c43 + g(3, ∅ ) = c43 + c31 = 12 + 15 = 27    p(4,{3}) = 3

Set {4}:

       g(2,{4}) = c24 + g(4, ∅ ) = c24 + c41 = 4 + 6 = 10      p(2,{4}) = 4
       g(3,{4}) = c34 + g(4, ∅ ) = c34 + c41 = 8 + 6 = 14      p(3,{4}) = 4

k = 2, consider sets of 2 elements:

Set {2,3}:

         g(4,{2,3}) = min {c42 + g(2,{3}), c43 + g(3,{2})} = min {3+21, 12+8}= min {24, 20}= 20
         p(4,{2,3}) = 3

Set {2,4}:

         g(3,{2,4}) = min {c32 + g(2,{4}), c34 + g(4,{2})} = min {7+10, 8+4}= min {17, 12} = 12
         p(3,{2,4}) = 4

Set {3,4}:

          g(2,{3,4}) = min {c23 + g(3,{4}), c24 + g(4,{3})} = min {6+14, 4+27}= min {20, 31}= 20
          p(2,{3,4}) = 3

Length of an optimal tour:

 f = g(1,{2,3,4}) = min { c12 + g(2,{3,4}), c13 + g(3,{2,4}), c14 + g(4,{2,3}) }
                  = min {2 + 20, 9 + 12, 10 + 20} = min {22, 21, 30} = 21

Predecessor of node 1: p(1,{2,3,4}) = 3

Predecessor of node 3: p(3, {2,4}) = 4

Predecessor of node 4: p(4, {2}) = 2

Predecessor of node 2: p(2, {}) = 1

Hence, an optimal TSP tour is given by 1 → 2→ 4 → 3→ 1.

Pseudocode

Source:[4]

function algorithm TSP (G, n) is
    for k := 2 to n do
        g({k}, k) := d(1, k)
    end for

    for s := 2 to n−1 do
        for all S ⊆ {2, ..., n}, |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).

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. http://www.mafy.lut.fi/study/DiscreteOpt/tspdp.pdf[bare URL PDF]
  4. http://www.lsi.upc.edu/~mjserna/docencia/algofib/P07/dynprog.pdf[yes|permanent dead link|dead link}}]