Edit Distance
Edit Distance
Source: a l g o r i t h m
Output:
Operations: (none)
Source: a l g o r i t h m
Output:
Operations: (none)
Source: a l g o r i t h m
Output: a
Operations: Copy
Source: a l g o r i t h m
Output: a l
Source: a l g o r i t h m
Output: a l l
Source: a l g o r i t h m
Output: a l l i
Source: a l g o r i t h m
Output: a l l i g
Source: a l g o r i t h m
Output: a l l i g
Source: a l g o r i t h m
Output: a l l i g
Source: a l g o r i t h m
Output: a l l i g
Source: a l g o r i t h m
Output: a l l i g a
Source: a l g o r i t h m
Output: a l l i g a t
Source: a l g o r i t h m
Output: a l l i g a t
Source: a l g o r i t h m
Output: a l l i g a t o r
a
Y
l
X
a l g o r i
a
Y
l
X
a l g o r i
a
Y
l
X
a l g o r i
a
Y
l
a
Y
l
X
a l g o r i
a
Y
l
X
a l g o r i
a
Y
l
X
a l g o r i
a
Y
l
X
a l g o r i
0
null
a
Y
l
X
a l g o r i
0
null
a
Y
l
0
null
a
Y
l
X
a l g o r i
0 10
null Del
a
Y
l
X
a l g o r i
0 10 20 30 40 50 60
null Del Del Del Del Del Del
a
Y
l
X
a l g o r i
0 10 20 30 40 50 60
null Del Del Del Del Del Del
a 10
Y Ins(a)
l
0 10 20 30 40 50 60
null Del Del Del Del Del Del
a 10
Y Ins(a)
l
0 10 20 30 40 50 60
null Del Del Del Del Del Del
a 10 5
Y Ins(a) Cop
l
X
a l g o r i
0 10 20 30 40 50 60
null Del Del Del Del Del Del
a 10 5
Y Ins(a) Cop
l
X
a l g o r i
0 10 20 30 40 50 60
null Del Del Del Del Del Del
a 10 5 30
Y Ins(a) Cop Ins(a)
l
X
a l g o r i
0 10 20 30 40 50 60
null Del Del Del Del Del Del
a 10 5 15
Y Ins(a) Cop Del
l
X
a l g o r i
0 10 20 30 40 50 60
null Del Del Del Del Del Del
a 10 5 15 25 35 45 55
Y Ins(a) Cop Del Del Del Del Del
l 20 15 10 20 30 40 50
Ins(l) Ins(l) Cop Del Del Del Del
CS 2: Introduction to Programming Methods The Edit Distance Problem 68
http://www.cs.caltech.edu/courses/cs2/ 9 Feburary 2004
Transforming “algori” to “al”
The cheapest sequence of operations has cost: 50
X
a l g o r i
0 10 20 30 40 50 60
null Del Del Del Del Del Del
a 10 5 15 25 35 45 55
Y Ins(a) Cop Del Del Del Del Del
l 20 15 10 20 30 40 50
Ins(l) Ins(l) Cop Del Del Del Del
CS 2: Introduction to Programming Methods The Edit Distance Problem 69
http://www.cs.caltech.edu/courses/cs2/ 9 Feburary 2004
Transforming “algori” to “al” (cont.)
Recover the sequence by working backwards.
We get: Copy, Copy, Delete, Delete, Delete, Delete.
X
a l g o r i
0 10 20 30 40 50 60
null Del Del Del Del Del Del
a 10 5 15 25 35 45 55
Y Ins(a) Cop Del Del Del Del Del
l 20 15 10 20 30 40 50
Ins(l) Ins(l) Cop Del Del Del Del
CS 2: Introduction to Programming Methods The Edit Distance Problem 70
http://www.cs.caltech.edu/courses/cs2/ 9 Feburary 2004
Why this worked
This worked becuase of the three observations we
made earlier.
X
a l g o r i
0 10 20 30 40 50 60
null Del Del Del Del Del Del
a 10 5 15 25 35 45 55
Y Ins(a) Cop Del Del Del Del Del
l 20 15 10 20 30 40 50
Ins(l) Ins(l) Cop Del Del Del Del
CS 2: Introduction to Programming Methods The Edit Distance Problem 71
http://www.cs.caltech.edu/courses/cs2/ 9 Feburary 2004
Why this worked (cont.)
The best answer to put in a location must use the best
solution to one of three possible subproblems.
X
a l g o r i
a
Y
l
a
Y
l
X
a l g o r i
0 10 20 30 40 50 60
null Del Del Del Del Del Del
a 10 5 15 25 35 45 55
Y Ins(a) Cop Del Del Del Del Del
l 20 15 10 20 30 40 50
Ins(l) Ins(l) Cop Del Del Del Del
CS 2: Introduction to Programming Methods The Edit Distance Problem 74
http://www.cs.caltech.edu/courses/cs2/ 9 Feburary 2004
Why this worked (cont.)
Each location’s operation tells us where to look for the
previous one in the sequence.
X
a l g o r i
0 10 20 30 40 50 60
null Del Del Del Del Del Del
a 10 5 15 25 35 45 55
Y Ins(a) Cop Del Del Del Del Del
l 20 15 10 20 30 40 50
Ins(l) Ins(l) Cop Del Del Del Del
CS 2: Introduction to Programming Methods The Edit Distance Problem 75
http://www.cs.caltech.edu/courses/cs2/ 9 Feburary 2004