recitation6

# recitation6 - Dynamic Programming: The Edit Distance...

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 Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the 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 bottom-up 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(n-1) + fib(n-2) 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(n-1) + fib(n-2)); } } 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(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) 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(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) 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(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) 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(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) 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

## recitation6 - Dynamic Programming: The Edit Distance...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online