Dynamic Programming. Algorithm Design Techniques We will cover in this class: ◦ Greedy Algorithms ◦ Divide and Conquer Algorithms ◦ Dynamic Programming.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
15-May-15 Dynamic Programming. 2 Algorithm types Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
16-May-15 Dynamic Programming. 2 Algorithm types Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
Greedy Algorithms. Announcements Exam #1 ◦ See me for 2 extra points if you got #2(a) wrong. Lab Attendance ◦ 12 Labs, so if anyone needs to miss a lab.
1 Dynamic Programming Jose Rolim University of Geneva.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
CMPS1371 Introduction to Computing for Engineers SORTING.
Fall 2008Programming Development Techniques 1 Topic 3 Linear Recursion and Iteration September 2008.
Computer Science II Recursion Professor: Evan Korth New York University.
CPSC 311, Fall 2009: Dynamic Programming 1 CPSC 311 Analysis of Algorithms Dynamic Programming Prof. Jennifer Welch Fall 2009.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Quicksort.
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
CPSC 411 Design and Analysis of Algorithms Set 5: Dynamic Programming Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 5 1.
Computer Science 1620 Programming & Problem Solving.
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.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
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.
Positional Number Systems
More Dynamic Programming Floyd-Warshall Algorithm.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Announcements.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2006 Dynamic programming  top-down vs. bottom up  caching  dynamic programming vs. divide & conquer.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2008 Dynamic programming  top-down vs. bottom-up  divide & conquer vs. dynamic programming  examples:
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Recursion When to use it and when not to use it. Basics of Recursion Recursion uses a method Recursion uses a method Within that method a call is made.
Announcements This Wednesday, Class and Labs are cancelled! The last lab is due this Wednesday … how many people are planning on doing it? Finally posted.
CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
1 Dynamic Programming Topic 07 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology.
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
Announcements Finish up Network Flow today Then Review for Final on Monday ◦ HW#5 is due on Monday, let me or the TA’s know if you have trouble starting.
Dynamic Programming … Continued
Problem of the Day  On the next slide I wrote today’s problem of the day. It has 3 possible answers. Can you guess which 1 of the following is the solution?
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Introduction to Recursion
Introduction to Recursion
Lesson #6 Modular Programming and Functions.
Loops BIS1523 – Lecture 10.
Lesson #6 Modular Programming and Functions.
Java 4/4/2017 Recursion.
Dynamic Programming.
Data Structures and Algorithms
Fundamentals of Programming
Dynamic Programming 23-Feb-19.
Lesson #6 Modular Programming and Functions.
Review of Previous Lesson
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Presentation transcript:

Dynamic Programming

Algorithm Design Techniques We will cover in this class: ◦ Greedy Algorithms ◦ Divide and Conquer Algorithms ◦ Dynamic Programming Algorithms ◦ And Backtracking Algorithms These are 4 common types of algorithms used to solve problems. ◦ For many problems, it is quite likely that at least one of these methods will work.

Dynamic Programming Intro We have looked at several algorithms that involve recursion. In some situations, these algorithms solve fairly difficult problems efficiently ◦ BUT in other cases they are inefficient because they recalculate certain function values many times. The example of this given in the text is the Fibonacci example.

Dynamic Programming Intro The Recursive solution to finding the nth Fibonacci number: public static int fibrec(int n) { if (n < 2) return n; else return fibrec(n-1)+fibrec(n-2); } The problem: ◦ Lots and lots of calls to Fib(1) and Fib(0) are made.  It would be nice if we only made those method calls once, then simply used those values as necessary. If I asked you to compute the 10th Fibonacci number, you would never do it using the recursive steps above. Instead, you'd start making a chart: ◦ F 1 = 1, F 2 = 1, F 3 = 2, F 4 = 3, F 5 = 5, F 6 = 8, F 7 = 13, F 8 = 21, F 9 = 34, F 10 = 55. ◦ First you calculate F 3 by adding F 1 and F 2, then F 4, by adding F 3 and F 4, etc.

Dynamic Programming The idea of dynamic programming is to avoid making redundant method calls ◦ Instead, one should store the answers to all necessary method calls in memory and simply look these up as necessary. Using this idea, we can code up a dynamic programming solution to the Fibonacci number question that is far more efficient than the recursive version: public static int fib(int n) { int[] fibnumbers = new int[n+1]; fibnumbers[0] = 0; fibnumbers[1] = 1; for (int i=2; i<n+1;i++) fibnumbers[i] = fibnumbers[i-1]+fibnumbers[i-2]; return fibnumbers[n]; }

Dynamic Programming The only requirement this program has that the recursive one doesn't is the space requirement of an entire array of values. ◦ (But, if you think about it carefully, at a particular moment in time while the recursive program is running, it has at least n recursive calls in the middle of execution all at once. The amount of memory necessary to simultaneously keep track of each of these is in fact at least as much as the memory the array we are using above needs.) Usually however, a dynamic programming algorithm presents a time-space trade off. ◦ More space is used to store values, ◦ BUT less time is spent because these values can be looked up. Can we do even better (with respect to memory) with our Fibonacci method above? What numbers do we really have to keep track of all the time?

Dynamic Programming To see an illustration of the difference in speed, Arup wrote a short main to test this: public static void main(String[] args) { long start = System.currentTimeMillis(); System.out.println("Fib 30 = "+fib(30)); long mid = System.currentTimeMillis(); System.out.println("Fib 30 = "+fibrec(30)); long end = System.currentTimeMillis(); System.out.println("Fib Iter Time = "+(mid-start)); System.out.println("Fib Rec Time = "+(end-mid)); } Output: ◦ Fib Iter Time = 4 ◦ Fib Rec Time = 258

Announcements Assignment #3 due tonight Exam is next Wednesday, Review on Monday ◦ I will post review questions and answers and a practice test we will go over on Monday by Friday Everything from Graphs until Today will be on the Exam ◦ There’s a list of topics posted online.

The Change Problem Given a certain amount of money, how many different ways are there to make change for that amount of money? Or more simply… Given a positive integer n, how many ways can we make change for n cents using pennies, nickels, dimes and quarters?

The Change Problem Recursively, we could break down the problem as follows: ◦ To make change for n cents we could: 1)Give the customer a quarter. Then we have to make change for n-25 cents 2)Give the customer a dime. Then we have to make change for n-10 cents 3)Give the customer a nickel. Then we have to make change for n-5 cents 4)Give the customer a penny. Then we have to make change for n-1 cents. If we let T(n) = number of ways to make change for n cents, we get the formula ◦ T(n) = T(n-25)+T(n-10)+T(n-5)+T(n-1) ◦ Is there anything wrong with this? ◦ In particular, for this recurrence relation T(6)=3, but in actuality, we want T(6)=2.)

The Change Problem So this can not be right. What is wrong with our logic? ◦ In particular, it can been seen that this formula is an OVERESTIMATE of the actual value. ◦ Specifically, this counts certain combinations multiple times. In the above example, the one penny, one nickel combination is counted twice. Why is this the case? The problem is that we are counting all combinations of coins that can be given out where ORDER matters. ◦ (We are counting giving a penny then a nickel separately from giving a nickel and then a penny.)

The Change Problem We have to find a way to NOT do this. ◦ One way to do this is IMPOSE an order on the way the coins are given. ◦ We could do this by saying that coins must be given from most value to least value.  Thus, if you "gave" a nickel, afterwards, you would only be allowed to give nickels and pennies. Using this idea, we need to adjust the format of our recursive computation: ◦ To make change for n cents using the largest coin d, we could 1)If d is 25, give out a quarter and make change for n-25 cents using the largest coin as a quarter. 2)If d is 10, give out a dime and make change for n-10 cents using the largest coin as a dime. 3)If d is 5, give out a nickel and make change for n-5 cents using the largest coin as a nickel. 4)If d is 1, we can simply return 1 since if you are only allowed to give pennies, you can only make change in one way.

The Change Problem There's a whole bunch of stuff going on here, but one of the things you'll notice is that the larger n gets, the slower and slower this will run, or maybe your computer will run out of stack space. ◦ Further analysis will show that many, many method calls get repeated in the course of a single initial method call. In dynamic programming, we want to AVOID these reoccurring calls. ◦ To do this, rather than making those three recursive calls above, we could store the values of each of those in a two dimensional array.

The Change Problem Essentially, each row label stands for the number of cents we are making change for Each column label stands for the largest coin value allowed to make change. Amount we’re making change for penny nickel dime quarter Denominations:

The Change Problem Originally our table looks like this: Because we know our initial conditions: There is only 1 way to make change for 1cent through 15 cents using only pennies. That’s the first row. And there is only one way to make change for 4 cents or less, you can only use pennies. That’s the first 4 columns Amount we’re making change for penny nickel dime quarter Denominations:

The Change Problem Now we need to fill in the cells initialized to 0. ◦ Sum for each denomination less than or equal to the one we are on, whether there we can use that denomination (and how many times based on previous values). ◦ For example, in the red space below:  can we make change for 5 cents using pennies? +1  Can we make change for 5 cents using nickels? +1  Stop. Amount we’re making change for penny nickel dime quarter Denominations:

public static int makeChangedyn(int n, int d) { // Take care of simple cases. if (n < 0) return 0; else if ((n>=0) && (n < 5)) return 1; // Build table here. else { int[] denominations = {1, 5, 10, 25}; int[][] table = new int[4][n+1]; // Initialize table for (int i=0; i<n+1;i++) table[0][i] = 1; for (int i=0; i<5; i++) { table[1][i] = 1; table[2][i] = 1; table[3][i] = 1; } for (int i=5;i<n+1;i++) { table[1][i] = 0; table[2][i] = 0; table[3][i] = 0; }

// Fill in table, row by row. for (int i=1; i<4; i++) { for (int j=5; j<n+1; j++) { for (int k=0; k<=i; k++) { if ( j >= denominations[k]) table[i][j] += table[k][j - denominations[k]]; } return table[lookup(d)][n]; } Now see if you can finish filling in the zeroed out portion of the table using the code above. Remember, int[] denominations = {1, 5, 10, 25};

The Change Problem An alternate way to code this up is to realize that we DON'T need to add many different cases up together. ◦ Instead, we note that the number of ways to make change for n cents using denomination d can be split up into counting two groups: 1)The number of ways to make change for n cents using denominations LESS than d  simply the value in the table that is directly above the one we are trying to fill. 2)The number of ways to make change for n cents using at least ONE coin of denomination d.  the value on the table that is on the same row, by d spots to the left

The Change Problem To make this change in the code, just substitute this line for the for loop with k in the previous code: if ( j >= denominations[i]) table[i][j] = table[i-1][j] + table[i][j - denominations[k]]; else table[i][j] = table[i-1][j] Now see if you can finish filling in the zeroed out portion of the table using the code above.

References Slides adapted from Arup Guha’s Computer Science II Lecture notes: ctures/ ctures/ Additional material from the textbook: Data Structures and Algorithm Analysis in Java (Second Edition) by Mark Allen Weiss Additional images: xkcd.com