Title: Dynamic Programming: The Edit Distance Problem
1Dynamic ProgrammingThe Edit Distance Problem
- CS 2 Introduction to Programming Methods
- 9 February 2004
2Today
- A review of dynamic programming.
- The Edit Distance problem.
3ReviewDynamic Programming
4Dynamic 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.
- Think of this as a bottom-up approach.
- Solve problems in order of increasing size.
5Example The fibonacci sequence
- fib(0) 0
- fib(1) 1
- fib(2) 1
- fib(3) 2
- fib(4) 3
- fib(5) 5
-
- fib(n) fib(n-1) fib(n-2)
6Naïvely computing fib(n)
- int fib(int n)
- if (n 0 n 1)
- return n
- else
- return (fib(n-1) fib(n-2))
-
7Naïvely computing fib(n)
fib(n)
fib(n-2)
fib(n-1)
fib(n-4)
fib(n-3)
fib(n-3)
fib(n-2)
fib(n-6)
fib(n-5)
fib(n-5)
fib(n-4)
fib(n-5)
fib(n-4)
fib(n-4)
fib(n-3)
8Naïvely computing fib(n)
fib(n)
fib(n-2)
fib(n-1)
fib(n-4)
fib(n-3)
fib(n-3)
fib(n-2)
fib(n-6)
fib(n-5)
fib(n-5)
fib(n-4)
fib(n-5)
fib(n-4)
fib(n-4)
fib(n-3)
9Naïvely computing fib(n)
fib(n)
fib(n-2)
fib(n-1)
fib(n-4)
fib(n-3)
fib(n-3)
fib(n-2)
fib(n-6)
fib(n-5)
fib(n-5)
fib(n-4)
fib(n-5)
fib(n-4)
fib(n-4)
fib(n-3)
10Naïvely computing fib(n)
fib(n)
fib(n-2)
fib(n-1)
fib(n-4)
fib(n-3)
fib(n-3)
fib(n-2)
fib(n-6)
fib(n-5)
fib(n-5)
fib(n-4)
fib(n-5)
fib(n-4)
fib(n-4)
fib(n-3)
11How to fix things...
- The solution to fib(n) depends on
- The solution to fib(n - 1).
- The solution to fib(n - 2).
- Use dynamic programming to organize everything
- Each subproblem is characterized by the value of
its input. - To compute fib(n), we need fib(n - 2) and fib(n
- 1). - So compute fib(0) first, and work upwards.
- Use an array to keep track of everything.
12Computing fib(n) Take 2
- int fib(int n)
- int seq new intn1
- seq0 0
- seq1 1
- for (int i 2 i lt n i)
- // looks like fib(i) fib(i - 1) fib(i - 2)
- seqi seqi - 1 seqi - 2
-
- return seqn
13Computing fib(n) Take 2
- int fib(int n)
- // This array will keep track of the solutions
to - // all n1 problems.
- int seq new intn 1
- ...
14Computing fib(n) Take 2
- int fib(int n)
- ...
- // Assume that (n gt 1). Otherwise, seq is to
small. - // The smallest subproblems have trivial
solutions. - seq0 0
- seq1 1
- ...
15Computing fib(n) Take 2
- int fib(int n)
- ...
- // Solve other subproblems in order of
increasing size. - // Use the fact that weve already solved even
smaller - // subproblems already.
- for (int i 2 i lt n i)
- seqi seqi - 1 seqi - 2
-
- ...
16Computing fib(n) Take 2
- int fib(int n)
- ...
- // Finally, return solution to the actual
problem. - return seqn
17The Edit Distance Problem
18The Edit Distance problem
- Problem what is the cheapest way to transform
one word (the source) into another word (the
output)? - Example transform algorithm into alligator.
- 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. - Copying and deleting move you to the next
character.
19Example algorithm ? alligator
- Source a l g o r i t h m
- Output
- Operations (none)
20Example algorithm ? alligator
- Source a l g o r i t h m
- Output
- Operations (none)
21Example algorithm ? alligator
- Source a l g o r i t h m
- Output a
- Operations Copy
22Example algorithm ? alligator
- Source a l g o r i t h m
- Output a l
- Operations Copy, Copy
23Example algorithm ? alligator
- Source a l g o r i t h m
- Output a l l
- Operations Copy, Copy, Insert(l)
24Example algorithm ? alligator
- Source a l g o r i t h m
- Output a l l i
- Operations Copy, Copy, Insert(l), Insert(i)
25Example algorithm ? alligator
- Source a l g o r i t h m
- Output a l l i g
- Operations Copy, Copy, Insert(l), Insert(i),
Copy
26Example algorithm ? alligator
- Source a l g o r i t h m
- Output a l l i g
- Operations Copy, Copy, Insert(l), Insert(i),
Copy - Delete
27Example algorithm ? alligator
- Source a l g o r i t h m
- Output a l l i g
- Operations Copy, Copy, Insert(l), Insert(i),
Copy - Delete, Delete
28Example algorithm ? alligator
- Source a l g o r i t h m
- Output a l l i g
- Operations Copy, Copy, Insert(l), Insert(i),
Copy - Delete, Delete, Delete
29Example algorithm ? alligator
- Source a l g o r i t h m
- Output a l l i g a
- Operations Copy, Copy, Insert(l), Insert(i),
Copy - Delete, Delete, Delete, Insert(a)
30Example algorithm ? alligator
- Source a l g o r i t h m
- Output a l l i g a t
- Operations Copy, Copy, Insert(l), Insert(i),
Copy - Delete, Delete, Delete, Insert(a), Copy
31Example algorithm ? alligator
- Source a l g o r i t h m
- Output a l l i g a t
- Operations Copy, Copy, Insert(l), Insert(i),
Copy - Delete, Delete, Delete, Insert(a), Copy,
Delete, - Delete
32Example algorithm ? alligator
- Source a l g o r i t h m
- Output a l l i g a t o r
- Operations Copy, Copy, Insert(l), Insert(i),
Copy - Delete, Delete, Delete, Insert(a), Copy,
Delete, - Delete, Insert(o), Insert(r)
33The Edit Distance problem
- Problem what is the cheapest way to transform
one word (the source) into another word (the
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. - 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.
34Example 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
- The cost of the above transformation is 47
- This is just 3 3 5 5 3 2 2 ...
35The Edit Distance problem
- Problem what is the cheapest way to transform
one word (the source) into another word (the
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. - By cheapest, we mean the transformation with
the least cost.
36How to solve this problem?
- Will trying out all possible transformations work?
37How to solve this problem?
- Will trying out all possible transformations
work? - Answer It can, but it would take way too long.
38Will dynamic programming work?
- Depends on our problem.
- Can our problem be solved based on the solution
of some subproblems? - What are the subproblems, if there any?
39Will dynamic programming work?
- Depends on our problem.
- Can our problem be solved based on the solution
of some subproblems? - What are the subproblems, if there any?
- As is typical when trying out dynamic
programming, it helps to find some key
observation or insight. - We will need three observations for this problem.
40Notation
- (s1, s2, s3, ..., sn) stands for a sequence of n
elements. - Used for a list of operations.
- Example (Copy, Copy, Insert(c), Delete).
- Also used for strings.
- Example help would correspond to (h, e, l ,p).
41Key Observation 1
- Given Strings X (x1, ..., xn) and Y (y1,
..., yt), anda sequence S of operations (s1, s2,
..., sm). - Let S (s1, ..., s(m-1)), X (x1, ...,
x(n-1)), andY (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.
42Key Observation 1 (cont.)
- Claim then, S is the cheapest sequence of
operations to transform X to Y. - Proof by contradiction. Suppose that T (t1,
..., tk) is cheaper than S and tranforms X to
Y. - Then (t1, ..., tk, sm) is cheaper than S and
transforms X to Y. This is a contradiction, as S
was supposed to be the cheapest way to transform
X to Y.
43Key Observation 2
- Given Strings X (x1, ..., xn) and Y (y1,
..., yt), anda 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.
44Key Observation 2 (cont.)
- Claim then, S is the cheapest sequence of
operations to transform X to Y. - Proof by contradiction. Suppose that T (t1,
..., tk) is cheaper than S and tranforms X to
Y. - Then (t1, ..., tk, sm) is cheaper than S and
transforms X to Y. This is a contradiction, as S
was supposed to be the cheapest way to transform
X to Y.
45Key Observation 3
- Given Strings X (x1, ..., xn) and Y (y1,
..., yt), anda 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.
46Key Observation 3 (cont.)
- Claim then, S is the cheapest sequence of
operations to transform X to Y. - Proof by contradiction. Suppose that T (t1,
..., tk) is cheaper than S and tranforms X to
Y. - Then (t1, ..., tk, sm) is cheaper than S and
transforms X to Y. This is a contradiction, as S
was supposed to be the cheapest way to transform
X to Y.
47An observation about the observations...
- Given X, Y, and S, we have covered all possible
cases for sm (the last operation).
48Putting the observations to use
- The best sequence of operations to transform X to
Y depended on one of the following - (1) The best way to transform X to Y.
- (2) The best way to transform X to Y.
- (3) The best way to transform X to Y.
- The three cases become the subproblems to
consider. - Given the solution to all three, we can find the
solution to our actual problem (transforming X to
Y). - Why? The best solution to transforming X to Y
must contain a solution to one of the three cases
by the three observations.
49How to organize the subproblems
- Each subproblem is characterized by some initial
part of the original strings X and Y. - So use a matrix.
X
Y
50How to organize the subproblems (cont.)
- The marked location contains the score for the
cheapest way to transform algo to a.
X
Y
51How to organize the subproblems (cont.)
- The marked location contains the score for the
cheapest way to transform alg to .
X
Y
52How to organize the subproblems (cont.)
- The marked location contains the score for the
cheapest way to transform to a.
X
Y
53How to organize the subproblems (cont.)
- The marked location contains the score for the
cheapest way to transform to . - This is the smallest problem possible.
X
Y
54Additional information to store
- Each location should store the last operation in
the cheapest sequence of operations used to get
there.
X
Y
55Additional information to store (cont.)
- Example the marked location would contain a
score, and possibly a Copy operation.
X
Y
56How to fill out the matrix
- For concreteness, assume that Copy costs 5,
Insert(c) costs 10, and Delete costs 10.
X
Y
57How to fill out the matrix (cont.)
- Start at the upper left.
- This is trivial score is zero, operation is null.
X
Y
58How to fill out the matrix (cont.)
- All other locations depend on the values in up to
three other places.
X
Y
59How to fill out the matrix (cont.)
- Diagonal movement corresponds to a Copy
operation. - Vertical ? Insert(c).
- Horizontal ? Delete.
X
Y
60How to fill out the matrix (cont.)
- Fill out each row in turn.
- Only one option for the first row...
X
Y
61How to fill out the matrix (cont.)
- Fill out each row in turn.
- Only one option for the first row...
X
Y
62How to fill out the matrix (cont.)
- Fill out each row in turn.
X
Y
63How to fill out the matrix (cont.)
- Three possibilities to consider for the marked
square. - Clear that Copy is cheapest. (5 versus 20 for
Insert(a) or Delete).
X
Y
64How to fill out the matrix (cont.)
- Three possibilities to consider for the marked
square. - Clear that Copy is cheapest. (5 versus 20 for
Insert(a) or Delete).
X
Y
65How to fill out the matrix (cont.)
- Two possibilities to consider for the marked
square. - Copy is not possible since a ! l.
X
Y
66How to fill out the matrix (cont.)
- If we chose Insert(a)....
X
Y
67How to fill out the matrix (cont.)
X
Y
68How to fill out the matrix (cont.)
- Fill the rest of the matrix out in a similar
fashion.
X
Y
69Transforming algori to al
- The cheapest sequence of operations has cost 50
X
Y
70Transforming algori to al (cont.)
- Recover the sequence by working backwards.
- We get Copy, Copy, Delete, Delete, Delete,
Delete.
X
Y
71Why this worked
- This worked becuase of the three observations we
made earlier.
X
Y
72Why this worked (cont.)
- The best answer to put in a location must use the
best solution to one of three possible
subproblems.
X
Y
73Why this worked (cont.)
- So we solved those subproblems first.
- Then we considered cases and figured out how best
to solve our current problem.
X
Y
74Why this worked (cont.)
- We could recover the cheapest sequence of
operations since we stored operations at each
step.
X
Y
75Why this worked (cont.)
- Each locations operation tells us where to look
for the previous one in the sequence.
X
Y