08 Dynamic Programming

# 08 Dynamic Programming - Dynamic Programming part 2 15-211...

This preview shows pages 1–14. Sign up to view the full content.

Dynamic Programming, part 2 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 4 February 2010 Reading for today: Section 7.6

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

View Full Document
2 Announcements HW 2 Theory due next Tuesday in class Programming due next Thursday night
3 Today’s outline More dynamic programming examples II: Change-making Strikes Back III: Return of the Change-making Longest Common Subsequence Optimal Binary Search Trees (maybe…)

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

View Full Document
Dynamic Programming
5 Last time…Dynamic Programming Observation: Inefficient, or “brute-force,” solutions to (optimization) problems often have simple recursive definitions that can apply dynamic programming solutions.

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

View Full Document
6 Ingredients of dynamic prog Simple subproblems. Problem can be broken into subproblems, typically with solutions that are easy to store in a table/array. Subproblem optimization. Optimal solution is composed of optimal subproblem solutions. Subproblem overlap. Optimal solutions to separate subproblems can have subproblems in common.
7 Dynamic programming Underlying idea: As smaller subproblems are solved, solving the larger subproblems might get easier. Avoid recomputing subproblems so that overlap can be exploited. Can sometimes reduce seemingly exponential problems to polynomial time.

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

View Full Document
8 Two main methods To keep track of subproblems use: Memoizing : Store partial results in a hashtable, array, … for later reuse. Keep track of solutions to subproblems that are actually needed to solved the problem. A top-down, recursive approach Dynamic Programming : Construct a table of partial results. A bottom-up, iterative approach
9 Top-down vs. Bottom-up Top-down Incurs cost of recursion overhead. Faster when reasonable fraction of subproblems are never needed. Often use a hash table. Bottom-up Need to determine correct order in which to compute values. Efficient (fast) when all (most) values are used. Use a one- or two-dimensional table. Can sometimes avoid maintaining a large table

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

View Full Document
10 Suppose we have infinite coins of denominations 1 d 1 < d 2 < … < d k How can we sum to x using as few coins as possible? Generalized change-making
11 d 1 = 1, d 2 = 5, d 3 = 10, d 4 = 21, d 5 = 25 x = 63 We know best solution is one of: One 1-cent coin, plus solution for 62 cents One 5-cent coin, plus solution for 58 cents One 10-cent coin, plus solution for 53 cents One 21-cent coin, plus solution for 42 cents One 25-cent coin, plus solution for 38 cents Suggests a recursive algorithm: Generalized change-making MakeChange(x): If (x = 0) Return 0; If (x < 0) Return INFINITY; Return 1 + Min i {MakeChange(x-d i )};

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

View Full Document
12 Generalized change-making Recursive: Dynamic Programming: MakeChange(x): If (x = 0) Return 0; If (x < 0) Return INFINITY; Return 1 + Min j {MakeChange(x-d j )}; MakeChange(x): c[0] 0; For i from –d k to -1 c[i] INFINITY; For i from 1 to x c[i] 1 + Min j {c[i-d j ]}; Return c[x];
Generalized change-making d 1 = 1, d 2 = 5, d 3 = 10, d 4 = 21, d 5 = 25 x = 63 How many coins to make i cents?

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.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern