Documente Academic
Documente Profesional
Documente Cultură
․Course contents:
⎯ Matrix-chain multiplication
⎯ Longest common subsequence
⎯ Assembly-line scheduling
⎯ Optimal binary search trees
․Reading:
⎯ Chapter 15
⎯ , exponential in n.
․Any efficient solution? Dynamic programming!
Lookup-Chain(p, i, j)
1. If m[i, j] < ∞
2. return m[i, j];
3. if i = j
4. m[ i, j] ← 0;
5. else for k ← i to j -1
6. q ← Lookup-Chain(p, i, k) + Lookup-Chain(p, k+1, j) + pi-1pkpj;
7. if q < m[i, j]
8. m[i, j] ← q;
9. return m[i, j];
TD-LCS(i, j)
1. if c[i,j] = NIL
2. if xi = yj
3. c[i, j] = TD-LCS(i-1, j-1) + 1
4. else c[i, j] = max(TD-LCS[i, j-1], TD-LCS[i-1, j])
5. return c[i, j]
Print-LCS(b, X, i, j)
1. if i = 0 or j = 0
2. return;
3. if b[i, j]=“↖ “
4. Print-LCS(b, X, i-1, j-1);
5. print xi;
6. else if b[i, j] = “ ↑ “
7. Print-LCS(b, X, i -1, j);
8. else Print-LCS(b, X, i, j-1);
line 2 8 5 6 4 5 7
S2,1 S2,2 S2,3 S2,4 S2,5 S2,6
line 2 8 5 6 4 5 7
S2,1 S2,2 S2,3 S2,4 S2,5 S2,6
f1[j] 9 18 20 24 32 35
f* = 38
f2[j] 12 16 22 25 30 37
j 1 2 3 4 5 6
l1[j] 1 2 1 1 2
l* = 1
l2[j] 1 2 1 2 2
Unit 4 Y.-W. Chang 27
Optimal Binary Search Tree
․ Given a sequence K = <k1, k2, …, kn> of n distinct keys in
sorted order (k1 < k2 < … < kn) and a set of probabilities P =
<p1, p2, …, pn> for searching the keys in K and Q = <q0, q1,
q2, …, qn> for unsuccessful searches (corresponding to D =
<d0, d1, d2, …, dn> of n+1 distinct dummy keys with di
representing all values between ki and ki+1), construct a
binary search tree whose expected search cost is smallest.
k2 p2 k2
p1 k k4 p4 k1
1 k5
d0 d1 k3 p3 k5 p5 d5
d0 d1 k4
q0 q1
d2 d3 d4 d5 k3 d4
q2 q3 q4 q5
d2 d3
i 0 1 2 3 4 5 n n
0.10×1 (depth 0) k2
0.15×2 k2
k1 k5
k1 k4 0.10×2
k4 d5
k3 k5 0.20×3 d0 d1
d0 d1
k3 d4
d2 d3 d4 d5 0.10×4
Cost = 2.80 d2 d3 Cost = 2.75
Optimal!!
n n
E[search cost in T ] = ∑ (depthT (ki ) + 1) ⋅ pi + ∑ (depthT (di ) + 1) ⋅ qi
i =1 i =0
n n
= 1 + ∑ depthT ( ki ) ⋅ pi + ∑ depthT (di ) ⋅ qi
i =1 i =0
Unit 4 Y.-W. Chang 29
Optimal Substructure
․If an optimal binary search tree T has a subtree T’
containing keys ki, …, kj, then this subtree T’ must be
optimal as well for the subproblem with keys ki, …, kj
and dummy keys di-1, …, dj.
⎯ Given keys ki, …, kj with kr (i ≤ r ≤ j) as the root, the left subtree
contains the keys ki, …, kr-1 (and dummy keys di-1, …, dr-1) and
the right subtree contains the keys kr+1, …, kj (and dummy keys
dr, …, dj).
⎯ For the subtree with keys ki, …, kj with root ki, the left subtree
contains keys ki, .., ki-1 (no key) with the dummy key di-1.
k2
k1 k5
d1 k4 d5
d0
k3 d4
d2 d3
Unit 4 Y.-W. Chang 30
Overlapping Subproblem: Recurrence
․e[i, j] : expected cost of searching an optimal binary
search tree containing the keys ki, …, kj. Node depths increase
⎯ Want to find e[1, n]. by 1 after merging two
subtrees, and so do
⎯ e[i, i -1] = qi-1 (only the dummy key di-1). the costs
․If kr (i ≤ r ≤ j) is the root of an optimal subtree containing
keys ki, …, kj and let w(i, j ) = ∑ p + ∑ q then
j j
l l
l =i l = i −1
k3 k5 0.20×2
d2 d3 d4 d5 0.10×3
Unit 4 Y.-W. Chang 31
Computing the Optimal Cost
․ Need a table e[1..n+1, 0..n] for e[i, j] (why e[1, 0] and e[n+1, n]?)
․ Apply the recurrence to compute w(i, j) (why?)
⎧ qi − 1 if j = i − 1
w[i, j ] = ⎨
⎩ w[i, j − 1] + pj + qj if i ≤ j
Optimal-BST(p, q, n)
1. for i ← 1 to n + 1 ․root[i, j] : index r for which
2. e[i, i-1] ← qi-1 kr is the root of an optimal
3. w[i, i-1] ← qi-1 search tree containing
4. for l ← 1 to n
keys ki, …, kj.
5. for i ← 1 to n – l + 1
6. j ← i + l -1 e
7. e[i, j] ← ∞ 5 1
2.75 2
8. w[i, j] ← w[i, j-1] + pj + qj 4
1.75 2.00 3 i
9. for r ← i to j j 3
10. t ← e[i, r-1] + e[r+1, j] + w[i, j] 2 1.25 1.20 1.30 4
11. if t < e[i, j] 1 0.90 0.70 0.60 0.90 5
12. e[i, j] ← t 0 0.45 0.40 0.25 0.30 0.50 6
13. root[i, j] ← r 0.05 0.10 0.05 0.05 0.05 0.10
14. return e and root