Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Dynamic Programming2

Uploaded by

Aparna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Dynamic Programming2

Uploaded by

Aparna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

DYNAMIC

PROGRAMMING
Subsequences
• A subsequence of a character string x0x1x2…xn-1
is a string of the form xi1xi2…xik, where ij < ij+1.
• Not the same as substring!
• Example String: ABCDEFGHIJK
• Subsequence: ACEGJIK
• Subsequence: DFGHK
• Not subsequence: DAGH
Longest Common Subsequence
• Given two strings X and Y, the longest common
subsequence (LCS) problem is to find a longest
subsequence common to both X and Y
• Has applications to DNA similarity testing
(alphabet is {A,C,G,T})
• Example:
• ACTGAACTCTGTGCACT
• TGACTCAGCACAAAAAC
Brute Force
• Enumerate all subsequences of X
• Test which ones are also subsequences of Y
• Pick the longest one.
• Analysis:
• If X is of length n, then it has 2n subsequences
• This is an exponential-time algorithm!
Dynamic Programming Strategy
• Define L[i,j] to be the length of the longest
common subsequence of X[0..i] and Y[0..j].
• Then we can define L[i,j] in the general case as
follows:
• If xi=yj, then L[i,j] = L[i-1,j-1] + 1 (we can add this
match)
• If xi≠yj, then L[i,j] = max{L[i-1,j], L[i,j-1]} (we have
no match here)
Recursive LCS
Recursion with Memoization
Iterative LCS
• Build Table bottom-up
• IterativeLCS(X,Y,m,n)
for i ← -1 to n-1 do L[i,-1] ← 0
for j ← 0 to m-1 do L[-1,j] ← 0
for i ← 0 to n-1 do
for j ← 0 to m-1 do
if X[i]=Y[j] then
L[i,j] ← L[i-1,j-1]+1
Else
L[i,j] ← max{L[i-1,j],L[i,j-1]}
Example

You might also like