Daa Unit III Dynamic Programming
Daa Unit III Dynamic Programming
DYNAMIC PROGRAMMING
Dynamic Programming is a method to solve the given problem by taking sequence of decisions. In
order to get the optimal solution of the given problem. We should write the possible decisions by using
principal of optimality; we will select the optimal solution of the given problem.
The difference between greedy method and dynamic programming is, In greedy method we
take only one decision to find out the solution. Whereas in dynamic programming we take sequence of
decisions, which satisfy the condition and finally we get the optimal solution by using principal of
optimality.
Principal of optimality:
The principle of optimality states that no matter what the first decision, the remaining decisions
must be optimal with respect to the state that results from this first decision. This principle implies that
an optimal decision sequence is comprised for some formulations of some problem.
Since the principle of optimality may not hold for some formulations of some problems, it is
necessary to verify that it does not hold for the problem being solved.
Dynamic programming cannot be applied when this principle does not hold.
Steps for Dynamic programming:
1: Characteristics the given problem by using mathematical equation which gives the solution (or)
4 : 0 /1 Knapsack Problem -
5: Reliability Design
1
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
OPTIMAL BINARY SEARCH TREE ( OBST ) : The given set of identifiers { a1, a2,…., an} with
a1<a2<a3…an. Let p (i) be the probability with which we can search for ai. Let q(i) be the probability that
the identifier x being searched. Such that ai < x < ai + 1 and 0 ≤ i ≤ n. In other words p(i) is the probability
of successful search and q(i) be the probability of unsuccessful search.
Clearly ∑ p (i) + ∑ q(i) then obtain a tree with minimum cost. Such a tree with optimum Cost is
1≤i≤n 1≤i≤n
To solve this problem using dynamic programming method by using following formulas.
1 : c ( i, j ) = min { c( i , k - 1 ) + c( k , j ) } + w ( i , j )
i<k≤j
2 : w( i , j ) = p(j) + q(j) + w ( i , j - 1 )
3 : r( i , j ) = k
Example1 : Using algorithm OBST compute w(i,j), r(i,j) and c(i,j), 0 ≤ i ≤ j ≤ 4 for the identifier set
( a1, a2, a3, a4 ) = ( do, while, for, if ) with ( p1, p2, p3, p4 ) = ( 3, 3, 1, 1 ) and
( q0, q1, q2, q3, q4 ) = ( 2, 3, 1, 1, 1 ) using r ( i , j ) construct the optimal binary search tree.
Solution :
Successful Probability: ( p1, p2, p3, p4 ) = ( 3, 3, 1, 1 )
UnSuccessful Probability: (q0, q1, q2, q3, q4 ) = ( 2, 3, 1, 1, 1 )
Identifier set : (a1, a2, a3, a4 ) = ( end, goto, print, stop )
Initial Conditions:
w(i,j)= q(i)
c(i,j) = 0
r(i,j) = 0
Formulas :
1. w( i , j ) = p(j) + q(j) + w ( i , j - 1 )
2. c ( i, j ) = min { c( i , k - 1 ) + c( k , j ) } + w ( i , j )
i<k≤j
3. r( i , j ) = k
2
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
Step1 : j – i = 0
w ( i , j ) = q(i)
w(0,0) = q(0)= 2 c(0,0) = 0 r(0,0) = 0
w(1,1) = q(1)= 3 c(1,1) = 0 r(1,1) = 0
w(2,2) = q(2)= 1 c(2,2) = 0 r(2,2) = 0
w(3,3) = q(3)= 1 c(3,3) = 0 r(3,3) = 0
w(4,4) = q(4)= 1 c(4,4) = 0 r(4,4) = 0
Step 2 : j – i = 1 , ( i =0, j = 1, k = 1 )
w( i , j ) = p(j) + q(j) + w ( i , j - 1 )
w ( 0, 1 ) = p(1) + q(1) + w(0,0)
=3 + 3 + 2 = 8
c ( i, j ) = min { c( i , k - 1 ) + c( k , j )} + w ( i , j )
i<k≤j
c( 0,1) = min { c(0,0)+c(1,1)} + w(0,1)
= min { 0 +0 } + 8
= 0+8 = 8
r(0,1) = 1
( i =1, j = 2, k = 2 )
w ( 1, 2 ) = p(2) + q(2) + w(1,1)
=3+1+3=7
c ( i, j ) = min { c( i , k - 1 ) + c( k , j )} + w ( i , j )
i<k≤j
c(1,2) = min { c(1,1)+c(2,2)} + w(1,2)
= min { 0 + 0 } + 7= 7
r(1,2) = 2
( i =2, j = 3, k = 3 )
w ( 2, 3 ) = p ( 3 ) + q(3) + w(2,2)
=1+1+1=3
c ( i, j ) = min { c( i , k - 1 ) + c( k , j )} + w ( i , j )
i<k≤j
c( 2,3) = min { c(2,2) + c(3,3)} + w(2,3)
= min { 0 + 0 } + 3 = 3
r(2,3) = 3
3
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
( i =3, j = 4, k = 4 )
w ( 3, 4 ) = p(4)+q(4)+w(3,3)
=1+1+1=3
c ( i, j ) = min { c( i , k - 1 ) + c( k , j )} + w ( i , j )
i<k≤j
c( 3,4) = min { c(3,3) + c( 4,4) } + w(3,4)
= min { 0 + 0} + 3 = 3
r(3,4) = 4
Step 3 : j – i = 2 , ( i =0, j= 2, k= 1,2 )
( i =1, j= 3, k=2,3 )
5
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
To build Optimal Binary Search Tree ( OBST )
w ( 0, 0 ) = 2 w ( 1, 1 ) = 3 w ( 2, 2 ) = 1 w ( 3, 3 ) = 1 w ( 4, 4 ) = 1
for a set( i, j )
c ( 0, 0 ) = 0 c (1, 1) = 0 c (2, 2) = 0 c (3, 3) = 0 c (4, 4) = 0
j–i=0
r ( 0, 0 ) = 0 r (1, 1 ) = 0 r (2, 2 ) = 0 r (3, 3 ) = 0 r (4, 4 ) = 0
w ( 0, 1 ) = 8 w ( 1, 2 ) = 7 w ( 2, 3 ) = 3 w ( 3, 4 ) = 3
r ( 0, 1 ) = 1 r (1, 2 ) = 2 r (2, 3 ) = 3 r ( 3, 4 ) = 4
w ( 0, 2 ) = 12 w (1, 3) = 9 w (2, 4) = 5
r ( 0, 2 ) = 1 r (1, 3 ) = 2 r (2, 4 ) = 3
w ( 0, 3 ) = 14 w (1, 4 ) = 11
j–i=3 c ( 0, 3 ) = 25 c (1, 4 ) = 19
r ( 0, 3 ) = 2 r (1, 4 ) = 2
w ( 0, 4 ) = 16
j–i=4 c ( 0, 4 ) = 32
r ( 0, 4 ) = 2
6
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
To build OBST, r ( 0,4) = 2, K =2.
r(i,k-1) r(k,j)
r(0,4)=2
r(i,k-1) r(k,j)
r(0,1)=1 r(2,4)= 3
r(i,k-1) r(k,j)
r(3,3)=0 r(4,4) = 0
( a1, a2, a3, a4 ) = ( do, while, for, if )
while
do for
if
7
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
Example 2 : Using the algorithm OBST, compute W(i,j), R(i,j) and C(i,j), 0<=i<j<=4 for the identifier set
(a1,a2,a3,a4)=(end, goto, print, stop) with p(1)=1/20, p(2)=1/5, p(3)=1/10, p(4)=1/20; q(0)=1/5,
q(1)=1/10, q(2)=1/5, q(3)=1/20 and q(4)=1/20. Using the R(i,j)’s construct the OBST.
Solution :
Successful Probability
P(1) = 1/20 * 20 = 1
P(2) = 1/5 * 20 = 4
P(3) = 1/10 * 20 = 2
P(4) = 1/20 * 20 = 1
( p1,p2,p3,p4 ) = ( 1, 4, 2, 1 )
UnSuccessful Probability
q (0) =1/5 * 20 = 4
q (1) =1/10 * 20 = 2
q (2) =1/5 * 20 = 4
q (3) =1/20 * 20=1
q (4) =1/20* 20=1
(q0, q1, q2, q3, q4) = (4, 2, 4, 1, 1)
(a1, a2, a3, a4)= (end, goto, print, stop)
Initial Conditions:
W (i,j)= q(i)
C (i,j) = 0
R (i,j) = 0
Formulas:
1. w( i , j ) = p(j) + q(j) + w ( i , j - 1 )
2. c ( i, j ) = min { c( i , k - 1 ) + c( k , j ) } + w ( i , j )
i<k≤j
3. r( i , j ) = k
8
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
Step1: j – i = 0
w (i, j) = q (i)
w (0,0) = q (0)= 4 c(0,0) = 0 r(0,0) = 0
w(1,1) = q(1)= 2 c(1,1) = 0 r(1,1) = 0
w(2,2) = q(2)= 4 c(2,2) = 0 r(2,2) = 0
w(3,3) = q(3)= 1 c(3,3) = 0 r(3,3) = 0
w(4,4) = q(4)= 1 c(4,4) = 0 r(4,4) = 0
Step 2 : j – i = 1 , ( i =0, j = 1, k = 1 )
w( i , j ) = p(j) + q(j) + w ( i , j - 1 )
w ( 0, 1 ) = p(1) + q(1) + w(0,0)
=1 + 2 + 4 = 7
c ( i, j ) = min { c( i , k - 1 ) + c( k , j )} + w ( i , j )
i<k≤j
c( 0,1) = min { c(0,0)+c(1,1)} + w(0,1)
= min { 0 +0 } + 7
= 0+7 = 7
r(0,1) = 1
( i =1, j = 2, k = 2 )
w ( 1, 2 ) = p(2)+q(2)+w(1,1)
= 4 + 4 + 2 = 10
c ( i, j ) = min { c( i , k - 1 ) + c( k , j )} + w ( i , j )
i<k≤j
c(1,2) = min { c(1,1)+c(2,2)} + w(1,2)
= min { 0 + 0 } + 10= 10
r(1,2) = 2
( i =2, j = 3, k = 3 )
w ( 2, 3 ) = p ( 3 ) + q(3) + w(2,2)
=2+1+4=7
c ( i, j ) = min { c( i , k - 1 ) + c( k , j )} + w ( i , j )
i<k≤j
c( 2,3) = min { c(2,2) + c(3,3)} + w(2,3)
= min { 0 + 0 } + 7 = 7
r(2,3) = 3
9
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
( i =3, j = 4, k = 4 )
w ( 3, 4 ) = p(4)+q(4)+w(3,3)
=1+1+1=3
c ( i, j ) = min { c( i , k - 1 ) + c( k , j )} + w ( i , j )
i<k≤j
c( 3,4) = min { c(3,3) + c( 4,4) } + w(3,4)
= min { 0 + 0} + 3 = 3
r(3,4) = 4
Step 3 : j – i = 2 , ( i =0, j= 2, k= 1,2 )
10
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
Step 4 : j – i = 3 , ( i =0, j= 3, k=1,2,3 )
11
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
To build Optimal Binary Search Tree ( OBST )
w ( 0, 0 ) = 4 w ( 1, 1 ) = 2 w ( 2, 2 ) = 4 w ( 3, 3 ) = 1 w ( 4, 4 ) = 1
for a set( i, j )
c ( 0, 0 ) = 0 c (1, 1) = 0 c (2, 2) = 0 c (3, 3) = 0 c (4, 4) = 0
j–i=0
r ( 0, 0 ) = 0 r (1, 1 ) = 0 r (2, 2 ) = 0 r (3, 3 ) = 0 r (4, 4 ) = 0
w ( 0, 1 ) = 7 w ( 1, 2 )= 10 w ( 2, 3 ) = 7 w ( 3, 4 ) = 3
r ( 0, 1 ) = 1 r (1, 2 ) = 2 r (2, 3 ) = 3 r ( 3, 4 ) = 4
w ( 0, 2 ) = 15 w (1, 3) = 13 w (2, 4) = 9
r ( 0, 2 ) = 2 r (1, 3 ) = 2 r (2, 4 ) = 3
w ( 0, 3 ) = 18 w (1, 4 ) = 15
j–i=3 c ( 0, 3 ) = 21 c (1, 4 ) = 27
r ( 0, 3 ) = 2 r (1, 4 ) = 2
w ( 0, 4 ) = 20
j–i=4 c ( 0, 4 ) = 39
r ( 0, 4 ) = 2
12
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
To build OBST, r ( 0,4) = 2, K =2.
r(i,k-1) r(k,j)
r(0,4)=2
r(i,k-1) r(k,j)
r(0,1)=1 r(2,4)= 3
r(i,k-1) r(k,j)
r(3,3)=0 r(4,4) = 0
( a1, a2, a3, a4 )=( end, goto, print, stop )
goto
end print
Stop
13
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
ALL PAIRS SHORTEST PATHS PROBLEM: Let G = ( V, E ) be a directed graph consisting of n vertices
and each edge is associated with a weight. The problem of finding the shortest path between all pairs of
vertices in a graph is called all pairs shortest path problem.
This problem can be solved by using Dynamic Programming technique. The all pairs
shortest path problem is to determine a matrix A such that A( i , j ) is the length of a shortest path from
vertex i to vertex j. Assume that this path contains no cycles. If k is an intermediate vertex on this path,
then the sub paths from i to k and from k to j are the shortest paths from i to k and k to j respectively.
Otherwise the path I to j is not shortest path. If k is intermediate vertex with higest index, then the path i
to k is the shortest path going through no vertex with index greater than k – 1. Similarly the path k to j is
the shortest path going through no vertex with index greater than k – 1.
14
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
Ex : find the shortest path between all pairs of node in the following graph.
1 2
1
4 3
2
3 4
6
Sol :
1 2
1
4 3
2
3 4
6
0 5 4 1
1
5 0 2 3
2 = A0
4 2 0 6
3
1 3 6 0
4
The formula for finding the all pairs shortest path problem as follows,
Ak ( i, j ) = min { Ak – 1 ( i, j ), Ak – 1 ( i, k ) + Ak – 1 ( k, j ) }
Where if k ≥ 1.
Ak , number of iterations to be taken .
These iterations depends on the number of weights of vertices given, in our problem, we have 4 vertices.
We will take 4 iterations and we will always start from A0 is the given matrix. So we have to find A1 , A2 ,
A3 A4 for the shortest path. According to the formula
15
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
K=1
A1 ( 1, 1 ) = min { A1 – 1 ( 1, 1 ), A1– 1 ( 1, 1 ) + A1 – 1 ( 1, 1 ) }
= min {0, 0 + 0} = 0
A1 ( 1, 2 ) = min { A1 – 1 ( 1, 2 ), A1– 1 ( 1, 1 ) + A1 – 1 ( 1, 2 ) }
= min {5, 0 + 5} = 5
A1 ( 1, 3 ) = min { A1 – 1 ( 1, 3 ), A1– 1 ( 1, 1 ) + A1 – 1 ( 1, 3 ) }
= min {4, 0 + 4} = 4
A1 ( 1, 4 ) = min { A1 – 1 ( 1, 4 ), A1– 1 ( 1, 1 ) + A1 – 1 ( 1, 4 ) }
= min {1, 0 + 1} = 1
A1 ( 2, 1 ) = min { A1 – 1 ( 2, 1 ), A1– 1 ( 2, 1 ) + A1 – 1 ( 1, 1 ) }
= min {5, 5 + 0} = 5
A1 (2, 2) = min { A1 – 1 (2, 2), A1– 1 (2, 1) + A1 – 1 (1, 2)}
= min {0, 5 + 5} = 0
A1 ( 2, 3 ) = min { A1 – 1 ( 2, 3 ), A1– 1 ( 2, 1 ) + A1 – 1 ( 1, 3 ) }
= min {2, 5 + 4} = 2
A1 ( 2, 4 ) = min { A1 – 1 ( 2, 4 ), A1– 1 ( 2, 1 ) + A1 – 1 ( 1, 4 ) }
= min {3, 5 + 1} = 3
A1 ( 3, 1 ) = min { A1 – 1 ( 3, 1 ), A1– 1 ( 3, 1 ) + A1 – 1 ( 1, 1 ) }
= min {4, 4 + 0} = 4
A1 (3, 2) = min { A1 – 1 (3, 2), A1– 1 (3, 1) + A1 – 1 (1, 2)}
= min {2, 4 + 5} = 2
A1 ( 3, 3 ) = min { A1 – 1 ( 3, 3 ), A1– 1 ( 3, 1 ) + A1 – 1 ( 1, 3 ) }
= min { 0, 4 + 4 } = 0
A1 ( 3, 4 ) = min { A1 – 1 ( 3, 4 ), A1– 1 ( 3, 1 ) + A1 – 1 ( 1, 4 ) }
= min { 6, 4 + 1 } = 5
A1 ( 4, 1 ) = min { A1 – 1 ( 4, 1 ), A1– 1 ( 4, 1 ) + A1 – 1 ( 1, 1 ) }
= min { 1, 1 + 0 } = 1
A1 ( 4, 2 ) = min { A1 – 1 ( 4, 2 ), A1– 1 (4, 1 ) + A1 – 1 ( 1, 2 ) }
= min { 3,1 + 5 } = 3
A1 ( 4, 3 ) = min { A1 – 1 ( 4, 3 ), A1– 1 ( 4, 1 ) + A1 – 1 ( 1, 3 ) }
= min { 6, 1 + 4 } = 5
A1 ( 4, 4 ) = min { A1 – 1 ( 4, 4 ), A1– 1 ( 4, 1 ) + A1 – 1 ( 1, 4 ) }
= min { 0, 1 + 1 } = 0
16
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
0 5 4 1
5 0 2 3
A1 =
4 2 0 5
1 3 5 0
K=2
A2 ( 1, 1 ) = min { A2 – 1 ( 1, 1 ), A2– 1 ( 1, 2 ) + A2 – 1 ( 2, 1 ) }
= min { 0, 5 + 5 } = 0
A2 ( 1, 2 ) = min { A2 – 1 ( 1, 2 ), A2– 1 ( 1, 2 ) + A2 – 1 ( 2, 2 ) }
= min { 5, 5 + 0 } = 5
A2 ( 1, 3 ) = min { A2 – 1 ( 1, 3 ), A2– 1 ( 1, 2 ) + A2 – 1 ( 2, 3 ) }
= min { 4, 5 + 2 } = 4
A2 ( 1, 4 ) = min { A2 – 1 ( 1, 4 ), A2– 1 ( 1, 2 ) + A\2 – 1 ( 2, 4 ) }
= min { 1, 5 + 3 } = 1
A2 ( 2, 1 ) = min { A2 – 1 ( 2, 1 ), A2– 1 ( 2, 2 ) + A2 – 1 ( 2, 1 ) }
= min { 5, 0 + 5 } = 5
A2 ( 2, 2 ) = min { A2 – 1 ( 2, 2 ), A2– 1 (2, 2 ) + A2 – 1 ( 2, 2 ) }
= min { 0,0 + 0 } = 0
A2 ( 2, 3 ) = min { A2 – 1 ( 2, 3 ), A2– 1 ( 2, 2 ) + A2 – 1 ( 2, 3 ) }
= min { 2, 0 + 2 } = 2
A2 ( 2, 4 ) = min { A2 – 1 ( 2, 4 ), A2– 1 ( 2, 2 ) + A2 – 1 ( 2, 4 ) }
= min { 3, 0 + 3} = 3
A2 ( 3, 1 ) = min { A2 – 1 ( 3, 1 ), A2– 1 ( 3, 2 ) + A2 – 1 ( 2, 1 ) }
= min { 4, 2 + 5 } = 4
A2 ( 3, 2 ) = min { A2 – 1 ( 3, 2 ), A2– 1 (3, 2 ) + A2 – 1 ( 2, 2 ) }
= min { 2,2 + 0 } = 2
A2 ( 3, 3 ) = min { A2 – 1 ( 3, 3 ), A2– 1 ( 3, 2 ) + A2 – 1 ( 2, 3 ) }
= min { 0, 2 + 2 } = 0
A2 ( 3, 4 ) = min { A2 – 1 ( 3, 4 ), A2– 1 ( 3, 2 ) + A2 – 1 ( 2, 4 ) }
= min { 5, 2 + 3 } = 5
17
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
A2 ( 4, 1 ) = min { A2 – 1 ( 4, 1 ), A2– 1 ( 4, 2 ) + A2 – 1 ( 2, 1 ) }
= min { 1, 3 + 5 } = 1
A2 ( 4, 2 ) = min { A2 – 1 ( 4, 2 ), A2– 1 (4, 2 ) + A2 – 1 ( 2, 2 ) }
= min { 3,3 + 0 } = 3
A2 ( 4, 3 ) = min { A2 – 1 ( 4, 3 ), A2– 1 ( 4, 2 ) + A2 – 1 ( 2, 3 ) }
= min { 5, 3 + 2 } = 5
A2 ( 4, 4 ) = min { A2 – 1 ( 4, 4 ), A2– 1 ( 4, 2 ) + A2 – 1 ( 2, 4 ) }
= min { 0, 3 + 3 } = 0
0 5 4 1
5 0 2 3
A2 =
4 2 0 5
1 3 5 0
K=3
A3 ( 1, 1 ) = min { A3 – 1 ( 1, 1 ), A3– 1 ( 1, 3 ) + A3 – 1 ( 3, 1 ) }
= min { 0, 4 + 4 } = 0
A3 ( 1, 2 ) = min { A3 – 1 ( 1, 2 ), A3– 1 ( 1, 3 ) + A3 – 1 ( 3, 2 ) }
= min { 5, 5 + 0 } = 5
A3 ( 1, 3 ) = min { A3 – 1 ( 1, 3 ), A3– 1 ( 1, 3 ) + A3 – 1 ( 3, 3 ) }
= min { 4, 5 + 2 } = 4
A3 ( 1, 4 ) = min { A3 – 1 ( 1, 4 ), A3– 1 ( 1, 3 ) + A\3 – 1 ( 3, 4 ) }
= min { 1, 4 + 5 } = 1
A3 ( 2, 1 ) = min { A3 – 1 ( 2, 1 ), A3– 1 ( 2, 3 ) + A3 – 1 ( 3, 1 ) }
= min { 5, 4 + 4 } = 5
A3 ( 2, 2 ) = min { A3 – 1 ( 2, 2 ), A3– 1 (2, 3 ) + A3 – 1 ( 3, 2 ) }
= min { 0,2 + 2 } = 0
A3 ( 2, 3 ) = min { A3 – 1 ( 2, 3 ), A3– 1 ( 2, 3 ) + A3 – 1 ( 3, 3 ) }
= min { 2, 2 + 0 } = 2
A3 ( 2, 4 ) = min { A3 – 1 ( 2, 4 ), A3– 1 ( 2, 3 ) + A3 – 1 ( 3, 4 ) }
= min { 3, 2 + 5} = 3
18
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
A3 ( 3, 1 ) = min { A3 – 1 ( 3, 1 ), A3– 1 ( 3, 3 ) + A3 – 1 ( 3, 1 ) }
= min { 4, 0 + 4 } = 4
A3 ( 3, 2 ) = min { A3 – 1 ( 3, 2 ), A3– 1 (3, 3 ) + A3 – 1 ( 3, 2 ) }
= min { 2,0 + 2 } = 2
A3 ( 3, 3 ) = min { A3 – 1 ( 3, 3 ), A3– 1 ( 3, 3 ) + A3 – 1 ( 3, 3 ) }
= min { 0, 0 + 0 } = 0
A3 ( 3, 4 ) = min { A3 – 1 ( 3, 4 ), A3– 1 ( 3, 3 ) + A3 – 1 ( 3, 4 ) }
= min { 5, 0 + 5} = 5
A3 ( 4, 1 ) = min { A3 – 1 ( 4, 1 ), A3– 1 ( 4, 3 ) + A3 – 1 ( 3, 1 ) }
= min { 1, 5 + 4 } = 1
A3 ( 4, 2 ) = min { A3 – 1 ( 4, 2 ), A3– 1 (4, 3 ) + A3 – 1 ( 3, 2 ) }
= min { 3,5 + 2 } = 3
A3 ( 4, 3 ) = min { A3 – 1 ( 4, 3 ), A3– 1 ( 4, 3 ) + A3 – 1 ( 3, 3 ) }
= min { 5, 5 + 0 } = 5
A3 ( 4, 4 ) = min { A3 – 1 ( 4, 4 ), A3– 1 ( 4, 3 ) + A3 – 1 ( 3, 4 ) }
= min { 0, 5 + 5} = 0
0 5 4 1
5 0 2 3
A3 =
4 2 0 5
1 3 5 0
K=4
A4 ( 1, 1 ) = min { A4 – 1 ( 1, 1 ), A4– 1 ( 1, 4 ) + A4 – 1 ( 4, 1 ) }
= min { 0, 1 + 1 } = 0
A4 ( 1, 2 ) = min { A4 – 1 ( 1, 2 ), A4– 1 ( 1, 4 ) + A4 – 1 ( 4, 2 ) }
= min { 5, 1 + 3 } = 4
A4 ( 1, 3 ) = min { A4 – 1 ( 1, 3 ), A4– 1 ( 1, 4 ) + A4 – 1 ( 4, 3 ) }
= min { 4, 1 + 5 } = 4
A4 ( 1, 4 ) = min { A4 – 1 ( 1, 4 ), A4– 1 ( 1, 4 ) + A\4 – 1 ( 4, 4 ) }
= min { 1, 1 + 0 } = 1
19
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
A4 ( 2, 1 ) = min { A4 – 1 ( 2, 1 ), A4– 1 ( 2, 4 ) + A4 – 1 ( 4, 1 ) }
= min { 5, 3 + 1 } = 4
A4 ( 2, 2 ) = min { A4 – 1 ( 2, 2 ), A4– 1 ( 2, 4 ) + A4 – 1 ( 4, 2 ) }
= min { 0, 3 + 3 } = 0
A4 ( 2, 3 ) = min { A4 – 1 ( 2, 3 ), A4– 1 ( 2, 4 ) + A4 – 1 ( 4, 3 ) }
= min { 2, 3 + 5 } = 2
A4 ( 2, 4 ) = min { A4 – 1 ( 2, 4 ), A4– 1 ( 2, 4 ) + A\4 – 1 ( 4, 4 ) }
= min { 3, 3 + 0 } = 3
A4 ( 3, 1 ) = min { A4 – 1 ( 3, 1 ), A4– 1 ( 3, 4 ) + A4 – 1 ( 4, 1 ) }
= min { 4, 5 + 1 } = 4
A4 ( 3, 2 ) = min { A4 – 1 ( 3, 2 ), A4– 1 ( 3, 4 ) + A4 – 1 ( 4, 2 ) }
= min { 2, 5 + 3 } = 2
A4 ( 3, 3 ) = min { A4 – 1 ( 3, 3 ), A4– 1 ( 3, 4 ) + A4 – 1 ( 4, 3 ) }
= min { 0, 5 + 5 } = 0
A4 ( 3, 4 ) = min { A4 – 1 ( 3, 4 ), A4– 1 ( 3, 4 ) + A\4 – 1 ( 4, 4 ) }
= min { 5, 5 + 0 } = 5
A4 ( 4, 1 ) = min { A4 – 1 ( 4, 1 ), A4– 1 ( 4, 4 ) + A4 – 1 ( 4, 1 ) }
= min { 1, 0 + 1 } = 1
A4 ( 4, 2 ) = min { A4 – 1 ( 4, 2 ), A4– 1 ( 4, 4 ) + A4 – 1 ( 4, 2 ) }
= min { 3, 0 + 3 } = 3
A4 ( 4, 3 ) = min { A4 – 1 ( 4, 3 ), A4– 1 ( 4, 4 ) + A4 – 1 ( 4, 3 ) }
= min { 5, 0 + 5 } = 5
A4 ( 4, 4 ) = min { A4 – 1 ( 4, 4 ), A4– 1 ( 4, 4 ) + A\4 – 1 ( 4, 4 ) }
= min { 0, 0 + 0 } = 0
0 4 4 1
4 0 2 3
A4 =
4 2 0 5
1 3 5 0
20
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
Travelling Salesperson Problems: If there are n cities and cost of traveling from one city to other city is
given. A salesman has to start from any one of the city and has to visit all the cities exactly once and has
to return to the starting place with shortest distance or minimum cost.
1 : g(i, Φ ) = Ci , 1
Here g(i, S) means i is starting node and the nodes in S are to be traversed. min is considered as the
intermediate node g( j , S – { j } ) means j is already traversed. So next we have to traverse S – { j } with j
as starting point.
Example 1 : Construct an optimal travelling sales person tour using Dynamic Programming.
21
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
Step2 : Consider set of 1 elements, such that
S = 1, {2}, {3}, {4}
g( i, S ) = min { Ci j + g( j, S – { j })
g(i, S) = min { Ci j + g( j, S – { j }) }
22
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
g( 4, { 2,3 } ) = min { c42 + g( 2, {2,3 } – {2}) , c43 + g( 3, {2,3 } – {3}) }
= min { 3 + g( 2, {3}), 2 + g(3, {2}) }
= min { 3 + 17, 2 + 20 }
= min { 20, 22 }
= 20.
S = 3, { 2,3,4 }
23
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
Example 2 : Construct an optimal travelling sales person tour using Dynamic Programming.
g(i, S) = min { Ci j + g( j, S – { j }) }
S = 3, { 2,3,4 }
g(i, S) = min { Ci j + g( j, S – { j }) }
25
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
0 /1 Knapsack Problem: If we are given n objects and a knapsack or a bag in which the object i
that has weight wi is to be placed. The knapsack has a capacity W. Then the profit that can be earned is
pixi . The objective is to obtain filling of knapsack with maximum profit earned. Maximized pixi. Subject
to constraint wixi<=W Where 1<=i<=n and n is total no. of objects and xi =0 or 1.
26
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
Example 1 : Consider the following 0 / 1 Knapsack problem using dynamic programming m = 6,
n = 3, ( w1, w2, w3 ) = ( 2, 3, 4 ), ( p1, p2, p3 ) = ( 1, 2, 5 ).
Initially S0 = { ( 0,0 ) }
S3 = { (0,0),(1,2),(2,3),(5,4),(6,6),(7,7),(8,9) }
27
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
Here Capacity of the Knapsack is 6, Now we have to remove the pairs, in which wi>m, i.e wi>6
S3 = { (0,0),(1,2),(2,3),(5,4),(6,6) }
( 1,2 ) belongs to S1
28
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
Example 2 : Consider the following 0 / 1 Knapsack problem using dynamic programming m = 8,
n = 4.
Solution : we have to build the sequence of decisions S0, S1, S2, S3, S4
Initially S0 = { ( 0,0 ) }
29
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
S3 = S2 ∪ S21
S3 = { (0,0),(1,2),(2,3),(5,4),(6,6),(7,7),(8,9) }
Here Capacity of the Knapsack is 8, Now we have to remove the pairs, in which wi>m, i.e wi>8
( 2,3 ) belongs to S2
30
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
Therefore , We must set x3 = 0
( 2,3 ) belongs to S2
( 2,3 ) does not belongs to S1
31
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
Reliability Design:
The dynamic programming approach to solve a problem with multiplicative constraints. Let us
consider the example of a computer return in which a set of nodes are connected with each other. Let ri
be the reliability of a node i.e. the probability at which the node forwards the packets correctly in ri .
Then the reliability of the path connecting from one node s to another node d is
where k is the number of intermediate node. Similarly, we can also consider a system with n devices
connected in series, where the reliability of device i is ri . The reliability of the system is .
For example if there are 5 devices connected in series and the reliability of each device is 0.99 then the
reliability of the system is 0.99×0.99×0.99×0.99×0.99=0.951. Hence, it is desirable to connect multiple
copies of the same devices in parallel through the use of switching circuits. The switching circuits
determine the devices in any group functions properly. Then they make use of one such device at each
stage. Let mi be the number of copies of device Di in stage i. Then the probability that all mi have
malfunction i.e. (1-ri) m i .Hence, the reliability of stage i becomes 1-(1-ri) m i . Thus, if ri =0.99 and mi=2,
the reliability of stage i is 0.9999. However, in practical situations it becomes less because the switching
circuits are not fully reliable. Let us assume that the reliability of stage i in φi(mi), i≤n. Thus the reliability
of the system is
The reliability design problem is to use multiple copies of the devices at each stage to increase
reliability. However, this is to be done under a cost constraint. Let ci be the cost of each unit of device D i
and let c be the cost constraint. Then the objective is to maximize the reliability under the condition that
the total cost of the system mici will be less than c.
We can assume that each ci>0 and so each mi must be in the range 1 ≤ mi ≤ ui ,
Now we have to find out the Upper bound ( ui)
ui = ( c + ci-∑Cj ) / Ci
The dynamic programming approach finds the optimal solution m 1, m2…mn. An optimal sequence of
decision i.e. a decision for each mi can result an optimal solution.
32
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
Example: Design a Three stage system with device tape D1, D2, D3. The costs are 30, 15 and 20
respectively. The cost of the system is to be no more than 105. The reliability of each device type
is 0.9, 0.8 and 0.5 respectively.
Solution: Given data
c = 105
c1=30
c2=15
c3=20
r1=0.9
r2=0.8
r3=0.5
Now we have to find out the Upper bound ( ui)
ui = ( c + ci-∑Cj ) / Ci
u1 = ( c + c1-( c1 + c2 + c3 )) / c1
= 105 + 30 – ( 30+ 15 + 20 ) / 30
= 105 + 30 -65 / 30
= 135-65 / 30
= 70 / 30 = 2
u2 = ( c + c2-( c1 + c2 + c3 ) ) / c2
= ( 105 + 15 – ( 30+ 15 + 20 ) ) / 15
= ( 105 + 15 – (65) / 15
= 120-65 / 15
= 55 / 15 = 3
U3 = ( c + c3-( c1 + c2 + c3 ) ) / c3
= ( 105 + 20 – ( 30+ 15 + 20 ) ) / 20
= ( 105 + 20 – (65) / 20
= 125-65 / 20
= 60 / 20 = 3
We can require
2 Copies of D1
3 Copies of D2
3 Copies of D3
To improve the reliability of the three stage system
33
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
Let us start with S0 = ( r, c )
R= reliability
C = cost
Initially S0 = ( 1, 0)
Device 1 (D1 ) : How many copies has to purchase
Now 1<=mi<ui, i.e, 1<=m1<=u1
1<=m1<=2
Therefore m1 = 1 or 2
S11 calculated as i = 1, j= 1 and m1 = 1
Reliability is calculated as ∅1(m1) = 1 – ( 1-r1)m1
= 1 – ( 1-0.9)1
= 1-(0.1)1
= 1-0.1 = 0.9
S11 = { ( 0.9 *1 , 30+0 ) }
= { ( 0.9,30) }
S12 calculated as i = 1, j= 2 and m1 = 2
Reliability is calculated as ∅1(m1) = 1 – ( 1-r1)m1
= 1 – ( 1-0.9)2
= 1-(0.1)2
= 1-0.01 = 0.99
S12 = { ( 0.99*1, 30*2+0) }
= { ( 0.99, 60 ) }
S1 can be obtained by merging S11, S12
S1 = { ( 0.9,30), ( 0.99, 60 ) }
Device 2 (D2 ) : How many copies has to purchase
Now 1<=mi<ui, i.e, 1<=m2<=u2
1<=m2<=3
Therefore m2 = 1 , 2 or 3
S21 calculated as i = 2, j= 1 and m2 = 1
Reliability is calculated as ∅2(m2) = 1 – ( 1-r2)m2
= 1 – ( 1 – 0.8)1
= 1- ( 0.2 )1
= 1-0.2 = 0.8
34
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
S21 = { ( 0.8*0.9,30+15), ( 0.8*0.99,60+15) }
= { ( 0.72,45), (0.792,75) }
S22 calculated as i = 2, j= 2 and m2 = 2
Reliability is calculated as ∅2(m2) = 1 – ( 1-r2)m2
= 1 – ( 1 – 0.8)2
= 1- ( 0.2 )2
= 1-0.04 = 0.96
S22 = { ( 0.96*0.9, 30+2*15), ( 0.96*0.99,60+2*15) }
= { ( 0.864,60) , ( 0.9504,90) }
S23 calculated as i = 2, j= 3 and m2 = 3
Reliability is calculated as ∅2(m2) = 1 – ( 1-r2)m2
= 1 – ( 1 – 0.8)3
= 1- ( 0.2 )3
= 1-0.008 = 0.992
S23 = { ( 0.992*0.9,30+3*15) , ( 0.992*0.99, 60+3*15) }
= { ( 0.8928, 75 ), ( 0.98208, 105 ) }
S2 can be obtained by merging S21, S22, S23
S2 = { ( 0.72,45), (0.792,75), ( 0.864,60) , ( 0.9504,90), ( 0.8928, 75 ), ( 0.98208, 105 ) }
To apply Purging Rule
(0.792,75), ( 0.864,60)
To remove (0.792,75) because cost are always increasing order
35
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
S31 calculated as i = 3, j= 1 and m3 = 1
Reliability is calculated as ∅2(m3) = 1 – ( 1-r3)m3
= 1 – ( 1 – 0.5)1
= 1- ( 0.5 )1
= 1-0.5 = 0.5
S31 = { ( 0.5*0.72, 45 + 20), ( 0.5*0.864+ 60+20), ( 0.5*0.8928, 75+20), ( 0.5*0.98208, 105+20 ) }
= { ( 0.36,65 ) , (0.432,80), (0.4464,95),(0.49104,125) }
To remove the Pair (0.49104,125), it exceeds 105
S31 ={ ( 0.36,65 ) , (0.432,80), (0.4464,95) }
S32 calculated as i = 3, j= 2 and m3 = 2
Reliability is calculated as ∅3(m3) = 1 – ( 1-r3)m3
= 1 – ( 1 – 0.5)2
= 1- ( 0.5 )2
= 1-0.25 = 0.75
S32 = { ( 0.75*0.72, 45 + 2*20), ( 0.75*0.864, 60+2*20), ( 0.75*0.8928, 75+2*20),
( 0.75*0.98208, 105+2*20) }
( 0.875*0.98208, 105+3*20 ) }
S33 = { ( 0.63,105 ) }
36
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
S3 can be obtained by merging S31, S32, S33
(0.4464,95), ( 0.54,85)
37
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM