

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


10/12/2007 (^) S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine
Sofya Raskhodnikova
Dynamic Programming
10/12/2007 (^) S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine
Longest Common Subsequence (LCS)
x : A B C B D A B
y : B D C A B A
“a” not “the”
LCS( x , y )
10/12/2007 (^) S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine
Check every subsequence of x [1.. m ] to see if it is also a subsequence of y [1.. n ].
Analysis
Simplification:
Strategy: Consider prefixes of x and 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
c [ i , j ] =
c [ i –1, j –1] + 1 if x [ i ] = y [ j ],
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
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
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
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
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!
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
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: 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
same as before
10/12/2007 (^) S. Raskhodnikova; based on slides by C. Leiserson and E. Demaine
Compute the table bottom-up.