Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Held–Karp algorithm

From Wikipedia, the free encyclopedia
(Redirected fromHeld-Karp algorithm)
Solution of the traveling salesman problem

TheHeld–Karp algorithm, also called theBellman–Held–Karp algorithm, is adynamic programming algorithm proposed in 1962 independently byBellman[1] and by Held andKarp[2] to solve thetraveling salesman problem (TSP), in which the input is adistance 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 theHamiltonian cycle problem, inexponential time.

Algorithm description and motivation

[edit]

Number the cities1,2,,n{\displaystyle 1,2,\ldots ,n}, with1{\displaystyle 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 citiesS{2,,n}{\displaystyle S\subseteq \{2,\ldots ,n\}} and every citye1{\displaystyle e\neq 1} not contained inS{\displaystyle S}, the shortest one-way path from1{\displaystyle 1} toe{\displaystyle e} that passes through every city inS{\displaystyle S} in some order (but not through any other cities). Denote this distanceg(S,e){\displaystyle g(S,e)}, and writed(u,v){\displaystyle d(u,v)} for the length of the direct edge fromu{\displaystyle u} tov{\displaystyle v}. We'll compute values ofg(S,e){\displaystyle g(S,e)} starting with the smallest setsS{\displaystyle S} and finishing with the largest.

WhenS{\displaystyle S} has two or fewer elements, then calculatingg(S,e){\displaystyle g(S,e)} requires looking at one or two possible shortest paths. For example,g(,e){\displaystyle g(\emptyset ,e)} is simplyd(1,e){\displaystyle d(1,e)}, andg({2},3){\displaystyle g(\{2\},3)} is just the length of123{\displaystyle 1\rightarrow 2\rightarrow 3}. Likewise,g({2,3},4){\displaystyle g(\{2,3\},4)} is the length of either1234{\displaystyle 1\rightarrow 2\rightarrow 3\rightarrow 4} or1324{\displaystyle 1\rightarrow 3\rightarrow 2\rightarrow 4}, whichever is shorter.

OnceS{\displaystyle S} contains three or more cities, the number of paths throughS{\displaystyle S} rises quickly, but only a few such paths need to be examined to find the shortest. For instance, if1234{\displaystyle 1\rightarrow 2\rightarrow 3\rightarrow 4} is shorter than1324{\displaystyle 1\rightarrow 3\rightarrow 2\rightarrow 4}, then12345{\displaystyle 1\rightarrow 2\rightarrow 3\rightarrow 4\rightarrow 5} must be shorter than13245{\displaystyle 1\rightarrow 3\rightarrow 2\rightarrow 4\rightarrow 5}, and the length of13245{\displaystyle 1\rightarrow 3\rightarrow 2\rightarrow 4\rightarrow 5} is not a possible value ofg({2,3,4},5){\displaystyle g(\{2,3,4\},5)}. Similarly, if the shortest path from1{\displaystyle 1} through{2,3,4}{\displaystyle \{2,3,4\}} to5{\displaystyle 5} is14325{\displaystyle 1\rightarrow 4\rightarrow 3\rightarrow 2\rightarrow 5}, and the shortest path from1{\displaystyle 1} through{2,3,4,5}{\displaystyle \{2,3,4,5\}} to6{\displaystyle 6} ends with the edge56{\displaystyle 5\rightarrow 6}, then the whole path from1{\displaystyle 1} to6{\displaystyle 6} must be143256{\displaystyle 1\rightarrow 4\rightarrow 3\rightarrow 2\rightarrow 5\rightarrow 6}, and not any of the other five paths created by visiting{2,3,4}{\displaystyle \{2,3,4\}} in a different order.

More generally, supposeS={s1,,sk}{\displaystyle S=\{s_{1},\ldots ,s_{k}\}} is a set ofk{\displaystyle k} cities. For every integer1ik{\displaystyle 1\leq i\leq k}, writeSi=S{si}={s1,,si1,si+1,,sk}{\displaystyle S_{i}=S\setminus \{s_{i}\}=\{s_{1},\ldots ,s_{i-1},s_{i+1},\ldots ,s_{k}\}} for the set created by removingsi{\displaystyle s_{i}} fromS{\displaystyle S}. Then if the shortest path from1{\displaystyle 1} throughS{\displaystyle S} toe{\displaystyle e} hassi{\displaystyle s_{i}} as its second-to-last city, then removing the final edge from this path must give the shortest path from1{\displaystyle 1} tosi{\displaystyle s_{i}} throughSi{\displaystyle S_{i}}. This means there are onlyk{\displaystyle k} possible shortest paths from1{\displaystyle 1} toe{\displaystyle e} throughS{\displaystyle S}, one for each possible second-to-last citysi{\displaystyle s_{i}} with lengthg(Si,si)+d(si,e){\displaystyle g(S_{i},s_{i})+d(s_{i},e)}, andg(S,e)=min1ikg(Si,si)+d(si,e){\displaystyle g(S,e)=\min _{1\leq i\leq k}g(S_{i},s_{i})+d(s_{i},e)}.

This stage of the algorithm finishes wheng({2,,i1,i+1,,n},i){\displaystyle g(\{2,\ldots ,i-1,i+1,\ldots ,n\},i)} is known for every integer2in{\displaystyle 2\leq i\leq n}, giving the shortest distance from city1{\displaystyle 1} to cityi{\displaystyle i} that passes through every other city. The much shorter second stage adds these distances to the edge lengthsd(i,1){\displaystyle d(i,1)} to given1{\displaystyle 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 alongsideg(S,e){\displaystyle g(S,e)} the label of the second-to-last city on the path from1{\displaystyle 1} toe{\displaystyle e} throughS{\displaystyle S}, raising space requirements by only a constant factor.

Algorithmic complexity

[edit]

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

Time

[edit]

Computing one value ofg(S,e){\displaystyle g(S,e)} for ak{\displaystyle k}-element subsetS{\displaystyle S} of{2,,n}{\displaystyle \{2,\ldots ,n\}} requires finding the shortest ofk{\displaystyle k} possible paths, each found by adding a known value ofg{\displaystyle g} and an edge length from the original graph; that is, it requires time proportional tok{\displaystyle k}. There are(n1k){\textstyle {\binom {n-1}{k}}}k{\displaystyle k}-element subsets of{2,,n}{\displaystyle \{2,\ldots ,n\}}; and each subset givesnk1{\displaystyle n-k-1} possible values ofe{\displaystyle e}. Computing all values ofg(S,e){\displaystyle g(S,e)} where|S|=k{\displaystyle |S|=k} thus requires timek(nk1)(n1k)=(n1)(n2)(n3k1){\textstyle k(n-k-1){\binom {n-1}{k}}=(n-1)(n-2){\binom {n-3}{k-1}}}, for a total time across all subset sizes(n1)(n2)k=1n2(n3k1)=(n1)(n2)2n3=Θ(n22n){\textstyle (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})}. The second stage of the algorithm, finding a complete cycle fromn1{\displaystyle n-1} candidates, takesΘ(n){\displaystyle \Theta (n)} time and does not affect asymptotic performance.

Forundirected graphs, the algorithm can be stopped early after thek=n12{\textstyle k=\lfloor {\frac {n-1}{2}}\rfloor } step, and finding the minimumg(S,e)+g(Sc{1,e},e){\textstyle g(S,e)+g(S^{c}\setminus \{1,e\},e)} for every{(S,e):|S|=n12,eSc{1}}{\textstyle \{(S,e):|S|=\lfloor {\frac {n-1}{2}}\rfloor ,e\in S^{c}\setminus \{1\}\}}, whereSc{\textstyle S^{c}} is thecomplement set ofS{\textstyle S}. This is analogous to abidirectional search starting at1{\textstyle 1} and meeting at midpointe{\textstyle e}. However, this is a constant factor improvement and does not affect asymptotic performance.

Space

[edit]

Storing all values ofg(S,e){\displaystyle g(S,e)} for subsets of sizek{\displaystyle k} requires keeping(nk1)(n1k)=(n1)(n2k){\textstyle (n-k-1){\binom {n-1}{k}}=(n-1){\binom {n-2}{k}}} values. A complete table of values ofg(S,e){\displaystyle g(S,e)} thus requires spacek=0n2(n1)(n2k)=(n1)2n=Θ(n2n){\textstyle \sum _{k=0}^{n-2}(n-1){\binom {n-2}{k}}=(n-1)2^{n}=\Theta (n2^{n})}. This assumes thatn{\textstyle n} is sufficiently small enough such thatS{\textstyle S} can be stored as abitmask of constant multiple ofmachine words, rather than an explicit k-tuple.

If only the length of the shortest cycle is needed, not the cycle itself, thenspace complexity can be improved somewhat by noting that calculatingg(S,e){\displaystyle g(S,e)} for aS{\displaystyle S} of sizek{\displaystyle k} requires only values ofg{\displaystyle g} for subsets of sizek1{\displaystyle k-1}. Keeping only the(n1)[(n2k1)+(n2k)]{\textstyle (n-1)\left[{\binom {n-2}{k-1}}+{\binom {n-2}{k}}\right]} values ofg(S,e){\displaystyle g(S,e)} whereS{\displaystyle S} has size eitherk1{\displaystyle k-1} ork{\displaystyle k} reduces the algorithm's maximum space requirements, attained whenk=n2{\textstyle k=\left\lfloor {\frac {n}{2}}\right\rfloor }, toΘ(n2n){\displaystyle \Theta ({\sqrt {n}}2^{n})}.

Pseudocode

[edit]

Source:[3]

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

References

[edit]
  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. ^"Dynamic programming"(PDF). January 2020. Archived fromthe original(PDF) on 2015-02-08.
Retrieved from "https://en.wikipedia.org/w/index.php?title=Held–Karp_algorithm&oldid=1313090396"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp