Professional Documents
Culture Documents
Today
A review of dynamic programming. The Edit Distance problem.
Dynamic programming
A method for developing algorithms. Given a problem to solve, dynamic programming says:
Solve subproblems of the problem first. Remember these solutions for later. Then put together a solution to the problem.
10
11
13
14
15
16
Initially, you start at the first character of the source and have an empty output. At any point, you can:
Delete the current character of the source. Insert a new character into the output word. Copy the current character of the source into the output.
Example: algorithm
alligator
Source: Output:
Operations: (none)
19
Example: algorithm
alligator
Source: Output:
Operations: (none)
20
Example: algorithm
alligator
Source: Output:
a a
Operations: Copy
21
Example: algorithm
alligator
Source: Output:
a a
l l
22
Example: algorithm
alligator
Source: Output:
a a
l l
g l
23
Example: algorithm
alligator
Source: Output:
a a
l l
g l
o i
24
Example: algorithm
alligator
Source: Output:
a a
l l
g l
o i
r g
25
Example: algorithm
alligator
Source: Output:
a a
l l
g l
o i
r g
Example: algorithm
alligator
Source: Output:
a a
l l
g l
o i
r g
Example: algorithm
alligator
Source: Output:
a a
l l
g l
o i
r g
Example: algorithm
alligator
Source: Output:
a a
l l
g l
o i
r g
i a
Operations: Copy, Copy, Insert(l), Insert(i), Copy Delete, Delete, Delete, Insert(a)
CS 2: Introduction to Programming Methods http://www.cs.caltech.edu/courses/cs2/ The Edit Distance Problem 9 Feburary 2004 29
Example: algorithm
alligator
Source: Output:
a a
l l
g l
o i
r g
i a
t t
Operations: Copy, Copy, Insert(l), Insert(i), Copy Delete, Delete, Delete, Insert(a), Copy
CS 2: Introduction to Programming Methods http://www.cs.caltech.edu/courses/cs2/ The Edit Distance Problem 9 Feburary 2004 30
Example: algorithm
alligator
Source: Output:
a a
l l
g l
o i
r g
i a
t t
Operations: Copy, Copy, Insert(l), Insert(i), Copy Delete, Delete, Delete, Insert(a), Copy, Delete, Delete
CS 2: Introduction to Programming Methods http://www.cs.caltech.edu/courses/cs2/ The Edit Distance Problem 9 Feburary 2004 31
Example: algorithm
alligator
Source: Output:
a a
l l
g l
o i
r g
i a
t t
h o
m r
Operations: Copy, Copy, Insert(l), Insert(i), Copy Delete, Delete, Delete, Insert(a), Copy, Delete, Delete, Insert(o), Insert(r)
CS 2: Introduction to Programming Methods http://www.cs.caltech.edu/courses/cs2/ The Edit Distance Problem 9 Feburary 2004 32
Each operation has a cost associated with it. The cost of a transformation is the sum of the costs of each operation in the sequence.
33
Example: algorithm
alligator
Operations: Copy, Copy, Insert(l), Insert(i), Copy Delete, Delete, Delete, Insert(a), Copy, Delete, Delete, Insert(o), Insert(r) Assume that:
Copying costs 3 Inserting costs 5 Deleting costs 2
34
35
36
37
38
39
Notation
(s1, s2, s3, ..., sn) stands for a sequence of n elements. Used for a list of operations.
Example: (Copy, Copy, Insert(c), Delete).
40
Key Observation #1
Given: Strings X = (x1, ..., xn) and Y = (y1, ..., yt), and a sequence S of operations (s1, s2, ..., sm). Let S = (s1, ..., s(m-1)), X = (x1, ..., x(n-1)), and Y = (y1, ..., y(t-1)). Suppose that S is the cheapest sequence of operations to transform X into Y, and that sm is a Copy operation.
41
42
Key Observation #2
Given: Strings X = (x1, ..., xn) and Y = (y1, ..., yt), and a sequence S of operations (s1, s2, ..., sm). Let S = (s1, ..., s(m-1)) and X = (x1, ..., x(n-1)). Suppose that S is the cheapest sequence of operations to transform X into Y, and that sm is a Delete operation.
43
44
Key Observation #3
Given: Strings X = (x1, ..., xn) and Y = (y1, ..., yt), and a sequence S of operations (s1, s2, ..., sm). Let S = (s1, ..., s(m-1)) and Y = (y1, ..., y(t-1)). Suppose that S is the cheapest sequence of operations to transform X into Y, and that sm is an Insert(yn) operation.
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
X
a 0 null a 10 Del l 20 Del g 30 Del o 40 Del r 50 Del i 60 Del
Y
l
10 Ins(a)
62
Y
l
10 Ins(a)
63
Y
l
10 5 Ins(a) Cop
64
Y
l
10 5 Ins(a) Cop
65
X
a 0 null a 10 Del l 20 Del 30 Ins(a) g 30 Del o 40 Del r 50 Del i 60 Del
Y
l
10 5 Ins(a) Cop
66
X
a 0 null a 10 Del l 20 Del 15 Del g 30 Del o 40 Del r 50 Del i 60 Del
Y
l
10 5 Ins(a) Cop
67
X
a 0 null a 10 Del l 20 Del 15 Del g 30 Del 25 Del 20 Del o 40 Del 35 Del 30 Del r 50 Del 45 Del 40 Del i 60 Del 55 Del 50 Del
68
Y
l
15 10 Ins(l) Cop
Transforming algori to al
The cheapest sequence of operations has cost: 50
X
a 0 null a 10 Del l 20 Del 15 Del g 30 Del 25 Del 20 Del o 40 Del 35 Del 30 Del r 50 Del 45 Del 40 Del i 60 Del 55 Del 50 Del
69
Y
l
15 10 Ins(l) Cop
Y
l
15 10 Ins(l) Cop
Y
l
15 10 Ins(l) Cop
72
73
Y
l
15 10 Ins(l) Cop
Y
l
15 10 Ins(l) Cop