0% found this document useful (0 votes)
21 views

Solving TSP by Dynamic Programming

This document describes solving the traveling salesperson problem (TSP) using dynamic programming. It defines s(i,S) as the length of the shortest path starting at vertex i, passing through all vertices in S, and ending at vertex 1. It shows that s(i,S) can be calculated recursively using s values for smaller subsets S. Calculating all s values in this way finds the optimal TSP tour length s(1,V-{1}) in O(n22n) time but requires O(n2n) space, which is infeasible even for moderate n.

Uploaded by

Satish Jadhao
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Solving TSP by Dynamic Programming

This document describes solving the traveling salesperson problem (TSP) using dynamic programming. It defines s(i,S) as the length of the shortest path starting at vertex i, passing through all vertices in S, and ending at vertex 1. It shows that s(i,S) can be calculated recursively using s values for smaller subsets S. Calculating all s values in this way finds the optimal TSP tour length s(1,V-{1}) in O(n22n) time but requires O(n2n) space, which is infeasible even for moderate n.

Uploaded by

Satish Jadhao
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Solving TSP by

Dynamic
Programming

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 s(i, S) be the length of a shortest path starting at
vertex i, going through all vertices in S and terminating at
vertex 1.
s(1, V- {1}) is the length of an optimal salesperson tour.
From the principle of optimality it follows that:

s(1, V {1}) = min {c1k + s(k, V- {1, k})}


2k n

(1)

Generalizing from (1) we obtain


s(i, S) = minjS {cij + s(j, S - {j})}

(2)

Formula (2) may be solved for s(1, V - {1}) if we know


s(k, V - {1,k}) for all choices of k.

The s values may be obtained by using (2).

Clearly, s(i, ) = ci,1, 1 i n. Hence, we may use (2) to


obtain s(i, S) for all S of size 1.
Then we can obtain s(i, S) for S which |S| = 2.
Then, we can obtain s(i, S) for S which |S| = 3, etc.
When |S| < n -1, the value of i and S for which s(i, S) is
needed are such that i 1; 1S 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

s(2, ) = c21 = 5 ; s(3, ) = c31 = 6; s(4, ) = c41 = 8.


Using (2) we obtain
s(2, {3}) = c23 + s(3, ) = 9 + 6 = 15
s(2, {4}) = c24 + s(4, ) = 10 + 8 = 18
s(3, {2}) = c32 + s(2, ) = 13 + 5 = 18
s(3, {4}) = c34 + s(4, ) = 12 + 8 = 20
s(4, {2}) = c42 + s(2, ) = 8 + 5 = 13
s(4, {3}) = c43 + s(3, ) = 9 + 6 =15
4

Next, we compute s(i, S) with |S| = 2 and i 1; 1S and i S.


s(2, {3,4})= min {c23 + s(3,{4}), c24 + s(4,{3})} = 25
s(3, {2,4})= min {c32 + s(2,{4}), c34 + s(4,{2})} = 25
s(4, {2,3})= min {c42 + s(2,{3}), c43 + s(3,{2})} = 23

Finally, from (2) we obtain:


s(1, {2, 3,4})= min {c12 + s(2,{3, 4}), c13 + s(3,{2,4}), c14 + s(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 s(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 s(2, {3,4}). J(2, {3,4}) = 4.
Thus the next edge is <2,4>. The remaining tour is for s(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 s(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
s(i, S) with |S| = k requires k -1 comparisons when solving (2).
Its 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.

You might also like