Daniel Sutantyo
Department of Computing
Macquarie University
definition
brute force
brute force
brute force
brute force
brute force
brute force
Case A:
Case B:
optimal substructure
optimal substructure
kitten
Case A:
sitting
itten
sitting
kitten
itting
optimal substructure
Proof (by contradiction):
Case A:
optimal substructure
Case B:
itten
itting
tten
itting
itten
tting
tten
tting
optimal substructure
Case B:
Proof (by contradiction):
recursive relation
\(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\)
recursive relation
\[ \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_2$}\end{cases} \]
\(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\)
overlapping subproblems
\(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\)
overlapping subproblems
\(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_1\ x_2\ x_3 \dots x_m\)
\(y_3\dots y_n\)
\(x_2\ x_3 \dots x_m\)
\( x_3 \dots x_m\)
\(y_2\ y_3\dots y_n\)
\(\dots\)
\(\dots\)
\(\dots\)
\( y_2\ y_3\dots y_n\)
overlapping subproblems
kitten
sitting
itten
sitting
kitten
itting
tten
sitting
itten
itting
kitten
tting
itten
tting
tten
itting
tten
tting
top-down solution
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] = i=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)));
}
bottom-up solution
kitten
sitting
itten
sitting
kitten
itting
tten
sitting
itten
itting
kitten
tting
itten
tting
tten
itting
tten
tting
bottom-up solution
kitten
sitting
itten
sitting
kitten
itting
tten
sitting
ten
sitting
...
kitten
tting
kitten
ting
kitten
ing
...
itten
itting
itten
tting
itten
ting
itten
ing
...
tten
itting
tten
tting
tten
ting
tten
ing
...
ten
itting
ten
tting
ten
ting
ten
ing
...
...
...
...
...
bottom-up solution
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 |
bottom-up solution
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 |
bottom-up solution
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 |
bottom-up solution
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]);
}
}
how does the algorithm work
how does the algorithm work
kitten
sitting
(this is the example in https://en.wikipedia.org/wiki/Edit_distance)
how does the algorithm work
kitten
sitting
itten
sitting
kitten
itting
tten
sitting
itten
itting
kitten
tting
how does the algorithm work?
how does the algorithm work?
kitten
sitting
skitten
sitting
sitten
sitting
itten
sitting
kitten
itting
itten
itting
sitten
sitting
stten
sitting
tten
sitting
how does the algorithm work?
lack
sack
slack
sack
sack
sack
ack
sack
lack
ack
ack
ack
sack
sack
sck
sack
ck
sack
aack
ack
ck
ck
k
k
alack
ack
ack
ack
ck
ck
ack
ack
ck
ck
ck
ack
sck
sack
sk
sack
k
sack
k
ack
ck
ack
ack
ack
ak
ack
k
ack
ack
ck
lack
ck
how does the algorithm work?
\(\text{EditDistance}(X,Y)\)
\(\text{EditDistance}(y_1+X,Y)\)
\(\text{EditDistance}(y_1+X_2,Y)\)
\(\text{EditDistance}(X_2,Y)\)
insert
delete
replace
brute-force complexity
\(n\)
\(n-1\)
\(n-1\)
\(n-1\)
\(1\)
\(3n-3\)
\(9n-18\)
. . . . . . . . . . . . . . . . . . . . . . . . .
how does the algorithm work?
lack
sack
slack
sack
sack
sack
ack
sack
lack
ack
ack
ack
sack
sack
sck
sack
ck
sack
aack
ack
ck
ck
k
k
alack
ack
ack
ack
ck
ck
ack
ack
ck
ck
ck
ack
sck
sack
sk
sack
k
sack
k
ack
ck
ack
ack
ack
ak
ack
k
ack
ack
ck
lack
ck
bottom-up approach
lack
sack
ack
sack
ck
sack
k
sack
sack
slack
sack
???
sack
sack
bottom-up approach
lack
sack
ack
sack
ck
sack
k
sack
sack
slack
sack
???
sack
sack
bottom-up approach
lack
sack
ack
sack
ck
sack
k
sack
sack
lack
ack
ack
ack
lack
ck
lack
k
lack
bottom-up approach
lack
sack
ack
sack
ck
sack
k
sack
sack
lack
ack
ack
ack
lack
ck
lack
k
lack
bottom-up approach
lack
sack
ack
sack
ck
sack
k
sack
sack
lack
ack
ack
ack
ck
ack
k
ack
ack
lack
ck
ack
ck
ck
ck
k
ck
ck
lack
k
ack
k
ck
k
k
k
k
lack
ack
ck
k
bottom-up approach
lack | ack | ck | k | ||
sack | 1 | 1 | 2 | 3 | 4 |
ack | 1 | 0 | 1 | 2 | 3 |
ck | 2 | 1 | 0 | 1 | 2 |
k | 3 | 2 | 1 | 0 | 1 |
4 | 3 | 2 | 1 | 0 |
bottom-up approach
kitten
sitting
itten
sitting
tten
sitting
ten
sitting
...
kitten
itting
itten
itting
tten
itting
ten
itting
...
kitten
tting
itten
tting
tten
tting
ten
tting
...
kitten
ting
itten
ting
tten
ting
ten
ting
...
...
...
...
...
...
bottom-up approach
kitten | itten | tten | ten | en | n | ||
sitting | 7 | ||||||
itting | 6 | ||||||
tting | 5 | ||||||
ting | 4 | ||||||
ing | 3 | ||||||
ng | 2 | ||||||
g | 1 | 1 | |||||
6 | 5 | 4 | 3 | 2 | 1 | 0 |
bottom-up approach
kitten | itten | tten | ten | en | n | ||
sitting | 6 | 7 | |||||
itting | 5 | 6 | |||||
tting | 4 | 5 | |||||
ting | 3 | 4 | |||||
ing | 2 | 3 | |||||
ng | 2 | 1 | 2 | ||||
g | 6 | 5 | 4 | 3 | 2 | 1 | 1 |
6 | 5 | 4 | 3 | 2 | 1 | 0 |
bottom-up approach
kitten | itten | tten | ten | en | n | ||
sitting | 6 | 6 | 7 | ||||
itting | 5 | 5 | 6 | ||||
tting | 4 | 4 | 5 | ||||
ting | 3 | 3 | 4 | ||||
ing | 3 | 2 | 2 | 3 | |||
ng | 6 | 5 | 4 | 3 | 2 | 1 | 2 |
g | 6 | 5 | 4 | 3 | 2 | 1 | 1 |
6 | 5 | 4 | 3 | 2 | 1 | 0 |
bottom-up approach
kitten | itten | tten | ten | en | n | ||
sitting | 3 | 3 | 4 | 5 | 6 | 6 | 7 |
itting | 3 | 2 | 3 | 4 | 5 | 5 | 6 |
tting | 4 | 3 | 2 | 3 | 4 | 4 | 5 |
ting | 5 | 4 | 3 | 2 | 3 | 3 | 4 |
ing | 5 | 4 | 4 | 3 | 2 | 2 | 3 |
ng | 6 | 5 | 4 | 3 | 2 | 1 | 2 |
g | 6 | 5 | 4 | 3 | 2 | 1 | 1 |
6 | 5 | 4 | 3 | 2 | 1 | 0 |