Dynamic Programming
Dynamic Programming
In the worst case, the tree of Figure 5.12 (a) requires four
comparisons to find an identifier, whereas the tree of Figure
5.12 (b) requires only three.
On the average the two trees need 12/5 and 11/5
comparisons.
In the case of tree (a), it takes 1,2,2,3, and 4 comparisons,
respectively, to find the identifiers for, do, while, int and if.
Thus the average number of comparisons is
(1+2+2+3+4)/5=12/5.
In the case of tree (b), it takes 1, 2, 3, 2, and 3 comparisons,
respectively, to find the identifiers for, do, while, int and if.
Thus the average number of comparisons is
(1+2+3+2+3)/5=11/5.
This calculation assumes that each identifier is searched for
with equal probability and that no unsuccessful searches
(i.e., searches for identifiers not in the tree) are made.
Generally, different identifiers are searched for with
different frequencies (or probabilities).
In addition, unsuccessful searches also are made.
Let us assume that the given set of identifiers is {a1 , a2 , … , a n }
witha 1< a2 <…< an. Let p(i) be the probability with which we
search for a i .Let q ( i )be the probability that the identifier x
being searched for is such that a i< x < ai+1 , 0 ≤i ≤n (assume a 0=−∞
and a n+1=+ ∞)
0∑≤i ≤ n
q (i)
is the probability of unsuccessful searches and
∑ p ( i ) is the probability of successful searches.
1 ≤i ≤n
Clearly, 1∑
≤i ≤n
p ( i ) + ∑ q(i)=1
0 ≤i ≤n
¿ 1+ ∑ p ( i )∗depth ( a i ) + ∑ q ( i )∗depth ( Ei )
1 ≤i ≤n 0 ≤i ≤n
To apply dynamic programming to the problem of obtaining an
optimal binary search tree, we need to view the construction of
such a tree as the result of a sequence of decisions and then
observe that the principal of optimality holds when applied to the
problem state resulting from a decision.
A possible approach would be to make a decision as to which of
the a 'i sshould be assigned to the root node of the tree.
If we choose a k , then the internal nodes a 1 , a2 , … , ak−1 as well as the
external nodes for the classes E0 , E1 , … , E k−1 will lie in the left sub
tree and the remaining nodes a k+1 , ak +2 , … , a n and E k , Ek +1 , … , En
We define,
cost ( l )= ∑ p (i )∗(depth ( a i ) +1)+ ∑ q ( i )∗(depth ( Ei ) +1)
1 ≤i< k 0 ≤ i<k
Let
j j
w (i , j )=∑ p l+ ∑ ql
l =i l =i−1
Thus, if a kis the root of the tree, the cost of the resulting tree is:
c (0 , n)= p ( k )+ c ( 0 , k −1 ) +c ( k , n ) +w ( 0 , k −1 ) +w (k , n)
In order to construct an optimal binary search tree, we must
choose k such that c ( 0 , n )is minimum. That is,
c ( 0 , n )= min { p ( k ) +c ( 0 , k−1 ) +c ( k , n ) +w ( 0 , k−1 ) + w ( k , n ) }
1 ≤ k≤ n
More generally,
c ( i, j ) =min { c ( i , k−1 ) +c ( k , j ) + p ( k ) + w (i , k −1 )+ w ( k , j ) }
i≤ k ≤ j
c ( i , j ) =min { c ( i , k−1 ) +c ( k , j ) +w (i , j) }
i≤ k ≤ j
Initially,
w ( 0 , 1 )= p ( 1 ) +q ( 1 ) + w ( 0 , 0 )=8
c ( 0 , 1 )=w ( 0 ,1 ) +min { c ( 0 , 0 ) +c ( 1 ,1 ) } =8
r ( 0 ,1 ) =1
w ( 1 ,2 ) =p (2 )+ q ( 2 ) +w ( 1 , 1 )=7
c ( 1 ,2 ) =w ( 1 ,2 )+ min {c ( 1 ,1 ) +c ( 2 ,2 ) }=7
r ( 1 ,2 )=2
w ( 2 , 3 )= p ( 3 ) +q ( 3 )+ w ( 2 , 2 )=3
c ( 2 ,3 )=w ( 2 , 3 ) +min { c ( 2 ,2 ) + c ( 3 , 3 ) }=3
r ( 2 , 3 )=3
w ( 3 , 4 )= p ( 4 ) +q ( 4 ) + w ( 4 , 4 )=3
c ( 3 , 4 )=w ( 3 , 4 ) +min {c ( 3 , 3 ) +c ( 4 , 4 ) }=3
r ( 3 , 4 ) =4
w ( 0 , 2 )= p ( 2 ) +q ( 2 )+ w ( 0 , 1 )=12
c ( 0 , 2 )=w ( 0 ,2 ) +min { c ( 0 , 0 ) +c ( 1 ,2 ) , c ( 0 ,1 ) + c ( 2 , 2 ) }=12+min { 7 , 8 } =19
r ( 0 , 2 )=1
w ( 1, 3 ) =p ( 3 )+ q ( 3 ) +w (1 , 2 )=9
c ( 1 ,3 )=w ( 1 ,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( 3 ,3 )=0 c ( 4 , 4 ) =0
r ( 0 , 0 )=0 r ( 1 ,1 ) =0 r ( 2 ,2 )=0 r ( 3 , 3 )=0 r ( 4 , 4 )=0
w ( 0 ,3 )=14 w (1 , 4 )=11
c ( 0 , 3 )=25 c ( 1 , 4 )=19
r ( 0 , 3 )=2 r ( 1 , 4 )=2
w ( 0 , 4 ) =16
c ( 0 , 4 )=32
r ( 0 , 4 )=2
Notice that 0
S ={( 0 , 0 ) }
i
S1={(P , W )∨(P− pi ,W −wi)∈ S }
i
(3)
Now, S can be computed by merging the pairs in
i +1 i
S ∧S 1
i
together.
Note that if S contains two pairs ( P j , W j )and ( Pk , W k ) such
i +1
S = { ( 0 , 0 ) } ; S 1= {( 1 , 2 ) }
0 0
S 1= {( 0 ,0 ) , ( 1 , 2 ) } ; S 11={ ( 2 ,3 ) , ( 3 , 5 ) }
S2= { ( 0 , 0 ) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 ,5 ) } ; S21 ={ ( 5 , 4 ) , ( 6 , 6 ) , ( 7 , 7 ) , ( 8 , 9 ) }
S = { ( 0 , 0 ) , ( 1 , 2 ) , ( 2 , 3 ) , ( 5 , 4 ) , ( 6 , 6 ) , ( 7 ,7 ) , ( 8 ,9 ) }
3
Note that the pair( 3 , 5 )has been eliminated from S3as( 5 , 4 ) dominates
( 3 , 5)
Withm=6 , the value of f 3 ( 6 )is given by the tuple( 6 , 6 ) in S3; the tuple
( 6 , 6 ) ∉ S 2, and so we must set x 3=1. The pair( 6 , 6 )came from the pair
( 6− p3 , 6−w3 )= (1 , 2 ) ;it can be noted that( 1 , 2 ) ∈ S 2 , ( 1 , 2 ) ∈ S 1∧( 1 ,2 ) ∉ S 0 ; hence
x 2=0∧x 1=1; finally,( x 1 , x 2 , x 3 ) =(1 , 0 ,1).
S ={ ( 0 , 0 ) , ( 8 , 6 ) , ( 9 ,15 ) , ( 13 , 21 ) , ( 14 ,30 ) , ( 15 , 31 ) , ( 16 , 40 ) }
4
RELIABILITY DESIGN
In this section, we look at an example of how to use dynamic
programming to solve a problem with a multiplicative
optimization function.
The problem is to design a system that is composed of several
devices connected in series (Figure 5.19).
one), the reliability of the system may not be very good. Hence, it
is desirable to duplicate devices. Multiple copies of the same
device type are connected in parallel (Figure 5.20).
subject to ∑ ci mi ≤ c , mi ≥ 1, ∀ 1≤ i≤ n
1 ≤i ≤n
( )
n
c+ ci −∑ c j
j =1
ui=⌊ ⌋
ci
constraints1∑
≤ j≤ i
c j m j ≤ x and1 ≤m ≤ u , 1 ≤ j≤ i.
j j
Clearly, f 0 ( x )=1 , ∀ x , 0 ≤ x ≤ c
f i ( x )can be solved using an approach similar to that used for
( )
3
105+30−∑ c j
j=1
u1=⌊ ⌋ =2
30
( 105+15−65 )
u2=⌊ ⌋=3
15
( 105+20−65 )
u3=⌊ ⌋=3
20
∅ 1 (1 )=0.9 , ∅ 1 ( 2 ) =1−(1−0.9)2=0.99
∅ 2 ( 1 )=0.8 , ∅ 2 ( 2 ) =1−(1−0.8)2=0.96 , ∅2 ( 3 )=1−(1−0.8)3 =0.992
∅ 3 ( 1 )=0.5 , ∅ 3 (2 )=1−(1−0.5)2=0.75 , ∅3 ( 3 )=1−(1−0.5)3 =0.875
We can obtain Sifrom Si−1 by trying all possible values for miand
combining the resulting tuples together.
S ={ ( 1 ,0 ) }
0
1 1 1
S1={(0.9 , 30)}, S 2={ ( 0.9 ,30 ) ,( 0.99 ,60) }S ={( 0.9 , 30 ) ,(0.99 , 60)}
S1= { ( 0.72 , 45 ) , ( 0.792, 75 ) } , S 2= { ( 0.864 , 60 ) , ( 0.95 , 90 ) } , S 3={ ( 0.893 , 75 ) , ( 0.982,105 ) }
2 2 2
2
S ={(0.72 , 45),(0.864 , 60) ,(0.893 ,75)}
S1= { ( 0.36 , 65 ) , ( 0.432, 80 ) , ( 0.447 , 95 ) }
3
3
S2={( 0.54 , 85 ) , ( 0.648,100 ) , ( 0.670,115 ) }
S3= { ( 0.63,105 ) , ( 0.756,120 ) , ( 0.781,135 ) }
3
3
S ={( 0.36 , 65 ) , ( 0.432 ,80 ) , ( 0.54 , 85 ) , ( 0.648,100 ) }
Tracking back we havem1=1 , m2=2 ,m3=2
1 2 3 4 1 2 3 4 1 2 3 4
0 1 2
A A A
1 0 5 ∞ ∞ 1 0 5 ∞ ∞ 1 0 5 6 ∞
2 ∞ 0 1 ∞ 2 ∞ 0 1 ∞ 2 ∞ 0 1 ∞
3 8 ∞ 0 3 3 8 13 0 3 3 8 1 0 3
3
4 2 ∞ ∞ 0 4 2 7 ∞ 0 4 2 7 8 0
1 2 3 4 1 2 3 4
3 4
A A
1 0 5 6 9 1 0 5 6 9
2 9 0 1 4 2 6 0 1 4
3 8 13 0 3 3 5 10 0 3
4 2 7 8 0 4 2 7 8 0
The line 11 is iterated n3 times, and so the time for all pairs
shortest paths algorithm using dynamic programming isO(n 3).