DAA U-4 Dynamic Programming
DAA U-4 Dynamic Programming
DAA U-4 Dynamic Programming
Ø It is used when the solution to a problem can be viewed as the result of a sequence of
decisions. It avoid duplicate calculation in many cases by keeping a table of known
results and fills up as sub instances are solved.
Ø In DynamicProgramming We usually start with the smallest and hence the simplest
sub- instances.by combining their solutions, we obtain the answers to sub-instances of
increasing size, until finally we arrive at the solution of the original instances.
Ø It follows a bottom-up technique by which it start with smaller and hence simplest sub
instances. Combine their solutions to get answer to sub instances of bigger size until we
arrive at solution for original instance.
Ø Dynamic programming differs from Greedy method because Greedy method makes
only one decision sequence but dynamic programming makes more than one decision
However, sequences containing sub-optimal sub-sequences can not be optimal and so
will not be generated.
Ø When a problem is solved by divide and conquer, we immediately attack the complete
instance, which we then divide into smaller and smaller sub-instances as the algorithm
progresses.
Ø The difference between Dynamic Programming and Divide and Conquer is that the sub
problems in Divide and Conquer are considered to be disjoint and distinct where as in
Dynamic Programming they are overlapping.
Principle of Optimality
Ø An optimal sequence of decisions has the property that whatever the initial state and
decisions are, the remaining decisions must constitute an optimal decision sequence
with regard to the state resulting from the first decision.
Ch Murty
Ø i.e principle of optimality is satisfied when an optimal solution is found for a
problem then optimal solutions are also found for its sub-problems also.
General Method
1. The structure of the solution to be charactirized.
2. Representing the optimal solution wherever needed in the sub-problem(defining
recursively)
3. The optimal solution has to be costructed from the information
4. The final solution has to be costructed from the information
cost(i, i) = 0,
cost(i, j) is the length of the edge <i, j> if <i, j> ∈ E and
The graph allows edges with negative cost value, but negative valued cycles are not
allowed. All pairs shortest path problem is to find a matrix A such that A[i][j] is the
length of the shortest path from i to j.
Consider a shortest path from i to j, i≠j. The path originates at i goes through possibly
many vertices and terminates at j. Assume that there is no cycles. If there is a cycle we
can remove them without increase in the cost.Because there is no cycle with negative
cost.
Algorithm makes n passes over A. Let A0, A1, .. An represent the matrix on each pass.
Let Ak-1[i,j] represent the smallest path from i to j passing through no intermediate
vertex greater than k-1. This will be the result after k-1 iterations. Hence kth iteration
explores whether k lies on optimal path. A shortest path from I to j passes through no
vertex greater than k, either it goes through k or does not.
If it does not, then no intermediate vertex has index greater than k-1. Then Ak[i,j] = Ak-1[i,j]
Combining these two conditions we get
Algorithm
A[i][j] = cost[i][j];
APPLICATION:
1. Suppose we have to route a postal van to pick up mail from the mail boxes located at
‘n’ different sites.
2. An n+1 vertex graph can be used to represent the situation.
3. One vertex represents the post office from which the postal van starts and return.
4. Edge <i,j> is assigned a cost equal to the distance from site ‘i’ to site ‘j’.
5. the route taken by the postal van is a tour and we are finding a tour of minimum
length.
6. every tour consists of an edge <1,k> for some k ∈ V-{} and a path from vertex k to
vertex 1.
7. the path from vertex k to vertex 1 goes through each vertex in V-{1,k} exactly once.
8. the function which is used to find the path is
g(1,V-{1}) = min{ cij + g(j,s-{j})}
1. Find g(i,Φ) =ci1, 1<=i<n, hence we can use equation(2) to obtain g(i,s) for all s to
size 1.
2. That we have to start with s=1,(ie) there will be only one vertex in set ‘s’.
3. Then s=2, and we have to proceed until |s| <n-1.
4. for example consider the graph.
G (i,s) set of nodes/vertex have to visited.
starting position
s = 0.
i =1 to n.
s =1
i =2 to 4
= 9+6 =15
= 10+8 =18
= 13+5 =18
= 12+8 =20
= 8+5 =13
g(4,{3}) = c43 + g(3,Φ)
= 9+6 =15
s =2
i ≠ 1, 1∈ s and i ∈ s.
g(2,{3,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{4}),c34+g(4,{2})}
min{13+18,12+13}
min{31,25}
=25
g(4,{2,3}) = min{c42+g(2{3}),c43+g(3,{2})}
min{8+15,9+18}
min{23,27}
=23
s = 3
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}
min{35,35,43}
=35
optimal cost is 35
0/1 KNAPSACK
The 0/1 knapsack problem is similar to the knapsack problem as in the Greedy method
expect that the xi’s are restricted to have a values either 0 or 1( the decisions on me xi are
made in the order xn,xn-1 …x1 following a decision on xn, we may be in one of two possible
states the capacity remaining in the knapsack is m and no profit has accrued or the capacity
remaining is m-wn and profit of pn has accrued) let fi(y) be the value of optimal solution to
KNAP (i,j,m) we can represent the 0/1 knapsack problem as
The 0/1 knapsack problem is KNAP( l,n,m) The principal of optimality holds in the
solution of 0/1 knapsack problem in dynamic programming approach The dynamic
programming technique can also be used to represent the solution starts in tuples .Each tuple
contains member (pi,wi)a where pi is the profit earned on object i and wi is the weight of me
object i, one solution to the knapsack problem can be obtained by making sequence of
decisions on one var x1,x2 ….xn.
The capacity of knapsack remains the same and no profit is gained or capacity of knapsack
remains M-wn and a profit of pn is occurred .It is clear that the remain decision must be
optimal w.r.t problem state resulting from the decision on xn….x1 will not be optimal
Where Si is the set of all pairs for fi including (0,0) and fi is completely defined by the pairs
(pi,wi)
Si may obtained by mergeing Si and S1 i. This merge corresponding to taking the make of
two functions fi-1(x) and fi-1(x-wi)+pi in the object function of 0/1 Knapsack problem
So if one of Si-1 and S1i has a pair (pj, wj) and the other has a pair (pk, wk) and pj≤pk white
wj≤wk. Then the pair (pj ,wj) is discarded this rule is called purging or dominance rule when
generating Si all me pairs (p,w)with w>m may also be purged
Si 1 = {(pi , wi) / (p-pi , w-wi)€ Si }
If we try to remove ith item then profit of ith item is reduced from total profit and weight of
ith item is reduced from total weight which belongs to the profit and weight of (i-1)th item
S00= {0, 0}
Take any 2 sets Si and Si 1 consisting of pairs(Pj , Wj) and (Pk ,Wk) The purging rule states
that if Pj≤ Pk and Wj ≥ Wk then (Pj, Wj) will be deleted.
To find optimal sol by performing searching process for knowing values based on.
(P3,W3) = (4,3)
Addition :
S1 1 = S0 + {P1, W1)
S1 1 = {(0,0)} + {(1,2)}
S1 1 = {(1,2)}
Merging operation :
S1 = Si-1 + S1 1
no deletion of tuple
= {(0,0),(1,2)}+{2,3)}
S12 = {(2,3),(3,5)}
S2 = S1 U S12 = {(0,0)(1,2)}U{(2,3),(3,5)}
S2 = {(0,0),(1,2),(2,3),(3,5)}
No deletion
= {(0,0),(1,2),(2,3),(3,5)}+{(4,3)}
S1 3 = {(4,3),(5,5),(6,6),(7,8)}
= {(0,0),(1,2),(2,3),(3,5)}U{(4,3),5,5),(6,6),(7,8)}
={(0,0),(1,2),(2,3),(3,5),(4,3),(5,5),(6,6),(7,8)}
So. S3 ={(0,0),(1,2),(2,3),(3,5),(4,3),(5,5),(6,6)
S3 = {(0,0),(1,2),(2,3),(4,3),(5,5),(6,6)}
S3 = {(0,0),(1,2),(4,3),(5,5),(6,6)}
(6, 6) - (4,3)=(2,3)
Hence x3=1
(2, 3) € S2
(2,3) - (2,3)=(0,0)
Here we have to multiply ‘n’ matrices A1, A2, A3……An, which can be
accomplished by a series of matrix multiplication.
A1*A2*A3*……*An,
For ex,
Consider 3 matrices
Multiplication cost of
((AB)C)=(5x4x3)+(5x3x5)=135.
Multiplication cost of
(A(BC))=(4x3x5)+(5x4x5)=160.
Therefore, we can see from above that different evalution sequence implies
different cost of opreation.
Let Ai-j denote the multiplication of matrices through i to j and the order of
Ai-j is ri-1 x rj.
We make a no of decitions, our job is to break the problem into several no of
sub-problem of same kind as the original problem and start the parenthasizing
the subsequences as the situation needs. Let us consider the highest level of
How we can split the order of matrices? And how can we parenthasizing the
N[i,k] and N[k+1,j] are the optimum multiplication needed to compute Ai..k and
Ak+1…j
Since ri-1 * rk is the order of Ai..k and rk * rj is the order of Ak+1..j, the total no of
operations needed to multiply Ai..k and Ak+1..j is ri-1*rk.rj
Algorithem MATRIXMUL(A)
Begin
for iß1 to n do
for bß2 to n do
Jßi+b-1
N[i,j]ß
N[i.j]ß Min[N[i,k]+N[k+1,j]+ri-1,rk,rj]
end
end
end
end MATRIXMUL
ALGORITHM:
Procedure cmatrix(n,d[0..n])
if(s= 0)
for I=1 to n do
m(i,j) =0
if(s==1)
m(i,i+1) =d(i-1)*d(i)*d(i+1)
else
m=∝
if (min>[m(i,k) +m(k+1,i+s)+d(i-1)*d(k)*d(i+s)])
min=[m(i,k) +m(k+1,i+s)+d(i-1)*d(k)*d(i+s)]
m(i,i+s) =min
Reliability Design
The reliability design is to design a system that is composed of several devices connected in
series
D1 D2 D3 Dn
Let ri be the reliability of device Di (i.e ri is the probability that device will work properly)
then
The reliability of the entire system is ∏ ri . even the individual device are very reliable (i.e ri
are very close to one) the reliability of the system may not be very good.
For Ex : if n=10 and ri =0.99 1<=i<=10 then ∏ ri =0.904 Hence it is desirable to maintain
duplicate devices and multiple copies of the same device type are connected in parallel
through the use of switching Circuits
D1 D2 D3 Dn
D1 D3 Dn
D1 D2 D3
D3 Dn
If stage c contain mi copies of device Di from the probability that all mi have a malfunction
is (1- ri)mi
1<=i<=n
Our goal is to use device duplication to maximize reliability under a cost constraint
let Ci be the cost of each unit of device i and let C be the max allowable cost of the system
being designed
Maximize ∏ Φi (mi )
1<=i<=n
1) For each ci >0 each mi must be in the range 1<= mi <ui where
The dominance rule (f1,x1 ) dominanates(f2,x2) iff f1>=f2 and x1<=x2 holds.then
the dominated tuples can be discarded from Si .
We begin with S0 ={(1,0)} we can obtain Si form Si-1 by trying all possible
values for mi and combining the resulting tuples together.
problem
Let the costs of a 3 stage system with device types D1,D2 and D3 then costs are
Rs30, Rs15, Rs 20 respectively.the cost of the system is to be done no more than
$105 the reliability of each device is 0.9,0.8,0.5 respectivly
Sol :
u1 =[(105+30-65)/30]
=2.3
=2
u2 =[(105+15-65)/15]
=3.6
=3
U3=[(105+20-65)/20]
=3
Step2:
S0 ={(1,0)}
Φ1m1 =1-(1-r1)1=(1-(1-0.9)=0.9
r = (1-0.8)2 0.99*0.96
= 0.96
={(0.864,60) , (0.95,90)}
discarded
(0.95 , 90) has been eliminated because mis leaves only Rs 10 .This is not to allow
m3=1
r = (1- (1-0.8)3
= 1 – (0.2)3
= 1 – 0.008 = 0.992
105 reached but D3 is not considered until now so for consideration discard it
By Dominance Rule
r = (1-(1-0.5 )2
= 1 - 0.25
= 0.75
S2 3 = {0 .54 , 85) , (0.648,100)
=1 – (1-0.5)3
= 1- 0.125
=0.875
S3 3 = (0.63,105)
By consider me type with max reliability & min cost following the method of back
tracking we can datamin me total no of individual copies
if the following are Two Binary Search Tree B S T For given set S The assumption are
each word has same probability no unsucrssful search
for
for
do int
do
whil
if whil
int
if
(a) (b)
We require 4 comparisons to find out an identifier in worst case in fig(a).But in fig (b) only
3 compressing on avg the two trees need 12/5 ,11/5 comparisons
In General, we can consider different words with different frequencies (probabilities) and un
successful searches also
Let Given set of words are {a1, a2, a3,…….an} With a1< a2 <….. an
∑p(i) + ∑q(i) =1
1≤i≤n 0≤i≤n
for
for
do int
do whil
int
if whil
if
If BST represents ‘n’ identifiers then there will be exately ‘n ‘ internal nodes and n+ 1
external nodes every internal node represent a point where successful search may terminate
and every external node represents a point where an unsuccessful search may terminates
For unsuccessful search terminate at external node the words which are not in BST can be
partioned into n+1 Equalnce classes. Equvalence class E0 represents least among all the
identifers in BST& The equalence classes ‘En’ represents greatest among all Elements in
BST Hence search terment at level Ei-1
1≤i≤n 0≤i≤n
Let P(i)=q(i)=1/7, for all i ;in me BST The following word set(a1,a2,a3)= (do,it,while)
do
Whil if
if do Whil if
Whil
do
do
While
do While
if
if
(d) (e)
= 15/7
Cost (b) = 1/7*1+1/7*2+1/7*2+1/7*2+1/7*2+1/7*2+1/7*2
= 13/7
Tree b is optimal
cost(true a)=2.05
+3*0.15+3*0.11+2*0.05 +1 * 0.05