Dynamic Programming CSC 172 SPRING 2002 LECTURE 6.

Slides:



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

Dynamic Programming (DP)
CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
§3 Dynamic Programming Use a table instead of recursion 1. Fibonacci Numbers: F(N) = F(N – 1) + F(N – 2) int Fib( int N ) { if ( N
Types of Algorithms.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),
15-May-15 Dynamic Programming. 2 Algorithm types Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
Data Structures and Algorithms (60-254)
CSC 427: Data Structures and Algorithm Analysis
16-May-15 Dynamic Programming. 2 Algorithm types Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
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.
Dynamic Programming and Perl Arrays Ellen Walker Bioinformatics Hiram College.
CSE115/ENGR160 Discrete Mathematics 02/28/12
1 Dynamic Programming Jose Rolim University of Geneva.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CPSC 311, Fall 2009: Dynamic Programming 1 CPSC 311 Analysis of Algorithms Dynamic Programming Prof. Jennifer Welch Fall 2009.
Dynamic Programming CIS 606 Spring 2010.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
CSE 326: Data Structures Lecture #3 Analysis of Recursive Algorithms Alon Halevy Fall Quarter 2000.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Dynamic Programming Optimization Problems Dynamic Programming Paradigm
CPSC 411 Design and Analysis of Algorithms Set 5: Dynamic Programming Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 5 1.
UNC Chapel Hill Lin/Manocha/Foskey Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject.
CS 206 Introduction to Computer Science II 10 / 16 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
CSC 172 DATA STRUCTURES. DYNAMIC PROGRAMMING TABULATION MEMMOIZATION.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Dynamic Programming Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have any questions.
1 CSC 421: Algorithm Design & Analysis Spring 2013 Dynamic programming  top-down vs. bottom-up  divide & conquer vs. dynamic programming  examples:
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Compsci 201 Recitation 12 Professor Peck Jimmy Wei 11/15/2013.
Recursion and Dynamic Programming CSC 172 SPRING 2004 LECTURE 9.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2006 Dynamic programming  top-down vs. bottom up  caching  dynamic programming vs. divide & conquer.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
Fundamentals of Algorithms MCS - 2 Lecture # 7
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2008 Dynamic programming  top-down vs. bottom-up  divide & conquer vs. dynamic programming  examples:
Dynamic Programming. What is dynamic programming? Break problem into subproblems Work backwards Can use ‘recursion’ ‘Programming’ - a mathematical term.
Algorithm Paradigms High Level Approach To solving a Class of Problems.
Dynamic Programming. Algorithm Design Techniques We will cover in this class: ◦ Greedy Algorithms ◦ Divide and Conquer Algorithms ◦ Dynamic Programming.
Honors Track: Competitive Programming & Problem Solving Optimization Problems Kevin Verbeek.
CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
1 Dynamic Programming Andreas Klappenecker [partially based on slides by Prof. Welch]
Topic 25 Dynamic Programming "Thus, I thought dynamic programming was a good name. It was something not even a Congressman could object to. So I used it.
Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject to some constraints. (There may.
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
CSC5101 Advanced Algorithms Analysis
9-Feb-16 Dynamic Programming. 2 Algorithm types Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide and.
1 Today’s Material Dynamic Programming – Chapter 15 –Introduction to Dynamic Programming –0-1 Knapsack Problem –Longest Common Subsequence –Chain Matrix.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 18.
Compsci 201 Recitation 12 Professor Peck Jimmy Wei 4/11/2014.
Fundamental Data Structures and Algorithms Ananda Guna March 18, 2003 Dynamic Programming Part 1.
Dynamic Programming. What is Dynamic Programming  A method for solving complex problems by breaking them down into simpler sub problems. It is applicable.
Greedy Algorithms Prof. Kharat P. V. Department of Information Technology.
Dynamic Programming 26-Apr-18.
Lecture 12.
CSC 172 DATA STRUCTURES.
Advanced Design and Analysis Techniques
Topic 25 Dynamic Programming
Dynamic Programming.
Dynamic Programming.
Data Structures and Algorithms
Types of Algorithms.
Dynamic Programming 23-Feb-19.
Types of Algorithms.
This is not an advertisement for the profession
Presentation transcript:

Dynamic Programming CSC 172 SPRING 2002 LECTURE 6

Dynamic Programming  If you can mathematically express a problem recursively, then you can express it as a recursive algorithm.  However, sometimes, this can be inefficiently expressed by a compiler  Fibonacci numbers  To avoid this recursive “explosion” we can use dynamic programming

Example Problem: Making Change  For a currency with coins C 1,C 2,…C n (cents) what is the minimum number of coins needed to make K cents of change?  US currency has 1,5,10, and 25 cent denominations  Anyone got a 50-cent piece?  We can make 63 cents by using two quarters & 3 pennies  What if we had a 21 cent piece?

63 cents  25,25,10,1,1,1  Suppose a 21 cent coin?  21,21,21 is optimal

Recursive Solution 1. If we can make change using exactly one coin, then that is a minimmum 2. Otherwise for each possible value j compute the minimum number of coins needed to make j cents in change and K – j cents in change independently. Choose the j that minimizes the sum of the two computations.

public static int makeChange (int[] coins, int change){ int minCoins = change; for (int k = 0;k<coins.length;k++) if (coins[k] == change) return 1; for (int j = 1;j<= change/2;j++) { int thisCoins = makeChange(coins,j) +makeChange(coins,change-j); if (thisCoins < minCoins) minCoins = thisCoins; } return minCoins; }// How long will this take?

How many calls? 63¢ 62¢2¢2¢ 1¢1¢ 61¢31¢32¢...

How many calls? 63¢ 3¢3¢ 1¢1¢ 4¢4¢61¢62¢... 2¢2¢

How many calls? 63¢ 1¢1¢ 3¢3¢ 1¢1¢ 4¢4¢61¢62¢... 2¢2¢ 1¢

How many calls? 63¢ 1¢1¢ 3¢3¢ 1¢1¢ 4¢4¢61¢62¢... 2¢2¢ 1¢ 3¢3¢1¢1¢4¢4¢ 61¢... 2¢2¢

How many times do you call for 2¢? 63¢ 1¢1¢ 3¢3¢ 1¢1¢ 4¢4¢61¢62¢... 2¢2¢ 1¢ 3¢3¢1¢1¢4¢4¢ 61¢... 2¢2¢

Some Solutions 1(1) & 62(21,21,10,10) 2(1,1) & 61(25,25,10,1).. 21(21) & 42(21,21) …. 31(21,10) & 32(21,10,1)

Improvements?  Limit the inner loops to the coins 1 & 21,21,10,10 5 & 25,21,10,1,1 10 & 21,21,10,1 21 & 21,21 25 & 25,10,1,1,1 Still, a recursive branching factor of 5 How many times do we solve for 52 cents?

public static int makeChange (int[] coins, int change){ int minCoins = change; for (int k = 0;k<coins.length;k++) if (coins[k] == change) return 1; for (int j = 1;j<= coins.length;j++) { if (change < coins[j]) continue; int thisCoins = 1+makeChange(coins,change-coins[j]); if (thisCoins < minCoins) minCoins = thisCoins; } return minCoins; }// How long will this take?

How many calls? 63¢ 58¢53¢62¢42¢38¢ 48¢43¢52¢32¢13¢ 57¢52¢61¢41¢37¢

Tabulation aka Dynamic Programming  Build a table of partial results.  The trick is to save answers to the sub-problems in an array.  Use the stored sub-solutions to solve the larger problems

DP for change making  Find optimum solution for 1 cent  Find optimum solution for 2 cents using previous  Find optimum solution for 3 cents using previous  …etc.  At any amount a, for each denomination d, check the minimum coins for the (previously calculated) amount a-d  We can always get from a-d to a with one more coin

public static int makeChange (int[] coins, int differentcoins, int maxChange, int[] coinsUsed, int [] lastCoin){ coinsUsed[0] = 0; lastCoin[0]=1; for (int cents = 1; cents <= maxChange; cents++) { int minCoins = cents; int newCoin = 1; for (int j = 0;j<differentCoins;j++) { if (coins[j] > cents) continue; if (coinsUsed[cents – coins[j]]+1 < minCoins){ minCoins=coinsUsed[cents – coins[j]]+1; newCoin = coins[j]; } coinsUsed[cents] = minCoins; lastCoin[cents] = newCoin; }

Dynamic Programming solution  O(NK)  N denominations  K amount of change  By backtracking through the lastCoin[] array, we can generate the sequence needed for the amount in question.