SlideShare a Scribd company logo
1 of 7
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 <1,k> 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 {c1k + g(k, V- {1, k})} (1)
2≤k ≤n
3
 Generalizing from (1) we obtain
g(i, S) = minj∈S {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; 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, ∅) = 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
5
 Next, we compute g(i, S) with |S| = 2 and i ≠ 1; 1∉S 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>
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)2n-2
k=0
 An algorithm that proceeds to find an optimal tour by making use
of (1) and (2) will require O(n2
2n
) 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.
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)2n-2
k=0
 An algorithm that proceeds to find an optimal tour by making use
of (1) and (2) will require O(n2
2n
) 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.

More Related Content

What's hot

Core 3 assignement 5 10 11 (trigonometry 2 and differentiation 1)
Core 3 assignement 5 10 11 (trigonometry 2 and differentiation 1)Core 3 assignement 5 10 11 (trigonometry 2 and differentiation 1)
Core 3 assignement 5 10 11 (trigonometry 2 and differentiation 1)Sonia Naqvi
 
Radix sort concept
Radix sort conceptRadix sort concept
Radix sort conceptZeeshan Ali
 
Integrales
IntegralesIntegrales
Integralescesarcsl
 
Tangent and curvature
Tangent and curvatureTangent and curvature
Tangent and curvatureAnik Syed
 
PC Test 2 study guide 2011
PC Test 2 study guide 2011PC Test 2 study guide 2011
PC Test 2 study guide 2011vhiggins1
 
Euclid algorithm and congruence matrix
Euclid algorithm and congruence matrixEuclid algorithm and congruence matrix
Euclid algorithm and congruence matrixJanani S
 
Pythagorean theorem and distance formula
Pythagorean theorem and distance formulaPythagorean theorem and distance formula
Pythagorean theorem and distance formula42226823
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programmingSwetha S
 
Metodo de bairstow
Metodo de bairstowMetodo de bairstow
Metodo de bairstowJuan Tovare
 
Pythagorean theorem and distance formula
Pythagorean theorem and distance formulaPythagorean theorem and distance formula
Pythagorean theorem and distance formula41178582
 
Pythagorean theorem and distance formula
Pythagorean theorem and distance formulaPythagorean theorem and distance formula
Pythagorean theorem and distance formula41178582
 
Alg 2 : multiply ing & factoring
Alg 2 : multiply ing & factoringAlg 2 : multiply ing & factoring
Alg 2 : multiply ing & factoringpaksukur
 

What's hot (18)

Core 3 assignement 5 10 11 (trigonometry 2 and differentiation 1)
Core 3 assignement 5 10 11 (trigonometry 2 and differentiation 1)Core 3 assignement 5 10 11 (trigonometry 2 and differentiation 1)
Core 3 assignement 5 10 11 (trigonometry 2 and differentiation 1)
 
Radix sort concept
Radix sort conceptRadix sort concept
Radix sort concept
 
Integrales
IntegralesIntegrales
Integrales
 
Tangent and curvature
Tangent and curvatureTangent and curvature
Tangent and curvature
 
PC Test 2 study guide 2011
PC Test 2 study guide 2011PC Test 2 study guide 2011
PC Test 2 study guide 2011
 
Curvature (2)
Curvature (2)Curvature (2)
Curvature (2)
 
U03301080122
U03301080122U03301080122
U03301080122
 
Euclid algorithm and congruence matrix
Euclid algorithm and congruence matrixEuclid algorithm and congruence matrix
Euclid algorithm and congruence matrix
 
Introduction to MATLAB
Introduction to MATLAB Introduction to MATLAB
Introduction to MATLAB
 
Pythagorean theorem and distance formula
Pythagorean theorem and distance formulaPythagorean theorem and distance formula
Pythagorean theorem and distance formula
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Class notes precalc
Class notes precalcClass notes precalc
Class notes precalc
 
Metodo de bairstow
Metodo de bairstowMetodo de bairstow
Metodo de bairstow
 
Pythagorean theorem and distance formula
Pythagorean theorem and distance formulaPythagorean theorem and distance formula
Pythagorean theorem and distance formula
 
Pythagorean theorem and distance formula
Pythagorean theorem and distance formulaPythagorean theorem and distance formula
Pythagorean theorem and distance formula
 
Alg 2 : multiply ing & factoring
Alg 2 : multiply ing & factoringAlg 2 : multiply ing & factoring
Alg 2 : multiply ing & factoring
 
2-rankings of Graphs
2-rankings of Graphs2-rankings of Graphs
2-rankings of Graphs
 
Imaginary Number
Imaginary NumberImaginary Number
Imaginary Number
 

Viewers also liked

Travelling Salesman Problem
Travelling Salesman ProblemTravelling Salesman Problem
Travelling Salesman ProblemDaniel Raditya
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic ProgrammingSahil Kumar
 
The Traveling Salesman Problem
The Traveling Salesman ProblemThe Traveling Salesman Problem
The Traveling Salesman ProblemMaryam Alipour
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programingrupali_2bonde
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programmingShakil Ahmed
 
Solving travelling salesman problem using firefly algorithm
Solving travelling salesman problem using firefly algorithmSolving travelling salesman problem using firefly algorithm
Solving travelling salesman problem using firefly algorithmishmecse13
 
The Travelling Salesman Problem
The Travelling Salesman ProblemThe Travelling Salesman Problem
The Travelling Salesman Problemguest3d82c4
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programmingOye Tu
 

Viewers also liked (9)

Travelling Salesman Problem
Travelling Salesman ProblemTravelling Salesman Problem
Travelling Salesman Problem
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
The Traveling Salesman Problem
The Traveling Salesman ProblemThe Traveling Salesman Problem
The Traveling Salesman Problem
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Solving travelling salesman problem using firefly algorithm
Solving travelling salesman problem using firefly algorithmSolving travelling salesman problem using firefly algorithm
Solving travelling salesman problem using firefly algorithm
 
Dynamic pgmming
Dynamic pgmmingDynamic pgmming
Dynamic pgmming
 
The Travelling Salesman Problem
The Travelling Salesman ProblemThe Travelling Salesman Problem
The Travelling Salesman Problem
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
 

Similar to Appendix b 2

On the 1-2-3-edge weighting and Vertex coloring of complete graph
On the 1-2-3-edge weighting and Vertex coloring of complete graphOn the 1-2-3-edge weighting and Vertex coloring of complete graph
On the 1-2-3-edge weighting and Vertex coloring of complete graphijcsa
 
DISTANCE TWO LABELING FOR MULTI-STOREY GRAPHS
DISTANCE TWO LABELING FOR MULTI-STOREY GRAPHSDISTANCE TWO LABELING FOR MULTI-STOREY GRAPHS
DISTANCE TWO LABELING FOR MULTI-STOREY GRAPHSgraphhoc
 
Goldberg-Coxeter construction for 3- or 4-valent plane maps
Goldberg-Coxeter construction for 3- or 4-valent plane mapsGoldberg-Coxeter construction for 3- or 4-valent plane maps
Goldberg-Coxeter construction for 3- or 4-valent plane mapsMathieu Dutour Sikiric
 
parameterized complexity for graph Motif
parameterized complexity for graph Motifparameterized complexity for graph Motif
parameterized complexity for graph MotifAMR koura
 
Graph Analytics and Complexity Questions and answers
Graph Analytics and Complexity Questions and answersGraph Analytics and Complexity Questions and answers
Graph Analytics and Complexity Questions and answersAnimesh Chaturvedi
 
On the Odd Gracefulness of Cyclic Snakes With Pendant Edges
On the Odd Gracefulness of Cyclic Snakes With Pendant EdgesOn the Odd Gracefulness of Cyclic Snakes With Pendant Edges
On the Odd Gracefulness of Cyclic Snakes With Pendant EdgesGiselleginaGloria
 
The International Journal of Engineering and Science (IJES)
The International Journal of Engineering and Science (IJES)The International Journal of Engineering and Science (IJES)
The International Journal of Engineering and Science (IJES)theijes
 
New Classes of Odd Graceful Graphs - M. E. Abdel-Aal
New Classes of Odd Graceful Graphs - M. E. Abdel-AalNew Classes of Odd Graceful Graphs - M. E. Abdel-Aal
New Classes of Odd Graceful Graphs - M. E. Abdel-AalGiselleginaGloria
 
Question answers Optical Fiber Communications 4th Edition by Keiser
Question answers Optical Fiber Communications 4th Edition by KeiserQuestion answers Optical Fiber Communications 4th Edition by Keiser
Question answers Optical Fiber Communications 4th Edition by Keiserblackdance1
 
121593320 teorema-stokes
121593320 teorema-stokes121593320 teorema-stokes
121593320 teorema-stokessaidattamimi1
 
Magicness in Extended Duplicate Graphs
Magicness  in Extended Duplicate GraphsMagicness  in Extended Duplicate Graphs
Magicness in Extended Duplicate GraphsIRJET Journal
 

Similar to Appendix b 2 (20)

Kumegawa russia
Kumegawa russiaKumegawa russia
Kumegawa russia
 
Unit 3 daa
Unit 3 daaUnit 3 daa
Unit 3 daa
 
On the 1-2-3-edge weighting and Vertex coloring of complete graph
On the 1-2-3-edge weighting and Vertex coloring of complete graphOn the 1-2-3-edge weighting and Vertex coloring of complete graph
On the 1-2-3-edge weighting and Vertex coloring of complete graph
 
DISTANCE TWO LABELING FOR MULTI-STOREY GRAPHS
DISTANCE TWO LABELING FOR MULTI-STOREY GRAPHSDISTANCE TWO LABELING FOR MULTI-STOREY GRAPHS
DISTANCE TWO LABELING FOR MULTI-STOREY GRAPHS
 
algorithm Unit 3
algorithm Unit 3algorithm Unit 3
algorithm Unit 3
 
Goldberg-Coxeter construction for 3- or 4-valent plane maps
Goldberg-Coxeter construction for 3- or 4-valent plane mapsGoldberg-Coxeter construction for 3- or 4-valent plane maps
Goldberg-Coxeter construction for 3- or 4-valent plane maps
 
On Cubic Graceful Labeling
On Cubic Graceful LabelingOn Cubic Graceful Labeling
On Cubic Graceful Labeling
 
parameterized complexity for graph Motif
parameterized complexity for graph Motifparameterized complexity for graph Motif
parameterized complexity for graph Motif
 
Graph Analytics and Complexity Questions and answers
Graph Analytics and Complexity Questions and answersGraph Analytics and Complexity Questions and answers
Graph Analytics and Complexity Questions and answers
 
balakrishnan2004
balakrishnan2004balakrishnan2004
balakrishnan2004
 
On the Odd Gracefulness of Cyclic Snakes With Pendant Edges
On the Odd Gracefulness of Cyclic Snakes With Pendant EdgesOn the Odd Gracefulness of Cyclic Snakes With Pendant Edges
On the Odd Gracefulness of Cyclic Snakes With Pendant Edges
 
Mathematics
MathematicsMathematics
Mathematics
 
Mathematics
MathematicsMathematics
Mathematics
 
Maths05
Maths05Maths05
Maths05
 
The International Journal of Engineering and Science (IJES)
The International Journal of Engineering and Science (IJES)The International Journal of Engineering and Science (IJES)
The International Journal of Engineering and Science (IJES)
 
New Classes of Odd Graceful Graphs - M. E. Abdel-Aal
New Classes of Odd Graceful Graphs - M. E. Abdel-AalNew Classes of Odd Graceful Graphs - M. E. Abdel-Aal
New Classes of Odd Graceful Graphs - M. E. Abdel-Aal
 
1452 86301000013 m
1452 86301000013 m1452 86301000013 m
1452 86301000013 m
 
Question answers Optical Fiber Communications 4th Edition by Keiser
Question answers Optical Fiber Communications 4th Edition by KeiserQuestion answers Optical Fiber Communications 4th Edition by Keiser
Question answers Optical Fiber Communications 4th Edition by Keiser
 
121593320 teorema-stokes
121593320 teorema-stokes121593320 teorema-stokes
121593320 teorema-stokes
 
Magicness in Extended Duplicate Graphs
Magicness  in Extended Duplicate GraphsMagicness  in Extended Duplicate Graphs
Magicness in Extended Duplicate Graphs
 

More from Nv Thejaswini (11)

Mea notes
Mea notesMea notes
Mea notes
 
Chapter9 4
Chapter9 4Chapter9 4
Chapter9 4
 
Huffman coding01
Huffman coding01Huffman coding01
Huffman coding01
 
Lecture11
Lecture11Lecture11
Lecture11
 
Ch8
Ch8Ch8
Ch8
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Unit 5 jwfiles
Unit 5 jwfilesUnit 5 jwfiles
Unit 5 jwfiles
 
Unit 4 jwfiles
Unit 4 jwfilesUnit 4 jwfiles
Unit 4 jwfiles
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
Ch8 of OS
Ch8 of OSCh8 of OS
Ch8 of OS
 
Presentation solar
Presentation solarPresentation solar
Presentation solar
 

Appendix b 2

  • 1. 1 Appendix B: Solving TSP by Dynamic Programming Course: Algorithm Design and Analysis
  • 2. 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 <1,k> 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 {c1k + g(k, V- {1, k})} (1) 2≤k ≤n
  • 3. 3  Generalizing from (1) we obtain g(i, S) = minj∈S {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; 1∉S and i ∉S.
  • 4. 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, ∅) = 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
  • 5. 5  Next, we compute g(i, S) with |S| = 2 and i ≠ 1; 1∉S 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>
  • 6. 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)2n-2 k=0  An algorithm that proceeds to find an optimal tour by making use of (1) and (2) will require O(n2 2n ) 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.
  • 7. 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)2n-2 k=0  An algorithm that proceeds to find an optimal tour by making use of (1) and (2) will require O(n2 2n ) 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.