Daniel Sutantyo, Department of Computing, Macquarie University
7.0 - Longest Common Subsequence
7.0 - Longest Common Subsequence
7.0 - Longest Common Subsequence
7.0 - Longest Common Subsequence
7.0 - Longest Common Subsequence
7.0 - Longest Common Subsequence
7.0 - Longest Common Subsequence
7.0 - Longest Common Subsequence
7.0 - Longest Common Subsequence
kitten
sitting
itten
sitting
kitten
itting
itten
itting
tten
tting
ten
ting
en
ing
7.0 - Longest Common Subsequence
x1 x2 x3…xm
y1 y2 y3…yn
x2 x3…xm
y1 y2 y3…yn
x1 x2 x3…xm
y2 y3…yn
x2 x3…xm
y2 y3…yn
x1=y1
x1=y1
x1=y1
7.0 - Longest Common Subsequence
x1 x2 x3…xm
y1 y2 y3…yn
x2 x3…xm
y1 y2 y3…yn
x1 x2 x3…xm
y2 y3…yn
x2 x3…xm
y2 y3…yn
x1=y1
x1=y1
x1=y1
LCSS(X,Y)=⎩⎨⎧01+LCSS(X2,Y2)max(LCSS(X2,Y1),LCSS(X1,Y2))if X or Y is emptyif x1=y1if x1=y1
7.0 - Longest Common Subsequence
x1 x2 x3…xm
y1 y2 y3…yn
x2 x3…xm
y1 y2 y3…yn
x1 x2 x3…xm
y2 y3…yn
x3…xm
y1 y2 y3…yn
x2 x3…xm
y2 y3…yn
x1 x2 x3…xm
y3…yn
x2 x3…xm
y2 y3…yn
x3…xm
y2 y3…yn
x3…xm
y2 y3…yn
x3…xm
y2 y3…yn
…
…
…
…
…
…
7.0 - Longest Common Subsequence
x1 x2 x3…xm
y1 y2 y3…yn
x2 x3…xm
y1 y2 y3…yn
x1 x2 x3…xm
y2 y3…yn
x3…xm
y1 y2 y3…yn
x2 x3…xm
y2 y3…yn
x1 x2 x3…xm
y3…yn
x2 x3…xm
y2 y3…yn
x3…xm
y2 y3…yn
x3…xm
y2 y3…yn
x3…xm
y2 y3…yn
…
…
…
…
7.0 - Longest Common Subsequence
x1 x2 x3…xm
y1 y2 y3…yn
x2 x3…xm
y1 y2 y3…yn
x1 x2 x3…xm
y2 y3…yn
x3…xm
y1 y2 y3…yn
x2 x3…xm
y2 y3…yn
x1 x2 x3…xm
y3…yn
x3…xm
y2 y3…yn
…
…
…
…
7.0 - Longest Common Subsequence
kitten
sitting
itten
sitting
kitten
itting
tten
sitting
itten
itting
kitten
tting
itten
tting
tten
itting
tten
tting
7.0 - Longest Common Subsequence
7.0 - Longest Common Subsequence
Case A:
Case B:
7.0 - Longest Common Subsequence
kitten
Case A:
sitting
itten
sitting
kitten
itting
7.0 - Longest Common Subsequence
Proof (by contradiction):
Case A:
7.0 - Longest Common Subsequence
Case B:
itten
itting
tten
itting
itten
tting
tten
tting
7.0 - Longest Common Subsequence
Proof (by contradiction):
Case B:
7.0 - Longest Common Subsequence
kitten
sitting
itten
sitting
kitten
itting
tten
sitting
itten
itting
kitten
tting
itten
tting
tten
itting
tten
tting
7.0 - Longest Common Subsequence
x1 x2 x3…xm
y1 y2 y3…yn
x2 x3…xm
y1 y2 y3…yn
x1 x2 x3…xm
y2 y3…yn
x2 x3…xm
y2 y3…yn
x1=y1
x1=y1
x1=y1
LCSS(X,Y)=⎩⎨⎧01+LCSS(X2,Y2)max(LCSS(X2,Y1),LCSS(X1,Y2))if X or Y is emptyif x1=y1if x1=y1
7.0 - Longest Common Subsequence
public static int lcss_top_down(String x, String y) {
int i = x.length()-1, j = y.length()-1;
if (x.length() == 0 || y.length() == 0)
return 0;
if (lcss[i][j] != -1)
return lcss[i][j];
else if (x.charAt(0) == y.charAt(0))
return lcss[i][j] = 1 + lcss_top_down(x.substring(1),y.substring(1));
else
return lcss[i][j] = Math.max(lcss_top_down(x.substring(1),y),
lcss_top_down(x,y.substring(1)));
}
7.0 - Longest Common Subsequence
kitten
sitting
itten
sitting
kitten
itting
tten
sitting
itten
itting
kitten
tting
itten
tting
tten
itting
tten
tting
7.0 - Longest Common Subsequence
kitten
sitting
itten
sitting
kitten
itting
tten
sitting
itten
itting
kitten
tting
itten
tting
tten
itting
tten
tting
7.0 - Longest Common Subsequence
for (int i = 0; i < n+1; i++) lcss[m][i] = 0;
for (int i = 0; i < m+1; i++) lcss[i][n] = 0;
for (int i = m-1; i > -1; i--){
for (int j = n-1; j > -1; j--){
// if character matches, then go diagonally
if(a.charAt(i) == b.charAt(j))
lcss[i][j] = 1 + lcss[i+1][j+1];
// else, compare the cell to your right and to your bottom,
// and pick the larger one
else
lcss[i][j] = Integer.max(lcss[i][j+1], lcss[i+1][j]);
}
}
7.0 - Longest Common Subsequence
sitting | itting | tting | ting | ing | ng | g | ||
kitten | 0 | |||||||
itten | 0 | |||||||
tten | 0 | |||||||
ten | 0 | |||||||
en | 0 | |||||||
n | 0 | |||||||
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
7.0 - Longest Common Subsequence
sitting | itting | tting | ting | ing | ng | g | ||
kitten | 0 | 0 | ||||||
itten | 0 | 0 | ||||||
tten | 0 | 0 | ||||||
ten | 0 | 0 | ||||||
en | 0 | 0 | ||||||
n | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
7.0 - Longest Common Subsequence
sitting | itting | tting | ting | ing | ng | g | ||
kitten | 1 | 0 | 0 | |||||
itten | 1 | 0 | 0 | |||||
tten | 1 | 0 | 0 | |||||
ten | 1 | 0 | 0 | |||||
en | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
n | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
7.0 - Longest Common Subsequence
sitting | itting | tting | ting | ing | ng | g | ||
kitten | 2 | 1 | 0 | 0 | ||||
itten | 2 | 1 | 0 | 0 | ||||
tten | 1 | 1 | 0 | 0 | ||||
ten | 1 | 1 | 0 | 0 | ||||
en | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
n | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
7.0 - Longest Common Subsequence
sitting | itting | tting | ting | ing | ng | g | ||
kitten | 2 | 1 | 0 | 0 | ||||
itten | 2 | 1 | 0 | 0 | ||||
tten | 1 | 1 | 0 | 0 | ||||
ten | 2 | 2 | 2 | 2 | 1 | 1 | 0 | 0 |
en | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
n | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
7.0 - Longest Common Subsequence
sitting | itting | tting | ting | ing | ng | g | ||
kitten | 3 | 2 | 2 | 1 | 0 | 0 | ||
itten | 3 | 2 | 2 | 1 | 0 | 0 | ||
tten | 3 | 3 | 3 | 2 | 1 | 1 | 0 | 0 |
ten | 2 | 2 | 2 | 2 | 1 | 1 | 0 | 0 |
en | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
n | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
7.0 - Longest Common Subsequence
sitting | itting | tting | ting | ing | ng | g | ||
kitten | 4 | 4 | 3 | 2 | 2 | 1 | 0 | 0 |
itten | 4 | 4 | 3 | 2 | 2 | 1 | 0 | 0 |
tten | 3 | 3 | 3 | 2 | 1 | 1 | 0 | 0 |
ten | 2 | 2 | 2 | 2 | 1 | 1 | 0 | 0 |
en | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
n | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |