Dynamic Programming
Dynamic Programming
Computing entries of table F for the 0/1 knapsack problem. The computation of
entry F[i,j] requires communication with processing elements containing
entries F[i-1,j] and F[i-1,j-wi].
0/1 Knapsack Problem
• We have:
Optimal Matrix-chain multiplication Problem
and the time to compute n/p entries of the table in the lth diagonal is
ltcn/p.
Success Failure
• Because we have probabilities of searches for each key
and each dummy key, we can determine the expected
cost of a search in a given binary search tree T. Let us
assume that the actual cost of a search is the number of
nodes examined, i.e., the depth of the node found by the
search in T,plus1. Then the expected cost of a search in
T is : (The second statement)
• E[ search cost in T]
= (i=1~n) ∑ pi .(depthT(ki)+1)
+ (i=0~n) ∑ qi .(depthT(di)+1)
=1 + (i=1~n) ∑ pi .depthT(ki)
+ (i=0~n) ∑ qi .depthT(di)
Where depthT denotes a node’s depth in the tree T.
k2 k2
k1 k4 k1 k5
d0 d1
d0 d1 d5
k3 k5 k4
d2 d3 d4 d5 d4
k3
Figure (a)
i 0 1 2 3 4 5
d2 d3
Cost=
Node# Depth probability cost
Probability *
k1 1 0.15 0.30 (Depth+1)
k2 0 0.10 0.10
k3 2 0.05 0.15
k4 1 0.10 0.20
K5 2 0.20 0.60
d0 2 0.05 0.15
d1 3 0.10 0.30
d2 3 0.05 0.20
d3 3 0.05 0.20
d4 3 0.05 0.20
d5 3 0.10 0.40
• And the total cost = (0.30 + 0.10 + 0.15 + 0.20 + 0.60 +
0.15 + 0.30 + 0.20 + 0.20 + 0.20 + 0.40 ) = 2.80
• So Figure (a) costs 2.80 ,on another, the Figure (b) costs
2.75, and that tree is really optimal.
• We can see the height of (b) is more than (a) , and the
key k5 has the greatest search probability of any key, yet
the root of the OBST shown is k2.(The lowest expected
cost of any BST with k5 at the root is 2.85)
Step1:The structure of an OBST
root
5 1
4 2 2
3 2 4 3
2 2 2 5 4
1 2 4 5 5
1
1 2 3 4 5