DAA Dynamic Programming
DAA Dynamic Programming
DAA Dynamic Programming
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
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)}
lVi+1
j, lE
where i is the level number.
V1 V2 V3 V4 V5
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)}
lVi-1
l, jE
where i is the level number.
V1 V2 V3 V4 V5
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}
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]];
}
• 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
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
• 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
• 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.
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)}
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
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
• 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.
Pi = {1, 2, 5, 6}
wi = (2, 3, 4, 5}
m = 8, n = 4
x1 = 0, x2 = 1, x3 = 0, x4 = 1
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];
}
}
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