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

Daa Unit III Dynamic Programming

Uploaded by

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

Daa Unit III Dynamic Programming

Uploaded by

Abhijith
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

UNIT-III

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)

Sub-solution for given problem.

2: Recursively identify the value of the optimal solution.

3: By using backtracking calculate the optimal solution.

4: Finalize the optimal solution from computing information.

Applications of Dynamic Programming

1: Optimal Binary Search Tree ( OBST)

2: All Pairs Shortest Path Problem

3 : Travelling Sales Person Problem.

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

Called optimal binary search tree.

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 )

w( 0,2 ) = p(2) + q(2) + w ( 0,1)


= 3 + 1 + 8 = 12
c(0,2) = min { c(0,0)+c(1,2), c(0,1)+c(2,2)} + w(0,2)
= min { 0 + 7 , 8 + 0 } + 12
= min { 7, 8 } + 12
= 7 + 12 = 19
r(0,2) = 1

( i =1, j= 3, k=2,3 )

w(1,3) = p(3) + q(3) + w( 1,2)


=1+1+7=9
c(1,3) = min{c(1,1)+c(2,3), c(1,2) +c(3,3) } + w(1,3)
=min { 0 + 3, 7 + 0 } + 9
= min { 3, 7 } + 9
= 3 + 9 = 12
r(1,3) = 2
( i =2, j= 4, k=3, 4 )
w(2,4) = p(4) + q(4) + w( 2, 3)
=1+1+3=5
c(2,4) = min { c(2, 3-1)+c(3,4), c(2,4-1)+c(4,4) } + w(2,4)
=min { c(2,2) +c(3, 4), c(2,3)+c(4,4) } + w(2,4)
= min { 0 + 3, 3 + 0 } + 5
=3+5=8
r(2,4) = 3
4
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
Step 4 : j – i = 3 , ( i =0, j= 3, k=1,2,3 )

w(0,3) = p(3) + q(3) + w(0,2)


= 1 + 1 + 12 = 14
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)
= min { 0 + 12, 8 + 3 , 19 + 0 } + 14
= min { 12, 11, 19 } + 14
= 11 + 14 = 25
r(0,3) = 2
( i =1, j= 4, k=2,3,4)

w(1,4) = p(4) + q(4) + w( 1,3)


= 1 + 1 + 9 = 11
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)
= min { 0 + 8, 7 + 3, 12 + 0 } + 11
= min { 8, 10, 12 } + 11
= 8 + 11 = 19
r(1,4) = 2
Step 5 : j – i = 4 (i =0, j= 4, k=1,2,3,4)

w(0,4) = p(4) + q(4) + w(0,3)


= 1 + 1 + 14 = 16
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)
=min{ 0 + 19, 8+8, 19+3 , 25+0} + 16
=min{19, 16, 21, 25 }+16
=16+16= 32
r(0,4) = 2

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

j–i=1 c ( 0,1 ) = 8 c (1, 2) = 7 c (2, 3) = 3 c ( 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

j–i=2 c ( 0, 2 ) = 19 c (1, 3 ) = 12 c (2, 4 ) = 8

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.

Hence a2 becomes root node.


r(i,j) = k

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(i,k-1) r(k,j)


r(0,0) = 0 r(1,1) = 0 r( 2,2) =0 r(3,4) = 4

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

Optimal Binary Search Tree with cost = 32

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 )

w( 0,2 ) = p(2) + q(2) + w ( 0,1)


= 4 + 4 + 7 = 15
c(0,2) = min { c(0,0)+c(1,2), c(0,1)+c(2,2)} + w(0,2)
= min { 0 + 10 , 7 + 0 } + 15
= min { 10, 7 } + 15
= 7 + 15 = 22
r(0,2) = 2
( i =1, j= 3, k=2,3 )

w(1,3) = p(3) + q(3) + w( 1,2)


= 2 + 1 + 10 = 13
c(1,3) = min{c(1,1)+c(2,3), c(1,2) +c(3,3) } + w(1,3)
=min { 0 + 7, 10 + 0 } + 14
= min { 7, 10 } + 13
= 7+13 = 20
r(1,3) = 2
( i =2, j= 4, k=3 )

w(2,4) = p(4) + q(4) + w( 2, 3)


=1+1+7=9
c(2,4) = min { c(2,2)+c(3,4)} + w(2,4)
=min{ 0 + 3 }+9 = 12
r(2,4) = 3

10
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
Step 4 : j – i = 3 , ( i =0, j= 3, k=1,2,3 )

w(0,3) = p(3) + q(3) + w(0,2)


= 2 + 1 + 15 = 18
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)
= min { 0 + 20, 7 + 7 , 22 + 0 } + 18
= min { 17,14,22} + 18
= 14 + 18 = 32
r(0,3) = 2
( i =1, j= 4, k=2,3,4)

w(1,4) = p(4) + q(4) + w( 1,3)


= 1 + 1 + 13 = 15
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)
= min { 0 + 12, 10 + 3, 20 + 0 } + 15
= min { 12, 13, 20 } + 16
= 12 + 15 = 27
r(1,4) = 2

Step 5 : j – i = 4 (i =0, j= 4, k=1,2,3,4)

w(0,4) = p(4) + q(4) + w(0,3)


= 1 + 1 + 18 = 20
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)
=min{ 0 + 27, 7+12, 22+3 , 32+0} + 21
=min{27, 19, 25, 32 }+20
=19+20= 39
r(0,4) = 2

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

j–i=1 c ( 0,1 ) = 7 c (1, 2) = 10 c (2, 3) = 7 c ( 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

j–i=2 c ( 0, 2 ) = 22 c (1, 3 ) = 20 c (2, 4 ) = 12

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.

Hence a2 becomes root node.


r(i,j) = k

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(i,k-1) r(k,j)


r(0,0) = 0 r(1,1) = 0 r( 2,2) =0 r(3,4) = 4

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

Optimal Binary Search Tree with cost = 39

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.

The shortest path can be computed using following recursive method.


Ak ( i, j ) = w ( i, j ), if k = 0
= min { Ak – 1 ( i, j ), Ak – 1 ( i, k ) + Ak – 1 ( k, j ) } , if 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

Weight matrix for this undirected graph is as follows.

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.

Travelling Sales person problem can be computed following recursive method.

1 : g(i, Φ ) = Ci , 1

2 : g(i, S) = min { Cij + g( j, S – { j } }

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.

Solution : The formula for solving this problem is,


1 : g(i, Φ ) = Ci 1
2 : g(i, S) = min { Ci j + g( j, S – { j } }
Step1 : Consider set of 0 elements, such that
S=Φ
g(i, Φ ) = Ci 1
g(1, Φ ) = C1 1 = ∞
g(2, Φ ) = C2 1 = 11
g(3, Φ ) = C3 1 = 4
g(4, Φ ) = C4 1 = 10

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( 2, {3} ) = min { c23 + g( 3, {3} – {3}) }


= min { c23 + g( 3, Φ ) }
= min { 13 + 4 } = 17.

g( 2, {4} ) = min { c24 + g( 4, {4} – {4}) }


= min { c24 + g( 4, Φ ) }
= min { 6 + 10 } = 16.

g( 3, {4} ) = min { c34 + g( 4, {4} – {4}) }


= min { c34 + g( 4, Φ ) }
= min { 18 + 10 } = 28.

g( 3, {2} ) = min { c32 + g( 2, {2} – {2}) }


= min { c32 + g( 2, Φ ) }
= min { 9 + 11 }
= 20.

g( 4, {2} ) = min { c42 + g( 2, {2} – {2}) }


= min { c42 + g( 2, Φ ) }
= min { 3 + 11 } = 14.

g( 4, {3} ) = min { c43 + g( 3, {3} – {3}) }


= min { c43 + g( 3, Φ ) }
= min { 2 + 4 } = 6.

Step 3 : Consider set of 2 elements, such that

S =2, { 2,3} { 2,4 }, { 3,4}

g(i, S) = min { Ci j + g( j, S – { j }) }

g( 2, { 3,4 } ) = min { c23 + g( 3, {3,4 } – {3}) , c24 + g( 4, {3,4 } – {4}) }


= min { c23 + g( 3, {4}), c24 + g(4, {3}) }
= min { 13 + 28, 6 + 6 }
= min { 41, 12 }
= 12.

g( 3, { 2,4 } ) = min { c32 + g( 2, {2,4 } – {2}) , c34 + g( 4, {2,4 } – {4}) }


= min { 9 + g( 2, {4}), 18 + g(4, {2}) }
= min { 9 + 16, 18 + 14 }
= min { 25, 32 }
= 25.

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.

Step 4: Consider set of 4 elements, such that

S = 3, { 2,3,4 }

g( 1, { 2,3,4 } ) = min{ c12 + g( 2,{2,3,4 }–{2}) , c13 + g( 3,{2,3,4}–{3}), c14 + g( 4,{2,3,4}–{4}) }


= min { c12 + g( 2, {3,4}), 5 + g(3, {2,4}), 7 + g(4, {2,3}) }
= min { 12 + 12, 5 + 25, 7 + 20 }
= min { 24, 30, 27 }
= 24

The optimal Solution is : c12 + g ( 2, { 3,4 } ),


c12 + c24 + g(4, {3}),
c12 + c24 + c43 + g( 3, Φ ),
c12 + c24 + c43 + C3 1

The optimal Cost : 1-2-4-3-1 = 12 + 6 + 2 + 4 = 24

23
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
Example 2 : Construct an optimal travelling sales person tour using Dynamic Programming.

Solution : The formula for solving this problem is,


1 : g(i, Φ ) = Ci 1
2 : g(i, S) = min { Ci j + g( j, S – { j } }
Step1 : Consider set of 0 elements, such that
S=Φ
g(i, Φ ) = Ci 1
g(1, Φ ) = C1 1 = 0
g(2, Φ ) = C2 1 = 5
g(3, Φ ) = C3 1 = 6
g(4, Φ ) = C4 1 = 8
Step2 : Consider set of 1 elements, such that
S = 1, {2} , { 3 } , { 4 }
g(i, S) = min { Ci j + g( j, S – { j }) }

g(2,{3}) = min { c23 + g( 3, { 3 } – { 3 } ) }


= min { c23 + g( 3, Φ ) }
= min { 9 + 6 } = 15
g(2,{4}) = min { c24 + g( 4, { 4 } – { 4 } ) }
= min { c24 + g( 4, Φ ) }
= min { 10 + 8 } = 18
g(3,{2}) = min { c32 + g( 2, { 2 } – { 2 } ) }
= min { c32 + g( 2, Φ ) }
= min { 13 + 5 } = 18
g(3,{4}) = min { c34 + g( 4, { 4} – { 4 } ) }
= min { c34 + g( 4, Φ ) }
= min { 12 + 8 } = 20
g(4,{2}) = min { c42 + g( 2, { 2 } – { 2 } ) }
= min { c42 + g( 2, Φ ) }
= min { 8 + 5 } = 13
g(4,{3}) = min { c43 + g( 3, { 3 } – { 3 } ) }
= min { c43 + g( 3, Φ ) }
= min { 9 + 6 } = 15
24
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
Step 3 : Consider set of 2 elements, such that

S =2, { 2,3} { 2,4 }, { 3,4}

g(i, S) = min { Ci j + g( j, S – { j }) }

g(2,{3,4}) = min { c23 + g( 3,{3,4} – { 3 }), c24 + g( 4,{3,4}- { 4 }) }


= min { c23 + g ( 3, { 4 } ), c24 + g ( 4 , { 3 } ) }
= min { 9 + 20, 10 + 15 }
= min { 29 , 25 }
= 25

g(3,{ 2,4}) = min { c32 + g( 2,{2,4} – { 2 }), c34 + g( 4,{2,4}- { 4 }) }


= min { c32 + g( 2, { 4 } ), c34 + g( 4, { 2 } ) }
= min { 13 + 18, 12 + 13 }
= min { 31,25 }
= 25

g(4,{2,3}) = min { c42 + g( 2,{2,3} – { 2 }), c43 + g( 3,{2,3}- { 3 }) }


=min { c42 + g( 2, { 3 } ), c43 + g( 3, { 2 } ) }
= min { 8 + 15 , 9 + 18 }
= min {23, 27}
= 23

Step 4: Consider set of 3 elements, such that

S = 3, { 2,3,4 }

g(i, S) = min { Ci j + g( j, S – { j }) }

g(1, { 2,3,4 } ) = min { c12 + g ( 2, { 2,3,4 } – { 2 } ), c13 + g ( 3, { 2,3,4 } – { 3 } ), c14 + g ( 4, { 2,3,4 } – { 4 } ) }


= min { c12 + g ( 2, { 3,4 } ), c13 + g ( 3, { 2,4 } ), c14 + g ( 4, { 2,3 })}
= min { 10 + 25, 15 + 25, 20 + 23 }
= min { 35, 40, 43 }
= 35
The optimal Solution is : c12 + g ( 2, { 3,4 } ),
c12 + c24 + g ( 4 , { 3 } ),
c12 + c24 + c43 + g( 3, Φ ),
c12 + c24 + c43 + c3 1

The optimal Cost: 1-2-4-3-1 =10 + 10 + 9 + 6 = 35

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.

0 /1 Knapsack Problem can be computed following recursive method.

1. Initially s0 = {(0,0)} ( P,W)


2. Merging Operation
Si + 1 = Si + Si1
3. Purging rule ( OR ) dominance rule
If Si + 1 contains (Pj , Wj) and (Pk, Wk) these two pairs such that Pj<=Pk and Wj>=Wk, then
(Pj ,Wj) can be eliminated . This purging rule is also called as dominance rule. In purging rule
basically the dominated tuples gets purged. In short remove the pair with less profit and more
weight.

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

Solution : we have to build the sequence of decisions S0, S1, S2, S3

Initially S0 = { ( 0,0 ) }

S01 = Select next ( p1,w1) pair and add it with S0


= ( 1,2 ) + { ( 0,0 ) }
= { ( 1+0), ( 2 + 0 ) } = { ( 1,2 ) }
Si+1= Si ∪ Si1
S1 = S0 + S01
= { ( 0,0 ) } ∪ { ( 1,2 ) }
= { ( 0,0 ), ( 1,2 ) }
To apply Purging Rule : There will be no deleted
S11 = Select next (P2,W2) pair and add it with S1
= ( 2,3 ) + { ( 0,0 ), ( 1,2 ) }
= { ( 2 + 0, 3 + 0 ), ( 2 + 1, 3 + 2 ) }
= { ( 2,3 ) , ( 3,5 ) }
S2 = S1 ∪ S11
= { ( 0,0 ), ( 1,2 ) } ∪ { ( 2,3 ) , ( 3,5 ) }
= { ( 0,0 ) , (1,2), (2,3),(3,5) }
To apply Purging Rule : There will be no deleted
S21 = Select next (P3,W3) pair and add it with S2
= ( 5,4 ) + { ( 0,0 ) , (1,2), (2,3),(3,5) }
= { ( 5+0,4+0),(5+1,4+2),(5+2,4+3),(5+3,4+5)}
= { ( 5,4 ),(6,6), (7,7), (8,9) }
S3 = S2 ∪ S21
= { ( 0,0 ) , (1,2), (2,3),(3,5) } ∪ { ( 5,4 ),(6,6), (7,7), (8,9) }
= { (0,0),(1,2),(2,3),(3,5),(5,4),(6,6),(7,7),(8,9) }

To apply Purging Rule


( 3,5) and ( 5,4 )
(pj,wj) and ( pk,wk)

Pj<=pk and wj>=wk


3<=5 and 5>=4 -- True, ( pj,wj) pair can be deleted ( 3,5)

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) }

Here M = 6, we will find the tuple, denoting the weight ‘ 6 ‘

i,e ( 6,6 ) belongs to S3

( 6,6 ) does not belongs to S2

Therefore , We must set x3 = 1

The pair ( 6,6 ) came from the pair ( 6-p3,6-w3)

( 6-5, 6-4) = ( 1,2 )

Here ( 1,2 ) belongs to S2

( 1,2 ) belongs to S1

Therefore , We must set x2 = 0

( 1,2 ) does not belongs to S0

Therefore , We must set x1 = 1

Hence an Optimal Solution is ( x1 , x2, x3 ) = ( 1, 0 , 1 )

Maximum Profit = p1x1+p2x2+p3x3


= 1*1+2.0+5.1
= 1+0+5=6
Maximum Weight = w1x1+w2x2+w3x3
= 2*1+3.0+4.1
= 2+0+4=6

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 ) }

S01 = Select next ( p1,w1) pair and add it with S0


= ( 1,2 ) + { ( 0,0 ) }
= { ( 1+0), ( 2 + 0 ) } = { ( 1,2 ) }
Si+1= Si ∪ Si1
S1 = S0 ∪ S01
= { ( 0,0 ) } ∪ { ( 1,2 ) }
= { ( 0,0 ), ( 1,2 ) }
To apply Purging Rule : There will be no deleted
S11 = Select next (P2,W2) pair and add it with S1
= ( 2,3 ) + { ( 0,0 ), ( 1,2 ) }
= { ( 2 + 0, 3 + 0 ), ( 2 + 1, 3 + 2 ) }
= { ( 2,3 ) , ( 3,5 ) }
S2 = S1 ∪ S11
= { ( 0,0 ), ( 1,2 ) } ∪{ ( 2,3 ) , ( 3,5 ) }
= { ( 0,0 ) , (1,2), (2,3),(3,5) }
To apply Purging Rule : There will be no deleted
S21 = Select next (P3,W3) pair and add it with S2
= ( 5,4 ) + { ( 0,0 ) , (1,2), (2,3),(3,5) }
= { ( 5+0,4+0),(5+1,4+2),(5+2,4+3),(5+3,4+5)}
= { ( 5,4 ),(6,6), (7,7), (8,9) }

29
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
S3 = S2 ∪ S21

= { ( 0,0 ) , (1,2), (2,3),(3,5) } ∪ { ( 5,4 ),(6,6), (7,7), (8,9) }


= { (0,0),(1,2),(2,3),(3,5),(5,4),(6,6),(7,7),(8,9) }
To apply Purging Rule
( 3,5) and ( 5,4 )
(pj,wj) and ( pk,wk)

Pj<=pk and wj>=wk


3<=5 and 5>=4 -- True, ( pj,wj) pair can be deleted ( 3,5)

S3 = { (0,0),(1,2),(2,3),(5,4),(6,6),(7,7),(8,9) }

S31 = Select next (P4,W4) pair and add it with S3


= ( 6,5 ) + { (0,0),(1,2),(2,3),(5,4),(6,6),(7,7),(8,9) }
= { ( 6+0,5+0), (6+1,5+2),(6+2,5+3),(6+5,5+4),(6+6,5+6), ( 6+7 , 5+7),(6+8 , 5+9) }
= { ( 6,5 ),( 7,7) , (8,8), (11,9), (12,11), ( 13,12 ), ( 14,14) }
S4 = S3 ∪ S31
= { (0,0),(1,2),(2,3),(5,4),(6,6),(7,7),(8,9) } ∪ { ( 6,5 ),( 7,7) , (8,8), (11,9), (12,11), ( 13,12 ), ( 14,14) }
= { (0,0),(1,2),(2,3),(5,4), ( 6,5 ) ,(6,6),(7,7), (8,8), (8,9), (11,9), (12,11), ( 13,12 ), ( 14,14) }

To apply Purging Rule : There will be no deleted

Here Capacity of the Knapsack is 8, Now we have to remove the pairs, in which wi>m, i.e wi>8

Therefore S4 = { (0,0),(1,2),(2,3),(5,4), ( 6,5 ) ,(6,6),(7,7), (8,8) }

Here M = 8, we will find the tuple, denoting the weight ‘ 8 ‘

i,e ( 8,8 ) belongs to S4

( 8,8 ) does not belongs to S3

Therefore , We must set x4 = 1

The pair ( 8,8 ) came from the pair ( 8-p4,8-w4)

( 8-6, 8-5) = ( 2,3 )

Here ( 2,3 ) belongs to S3

( 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

Therefore , We must set x2 = 1

The pair ( 2,3 ) came from the pair ( 2-p2,3-w2)


( 2-2, 3-3 ) = ( 0,0 )
Here ( 0,0 ) belongs to S1
( 0,0 ) belongs to S0
Therefore , We must set x1 = 0

Hence an Optimal Solution is ( x1 , x2, x3, x4 ) = ( 0, 1 , 0, 1 )

Maximum Profit = p1x1+p2x2+p3x3+p4x4


= 1.0+2.1+5.0 + 6.1
= 0+2+0+6=8
Maximum Weight = w1x1+w2x2+w3x3+w4x4
= 2.0+3.1+4.0+5.1
= 0+3+0+5=8

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

Therefore S2 = { ( 0.72,45), ( 0.864,60) , ( 0.9504,90), ( 0.8928, 75 ), ( 0.98208, 105 ) }

To remove ( 0.9504,90), because cost are always increasing order

Therefore S2 = { ( 0.72,45), ( 0.864,60) , ( 0.8928, 75 ), ( 0.98208, 105 ) }

Device 3 (D3 ) : How many copies has to purchase


Now 1<=mi<ui, i.e, 1<=m3<=u3
1<=m3<=3
Therefore m3 = 1 , 2 or 3

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) }

S32 = { ( 0.54,85), ( 0.648,100 ), ( 0.6696, 115) , ( 0.73656,145 ) }

To remove the Pairs ( 0.6696, 115) , ( 0.73656,145 ) , because it exceeds 105

S32 = { ( 0.54,85), ( 0.648,100 ) }

S33 calculated as i = 3, j= 3 and m3 = 3


Reliability is calculated as ∅3(m3) = 1 – ( 1-r3)m3
= 1 – ( 1 – 0.5)3
= 1- ( 0.5 )3
= 1-0.125 = 0.875
S33={ ( 0.875*0.72, 45+3*20 ),( 0.875*0.864, 60+3*20 ),( 0.875*0.8928, 75+3*20 ),

( 0.875*0.98208, 105+3*20 ) }

S33 = { ( 0.63,105 ) , ( 0.75,120) , ( 0.7812,135) , ( 0.8575,165 ) }

To remove the Pairs ( 0.75,120) , ( 0.7812,135) , ( 0.8575,165 ), because it exceeds 105

S33 = { ( 0.63,105 ) }

36
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM
S3 can be obtained by merging S31, S32, S33

S2 = { ( 0.36,65 ) , (0.432,80), (0.4464,95), ( 0.54,85), ( 0.648,100 ), ( 0.63,105 ) }

To apply Purging Rule

(0.4464,95), ( 0.54,85)

To remove (0.4464,95), because cost are always increasing order

Therefore S3 = { ( 0.36,65 ) , (0.432,80), ( 0.54,85), ( 0.648,100 ), ( 0.63,105 ) }

Now remove the pair ( 0.63,105 )

Therefore S3 = { ( 0.36,65 ) , (0.432,80), ( 0.54,85), ( 0.648,100 ) }

The best design is 0.648 is the reliability with cost 100


So, ( 0.648,100 ) is present in S32
Here i =3, j = 2 , m3 =2
( 0.648,100 ) can be obtained from ( 0.864,60 ) which is present in S22
Here i =2, j = 2 , m2 =2
( 0.864,60 ) can be obtained from ( 0.9,30 ) which is present in S11
Here i =1, j = 1 , m1 =1
Therefore
m1 =1
m2= 2
m3 =2
We require 1 copy of device D1
2 copy of device D2
2 copy of device D3

37
CH SUBBAREDDY_DAA NOTES – III YEAR II SEM

You might also like