Dynamic Programing in Dsa
Dynamic Programing in Dsa
Optimal BST: Given sequence K = k1 < k2 <··· < kn of n sorted keys, with a
search probability pi for each key ki, build a binary search tree (BST) with
minimum expected search cost.
Matrix chain multiplication: Given a sequence of matrices A1 A2 … An, with
Ai of dimension mini, insert parenthesis to minimize the total number of
scalar multiplications.
Minimum convex decomposition of a polygon,
Hydrogen placement in protein structures, …
Dynamic Programming
Notation:
prefix Xi = x1,...,xi is the first i letters of X.
This says what any longest common subsequence must look like;
do you believe it?
Optimal Substructure
Theorem
Theorem
LetZZ==zz1, ,. .. .. ., ,zzkbe
Let
1 k beany
anyLCS
LCSofofXXand
andYY. .
1.1.IfIfxxm ==yyn, ,then
thenzzk ==xxm ==yyn and
andZZk-1 isisan
anLCS
LCSofofXXm-1 and
andYYn-1. .
m n k m n k-1 m-1 n-1
2.2.IfIfxxm yyn, ,then either z x and Z is an LCS of X and Y .
m n then either zkk xmm and Z is an LCS of Xm-1
m-1 and Y .
3.3. or zzkkyynnand
or andZZisisan
anLCS
LCSofofXXand
andYYn-1..
n-1
Proof: (case 1: xm = yn)
Any sequence Z’ that does not end in xm = yn can be made longer by adding xm = yn to the end.
Therefore,
(1) longest common subsequence (LCS) Z must end in xm = yn.
(2) Zk-1 is a common subsequence of Xm-1 and Yn-1, and
(3) there is no longer CS of Xm-1 and Yn-1, or Z would not be an LCS.
Optimal Substructure
Theorem
Theorem
LetZZ==zz1, ,. .. .. ., ,zzkbe
Let
1 k beany
anyLCS
LCSofofXXand
andYY. .
1.1.IfIfxxm ==yyn, ,then
thenzzk ==xxm ==yyn and
andZZk-1 isisan
anLCS
LCSofofXXm-1 and
andYYn-1. .
m n k m n k-1 m-1 n-1
2.2.IfIfxxm yyn, ,then either z x and Z is an LCS of X and Y .
m n then either zkk xmm and Z is an LCS of Xm-1
m-1 and Y .
3.3. or zzkkyynnand
or andZZisisan
anLCS
LCSofofXXand
andYYn-1..
n-1
00 ififempty
emptyor or empty
empty,,
cc[[,,]]cc[[prefix
prefix,,prefix
prefix]]11 end())end(
ifif end( end()),,
max(c[ prefix , ], c[ , prefix ])
max(c[ prefix , ], c[ , prefix ]) end())end(
ifif end( end())..
c[springtime, printing]
• Problem
• Given sequence K = k1 < k2 <··· < kn of n sorted keys,
with a search probability pi for each key ki.
• Want to build a binary search tree (BST)
with minimum expected search cost.
• Actual cost = # of items examined.
• For key ki, cost = depthT(ki)+1, where depthT(ki) = depth of
ki in BST T .
Expected Search Cost
E[search cost in T ]
n
(depth T (ki ) 1) pi
i 1
n n
depth T (ki ) pi pi
i 1 i 1
n Sum of probabilities is 1.
1 depth T (ki ) pi (15.16)
i 1
Example
i depthT(ki) depthT(ki)·pi
1 1 0.25
k1 k4 2 0 0
3 2 0.1
4 1 0.2
5 2 0.6
k3 k5
1.15
Therefore, E[search cost] = 2.15.
Example
i depthT(ki) depthT(ki)·pi
1 1 0.25
k1 k5
2 0 0
3 3 0.15
4 2 0.4
k4 5 1 0.3
1.10
Therefore, E[search cost] = 2.10.
k3
This tree turns out to be optimal for this set of keys.
Example
• Observations:
• Optimal BST may not have smallest height.
• Optimal BST may not have highest-probability key at root.
• Build by exhaustive checking?
• Construct each n-node BST.
• For each,
assign keys and compute expected search cost.
• But there are (4n/n3/2) different BSTs with n nodes.
Optimal Substructure
• Any subtree of a BST contains keys in a contiguous range ki, ..., kj for
some 1 ≤ i ≤ j ≤ n.
T
T
keys.
• Left subtree of kr contains ki,...,kr1.
• Right subtree of kr contains kr+1, ...,kkj. k k kj
i r-1 r+1
• Optimal substructure
• Overlapping subproblems
Optimal Substructure