Lecture 9. Dynamic Programming: optimal coin change We have seen this problem before: you are given an amount in cents, and you want to make change using.

Slides:



Advertisements
Similar presentations
Dynamic Programming 25-Mar-17.
Advertisements

Greedy Algorithms (Chap. 16)
Euclidean Algorithm Applied Symbolic Computation CS 567 Jeremy Johnson.
Chapter 5 Fundamental Algorithm Design Techniques.
WS Algorithmentheorie 03 – Randomized Algorithms (Primality Testing) Prof. Dr. Th. Ottmann.
Greedy Algorithms Basic idea Connection to dynamic programming
15-May-15 Dynamic Programming. 2 Algorithm types Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
Greedy Algorithms Basic idea Connection to dynamic programming Proof Techniques.
16-May-15 Dynamic Programming. 2 Algorithm types Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
CSE115/ENGR160 Discrete Mathematics 02/28/12
Merge Sort 4/15/2017 6:09 PM The Greedy Method The Greedy Method.
1 Dynamic Programming Jose Rolim University of Geneva.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Elementary Number Theory and Methods of Proof. Basic Definitions An integer n is an even number if there exists an integer k such that n = 2k. An integer.
3 Gallon Jug5 Gallon Jug Greatest Common Divisor Lecture 8: Sep 30.
Chapter II. THE INTEGERS
6/20/2015 5:05 AMNumerical Algorithms1 x x1x
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Lecture 8: Dynamic Programming Shang-Hua Teng. First Example: n choose k Many combinatorial problems require the calculation of the binomial coefficient.
CS 206 Introduction to Computer Science II 10 / 16 / 2009 Instructor: Michael Eckmann.
Lecture 37 CSE 331 Dec 1, A new grading proposal Towards your final score in the course MAX ( mid-term as 25%+ finals as 40%, finals as 65%) .
1 The Greedy Method CSC401 – Analysis of Algorithms Lecture Notes 10 The Greedy Method Objectives Introduce the Greedy Method Use the greedy method to.
10/31/02CSE Greedy Algorithms CSE Algorithms Greedy Algorithms.
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
Dan Boneh Intro. Number Theory Modular e’th roots Online Cryptography Course Dan Boneh.
9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Guest lecturer: Martin Furer Algorithm Design and Analysis L ECTURE.
10/31/02CSE Greedy Algorithms CSE Algorithms Greedy Algorithms.
Great Theoretical Ideas in Computer Science for Some.
CS1101: Programming Methodology Aaron Tan.
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Chapter 2 The Fundamentals: Algorithms, the Integers, and Matrices
Mathematics of Cryptography Part I: Modular Arithmetic
COMP 170 L2 Page 1 L05: Inverses and GCDs l Objective: n When does have an inverse? n How to compute the inverse? n Need: Greatest common dividers (GCDs)
Approximation Algorithms Pages ADVANCED TOPICS IN COMPLEXITY THEORY.
Greatest Common Divisor
Additional Problems.
1 Introduction to Abstract Mathematics Chapter 3: Elementary Number Theory and Methods of Proofs Instructor: Hayk Melikya Direct.
CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.
Chapter Algorithms 3.2 The Growth of Functions 3.3 Complexity of Algorithms 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors.
1 Algorithms & Data Structures for Games Lecture 2A Minor Games Programming.
COMPSCI 102 Introduction to Discrete Mathematics.
Application: Algorithms Lecture 20 Section 3.8 Wed, Feb 21, 2007.
PROBLEM-SOLVING TECHNIQUES Rocky K. C. Chang November 10, 2015.
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
AF2. Turn off your phones Primes, gcd, some examples, reading.
Lecture. Today Problem set 9 out (due next Thursday) Topics: –Complexity Theory –Optimization versus Decision Problems –P and NP –Efficient Verification.
AF2. Turn off your phones Primes, gcd, some examples, reading.
Chapter 4 With Question/Answer Animations 1. Chapter Summary Divisibility and Modular Arithmetic - Sec 4.1 – Lecture 16 Integer Representations and Algorithms.
Greedy Algorithms Prof. Kharat P. V. Department of Information Technology.
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett.
Dynamic Programming 26-Apr-18.
CSE 311 Foundations of Computing I
Merge Sort 7/29/ :21 PM The Greedy Method The Greedy Method.
Great Theoretical Ideas in Computer Science
Greatest Common Divisor
Greedy Algorithms Basic idea Connection to dynamic programming
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
Lecture 10. Dynamic Programming I
Greedy Algorithms / Interval Scheduling Yin Tat Lee
Lecture 20 Guest lecturer: Neal Gupta
Merge Sort 11/28/2018 2:18 AM The Greedy Method The Greedy Method.
Merge Sort 11/28/2018 8:16 AM The Greedy Method The Greedy Method.
Discrete Math for CS CMPSC 360 LECTURE 12 Last time: Stable matching
Merge Sort 1/12/2019 5:31 PM Dynamic Programming Dynamic Programming.
Merge Sort 1/17/2019 3:11 AM The Greedy Method The Greedy Method.
Merge Sort 1/18/ :45 AM Dynamic Programming Dynamic Programming.
Merge Sort 2/22/ :33 AM Dynamic Programming Dynamic Programming.
Dynamic Programming 23-Feb-19.
Merge Sort 5/2/2019 7:53 PM The Greedy Method The Greedy Method.
Presentation transcript:

Lecture 9. Dynamic Programming: optimal coin change We have seen this problem before: you are given an amount in cents, and you want to make change using a system of denominations, using the smallest number of coins possible. Sometimes the greedy algorithm gives the optimal solution. But sometimes (as we have seen) it does not -- an example was the system (12, 5, 1), where the greedy algorithm gives 15 = but a better answer is 15 = And sometimes Greedy cannot even find the proper changes: (25,10,4), change for 41 cents. So how can we find the optimal solution (fewest coins) when the greedy algorithm may not work? One way is using dynamic programming.

Optimal coin-changing -- The idea: to make change for n cents, the optimal method must use some denomination d i. That is, the optimal is made by choosing the optimal solution for n – d i for some d i, and adding one coin of d i to it. We don't know which d i to use, but some must work. So we try them all, assuming we know how to make optimal changes for < n cents. To formalize: letting opt[j] be the optimal number of coins to make change for j cents, we have: opt[j] = 1 + min( opt[j-d i ]) over all d i ≤j

Optimal coin-change: coins(n, d[1..k]) /* returns optimal number of coins to make change for n using denominations d[1..k], with d[1] = 1 */ for j := 1 to n do opt[j] := infinity; for i := k downto 1 do if d[i] = j then opt[j] := 1; largest[j] := j; else if d[i] < j then a := 1+opt[j-d[i]]; if a < opt[j] then opt[j] := a; largest[j] := d[i]; return(opt[n]) Running time: O(nk) Input size: ?

Example. Suppose we use the system of denominations (1,5,18,25). To represent 29, the greedy algorithm gives , Our algorithm: largest coin =11, which has a representation of size 3, with largest coin =6, which has a representation of size 2, with largest coin =1. So 29 = j opt[j] largest[j] j opt[j] largest[j]

Paradigm #7. Exploiting the problems’ structure Example: Computing GCD We say d is a divisor of u if there exists an integer k such that u = kd. We write d | u. We say d is a common divisor of u and v if d | u and d | v. We say d is the greatest common divisor if d is a common divisor of u and v and no other common divisor is larger. Example: gcd(12,18)=6. Observe: d | u & d | v iff d | v and d | (u mod v) Euclid’s Alg (300BC) Euclid(u,v) while (v ≠ 0) do (u, v) := (v, u mod v) return(u) Thm (Finck 1841). This algorithm runs in O(log v) steps, for u>v>0. Proof: Let r = u mod v. Case 1. v>u/2, then r =u-v < u/2 Case 2. v=u/2, then r=0 Case 3. v< u/2, then r<v<u/2 So after 2 steps, the numbers decrease by factor of 2. Hence after O(log v) steps, to 0.