08 Dynamic Programming

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

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

View Full Document Right Arrow Icon
Dynamic Programming, part 2 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 4 February 2010 Reading for today: Section 7.6
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Announcements HW 2 Theory due next Tuesday in class Programming due next Thursday night
Background image of page 2
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…)
Background image of page 3

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

View Full DocumentRight Arrow Icon
Dynamic Programming
Background image of page 4
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.
Background image of page 5

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

View Full DocumentRight Arrow Icon
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.
Background image of page 6
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.
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
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
Background image of page 9

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

View Full DocumentRight Arrow Icon
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
Background image of page 10
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 )};
Background image of page 11

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

View Full DocumentRight Arrow Icon
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];
Background image of page 12
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?
Background image of page 13

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

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 70

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online