SlideShare a Scribd company logo
Appendix B: Solving TSP by
Dynamic Programming
Course: Algorithm Design and Analysis

1
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})}
2≤k ≤n

2

(1)


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.

3
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
4


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>
5
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.

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(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.

6

More Related Content

PPT
Appendix b 2
PPTX
Traveling salesperson
PPT
PPT
Waldie pd2
PDF
1 fichas refuerzo operaciones combinadas numeros
ODP
Algebra pythagorean powerpoint
PPTX
Green Theorem
PPTX
Euclid algorithm and congruence matrix
Appendix b 2
Traveling salesperson
Waldie pd2
1 fichas refuerzo operaciones combinadas numeros
Algebra pythagorean powerpoint
Green Theorem
Euclid algorithm and congruence matrix

What's hot (17)

PPTX
Radix sort concept
PDF
U03301080122
DOCX
PC Test 2 study guide 2011
DOCX
Integrales
DOCX
Metodo de bairstow
PPT
Dynamic programming
PPTX
Modular arithmetic
PPTX
Alg 2 : multiply ing & factoring
PPT
Curvature (2)
PPT
Tangent and curvature
PDF
Worksheet 3 addition & subtraction
PPTX
Longest Common Subsequence & Matrix Chain Multiplication
DOCX
Class notes precalc
PDF
The Fundamental Solution of an Extension to a Generalized Laplace Equation
PPTX
Pythagorean theorem and distance formula 12
Radix sort concept
U03301080122
PC Test 2 study guide 2011
Integrales
Metodo de bairstow
Dynamic programming
Modular arithmetic
Alg 2 : multiply ing & factoring
Curvature (2)
Tangent and curvature
Worksheet 3 addition & subtraction
Longest Common Subsequence & Matrix Chain Multiplication
Class notes precalc
The Fundamental Solution of an Extension to a Generalized Laplace Equation
Pythagorean theorem and distance formula 12
Ad

Viewers also liked (8)

PPSX
The Wise Way
PPTX
Fahmy partnerships
PPT
Konflik psda dan peluang para pihak
PPS
INFOPROFIL - VAS SERVICE
PPTX
Open Access in Architectural Research
PPTX
Winning the Tour de France, Research Data and Data Stewardship
PPTX
The Landscape of Research Data Management
PPT
CSCW 2008 Closing Plenary
The Wise Way
Fahmy partnerships
Konflik psda dan peluang para pihak
INFOPROFIL - VAS SERVICE
Open Access in Architectural Research
Winning the Tour de France, Research Data and Data Stewardship
The Landscape of Research Data Management
CSCW 2008 Closing Plenary
Ad

Similar to Appendix b 2 (20)

PDF
Kumegawa russia
DOC
Unit 3 daa
PDF
On the 1-2-3-edge weighting and Vertex coloring of complete graph
PDF
DISTANCE TWO LABELING FOR MULTI-STOREY GRAPHS
PDF
Graph Analytics and Complexity Questions and answers
DOC
algorithm Unit 3
PDF
On Cubic Graceful Labeling
ODP
parameterized complexity for graph Motif
PDF
Mathematics
PPTX
Multistage graph unit 4 of algorithm.ppt
PDF
Goldberg-Coxeter construction for 3- or 4-valent plane maps
PPTX
unit3_Dynamic_Progrghaiajawzjabagamming1.pptx
DOCX
mathematics part-2.docx
PDF
Maths05
PDF
Question answers Optical Fiber Communications 4th Edition by Keiser
PDF
E33018021
DOCX
121593320 teorema-stokes
PDF
The International Journal of Engineering and Science (IJES)
Kumegawa russia
Unit 3 daa
On the 1-2-3-edge weighting and Vertex coloring of complete graph
DISTANCE TWO LABELING FOR MULTI-STOREY GRAPHS
Graph Analytics and Complexity Questions and answers
algorithm Unit 3
On Cubic Graceful Labeling
parameterized complexity for graph Motif
Mathematics
Multistage graph unit 4 of algorithm.ppt
Goldberg-Coxeter construction for 3- or 4-valent plane maps
unit3_Dynamic_Progrghaiajawzjabagamming1.pptx
mathematics part-2.docx
Maths05
Question answers Optical Fiber Communications 4th Edition by Keiser
E33018021
121593320 teorema-stokes
The International Journal of Engineering and Science (IJES)

More from Loc Tran (13)

DOCX
đạI học quốc gia thành phố hồ chí minh
PPT
Chap8 new
PPT
Chap7 new
PPT
Chap6 new
PPT
Chap5 new
PPT
Chap4 new
PPT
Chap4 new (tran dai's conflicted copy 2013 04-02)
PPT
Chap3 new
PPT
Chap2 new
PPT
Chap2 new (tran dai's conflicted copy 2013 04-02)
PPT
Chap1 new (tran dai's conflicted copy 2013 04-02)
PPT
Appendix a 2
PPT
Chap8 part ii
đạI học quốc gia thành phố hồ chí minh
Chap8 new
Chap7 new
Chap6 new
Chap5 new
Chap4 new
Chap4 new (tran dai's conflicted copy 2013 04-02)
Chap3 new
Chap2 new
Chap2 new (tran dai's conflicted copy 2013 04-02)
Chap1 new (tran dai's conflicted copy 2013 04-02)
Appendix a 2
Chap8 part ii

Recently uploaded (20)

PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Cell Types and Its function , kingdom of life
PDF
Classroom Observation Tools for Teachers
PPTX
master seminar digital applications in india
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Computing-Curriculum for Schools in Ghana
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Lesson notes of climatology university.
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
O5-L3 Freight Transport Ops (International) V1.pdf
Pharma ospi slides which help in ospi learning
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Cell Types and Its function , kingdom of life
Classroom Observation Tools for Teachers
master seminar digital applications in india
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Computing-Curriculum for Schools in Ghana
Microbial disease of the cardiovascular and lymphatic systems
2.FourierTransform-ShortQuestionswithAnswers.pdf
RMMM.pdf make it easy to upload and study
Complications of Minimal Access Surgery at WLH
Lesson notes of climatology university.
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
O7-L3 Supply Chain Operations - ICLT Program
TR - Agricultural Crops Production NC III.pdf
Renaissance Architecture: A Journey from Faith to Humanism

Appendix b 2

  • 1. Appendix B: Solving TSP by Dynamic Programming Course: Algorithm Design and Analysis 1
  • 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})} 2≤k ≤n 2 (1)
  • 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. 3
  • 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 4
  • 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> 5
  • 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(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. 6
  • 7. 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. 6