0% found this document useful (0 votes)
85 views36 pages

Dynamic Progr.

The document discusses the algorithm design method of dynamic programming and its application to solving problems like all-pair shortest paths, optimal binary search trees, knapsack problem, and travelling salesperson problem. It specifically describes how dynamic programming can be used to solve multistage graph problems using either the forward or backward method to find the minimum cost path from source to destination.

Uploaded by

saliniprabha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views36 pages

Dynamic Progr.

The document discusses the algorithm design method of dynamic programming and its application to solving problems like all-pair shortest paths, optimal binary search trees, knapsack problem, and travelling salesperson problem. It specifically describes how dynamic programming can be used to solve multistage graph problems using either the forward or backward method to find the minimum cost path from source to destination.

Uploaded by

saliniprabha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Design and Analysis of

Algorithms

Unit - III
Periyar Govt. Arts College
Cuddalore
Dr. R. Bhuvaneswari
Assistant Professor
Department of Computer Science
Periyar Govt. Arts College, Cuddalore.

1 Dr. R. Bhuvaneswari
Dynamic Programming

Syllabus
UNIT-III
Dynamic Programming: General Method – Multistage Graphs –
All-Pair shortest paths –Optimal binary search trees – 0/1 Knapsack
– Travelling salesperson problem.

Text Book:
Ellis Horowitz, Sartaj Sahni and Sanguthevar Rajasekaran,
Computer Algorithms C++, Second Edition, Universities Press,
2007. (For Units II to V)

Periyar Govt. Arts College


2 Dr. R. Bhuvaneswari Cuddalore
Dynamic Programming

General Method:
• It is an algorithm design method that can be used when the solution to
a problem can be viewed as a sequence of decisions.
• It obtains the solution using “Principle of Optimality”.
• It states that “ In an optimal sequence of decisions or choices, each
subsequence must also be optimal”, ie., whatever the initial state and
decision are, the remaining decisions must constitute an optimal
decision sequence.
• The difference between the greedy method and dynamic programming
is that in the greedy method only one decision sequence is ever
generated.
• In dynamic programming, many decision sequences may be generated.
• Sequences containing suboptimal subsequences cannot be optimal and
so will not be generated.
Periyar Govt. Arts College
3 Dr. R. Bhuvaneswari Cuddalore
Multistage Graphs

• A multistage graph G = (V, E) is a directed graph in which the vertices


are partitioned into k  2 disjoint sets Vi, 1  i  k.
• If u, v is an edge in E, then u  Vi and v  Vi+1.
• The sets V1 and Vk are such that |V1| = |Vk| = 1.
• The vertex s is the source and the t the sink (destination).
• The multistage graph problem is to find a minimum cost path from s
to t.
• The cost of s to t is the sum of the cost of the edges on the path.
• The multistage graph problem can be solved in 2 ways.
Forward method
Backward method

Periyar Govt. Arts College


4 Dr. R. Bhuvaneswari Cuddalore
Multistage Graphs

Forward Approach
• In the forward approach, the cost of each and every node is found
starting from the k stage to the 1st stage.
• The minimum cost path from the source to destination is found ie.,
stage 1 to stage k.
• For forward approach,
Cost(i ,j) = min{c(j, l) + cost(i+1, l)}
lVi+1
j, lE
where i is the level number.
• Time complexity: O(V+E)

Periyar Govt. Arts College


5 Dr. R. Bhuvaneswari Cuddalore
Multistage Graphs

V1 V2 V3 V4 V5

Periyar Govt. Arts College


6 Dr. R. Bhuvaneswari Cuddalore
Minimum cost estimation – Forward Approach

Cost(i ,j) = min{c(j, l) + cost(i+1, l)}


lVi+1
j, lE
Min. Cost
cost(5,12) 0 0
cost(4,9) min{c(9,12)+cost(5,12)} = {4 + 0} 4
cost(4,10) min{c(10,12)+cost(5,12)} = {2 + 0} 2
cost(4,11) min{c(11,12)+cost(5,12)} = {5+ 0} 5
cost(3,6) min{c(6,9)+cost(4,9), c(6,10)+cost(4,10)} 7
= min{6+ 4, 5+2}
cost(3,7) min{c(7,9)+cost(4,9), c(7,10)+cost(4,10)} 5
= min{4+4, 3+2}
cost(3,8) min{c(8,10)+cost(4,10), c(8,11)+cost(4,11)} 7
= min{5+2, 6+5}

Periyar Govt. Arts College


7 Dr. R. Bhuvaneswari Cuddalore
Minimum cost estimation – Forward Approach

Min. Cost
cost(2,2) min{c(2,6)+cost(3,6), c(2,7)+cost(3,7), 7
c(2,8)+cost(3,8)}
= min{4+7, 2+5, 1+7}
cost(2,3) min{c(3,6)+cost(3,6), c(3,7)+cost(3,7)} 9
= min{2+7, 7+5}
cost(2,4) min{c(4,8)+cost(3,8)} 18
= min{11+7}
cost(2,5) min{c(5,7)+cost(3,7), c(5,8)+cost(3,8)} 15
= min{11+5, 8+7}
cost(1,1) min{c(1,2)+cost(2,2), c(1,3)+cost(2,3), 16
c(1,4)+cost(2,4), c(1,5)+cost(2,5)} =
min{9+7, 7+9, 3+18, 2+15}

1  2  7  10  12
1  3  6  10  12 Periyar Govt. Arts College
8 Dr. R. Bhuvaneswari Cuddalore
Multistage Graphs

Algorithm FGraph(G, k, n, p)
//p[1:k] is a minimum cost path
{
cost[n] = 0.0;
for j = n-1 to 1 step -1 do
{
Let r be a vertex such that j, r is an edge of G and c[j, r]+cost[r] is
minimum;
cost[j] = c[j, r] + cost[r];
d[j] = r;
}
p[1] = 1; p[k] = n;
for j = 2 to k-1 do
p[j] = d[p[j-1]];
}
Periyar Govt. Arts College
9 Dr. R. Bhuvaneswari Cuddalore
Multistage Graphs

Backward Approach
• In the backward approach, the cost of each and every node is found
starting from the 1st stage to the kth stage.
• The minimum cost path from the source to destination is found ie., stage
k to stage 1.
• For backward approach,
bcost(i, j) = min{bcost(i-1, l) + c(l, j)}
lVi-1
l, jE
where i is the level number.

Periyar Govt. Arts College


10 Dr. R. Bhuvaneswari Cuddalore
Multistage Graphs

V1 V2 V3 V4 V5

Periyar Govt. Arts College


11 Dr. R. Bhuvaneswari Cuddalore
Minimum cost estimation – Backward Approach

bcost(i, j) = min{bcost(i-1, l) + c(l, j)}


lVi-1
l, jE
Min. Cost
bcost(1,1) 0 0
bcost(2,2) min{bcost(1,1)+c(1,2)} =min{0+9} 9
bcost(2,3) min{bcost(1,1)+c(1,3)} =min{0+7} 7
bcost(2,4) min{bcost(1,1)+c(1,4)} =min{0+3} 3
bcost(2,5) min{bcost(1,1)+c(1,5)} =min{0+2} 2
bcost(3,6) min{bcost(2,2)+c(2,6),bcost(2,3)+c(3,6)} 9
= min{9+4,7+2}
bcost(3,7) min{bcost(2,2)+c(2,7),bcost(2,3)+c(3,7), 11
bcost(2,5)+c(5,7)}
= min{9+2,7+7,2+11}

Periyar Govt. Arts College


12 Dr. R. Bhuvaneswari Cuddalore
Minimum cost estimation – Backward Approach

Min. Cost
bcost(3,8) min{bcost(2,2)+c(2,8),bcost(2,4)+c(4,8), 10
bcost(2,5)+c(5,8)}
= min{9+1,3+11,2+8}
bcost(4,9) min{bcost(3,6)+c(6,9),Bcost(3,7)+c(7,9)} 15
= min{9+6,11+4}
bcost(4,10) min{bcost(3,6)+c(6,10),bcost(3,7)+c(7,10), 14
bcost(3,8)+c(8,10)}
= min{9+5,11+3,10+5}
bcost(4,11) min{bcost(3,8)+c(8,11)} = min{10+6} 16
Bcost(5,12) min{bcost(4,9)+c(9,12),bcost(4,10)+c(10,12), 16
bcost(4,11)+c(11,12)}
= min{15+4,14+2,16+5}

12  10  7  2  1 1 2  7  10  12
12  10  6  3  1 1 3  6  10  12 Periyar Govt. Arts College
13 Dr. R. Bhuvaneswari Cuddalore
Multistage Graphs

Algorithm BGraph(G, k, n, p)
{
bcost[1] = 0.0;
for j = 2 to n do
{
Let r be such that r, j is an edge of G and bcost[r] + c[r, j] is minimum;
bcost[j] = bcost[r] + c[r, j];
d[j] = r;
}
p[1] = 1; p[k] = n;
for j = k-1 to 2 step -1 do
p[j] = d[p[j+1]];
}

Periyar Govt. Arts College


14 Dr. R. Bhuvaneswari Cuddalore
All pair shortest paths

• All pairs shortest path problem is the determination of the shortest graph
distances between every pair of vertices in a given directed graph G.
• That is, for every pair of vertices (i, j), we are to find a shortest path from
i to j as well as from j to i. These two paths are the same when G is
undirected.
• Let G = (V, E) be a directed graph with n vertices.
• Let cost be a cost adjacency matrix for G such that cost(i, i) = 0, 1  i  n.
• Cost(i, j) is the length of edge i, j if i, j  E(G) and cost(i, j) =  if i  j
and i, j  E(G).
• All pair shortest path problem is to determine a matrix A such that A(i, j)
is the length of a shortest path from i to j.
• Since each application of this procedure requires O(n2) time, the matrix A
can be obtained in O(n3) time.
Periyar Govt. Arts College
15 Dr. R. Bhuvaneswari Cuddalore
All pair shortest paths

• The shortest i to j path in G, i ≠ j originates at vertex i and goes through


some intermediate vertices and terminates at vertex j.
• If k is an intermediate vertex on this shortest path, then the subpaths from
i to k and from k to j must be shortest paths from i to k and k to j,
respectively.
• Otherwise, the i to j path is not of minimum length.
• So, the principle of optimality holds.
• Let Ak(i, j) represent the length of a shortest path from i to j going through
no vertex of index greater than k, we obtain:

𝑨 𝒊, 𝒋 = 𝐦𝐢𝐧 {𝑨𝒌−𝟏 𝒊, 𝒌 + 𝑨𝒌−𝟏 𝒌, 𝒋 , 𝒄𝒐𝒔𝒕(𝒊, 𝒋)}


𝟏≤𝒌≤𝒏

• Time complexity of this algorithm is O(n3)

Periyar Govt. Arts College


16 Dr. R. Bhuvaneswari Cuddalore
All pair shortest paths
6
Algorithm AllPaths(cost, A, n)
{ 4
for i =1 to n do 1 2
{
11
for j = 1 to n do 2

A[i, j] = cost[i, j]; 3


3
}
for k = 1 to n do
{ 1->3 = 11
for j = 1 to n do 1->2->3 = 6
{ 2->1 = 6
for j = 1 to n do 2->3->1 = 5
A[i, j] = min{A[i, j], A[i, k]+A[k, j]};
}
}
} Periyar Govt. Arts College
17 Dr. R. Bhuvaneswari Cuddalore
All pair shortest paths

Solve the problem for k = 1, 2, 3


A0 1 2 3
Cost adjacency matrix
1 0 4 11
2 6 0 2
3 3  0

Solving the Solving the Solving the


equation for, k = 1 equation for, k = 2 equation for, k = 3
A1 1 2 3 A2 1 2 3 A3 1 2 3
1 0 4 11 1 0 4 6 1 0 4 6
2 6 0 2 2 6 0 2 2 5 0 2
3 3 7 0 3 3 7 0 3 3 7 0

Periyar Govt. Arts College


18 Dr. R. Bhuvaneswari Cuddalore
Optimal Binary Search Trees

• A Binary Search Tree (BST) is a tree in which all the nodes follow the
below mentioned properties :
 The value of the key of the left sub-tree is less than the value of its
parent (root) node's key.
 The value of the key of the right sub-tree is greater than or equal to
the value of its parent (root) node's key.

• An optimal binary search tree (OBST) is a binary search tree which


provides the smallest possible search time.
Periyar Govt. Arts College
19 Dr. R. Bhuvaneswari Cuddalore
Optimal Binary Search Trees

• The problem is to construct an optimal binary search tree (in terms of search
time) for a set of integer keys, given the frequencies with which each key
will be accessed.
Keys 20 30 40
Frequencies 2 1 6
• As there are three different keys, we can get a total of 5 various BSTs by
changing order of the keys. ie., 2nCn/(n+1) number of tree can be generated.

Periyar Govt. Arts College


20 Dr. R. Bhuvaneswari Cuddalore
Optimal Binary Search Trees

• The cost is computed by multiplying the each node’s frequency with the
level of tree( Here we are assuming that the tree starts from level 1) and
then add them to compute the overall cost of BST.
• The above example, the 4th BST has the least cost among all, so it is the
Optimal Binary Search Tree for the given data.
• If the number of nodes are less we can find optimal BST by checking all
possible arrangements, but if the nodes are greater than 3 like 4,5,6….. then
respectively 14,42,132….. , different BST’s are possible so by checking all
arrangements to find Optimal Cost may lead to extra overhead.
• So Dynamic Programming approach can be used to find the Optimal
Binary Search Tree.
• The search time can be improved in Optimal Cost Binary Search Tree,
placing the most frequently used data in the root and closer to the root
element, while placing the least frequently used data near leaves and in
leaves.
Periyar Govt. Arts College
21 Dr. R. Bhuvaneswari Cuddalore
Optimal Binary Search Trees

• It has n keys (representation


k1,k2,…,kn) in sorted order (so that
k1<k2<…<kn), and we build a binary
search tree from these keys.
• For each ki ,we have a probability pi
that a search will be for ki.
• In contrast, some searches may be for
values not in ki, and so we also have
n+1 “dummy keys” d0,d1,…,dn
representating not in ki.
• In particular, d0 represents all values
less than k1, and dn represents all
values greater than kn, and for
i=1,2,…,n-1, the dummy key di
represents all values between ki and
ki+1.
Periyar Govt. Arts College
22 Dr. R. Bhuvaneswari Cuddalore
Optimal Binary Search Trees

• The dummy keys are leaves (external nodes), and the data keys mean
internal nodes.
• For n internal nodes n+1 external nodes will be present.
• The terminal node that is the left successor of k1 can be interpreted as
representing all key values that are not stored and are less than k1.
Similarly, the terminal node that is the right successor of kn, represents all
key values not stored in the tree that are greater than kn.
Using Dynamic Approach
𝒄 𝒊, 𝒋 = 𝐦𝐢𝐧 𝒄 𝒊, 𝒌 − 𝟏 + 𝒄 𝒌, 𝒋 + 𝒘[𝒊, 𝒋]
𝟏 <𝒌 ≤𝒋

𝐰 𝐢, 𝐣 = 𝐰 𝐢, 𝐣 − 𝟏 + 𝐩𝐣 + 𝐪𝐣

Let pi be the probability of successful search and qj be the probability of


unsuccessful search.

Periyar Govt. Arts College


23 Dr. R. Bhuvaneswari Cuddalore
Optimal Binary Search Trees

Example:
Keys = {10, 20, 30, 0} j-i =0 w00 =2 w11=3 w22=1 w33 =1 w44=1
p(1:4) = {3, 3, 1, 1} c00 =0 c11 =0 c22 =0 c33 =0 c44 =0
q(0:4) = {2, 3,1, 1, 1} r00 =0 r11 =0 r22 =0 r33=0 r44 =0
j-i=1 w01=8 w12 =7 w23 =3 w34 =3
c01 =8 c12 =7 c23 =3 c34 =3
Initially, w(i, i) = q(i); c(i, i) = 0; r01 =1 r12 =2 r23 =3 r34=4
r(i, i) = 0 j-i=2 w02=12 w13 =9 w24=5
W[0,1] = w[0,0]+p1+q1=2+6=8 c02=19 c13=12 c24 =8
r02 =1 r13=2 r24 =3
W[1,2] = w[1,1]+p2+q2=3+3+1=7
j-i=3 w03=14 w14=11
W[2,3] = w[2,2]+p3+q3=1+1+1=3 c03=25 c14=19
W[3,4] = w[3,3]+p4+q4 =1+1+1=3 r03 =2 r14 =2
W[0,2] = w[0,1]+p2+q2 j-i=4 w04 =16
W[1,3] = w[1,2]+p3+q3 c04 =32
r04 =2
W[2,4] = w[2,3]+p4+q4
W[0,3] = w[0,2]+p3+q3
W[1,4] = w[1,3]+p4+q4
W[0,4] = w[0,3]+p4+q4 Periyar Govt. Arts College
24 Dr. R. Bhuvaneswari Cuddalore
Optimal Binary Search Trees

c[0,1] = min{c[0,0]+c[1,1]}+w[0,1] = 0+0+8=8


0<k1
c[1,2] = min{c[1,1]+c[2,2]}+w[1,2]=0+0+7=7
1<k2
c[2,3] =min{c[2,2]+c[3,3]}+w[2,3]=0+0+3=3
2<k3
c[3,4]=min{c[3,3]+c[4,4]}+w[3,4]=0+0+3=3
3<k4
c[0,2]=min{c[0,0]+c[1,2], c[0,1]+c[2,2]}+w[0,2]
0<k2
=min{0+7,8+0}+12=19
c[1,3]=min{c[1,1]+c[2,3],c[1,2]+c[3,3]}+w[1,3]
1<k3
=min{0+3,7+0}+9 = 12
c[2,4]=min{c[2,2]+c[3,4],c[2,3]+c[4,4]}+w[2,4]
2<k4
=min{0+3,3+0}+5=8 Periyar Govt. Arts College
25 Dr. R. Bhuvaneswari Cuddalore
Optimal Binary Search Trees

c[0,3]=min{c[0,0]+c[1,3], c[0,1]+c[2,3], c[0,2]+c[3,3]}+w[0,3]


0<k3
=min{0+12,8+3,19+0}+14 = 25
c[1,4] = min{c[1,1]+c[2,4], c[1,2]+c[3,4], c[1,3]+c[4,4]}+w[1,4]
1<k4
= min{0+8,7+3,12+0}+11 = 19
c[0,4] = min{c[0,0]+c[1,4], c[0,1]+c[2,4], c[0,2]+c[3,4], c[0,3]+c[4,4]}+w[0,4]
0<k4
=min{0+19,8+8,19+3,25+0}+16 = 32

The Optimal Binary Search Tree:

The algorithm requires O (n3) time Periyar Govt. Arts College


26 Dr. R. Bhuvaneswari Cuddalore
0/1 Knapsack Problem

• Given n objects and a knapsack or bag.


• wi → weight of object i.
• m → knapsack capacity.
• As the name suggests, objects are indivisible in this method. No
fractional objects can be taken. An object can either be taken completely
or left completely.
• Objective is to fill the knapsack that maximizes the total profit earned.
• Problem can be stated as
maximize pi xi
1 ≤ i ≤n

subject to wi x i ≤ m
1≤ i ≤n

xi = 0 or 1, 1 ≤ i ≤ n
Periyar Govt. Arts College
27 Dr. R. Bhuvaneswari Cuddalore
0/1 Knapsack Problem

0/1 knapsack problem is solved using dynamic programming in the


following steps-
• Draw a table say ‘V’ with (n+1) number of rows and (w+1) number of
columns.
• Fill all the boxes of 0th row and 0th column with zeroes.
• Start filling the table row wise top to bottom from left to right.
• Use the following formula:
V[i ,W] = max{V[i-1 ,W] , V[i-1, W – w[i]] + p[i]}
• value of the last box represents the maximum possible value that can be
put into the knapsack.

Periyar Govt. Arts College


28 Dr. R. Bhuvaneswari Cuddalore
0/1 Knapsack Problem

• To identify the items that must be put into the knapsack to obtain the
maximum profit,
 Consider the last column of the table.
 Start scanning the entries from bottom to top.
 On encountering an entry whose value is not same as the value
stored in the entry immediately above it, mark the row label of that
entry.
 After all the entries are scanned, the marked labels represent the
items that must be put into the knapsack.
• O(nw) time is taken to solve 0/1 knapsack problem using dynamic
programming.

Periyar Govt. Arts College


29 Dr. R. Bhuvaneswari Cuddalore
0/1 Knapsack Problem

Pi = {1, 2, 5, 6}
wi = (2, 3, 4, 5}
m = 8, n = 4

V[i ,W] = max{V[i-1 ,W] , V[i-1, W – w[i]] + p[i]}


V[1,1] = max{V[0,1], V[0,1-2]+1} = max{0, -} = 0
V[1,2] = max{V[0,2], V[0,2-2]+1} = max{0, 0+1} = 1
V[1,3] = max{V[0,3], V[0,3-2]+1} = max{0,0+1} = 1
V[1,4] = max{V[0,4], V[0,4-2]+1} = max{0, 0+1} = 1
V[1,5] = max{V[0,5], V[0,5-2]+1} = max{0, 0+1} = 1
V[1,6] = max{V[0,6], V[0,6-2]+1} = max{0, 0+1} = 1
V[1,7] = max{V[0,7], V[0,7-2]+1} = max{0, 0+1} = 1
V[1,8] = max{V[0,8], V[0,8-2]+1} = max{0, 0+1} = 1
Periyar Govt. Arts College
30 Dr. R. Bhuvaneswari Cuddalore
0/1 Knapsack Problem

V[2,1] = max{V[1,1],V[1,1-3]+2} = max{0, -} = 0


V[2,2] = max{V[1,2],V[1,2-3]+2} = max{1, -} = 1
V[2,3] = max{V[1,3],V[1,3-3]+2} = max{1, 0+2} = 2
V[2,4] = max{V[1,4],V[1,4-3]+2} = max{1, 0+2} = 2
V[2,5] = max{V[1,5],V[1,5-3]+2} = max{1, 1+2} = 3
V[2,6] = max{V[1,6],V[1,6-3]+2} = max{1, 1+2} = 3
V[2,7] = max{V[1,7],V[1,7-3]+2} = max{1, 1+2} = 3
V[2,8] = max{V[1,8],V[1,8-3]+2} = max{1, 1+2} = 3

V[3,1] = max{V[2,1],V[2,1-4]+5} = max{0, -} = 0


V[3,2] = max{V[2,2],V[2,2-4]+5} = max{1, -} = 1
V[3,3] = max{V[2,3],V[2,3-4]+5} = max{2, -} = 2
V[3,4] = max{V[2,4],V[2,4-4]+5} = max{2, 0+5} = 5
V[3,5] = max{V[2,5],V[2,5-4]+5} = max{2, 0+5} = 5
V[3,6] = max{V[2,6],V[2,6-4]+5} = max{2, 1+5} = 6
Periyar Govt. Arts College
31 Dr. R. Bhuvaneswari Cuddalore
0/1 Knapsack Problem

V[3,7] = max{V[2,7],V[2,7-4]+5} = max{2, 2+5} = 7


V[3,8] = max{V[2,8],V[2,8-4]+5} = max{2, 2+5} = 7

V[4,1] = max{V[3,1],V[3,1-5]+6} = max{0, -} = 0


V[4,2] = max{V[3,2],V[3,2-5]+6} = max{1, -} = 1
V[4,3] = max{V[3,3],V[3,3-5]+6} = max{2, -} = 2
V[4,4] = max{V[3,4],V[3,4-5]+6} = max{5, -} = 5
V[4,5] = max{V[3,5],V[3,5-5]+6} = max{5, 0+6} = 6
V[4,6] = max{V[3,6],V[3,6-5]+6} = max{6, 0+6} = 6
V[4,7] = max{V[3,7],V[3,7-5]+6} = max{7, 1+6} = 7
V[4,8] = max{V[3,8],V[3,8-5]+6} = max{7, 2+6} = 8

x1 = 0, x2 = 1, x3 = 0, x4 = 1

Periyar Govt. Arts College


32 Dr. R. Bhuvaneswari Cuddalore
0/1 Knapsack Problem

for (i = 0; i  n; i++)
{
for(w = 0; w  m; w++)
{
if(i==0 || w==0)
k[i][w] = 0;
else if(wt[i]  w)
k[i][w] = max(p[i]+k[i-1][w-wt[i], k[i-1][w]);
else
k[i][w] = k[i-1][w];
}
}

Periyar Govt. Arts College


33 Dr. R. Bhuvaneswari Cuddalore
Traveling Salesperson Problem

• The traveling salesperson problem is to find a tour of minimum cost.


• Let G = (V, E) be a directed graph with edge cost Cij =  if i, j  E
• Let V = n and assume n > 1
• A tour G is a directed simple cycle that includes every vertex in V.
• The cost of a tour is the sum of the cost of the edges on the tour.
• 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.
• The function g(1, V-{1}) is the length of an optimal salesperson tour.

𝑔 1, 𝑉 − 1 = min 𝑐1𝑘 + 𝑔 𝑘, 𝑉 − 1, 𝑘 −−−−−1


2 ≤𝑘 ≤𝑛

𝑔 𝑖, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗 −−−−−2


𝑗 ∈𝑆

Periyar Govt. Arts College


34 Dr. R. Bhuvaneswari Cuddalore
Traveling Salesperson Problem

g(i,) = Ci1, 1  i  n.

S = 
g(2,) = c21 = 5
g(3,) = c31 = 6
g(4, ) = c41 = 8

Using equation 2, we obtain 1 2 3 4


S = 1 1 0 10 15 20
2 5 0 9 10
g(2,{3}) = c23 + g(3,) = 9+6 = 15
3 6 13 0 12
g(2,{4}) = c24 + g(4,) = 10+8 = 18 4 8 8 9 0
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
Periyar Govt. Arts College
35 Dr. R. Bhuvaneswari Cuddalore
Traveling Salesperson Problem

S = 2
g(2,{3,4}) = min{c23 +g(3,{4}), c24 + g(4,{3})}
= min{9+20, 10+15} = 25
g(3,{2,4}) = min{c32 + g(2,{4}), c34 + g(4,{2})}
= min{13+18, 12+13} = 25
g(4,{2,3}) = min{c42 + g(2,{3}), c43 + g(3,{2})}
= min{8+15, 9+18} = 23
Using equation 1, we obtain
g(1,{2,3,4}) = min{c12+g(2,{3,4}), c13+g(3,{2,4}), c14+g(4,{2,3})}
= min{10+25, 15+25, 20+23} = 35
The optimal tour is
1->2->4->3->1

O(2nn2) time is taken to solve the traveling salesperson problem


Periyar Govt. Arts College
36 Dr. R. Bhuvaneswari Cuddalore

You might also like