Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Design and Analysis of

Algorithms

Unit - IV
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

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
2 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


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

Periyar Govt. Arts College


4 Dr. R. Bhuvaneswari Cuddalore
Multistage Graphs

V1 V2 V3 V4 V5

Periyar Govt. Arts College


5 Dr. R. Bhuvaneswari Cuddalore
Multistage Graphs

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


6 Dr. R. Bhuvaneswari Cuddalore
Multistage Graphs

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
7 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
8 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


9 Dr. R. Bhuvaneswari Cuddalore
Multistage Graphs

V1 V2 V3 V4 V5

Periyar Govt. Arts College


10 Dr. R. Bhuvaneswari Cuddalore
Multistage Graphs

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


11 Dr. R. Bhuvaneswari Cuddalore
Multistage Graphs

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
12 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


13 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
14 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


15 Dr. R. Bhuvaneswari Cuddalore
All pair shortest paths

Algorithm AllPaths(cost, A, n) 6
{ 4
for i =1 to n do 1 2
{
for j = 1 to n do 11
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
16 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


17 Dr. R. Bhuvaneswari Cuddalore
String Editing

• Given two strings X = x1, x2, …. , xn and Y = y1, y2, ….., yn, where xi,
1  i  n, and yj, 1  j  m, are members of a finite set of symbols known
as the alphabet.
• We want to transform X into Y using a sequence of edit operations on X.
• The permissible edit operations are insert, delete and change, and there is a
cost associated with each operation.
• The cost of a sequence of operations is the sum of the costs of the
individual operations in the sequence.
• The problem of string editing is to identify a minimum-cost sequence of
edit operations that will transform X into Y.
• D(xi) – cost of deleting the symbol xi from X
• I(yj) – the cost of inserting the symbol yj into X
• C(xi, yj) – cost of changing the symbol xi of X into yj
• Cost of changing any symbol to any other symbol is 2.
Periyar Govt. Arts College
18 Dr. R. Bhuvaneswari Cuddalore
String Editing

• Cost associated with each insertion and deletion is 1.


• A dynamic programming solution to this problem can be obtained as
follows.
• Define cost(i, j) be the minimum cost of any edit sequence for
transforming x1, x2, …, xi into y1, y2, …, yj
• Compute cost(i, j) for each i and j.
• Then cost(n, m) is the cost of an optimal edit sequence.
• For i = j = 0, cost(i, j) = 0, since the two sequences are identical and
empty.
• If j = 0 and i > 0, we can transform X into Y by a sequence of deletes.
Cost(i, 0) = cost(i-1, 0) + D(xi).
• If i = 0 and j > 0, we can transform X into Y by a sequence of insertions
Cost(0, j) = cost(0, j-1) + I(yj)
Periyar Govt. Arts College
19 Dr. R. Bhuvaneswari Cuddalore
String Editing

• If i  0 and j  0, x1, x2, …., xi can transformed into y1, y2, … yj in one of
the following ways:
 Transform x1, x2, …., xi-1 into y1, y2, …, yj using a minimum-cost edit
sequence and then delete xi. The cost is cost(i-1, j) + D(xi)
 Transform x1, x2, …., xi-1 into y1, y2, …, yj-1 using a minimum-cost edit
sequence and then change the symbol xi to yj. The cost is cost(i-1, j-1)
+ C(xi, yj)
 Transform x1, x2, …., xi into y1, y2, …, yj-1 using a minimum-cost edit
sequence and then insert yj. The cost is cost(i, j-1) + I(yj)
• The minimum cost of any edit sequence is the minimum of the above
three costs, according to the principle of optimality.

Periyar Govt. Arts College


20 Dr. R. Bhuvaneswari Cuddalore
String Editing

• The recurrence equation for cost(i, j) is


𝟎 𝒊=𝒋=𝟎
𝒄𝒐𝒔𝒕 𝒊 − 𝟏, 𝟎 + 𝑫 𝒙𝒊 𝒋 = 𝟎, 𝒊 > 0
𝑐𝒐𝒔𝒕 𝒊, 𝒋 =
𝒄𝒐𝒔𝒕 𝟎, 𝒋 − 𝟏 + 𝑰 𝒚𝒋 𝒊 = 𝟎, 𝒋 > 0
𝒄𝒐𝒔𝒕′ 𝒊, 𝒋 𝒊 > 0, 𝒋 > 0

where
cost’ i, j = min{cost i-1, j) +D(xi), cost(i-1, j-1) + C(xi, yj),
cost(i, j-1) + I(yj)}

Periyar Govt. Arts College


21 Dr. R. Bhuvaneswari Cuddalore
String Editing

Given two strings, X and Y and edit operations (given below). Convert
string X into Y with minimum number of operations.
Allowed Operations:
 Insertion – Insert a new character.
 Deletion – Delete a character.
 Replace – Replace one character by another.
Example 1:
String X = "horizon"
String Y = "horzon"
Output: {remove 'i' from string X}
Example 2:
String X = a, a, b, a, b
String Y = b, a, b, b
For the cases i = 0, j > 1, and j = 0, i > 1, cost(i, j) can be computed first
and tabulated in the form of a table. The rest of the entries in the table can
be computed in the row-major order.
Periyar Govt. Arts College
22 Dr. R. Bhuvaneswari Cuddalore
String Editing

Cost(1,1) = min{cost(0,1)+D(x1), cost(0,0) + C(x1,y1), cost(1,0)+I(y1)}


= min{1+1, 0+2, 1+1} = 2
Cost(1,2) = min{cost(0,2)+D(x1), cost(0,1) + C(x1,y2), cost(1,1)+I(y2)}
= min{2+1, 1+0, 2+1} = 1
Cost(1,3) = min{cost(0,3)+D(x1), cost(0,2) + C(x1,y3), cost(1,2)+I(y3)}
= min{3+1, 2+2, 1+1} = 2
Cost(1,4) = min{cost(0,4)+D(x1), cost(0,3) + C(x1,y4), cost(1,3)+I(y4)}
= min{4+1, 3+2, 2+1} = 3
Cost(2,1) = min{cost(1,1)+D(x2), cost(1,0) + C(x2,y1), cost(2,0)+I(y1)}
= min{2+1, 1+2, 2+1} = 3
Cost(2,2) = min{cost(1,2)+D(x2), cost(1,1) + C(x2,y2), cost(2,1)+I(y2)}
= min{1+1, 2+0, 3+1} = 2
Cost(2,3) = min{cost(1,3)+D(x2), cost(1,2) + C(x2,y3), cost(2,2)+I(y3)}
= min{2+1, 1+2, 2+1} = 3
Cost(2,4) = min{cost(1,4)+D(x2), cost(1,3) + C(x2,y4), cost(2,3)+I(y4)}
= min{3+1, 2+2, 3+1} = 4 Periyar Govt. Arts College
23 Dr. R. Bhuvaneswari Cuddalore
String Editing

Cost(3,1) = min{cost(2,1)+D(x3), cost(2,0) + C(x3,y1), cost(3,0)+I(y1)}


= min{3+1, 2+0, 3+1} = 2
Cost(3,2) = min{cost(2,2)+D(x3), cost(2,1) + C(x3,y2), cost(3,1)+I(y2)}
= min{2+1, 3+2, 2+1} = 3
Cost(3,3) = min{cost(2,3)+D(x3), cost(2,2) + C(x3,y3), cost(3,2)+I(y3)}
= min{3+1, 2+0, 3+1} = 2
Cost(3,4) = min{cost(2,4)+D(x3), cost(2,3) + C(x3,y4), cost(3,3)+I(y4)}
= min{4+1, 3+0, 2+1} = 3
Cost(4,1) = min{cost(3,1)+D(x4), cost(3,0) + C(x4,y1), cost(4,0)+I(y1)}
= min{2+1, 3+2, 4+1} = 3
Cost(4,2) = min{cost(3,2)+D(x4), cost(3,1) + C(x4,y2), cost(4,1)+I(y2)}
= min{3+1, 2+0, 3+1} = 2
Cost(4,3) = min{cost(3,3)+D(x4), cost(3,2) + C(x4,y3), cost(4,2)+I(y3)}
= min{2+1, 3+2, 2+1} = 3
Cost(4,4) = min{cost(3,4)+D(x4), cost(3,3) + C(x4,y4), cost(4,3)+I(y4)}
= min{3+1, 2+2, 3+1} = 4 Periyar Govt. Arts College
24 Dr. R. Bhuvaneswari Cuddalore
String Editing
Cost(5,1) = min{cost(4,1)+D(x5), cost(4,0) + C(x5,y1), cost(5,0)+I(y1)}
= min{3+1, 4+0, 5+1} = 4
Cost(5,2) = min{cost(4,2)+D(x5), cost(4,1) + C(x5,y2), cost(5,1)+I(y2)}
= min{2+1, 3+2, 4+1} = 3
Cost(5,3) = min{cost(4,3)+D(x5), cost(4,2) + C(x5,y3), cost(5,2)+I(y3)}
= min{3+1, 2+0, 3+1} = 2
Cost(5,4) = min{cost(4,4)+D(x5), cost(4,3) + C(x5,y4), cost(5,3)+I(y4)}
= min{4+1, 3+0, 2+1} = 3
Optimal operations are:
 Insert y1, delete x2 and x4
 Change x1 by y1, delete x4
 Delete x1 and x2, insert y3
 Delete x1 and x2, insert y4
Time complexity: O(mn)
Periyar Govt. Arts College
25 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
26 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


27 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


28 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
29 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
30 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


31 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


32 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


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

You might also like