0% found this document useful (0 votes)
23 views

17 Dynprog2

The document discusses the longest common subsequence problem and how to solve it using dynamic programming. It defines the problem as finding the longest subsequence that is common to two input sequences. It then proves that the optimal substructure property holds: the longest common subsequence of the full sequences can be constructed by taking the longest common subsequence of prefixes of the sequences. This allows solving the problem using a dynamic programming approach of solving subproblems and storing their solutions.

Uploaded by

Amit Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

17 Dynprog2

The document discusses the longest common subsequence problem and how to solve it using dynamic programming. It defines the problem as finding the longest subsequence that is common to two input sequences. It then proves that the optimal substructure property holds: the longest common subsequence of the full sequences can be constructed by taking the longest common subsequence of prefixes of the sequences. This allows solving the problem using a dynamic programming approach of solving subproblems and storing their solutions.

Uploaded by

Amit Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 33

Dynamic

Dynamic Programming
Programming

Comp 122, Fall 2004


Longest Common Subsequence
 Problem: Given 2 sequences, X = x1,...,xm and
Y = y1,...,yn, find a common subsequence whose
length is maximum.

springtime ncaa tournament basketball

printing north carolina krzyzewski

Subsequence need not be consecutive, but must be in order.


Comp 122, Spring Lin / Devi
nprog - 2
2004
Other sequence questions
 Edit distance: Given 2 sequences, X = x1,...,xm
and Y = y1,...,yn, what is the minimum number of
deletions, insertions, and changes that you must do
to change one to another?
 Protein sequence alignment: Given a score
matrix on amino acid pairs, s(a,b) for
a,b{}A,
and 2 amino acid sequences, X = x1,...,xmAm
and Y = y1,...,ynAn, find the alignment with
lowest score…
Comp 122, Spring Lin / Devi
nprog - 3
2004
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 mini,
insert parenthesis to minimize the total number of
scalar multiplications.
Minimum convex decomposition of a polygon,
Hydrogen placement in protein structures, …
Comp 122, Spring Lin / Devi
nprog - 4
2004
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.
 Key: determine structure of optimal solutions

Comp 122, Spring Lin / Devi


nprog - 5
2004
Steps in Dynamic Programming
1. Characterize structure of an optimal solution.
2. Define value of optimal solution recursively.
3. Compute optimal solution values either top-
down with caching or bottom-up in a table.
4. Construct an optimal solution from computed
values.
We’ll study these with the help of examples.

Comp 122, Spring Lin / Devi


nprog - 6
2004
Longest Common Subsequence
 Problem: Given 2 sequences, X = x1,...,xm and
Y = y1,...,yn, find a common subsequence whose
length is maximum.

springtime ncaa tournament basketball

printing north carolina snoeyink

Subsequence need not be consecutive, but must be in order.


Comp 122, Spring Lin / Devi
nprog - 7
2004
Naïve Algorithm
 For every subsequence of X, check whether it’s 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.

Comp 122, Spring Lin / Devi


nprog - 8
2004
Optimal Substructure
Theorem
Theorem
LetZZ==zz1,,......,,zzkbe
Let
1 k beany
anyLCS
LCSof
ofXXand
andYY..
1.1.IfIfxxm ==yyn,,then z = x = y and Z isisan anLCS
LCSof
ofXXm-1 and Y ..
m n then zkk = xmm = ynn and Zk-1
k-1 m-1 and Yn-1
n-1
2.2.IfIfxxm yyn,,then eitherzzk xxm and
theneither andZZisisan
anLCS
LCSof
ofXXm-1 and
andYY..
m n k m m-1
3.3. or zzkkyynnand
or andZZisisan
anLCS
LCSof
ofXXand
andYYn-1
n-1
..
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?

Comp 122, Spring Lin / Devi


nprog - 9
2004
Optimal Substructure
Theorem
Theorem
LetZZ==zz1,,......,,zzkbe
Let
1 k beany
anyLCS
LCSof
ofXXand
andYY..
1.1.IfIfxxm ==yyn,,then z = x = y and Z isisan anLCS
LCSof
ofXXm-1 and Y ..
m n then zkk = xmm = ynn and Zk-1
k-1 m-1 and Yn-1
n-1
2.2.IfIfxxm yyn,,then eitherzzk xxm and
theneither andZZisisan
anLCS
LCSof
ofXXm-1 and
andYY..
m n k m m-1
3.3. or zzkkyynnand
or andZZisisan
anLCS
LCSof
ofXXand
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.

Comp 122, Spring Lin / Devi


nprog - 10
2004
Optimal Substructure
Theorem
Theorem
LetZZ==zz1,,......,,zzkbe
Let
1 k beany
anyLCS
LCSof
ofXXand
andYY..
1.1.IfIfxxm ==yyn,,then z = x = y and Z isisan anLCS
LCSof
ofXXm-1 and Y ..
m n then zkk = xmm = ynn and Zk-1
k-1 m-1 and Yn-1
n-1
2.2.IfIfxxm yyn,,then eitherzzk xxm and
theneither andZZisisan
anLCS
LCSof
ofXXm-1 and
andYY..
m n k m m-1
3.3. or zzkkyynnand
or andZZisisan
anLCS
LCSof
ofXXand
andYYn-1
n-1
..
Proof: (case 2: xm  yn, and zk  xm)
Since Z does not end in xm,
(1) Z is a common subsequence of Xm-1 and Y, and
(2) there is no longer CS of Xm-1 and Y, or Z would not be an LCS.

Comp 122, Spring Lin / Devi


nprog - 11
2004
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,,



cc[[ii,, jj]]cc[[ii11,, jj11]]11 ifif ii,, jj 00and
and xxi i  yyj j,,
max(c[i  1, j ], c[i, j  1]) ifif ii,, jj 00and
max(c[i  1, j ], c[i, j  1]) and xxi i  yyj j..

This gives a recursive algorithm and solves the problem.


But does it solve it well?

Comp 122, Spring Lin / Devi


nprog - 12
2004
Recursive Solution
00 ififempty
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]

c[springtim, printing] c[springtime, printin]

[springti, printing] [springtim, printin] [springtim, printin] [springtime, printi]

[springt, printing] [springti, printin] [springtim, printi] [springtime, print]

Comp 122, Spring Lin / Devi


nprog - 13
2004
Recursive Solution
00 ififempty
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())..
p r i n t i n g
•Keep track of c[] in a
table of nm entries: s

•top/down p
r
•bottom/up i
n
g
t
i
m
Comp 122, Spring Lin / Devi
nprog - 14 e
2004
Computing the length of an LCS
LCS-LENGTH
LCS-LENGTH(X, (X,Y) Y)
1.1. mm←←length[X]
length[X]
2.2. nn←←length[Y]
length[Y]
3.3. for
forii←
←11to tomm
4.4. do
doc[i,
c[i,0]
0]← ←00
5.5. for
forjj←
←00to tonn
6.6. do
doc[0,
c[0,jj]]← ←00 b[i, j ] points to table entry
7.7. for
forii←
←11to tomm whose subproblem we used
8.8. do
dofor
forjj← ←11to tonn
9.9. do in solving LCS of Xi
doififxxi i==yyj j
10.
10. then
thenc[i, c[i,jj]]← ←c[i c[i1,1,j
j1]1]++11 and Yj.
11.
11. b[i,
b[i,jj]]← ←““ ””
12.
12. else
elseififc[i c[i1,1,jj]]≥≥c[i,
c[i,j j1]
1] c[m,n] contains the length
13.
13. then
thenc[i, c[i,jj]]←←c[i c[i1,1,jj]] of an LCS of X and Y.
14.
14. b[i,
b[i,jj]]← ←“↑” “↑”
15.
15. else
elsec[i,
c[i,jj]]← ←c[i,c[i,j j1]
1]
16.
16. b[i,
b[i,jj]]← ←“←” “←”
Time: O(mn)
17.
17.return
returnccandandbb
Comp 122, Spring Lin / Devi
nprog - 15
2004
Constructing an LCS
PRINT-LCS
PRINT-LCS(b, (b,X,
X,i,i,j)j)
1.1. ififii==00ororjj==00
2.2. then thenreturn
return
3.3. ififb[i,
b[i,jj]]==““ ””
4.4. then thenPRINT-LCS(b,
PRINT-LCS(b,X, X,ii1,
1,jj1)
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,ii1,
1,j)j)
8.8. else
elsePRINT-LCS(b,
PRINT-LCS(b,X, X,i,i,jj1)
1)

•Initial call is PRINT-LCS (b, X,m, n).


•When b[i, j ] = , we have extended LCS by one character. So
LCS = entries with in them.
•Time: O(m+n)
Comp 122, Spring Lin / Devi
nprog - 16
2004
Steps in Dynamic Programming
1. Characterize structure of an optimal solution.
2. Define value of optimal solution recursively.
3. Compute optimal solution values either top-
down with caching or bottom-up in a table.
4. Construct an optimal solution from computed
values.
We’ll study these with the help of examples.

Comp 122, Spring Lin / Devi


nprog - 17
2004
Optimal Binary Search Trees
 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 .

Comp 122, Spring Lin / Devi


nprog - 18
2004
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

Comp 122, Spring Lin / Devi


nprog - 19
2004
Example
 Consider 5 keys with these search probabilities:
p1 = 0.25, p2 = 0.2, p3 = 0.05, p4 = 0.2, p5 = 0.3.
k2 i depthT(ki) depthT(ki)·pi
1 1 0.25
2 0 0
k1 k4 3 2 0.1
4 1 0.2
5 2 0.6
1.15
k3 k5
Therefore, E[search cost] = 2.15.

Comp 122, Spring Lin / Devi


nprog - 20
2004
Example
 p1 = 0.25, p2 = 0.2, p3 = 0.05, p4 = 0.2, p5 = 0.3.

k2 i depthT(ki) depthT(ki)·pi
1 1 0.25
2 0 0
k1 k5 3 3 0.15
4 2 0.4
5 1 0.3
1.10
k4
Therefore, E[search cost] = 2.10.

k3 This tree turns out to be optimal for this set of keys.


Comp 122, Spring Lin / Devi
nprog - 21
2004
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.

Comp 122, Spring Lin / Devi


nprog - 22
2004
Optimal Substructure
 Any subtree of a BST contains keys in a contiguous range
ki, ..., kj for some 1 ≤ i ≤ j ≤ n.

T

 If T is an optimal BST and


T contains subtree T with keys ki, ... ,kj ,
then T must be an optimal BST for keys ki, ..., kj.
 Proof: Cut and paste.
Comp 122, Spring Lin / Devi
nprog - 23
2004
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,...,kr1.
k
 Right subtree of kr contains kr+1, ...,kj.
r

ki kr-1 kr+1 kj
 To find an optimal BST:
» Examine all candidate roots kr , for i ≤ r ≤ j
» Determine all optimal BSTs containing ki,...,kr1 and
containing kr+1,...,kj
Comp 122, Spring Lin / Devi
nprog - 24
2004
Recursive Solution
 Find optimal BST for ki,...,kj, where i ≥ 1, j ≤ n, j ≥ i1.
When j = i1, the tree is empty.
 Define e[i, j ] = expected search cost of optimal BST for ki,...,kj.

 If j = i1, then e[i, j ] = 0.


 If j ≥ i,
» Select a root kr, for some i ≤ r ≤ j .
» Recursively make an optimal BSTs
• for ki,..,kr1 as the left subtree, and
• for kr+1,..,kj as the right subtree.

Comp 122, Spring Lin / Devi


nprog - 25
2004
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 from (15.16)
l l i i

 If kr is the root of an optimal BST for ki,..,kj :


» e[i, j ] = pr + (e[i, r1] + w(i, r1))+(e[r+1, j] + w(r+1, j))
= e[i, r1] + e[r+1, j] + w(i, j). (because w(i, j)=w(i,r1) + p r + w(r + 1, j))

 But, we don’t know kr. Hence,


00 ifif jjii11
ee[[ii, ,jj]]
min {e[i, r  1]  e[r  1, j ]  w(i, j )} ififii jj
r  j {e[i , r  1]  e[ r  1, j ]  w(i , j )}
i min
ir  j
Comp 122, Spring Lin / Devi
nprog - 26
2004
Computing an Optimal Solution
For each subproblem (i,j), store:
 expected search cost in a table e[1 ..n+1 , 0 ..n]
» Will use only entries e[i, j ], where j ≥ i1.
 root[i, j ] = root of subtree with keys ki,..,kj, for 1 ≤ i ≤ j ≤ n.
 w[1..n+1, 0..n] = sum of probabilities
» w[i, i1] = 0 for 1 ≤ i ≤ n.
» w[i, j ] = w[i, j-1] + pj for 1 ≤ i ≤ j ≤ n.

Comp 122, Spring Lin / Devi


nprog - 27
2004
Pseudo-code
OPTIMAL-BST(p,
OPTIMAL-BST(p,q,q,n) n)
1.1. for
forii←
←11to tonn++11
2.2. do e[i,ii1]
doe[i, 1]← ←00 Consider all trees with l keys.
3.3. w[i, i  1] ←
w[i, i 1] ← 0 0
4.4. for Fix the first key.
forll←
←11to tonn
5.5. do
dofor
forii← ←11to tonnll++11 Fix the last key
6.6. do
dojj←i ←i++ll11
7.7. e[i,
e[i,jj]←∞
]←∞
8.8. w[i,
w[i,jj]]← ←w[i,w[i,jj1]
1]++ppj j
9.9. for
forrr←i ←ito tojj
10.
10. dodott← ←e[i,e[i,rr1]
1]++e[r
e[r++1,1,jj]]++w[i,
w[i,jj]] Determine the root
11.
11. ififtt<<e[i,
e[i,jj]] of the optimal
12.
12. then
thene[i,
e[i,jj]]←
←tt (sub)tree
13.
13. root[i,
root[i,jj]]←r
←r
14.
14. return
returneeand
androotroot

Time: O(n3)
Comp 122, Spring Lin / Devi
nprog - 28
2004
Elements of Dynamic Programming
 Optimal substructure
 Overlapping subproblems

Comp 122, Spring Lin / Devi


nprog - 29
2004
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.
Comp 122, Spring Lin / Devi
nprog - 30
2004
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.
 Informally, running time depends on (# of subproblems
overall)  (# of choices).
 How many subproblems and choices do the examples
considered contain?
 Dynamic programming uses optimal substructure bottom
up.
» First find optimal solutions to subproblems.
» Then choose which to use in optimal solution to the problem.

Comp 122, Spring Lin / Devi


nprog - 31
2004
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:

Comp 122, Spring Lin / Devi


nprog - 32
2004
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.

Comp 122, Spring Lin / Devi


nprog - 33
2004

You might also like