Docsity

Dynamic Programming: Longest Common Subsequence (LCS), Study notes of Computer Science

Toggle sidebar
bg1
1
10/12/2007
S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine
Sofya Raskhodnikova
Algorithm Design and Analysis
L
ECTURE 20
Dynamic Programming
•LCS
10/12/2007
S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine
Longest Common Subsequence (LCS)
• Given two sequences x[1 . . m]and y[1 . . n], find
a longest subsequence common to them both.
x: A B C B D A B
y: B D C A B A
“a” not “the”
BCBA =
LCS(x, y)
10/12/2007
S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine
Brute-force LCS algorithm
Check every subsequence of x[1 . . m]to see
if it is also a subsequence of y[1 . . n].
Analysis
•Checking = O(n)time per subsequence.
•2
m
subsequences of x(each bit-vector of
length mdetermines a distinct subsequence
of x).
Worst-case running time = O(n2
m
)
= exponential time.
10/12/2007
S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine
Dynamic
programming
algorithm
Simplification:
1. Look at the length of a longest-common
subsequence.
2. Extend the algorithm to find the LCS itself.
Strategy: Consider prefixes of xand y.
•Define c[i, j] = |LCS(x[1 . . i], y[1 . . j]) |.
•Then, c[m, n] = |LCS(x, y)|.
Notation: Denote the length of a sequence sby |s|.
10/12/2007
S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine
Recursive formulation
c[i, j] = c[i–1, j–1] + 1 if x[i] = y[j],
max{c[i–1, j], c[i, j–1]}otherwise.
Base case: c[i,j]=0 if i=0 or j=0.
Case x[i] = y[j]:
1 2 i m
1 2 jn
x:
y:
=
The second case is similar.
10/12/2007
S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine
Dynamic-programming
hallmark #1
Optimal substructure
An optimal solution to a problem
(instance) contains optimal
solutions to subproblems.
If z= LCS(x, y), then any prefix of zis
an LCS of a prefix of xand a prefix of y.
pf3

Partial preview of the text

Download Dynamic Programming: Longest Common Subsequence (LCS) and more Study notes Computer Science in PDF only on Docsity!

10/12/2007 (^) S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine

Sofya Raskhodnikova

Algorithm Design and Analysis

LECTURE 20

Dynamic Programming

  • LCS

10/12/2007 (^) S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine

Longest Common Subsequence (LCS)

  • Given two sequences x [1.. m ] and y [1.. n ], find a longest subsequence common to them both.

x : A B C B D A B

y : B D C A B A

“a” not “the”

BCBA =

LCS( x , y )

10/12/2007 (^) S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine

Brute-force LCS algorithm

Check every subsequence of x [1.. m ] to see if it is also a subsequence of y [1.. n ].

Analysis

  • Checking = O ( n ) time per subsequence.
  • 2 m^ subsequences of x (each bit-vector of length m determines a distinct subsequence of x ). Worst-case running time = O ( n 2 m ) = exponential time. 10/12/2007 (^) S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine

Dynamic programming algorithm

Simplification:

  1. Look at the length of a longest-common subsequence.
  2. Extend the algorithm to find the LCS itself.

Strategy: Consider prefixes of x and y.

  • Define c [ i , j ] = | LCS( x [1.. i ], y [1.. j ]) |.
  • Then, c [ m , n ] = | LCS( x , y ) |.

Notation: Denote the length of a sequence s by | s |.

10/12/2007 (^) S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine

Recursive formulation

c [ i , j ] =

c [ i –1, j –1] + 1 if x [ i ] = y [ j ],

max{ c [ i –1, j ], c [ i , j –1]} otherwise.

Base case: c [ i , j ]=0 if i =0 or j =0.

Case x [ i ] = y [ j ]: 1 2 i m

1 2 j^ n

x :

y :



 The second case is similar. 10/12/2007 (^) S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine

Dynamic-programming

hallmark

Optimal substructure An optimal solution to a problem (instance) contains optimal solutions to subproblems.

If z = LCS( x , y ), then any prefix of z is an LCS of a prefix of x and a prefix of y.

10/12/2007 (^) S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine

Recursive algorithm for LCS

LCS( x , y , i , j ) // ignoring base cases if x [ i ] = y [ j ] then c [ i , j ] ← LCS( x , y , i –1, j –1) + 1

else c [ i , j ] ← max{LCS( x , y , i– 1, j ),

LCS( x , y , i , j –1)}

return c [ i , j ]

Worse case: x [ i ] ≠ y [ j ], in which case the algorithm evaluates two subproblems, each with only one parameter decremented. 10/12/2007 (^) S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine

Recursion tree

Height = m + n  work potentially exponential.

m = 7, n = 6: 7,67,

6,6 6,6 7,57,

6,5 6,

5,5 5,5 6,46,

6,5 6,

5,5 5,5 6,46,

5,6 5,

4,6 4,6 5,55,

7,4 7,

6,4 6,4 7,37,

m + n

10/12/2007 (^) S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine

same subproblem

but we’re solving subproblems already solved!

Recursion tree

HeightHeight == mm ++ nn  work potentially exponential.work potentially exponential.

m = 7, n = 6: 7,67,

6,6 6,6 7,57,

6,5 6,

5,5 5,5 6,46,

6,5 6,

5,5 5,5 6,46,

5,6 5,

4,6 4,6 5,55,

7,4 7,

6,4 6,4 7,37,

m + n

10/12/2007 (^) S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine

Dynamic-programming

hallmark

Overlapping subproblems A recursive solution contains a “small” number of distinct subproblems repeated many times.

The number of distinct LCS subproblems for two strings of lengths m and n is only mn.

10/12/2007 (^) S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine

Memoization algorithm

Memoization: After computing a solution to a subproblem, store it in a table. Subsequent calls check the table to avoid redoing work.

Time = Θ( mn ) = constant work per table entry. Space = Θ( mn ).

LCS( x , y , i , j ) if c [ i , j ] = NIL then if x [ i ] = y [ j ] then c [ i , j ] ← LCS( x , y , i –1, j –1) + 1

else c [ i , j ] ← max{LCS( x , y , i –1, j ),

LCS( x , y , i , j –1)}

same as before

10/12/2007 (^) S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine

D 00 00 11 11 11 22

C 00 00 11 22 22 22

A 00 11 11 22 22 22

B 00 11 22 22 33 33

A

Dynamic-programming

algorithm

IDEA:

Compute the table bottom-up.

A B C B D B

B

A 44