This preview shows pages 1–12. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Dynamic Programming: The Edit Distance Problem CS 2: Introduction to Programming Methods 8 February 2006 The Edit Distance Problem 8 Feburary 2006 CS 2: Introduction to Programming Methods http://www.cs.caltech.edu/courses/cs2/ 2 Today A review of dynamic programming. The Edit Distance problem. Review: Dynamic Programming The Edit Distance Problem 8 Feburary 2006 CS 2: Introduction to Programming Methods http://www.cs.caltech.edu/courses/cs2/ 4 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. Think of this as a bottomup approach. Solve problems in order of increasing size. The Edit Distance Problem 8 Feburary 2006 CS 2: Introduction to Programming Methods http://www.cs.caltech.edu/courses/cs2/ 5 Example: The fibonacci sequence fib(0) = 0 fib(1) = 1 fib(2) = 1 fib(3) = 2 fib(4) = 3 fib(5) = 5 fib(n) = fib(n1) + fib(n2) The Edit Distance Problem 8 Feburary 2006 CS 2: Introduction to Programming Methods http://www.cs.caltech.edu/courses/cs2/ 6 Navely computing fib(n) int fib( int n) { if (n == 0  n == 1) { return n; } else { return (fib(n1) + fib(n2)); } } The Edit Distance Problem 8 Feburary 2006 CS 2: Introduction to Programming Methods http://www.cs.caltech.edu/courses/cs2/ 7 Navely computing fib(n) fib(n) fib(n2) fib(n1) fib(n4) fib(n3) fib(n3) fib(n2) fib(n6) fib(n5) fib(n5) fib(n4) fib(n5) fib(n4) fib(n4) fib(n3) The Edit Distance Problem 8 Feburary 2006 CS 2: Introduction to Programming Methods http://www.cs.caltech.edu/courses/cs2/ 8 Navely computing fib(n) fib(n) fib(n2) fib(n1) fib(n4) fib(n3) fib(n3) fib(n2) fib(n6) fib(n5) fib(n5) fib(n4) fib(n5) fib(n4) fib(n4) fib(n3) The Edit Distance Problem 8 Feburary 2006 CS 2: Introduction to Programming Methods http://www.cs.caltech.edu/courses/cs2/ 9 Navely computing fib(n) fib(n) fib(n2) fib(n1) fib(n4) fib(n3) fib(n3) fib(n2) fib(n6) fib(n5) fib(n5) fib(n4) fib(n5) fib(n4) fib(n4) fib(n3) The Edit Distance Problem 8 Feburary 2006 CS 2: Introduction to Programming Methods http://www.cs.caltech.edu/courses/cs2/ 10 Navely computing fib(n) fib(n) fib(n2) fib(n1) fib(n4) fib(n3) fib(n3) fib(n2) fib(n6) fib(n5) fib(n5) fib(n4) fib(n5) fib(n4) fib(n4) fib(n3) The Edit Distance Problem 8 Feburary 2006 CS 2: Introduction to Programming Methods http://www.cs.caltech.edu/courses/cs2/ 11 How 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....
View Full
Document
 Winter '08
 Barr,A
 C Programming

Click to edit the document details