70 likes | 164 Vues
Appendix B: Solving TSP by Dynamic Programming. Course: Algorithm Design and Analysis. Solving TSP by Dynamic Programming. Regard a tour to be a simple path that starts and end at vertex 1.
E N D
Appendix B: Solving TSP by Dynamic Programming Course: Algorithm Design and Analysis
Solving TSP by Dynamic Programming • Regard a tour to be a simple path that starts and end at vertex 1. • Every tour consists of an edge <1,k> for some kV – {1} and a path from k to vertex 1. The path from vertex k to vertex 1 goes through each vertex in V – {1,k} exactly once. • Let g(i, S) be the length of a shortest path starting at vertex i, going through all vertices in S and terminating at vertex 1. • g(1, V- {1}) is the length of an optimal salesperson tour. • From the principle of optimality it follows that: g(1, V – {1}) = min {c1k + g(k, V- {1, k})} (1) 2≤k ≤n
Generalizing from (1) we obtain g(i, S) = minjS {cij + g(j, S - {j})} (2) • Formula (2) may be solved for g(1, V - {1}) if we know g(k, V - {1,k}) for all choices of k. • The g values may be obtained by using (2). • Clearly, g(i, ) = ci,1, 1 ≤ i ≤ n. Hence, we may use (2) to obtain g(i, S) for all S of size 1. • Then we can obtain g(i, S) for S which |S| = 2. • Then, we can obtain g(i, S) for S which |S| = 3, etc. • When |S| < n -1, the value of i and S for which g(i, S) is needed are such that i 1; 1S and i S.
Example • Consider the directed graph which has the adjacency matrix as follows: 0 10 15 20 5 0 9 10 6 13 0 12 8 8 9 0 g(2, ) = c21 = 5 ; g(3, ) = c31 = 6; g(4, ) = c41 = 8. Using (2) we obtain g(2, {3}) = c23 + g(3, ) = 9 + 6 = 15 g(2, {4}) = c24 + g(4, ) = 10 + 8 = 18 g(3, {2}) = c32 + g(2, ) = 13 + 5 = 18 g(3, {4}) = c34 + g(4, ) = 12 + 8 = 20 g(4, {2}) = c42 + g(2, ) = 8 + 5 = 13 g(4, {3}) = c43 + g(3, ) = 9 + 6 =15
Next, we compute g(i, S) with |S| = 2 and i 1; 1S and i S. g(2, {3,4})= min {c23+ g(3,{4}), c24+ g(4,{3})} = 25 g(3, {2,4})= min {c32 + g(2,{4}), c34 + g(4,{2})} = 25 g(4, {2,3})= min {c42+ g(2,{3}), c43 + g(3,{2})} = 23 • Finally, from (2) we obtain: g(1, {2, 3,4})= min {c12 + g(2,{3, 4}), c13 + g(3,{2,4}), c14+ g(4,{2,3})} = min {35, 40, 43} = 35 • An optimal tour of the graph has length 35. • A tour of this length may be constructed if we retain with each g(i, S) the value of j that minimizes the right hand side of (2). Let J(i, S) be this value. • J(1, {2,3,4}) = 2. Thus the tour starts from 1 and goes to 2. The remaining tour may be obtained from g(2, {3,4}). J(2, {3,4}) = 4. Thus the next edge is <2,4>. The remaining tour is for g(4,{3}). J(4, {3}) = 3. The optimal tour is <1, 2, 4, 3, 1>
Complexity of the method • Let N be the number of g(i, S) that have to be computed before (1) may be used to compute g(1, V – {1}). For each value of |S| there are n – 1 choices for i. The number of distinct sets of sets S of size k not including 1 and i is C(k, n -2). n-2 • Hence N = (n - 1) C(k, n - 2) = (n - 1)2n-2 k=0 • An algorithm that proceeds to find an optimal tour by making use of (1) and (2) will require O(n22n) time since the computation of g(i, S) with |S| = k requires k -1 comparisons when solving (2). • It’s better than enumerating all n! different tours to find the best one. • The most serious drawback of this dynamic programming solution is the space needed. The space needed in O(n2n). This is too large even for modest values of n.