08 Dynamic Programming

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

Info icon This 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
Image of page 1

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

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

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

View Full Document Right Arrow Icon
Dynamic Programming
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.
Image of page 5

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

View Full Document Right 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.
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.
Image of page 7

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

View Full Document Right 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
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
Image of page 9

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

View Full Document Right 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
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 )};
Image of page 11

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

View Full Document Right 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];
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?
Image of page 13

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern