
















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!

















A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Adam Smith
ECTURE
SubsequenceLongest Common
Segmented Least Squares
Least squares. Foundational problem in statistic and numerical analysis.
Given n points in the plane: (x
(^1) , y
(^1) ), (x
2 , y 2 ) ,... , (x
n , y n ).
Find a line y = ax + b that minimizes the sum of the squared error:
Solution. Calculus
min error is achieved when
SSE
=
( y i (^) ax i (^) b ) 2
i = 1^ n
a (^) = (^) n
x i y i (^) (
x i (^) )
i
(
y i (^) )
i
i
n
x i (^) 2 ^ (^) (
x i (^) ) 2
i
i
, b (^) =
y i (^) a
x i
i
i
n
x
y
O ( n ) time
Dynamic Programming: Multiway Choice
Notation. OPT(j) = minimum cost for points p
(^1) , p
i+ (^) ,... , p
j.
e(i, j)
= minimum sum of squares for points p
i , p
i+ (^) ,... , p
j.
To compute OPT(j): Last segment uses points p
i , p
i+ (^) ,... , p
j for some i.
Cost = e(i, j) + c + OPT(i-1).
OPT
( j ) (^) =
0
if j
(^) = (^0)
min (^1) (^) i j
e ( i , (^) j )
(^) c (^) + (^) OPT
( i (^)
{
}
otherwise
Segmented Least Squares: Algorithm
Running time.
O(n
3 ).
Bottleneck = computing e(i, j) for O(n
2 ) pairs, O(n) per pair using
previous formula.
INPUT: n, p
1 ,…,p
N ,
c
Segmented-Least-Squares() {
forM[0] = 0
j = 1 to n
for
i = 1 to j
compute
the
least
square
error
e ij
for
the segment p
i ,…,
p j
for
j = 1 to n
M[j]
= min
(^1) i j (e ij
+ c + M[i-1])
return
M[n]
}
can be improved to O(n
(^) ) by pre-computing various statistics (^2)
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Longest Common Subsequence (LCS)
Given two sequences
x [..
m
] and
y [..
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”
x , y )
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Longest Common Subsequence (LCS)
Given two sequences
x [..
m
] and
y [..
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”
x , y )
Approximate string matching
[Levenshtein, 1966]
Search for “occurance”, get results for “occurrence”
Computational biology [Needleman-Wunsch, 1970’s]
Simple measure of genome similarity
acgtacgtacgtacgtcgtacgtatcgtacgt
aacgtacgtacgtacgtcgtacgta
cgtacgt
n –
length(LCS(
x,y
)) is called the “edit distance”
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Check every subsequence of
x [..
m
] to see
if it is also a subsequence of
y [..
n ].
n ) time per subsequence.
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.
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
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.
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
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 .
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Height =
m
n
work potentially exponential.
m
n
= 6:
7,
6,
7,
6,
5,
6,
6,
5,
6,
5,
4,
5,
7,
6,
7,
m
n
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
same
subproblem
but we’re solving subproblems already solved!
Height =
m
n
work potentially exponential.
Height =
m
n
work potentially exponential.
m
n
= 6:
7,
6,
7,
6,
5,
6,
6,
5,
6,
5,
4,
5,
7,
6,
7,
m
n
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Memoization:
After computing a solution to a
Time = check the table to avoid redoing work.subproblem, store it in a table. Subsequent calls
m
(^) n ) = constant work per table entry.
Space =
m
(^) n ).
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)
beforeas same
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
DEA
table bottom-up. Compute the