 # 1 Appendix B: Solving TSP by Dynamic Programming Course: Algorithm Design and Analysis.

## Presentation on theme: "1 Appendix B: Solving TSP by Dynamic Programming Course: Algorithm Design and Analysis."— Presentation transcript:

1 Appendix B: Solving TSP by Dynamic Programming Course: Algorithm Design and Analysis

2 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 for some k  V – {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 {c 1k + g(k, V- {1, k})} (1) 2≤k ≤n

3 Generalizing from (1) we obtain g(i, S) = min j  S {c ij + 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,  ) = c i,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; 1  S and i  S.

4 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,  ) = c 21 = 5 ; g(3,  ) = c 31 = 6; g(4,  ) = c 41 = 8. Using (2) we obtain g(2, {3}) = c 23 + g(3,  ) = 9 + 6 = 15 g(2, {4}) = c 24 + g(4,  ) = 10 + 8 = 18 g(3, {2}) = c 32 + g(2,  ) = 13 + 5 = 18 g(3, {4}) = c 34 + g(4,  ) = 12 + 8 = 20 g(4, {2}) = c 42 + g(2,  ) = 8 + 5 = 13 g(4, {3}) = c 43 + g(3,  ) = 9 + 6 =15

5 Next, we compute g(i, S) with |S| = 2 and i  1; 1  S and i  S. g(2, {3,4})= min {c 23 + g(3,{4}), c 24 + g(4,{3})} = 25 g(3, {2,4})= min {c 32 + g(2,{4}), c 34 + g(4,{2})} = 25 g(4, {2,3})= min {c 42 + g(2,{3}), c 43 + g(3,{2})} = 23 Finally, from (2) we obtain: g(1, {2, 3,4})= min {c 12 + g(2,{3, 4}), c 13 + g(3,{2,4}), c 14 + 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. The remaining tour is for g(4,{3}). J(4, {3}) = 3. The optimal tour is

6 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)2 n-2 k=0 An algorithm that proceeds to find an optimal tour by making use of (1) and (2) will require O(n 2 2 n ) 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(n2 n ). This is too large even for modest values of n.

Download ppt "1 Appendix B: Solving TSP by Dynamic Programming Course: Algorithm Design and Analysis."

Similar presentations