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
\(x_1\ x_2\ x_3 \dots x_m\)
\(y_1\ y_2\ y_3\dots y_n\)
\(x_2\ x_3 \dots x_m\)
\(y_1\ y_2\ y_3\dots y_n\)
\(x_1\ x_2\ x_3 \dots x_m\)
\(y_2\ y_3\dots y_n\)
\(x_2\ x_3 \dots x_m\)
\(y_2\ y_3\dots y_n\)
\(x_1 = y_1\)
\(x_1 \ne y_1\)
\(x_1 \ne y_1\)
7.0 - Longest Common Subsequence
\(x_1\ x_2\ x_3 \dots x_m\)
\(y_1\ y_2\ y_3\dots y_n\)
\(x_2\ x_3 \dots x_m\)
\(y_1\ y_2\ y_3\dots y_n\)
\(x_1\ x_2\ x_3 \dots x_m\)
\(y_2\ y_3\dots y_n\)
\(x_2\ x_3 \dots x_m\)
\(y_2\ y_3\dots y_n\)
\(x_1 = y_1\)
\(x_1 \ne y_1\)
\(x_1 \ne y_1\)
\[ \text{LCSS}(X,Y) = \begin{cases} 0 & \text{if \(X\) or \(Y\) is empty}\\1 + \text{LCSS}(X_2,Y_2) & \text{if $x_1 = y_1$}\\\max\left(\text{LCSS}(X_2,Y_1),\text{LCSS}(X_1,Y_2)\right) & \text{if $x_1 \ne y_1$}\end{cases} \]
7.0 - Longest Common Subsequence
\(x_1\ x_2\ x_3 \dots x_m\)
\(y_1\ y_2\ y_3\dots y_n\)
\( x_2\ x_3 \dots x_m\)
\(y_1\ y_2\ y_3\dots y_n\)
\(x_1\ x_2\ x_3 \dots x_m\)
\( y_2\ y_3\dots y_n\)
\( x_3 \dots x_m\)
\(y_1\ y_2\ y_3\dots y_n\)
\(x_2\ x_3 \dots x_m\)
\(y_2\ y_3\dots y_n\)
\(x_1\ x_2\ x_3 \dots x_m\)
\(y_3\dots y_n\)
\(x_2\ x_3 \dots x_m\)
\(y_2\ y_3\dots y_n\)
\( x_3 \dots x_m\)
\(y_2\ y_3\dots y_n\)
\( x_3 \dots x_m\)
\(y_2\ y_3\dots y_n\)
\( x_3 \dots x_m\)
\(y_2\ y_3\dots y_n\)
\(\dots\)
\(\dots\)
\(\dots\)
\(\dots\)
\(\dots\)
\(\dots\)
7.0 - Longest Common Subsequence
\(x_1\ x_2\ x_3 \dots x_m\)
\(y_1\ y_2\ y_3\dots y_n\)
\( x_2\ x_3 \dots x_m\)
\(y_1\ y_2\ y_3\dots y_n\)
\(x_1\ x_2\ x_3 \dots x_m\)
\( y_2\ y_3\dots y_n\)
\( x_3 \dots x_m\)
\(y_1\ y_2\ y_3\dots y_n\)
\(x_2\ x_3 \dots x_m\)
\(y_2\ y_3\dots y_n\)
\(x_1\ x_2\ x_3 \dots x_m\)
\(y_3\dots y_n\)
\(x_2\ x_3 \dots x_m\)
\(y_2\ y_3\dots y_n\)
\( x_3 \dots x_m\)
\(y_2\ y_3\dots y_n\)
\( x_3 \dots x_m\)
\(y_2\ y_3\dots y_n\)
\( x_3 \dots x_m\)
\(y_2\ y_3\dots y_n\)
\(\dots\)
\(\dots\)
\(\dots\)
\(\dots\)
7.0 - Longest Common Subsequence
\(x_1\ x_2\ x_3 \dots x_m\)
\(y_1\ y_2\ y_3\dots y_n\)
\( x_2\ x_3 \dots x_m\)
\(y_1\ y_2\ y_3\dots y_n\)
\(x_1\ x_2\ x_3 \dots x_m\)
\( y_2\ y_3\dots y_n\)
\( x_3 \dots x_m\)
\(y_1\ y_2\ y_3\dots y_n\)
\(x_2\ x_3 \dots x_m\)
\(y_2\ y_3\dots y_n\)
\(x_1\ x_2\ x_3 \dots x_m\)
\(y_3\dots y_n\)
\( x_3 \dots x_m\)
\(y_2\ y_3\dots y_n\)
\(\dots\)
\(\dots\)
\(\dots\)
\(\dots\)
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
\(x_1\ x_2\ x_3 \dots x_m\)
\(y_1\ y_2\ y_3\dots y_n\)
\(x_2\ x_3 \dots x_m\)
\(y_1\ y_2\ y_3\dots y_n\)
\(x_1\ x_2\ x_3 \dots x_m\)
\(y_2\ y_3\dots y_n\)
\(x_2\ x_3 \dots x_m\)
\(y_2\ y_3\dots y_n\)
\(x_1 = y_1\)
\(x_1 \ne y_1\)
\(x_1 \ne y_1\)
\[ \text{LCSS}(X,Y) = \begin{cases} 0 & \text{if \(X\) or \(Y\) is empty}\\1 + \text{LCSS}(X_2,Y_2) & \text{if $x_1 = y_1$}\\\max\left(\text{LCSS}(X_2,Y_1),\text{LCSS}(X_1,Y_2)\right) & \text{if $x_1 \ne y_1$}\end{cases} \]
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 |