Beruflich Dokumente
Kultur Dokumente
Dynamic Programming
Programming
ncaa tournament
basketball
printing
north carolina
krzyzewski
Comp 122,
Lin / Devi
Comp 122,
Lin / Devi
More problems
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,
dynprog - 4
Comp 122,
Lin / Devi
Dynamic Programming
Dynamic Programming is an algorithm design technique for
optimization problems: often minimizing or maximizing.
Like divide and conquer, DP solves problems by combining
solutions to subproblems.
Unlike divide and conquer, subproblems are not independent.
Subproblems may share subsubproblems,
However, solution to one subproblem may not affect the solutions to other
subproblems of the same problem. (More on this later.)
DP reduces computation by
Solving subproblems in a bottom-up fashion.
Storing solution to a subproblem the first time it is solved.
Looking up the solution when subproblem is encountered again.
dynprog - 5
Comp 122,
Lin / Devi
dynprog - 6
Comp 122,
Lin / Devi
ncaa tournament
basketball
printing
north carolina
snoeyink
Comp 122,
Lin / Devi
Nave Algorithm
For every subsequence of X, check whether its a
subsequence of Y .
Time: (n2m).
2m subsequences of X to check.
Each subsequence takes (n) time to check:
scan Y for first letter, for second, and so on.
dynprog - 8
Comp 122,
Lin / Devi
Optimal Substructure
Theorem
Theorem
Let
LetZZ==zz1,,......,,zzkbe
beany
anyLCS
LCSof
ofXXand
andYY..
1
1.1.IfIfxxm ==yyn,,then
z = x = y and Z isisan
and Y ..
anLCS
LCSof
ofXXm-1
m
n then zkk = xmm = ynn and Zk-1
k-1
m-1 and Yn-1
n-1
2.2.IfIfxxm yyn,,then
theneither
eitherzzk xxm and
andZZisisan
anLCS
LCSof
ofXXm-1 and
andYY..
m
3.3.
m-1
or
..
or zzkkyynnand
andZZisisan
anLCS
LCSof
ofXXand
andYYn-1
n-1
Notation:
dynprog - 9
Comp 122,
Lin / Devi
Optimal Substructure
Theorem
Theorem
Let
LetZZ==zz1,,......,,zzkbe
beany
anyLCS
LCSof
ofXXand
andYY..
1
1.1.IfIfxxm ==yyn,,then
z = x = y and Z isisan
and Y ..
anLCS
LCSof
ofXXm-1
m
n then zkk = xmm = ynn and Zk-1
k-1
m-1 and Yn-1
n-1
2.2.IfIfxxm yyn,,then
theneither
eitherzzk xxm and
andZZisisan
anLCS
LCSof
ofXXm-1 and
andYY..
m
3.3.
m-1
or
..
or zzkkyynnand
andZZisisan
anLCS
LCSof
ofXXand
andYYn-1
n-1
dynprog - 10
Comp 122,
Lin / Devi
Optimal Substructure
Theorem
Theorem
Let
LetZZ==zz1,,......,,zzkbe
beany
anyLCS
LCSof
ofXXand
andYY..
1
1.1.IfIfxxm ==yyn,,then
z = x = y and Z isisan
and Y ..
anLCS
LCSof
ofXXm-1
m
n then zkk = xmm = ynn and Zk-1
k-1
m-1 and Yn-1
n-1
2.2.IfIfxxm yyn,,then
theneither
eitherzzk xxm and
andZZisisan
anLCS
LCSof
ofXXm-1 and
andYY..
m
3.3.
m-1
or
..
or zzkkyynnand
andZZisisan
anLCS
LCSof
ofXXand
andYYn-1
n-1
dynprog - 11
Comp 122,
Lin / Devi
Recursive Solution
Define c[i, j] = length of LCS of Xi and Yj .
We want c[m,n].
00
ifif ii 00or
or jj 00,,
ifif ii,, jj 00and
and xxi i yyj j,,
ifif ii,, jj 00and
and xxi i yyj j..
dynprog - 12
Comp 122,
Lin / Devi
Recursive Solution
00
cc[[,,]] cc[[prefix
prefix,,prefix
prefix]]11
max(c[ prefix , ], c[ , prefix ])
max(c[ prefix , ], c[ , prefix ])
ififempty
emptyor
or empty
empty,,
ifif end(
end())end(
end()),,
ifif end(
end())end(
end())..
c[springtime, printing]
c[springtim, printing]
[springti, printing] [springtim, printin]
c[springtime, printin]
[springtim, printin] [springtime, printi]
dynprog - 13
Comp 122,
Lin / Devi
Recursive Solution
ififempty
emptyor
or empty
empty,,
ifif end(
end())end(
end()),,
ifif end(
end())end(
end())..
00
cc[[,,]] cc[[prefix
prefix,,prefix
prefix]]11
max(c[ prefix , ], c[ , prefix ])
max(c[ prefix , ], c[ , prefix ])
top/down
bottom/up
r
n
g
t
i
m
dynprog - 14
e 122,
Comp
Lin / Devi
15.
else
15.
elsec[i,
c[i,jj]]
c[i,
c[i,j
j1]
1]
16.
b[i,
16.
b[i,jj]]
17.
17.return
returnccand
andbb
dynprog - 15
Comp 122,
Constructing an LCS
PRINT-LCS
PRINT-LCS(b,
(b,X,
X,i,i,j)j)
1.1. ififii==00or
orjj==00
2.2.
then
thenreturn
return
3.3. ififb[i,
b[i,jj]]==
4.4.
then
thenPRINT-LCS(b,
PRINT-LCS(b,X,
X,ii1,1,jj1)
1)
5.5.
print
printxxi i
6.6.
elseif
elseifb[i,
b[i,jj]]==
7.7.
then
thenPRINT-LCS(b,
PRINT-LCS(b,X,
X,ii1,
1,j)j)
8.8. else
elsePRINT-LCS(b,
PRINT-LCS(b,X,
X,i,i,jj1)
1)
Comp 122,
Lin / Devi
dynprog - 17
Comp 122,
Lin / Devi
dynprog - 18
Comp 122,
Lin / Devi
(depth T (ki ) 1) pi
i 1
n
i 1
i 1
depth T (ki ) pi pi
n
Sum of probabilities is 1.
i 1
dynprog - 19
Comp 122,
Lin / Devi
Example
Consider 5 keys with these search probabilities:
p1 = 0.25, p2 = 0.2, p3 = 0.05, p4 = 0.2, p5 = 0.3.
k2
k1
k4
k3
k5
i depthT(ki) depthT(ki)pi
1
1
0.25
2
0
0
3
2
0.1
4
1
0.2
5
2
0.6
1.15
dynprog - 20
Comp 122,
Lin / Devi
Example
p1 = 0.25, p2 = 0.2, p3 = 0.05, p4 = 0.2, p5 = 0.3.
k2
k1
k5
k4
i depthT(ki) depthT(ki)pi
1
1
0.25
2
0
0
3
3
0.15
4
2
0.4
5
1
0.3
1.10
Lin / Devi
Example
Observations:
Optimal BST may not have smallest height.
Optimal BST may not have highest-probability key at
root.
dynprog - 22
Comp 122,
Lin / Devi
Optimal Substructure
Any subtree of a BST contains keys in a contiguous range
ki, ..., kj for some 1 i j n.
T
T
Comp 122,
Lin / Devi
Optimal Substructure
One of the keys in ki, ,kj, say kr, where i r j,
must be the root of an optimal subtree for these keys.
Left subtree of kr contains ki,...,kr1.
k
Right subtree of kr contains kr+1, ...,kj.
r
ki
kr-1
kr+1
kj
Comp 122,
Lin / Devi
Recursive Solution
Find optimal BST for ki,...,kj, where i 1, j n, j i1.
When j = i1, the tree is empty.
Define e[i, j ] = expected search cost of optimal BST for ki,...,kj.
If j = i1, then e[i, j ] = 0.
If j i,
Select a root kr, for some i r j .
Recursively make an optimal BSTs
for ki,..,kr1 as the left subtree, and
for kr+1,..,kj as the right subtree.
dynprog - 25
Comp 122,
Lin / Devi
Recursive Solution
When the OPT subtree becomes a subtree of a node:
Depth of every node in OPT subtree goes up by 1.
Expected search cost increases by
jj
ww((ii,, jj))
ppl l
l l i i
from (15.16)
+ w(r + 1, j))
j
i
dynprog - 26
Comp 122,
ifif jjii11
ififii jj
Lin / Devi
dynprog - 27
Comp 122,
Lin / Devi
Pseudo-code
OPTIMAL-BST(p,
OPTIMAL-BST(p,q,q,n)
n)
1.1. for
forii
11to
tonn++11
2.2.
do
Consider all trees with l keys.
doe[i,
e[i,ii1]
1]
00
3.3.
w[i,
w[i,ii1]
1]
00
Fix the first key.
4.4. for
l
1
to
n
for l 1 to n
Fix the last key
5.5.
do
dofor
forii
11to
tonnll++11
6.6.
do
dojji
i++ll11
7.7.
e[i,
e[i,jj]
]
8.8.
w[i,
w[i,jj]]
w[i,
w[i,jj1]
1]++ppj j
9.9.
for
forrri
ito
tojj
Determine the root
10.
do
10.
dott
e[i,
e[i,rr1]
1]++e[r
e[r++1,1,jj]]++w[i,
w[i,jj]]
of the optimal
11.
ififtt<<e[i,
11.
e[i,jj]]
(sub)tree
12.
then
12.
thene[i,
e[i,jj]]
tt
13.
root[i,
13.
root[i,jj]]r
r
14.
14. return
returneeand
androot
root
Time: O(n3)
dynprog - 28
Comp 122,
Lin / Devi
dynprog - 29
Comp 122,
Lin / Devi
Optimal Substructure
Show that a solution to a problem consists of making a
choice, which leaves one or more subproblems to solve.
Suppose that you are given this last choice that leads to an
optimal solution.
Given this choice, determine which subproblems arise and
how to characterize the resulting space of subproblems.
Show that the solutions to the subproblems used within
the optimal solution must themselves be optimal. Usually
use cut-and-paste.
Need to ensure that a wide enough range of choices and
subproblems are considered.
dynprog - 30
Comp 122,
Lin / Devi
Optimal Substructure
Optimal substructure varies across problem domains:
1. How many subproblems are used in an optimal solution.
2. How many choices in determining which subproblem(s) to use.
Comp 122,
Lin / Devi
Optimal Substucture
Does optimal substructure apply to all optimization
problems? No.
Applies to determining the shortest path but NOT the
longest simple path of an unweighted directed graph.
Why?
Shortest path has independent subproblems.
Solution to one subproblem does not affect solution to another
subproblem of the same problem.
Subproblems are not independent in longest simple path.
Solution to one subproblem affects the solutions to other subproblems.
Example:
dynprog - 32
Comp 122,
Lin / Devi
Overlapping Subproblems
The space of subproblems must be small.
The total number of distinct subproblems is a polynomial
in the input size.
A recursive algorithm is exponential because it solves the same
problems repeatedly.
If divide-and-conquer is applicable, then each problem solved
will be brand new.
dynprog - 33
Comp 122,
Lin / Devi