CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
Advertisements

CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
Types of Algorithms.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 02 / 2009 Instructor: Michael Eckmann.
1 Dynamic Programming Jose Rolim University of Geneva.
CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 27 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
CS 325 Introduction to Computer Graphics 04 / 09 / 2010 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 20 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 01 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 13 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 01 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 09 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 24 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 16 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 29 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
Dynamic Programming Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have any questions.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
Recursion and Dynamic Programming CSC 172 SPRING 2004 LECTURE 9.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
Dynamic Programming. Algorithm Design Techniques We will cover in this class: ◦ Greedy Algorithms ◦ Divide and Conquer Algorithms ◦ Dynamic Programming.
CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 26 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 30 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 02 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 08 / 2009 Instructor: Michael Eckmann.
Presentation transcript:

CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Spring 2009 Today’s Topics Questions? Comments? More recursion –Examples Fibonacci Recursive definition of a tree Make change –Dynamic Programming

Recursion Read –Chapter 7 the first 3 sections and 7.6.

Recursion 1. have at least one base case that is not recursive 2. recursive case(s) must progress towards the base case 3. trust that your recursive call does what it says it will do (without having to unravel all the recursion in your head.)‏ 4. try not to do redundant work. That is, in different recursive calls, don't recalculate the same info.

Recursion The fibonacci sequence is: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,... Fibonacci numbers are simply defined recursively: F 0 = 0 F 1 = 1 F n = F n-1 + F n-2 How could we convert this to code to calculate the i th fibonacci number?

Recursion Fibonacci numbers are simply defined recursively: F 0 = 0 F 1 = 1 F n = F n-1 + F n-2 How could we convert this to code to calculate the i th fibonacci number? int fib(int i)‏ { if (i <= 1)‏ return i; else return ( fib(i-1) + fib(i-2) ); }

Recursion int fib(int i)‏ { if (i <= 1)‏ return i; else return ( fib(i-1) + fib(i-2) ); } Any problems with this code?

Recursion int fib(int i)‏ { if (i <= 1)‏ return i; else return ( fib(i-1) + fib(i-2) ); } Any problems with this code? Yes – it makes too many calls. And further, these calls are redundant. It violates that 4 th idea of recursion stated earlier: in different recursive calls, don't recalculate the same info.

Recursion We know what a tree is. Can anyone give a recursive definition of a tree?

Recursion We know what a tree is. Can anyone give a recursive definition of a tree? –A tree is empty or it has a root connected to 0 or more subtrees. –(Note a subtree, taken on its own, is a tree.)‏

Recursion Change making algorithms. –Problem: have some amount of money for which you need to make change in the fewest coins possible. –You have unlimited numbers of coins C 1... C N each with different values. example: make change for.63 and the coins you have are C 1 =.01, C 2 =.05, C 3 =.10, and C 4 =.25 only. ideas?

Recursion Change making algorithms. –Problem: have some amount of money for which you need to make change in the fewest coins possible. –You have unlimited numbers of coins C 1... C N each with different values. example: make change for.63 and the coins you have are C 1 =.01, C 2 =.05, C 3 =.10, and C 4 =.25 only. The algorithm that works for these denominations is a greedy algorithm (that is, one that makes an optimal choice at each step to achieve the optimal solution to the whole problem.) Let's write it in Java.

Recursion What if : make change for.63 and the coins you have are C 1 =.01, C 2 =.05, C 3 =.10, C 4 =.21 and C 5 =.25 only. A 21 cent piece comes into the picture.

Recursion What if : make change for.63 and the coins you have are C 1 =.01, C 2 =.05, C 3 =.10, C 4 =.21 and C 5 =.25 only. A 21 cent piece comes into the picture. The greedy algorithm doesn't work in this case because the minimum is 3 coins all of C 4 =.21 whereas the greedy algorithm would yield 2.25's, 1.10 and 3.01's for a total of 6 coins. We always assume we have.01 coin to guarantee a way to make change.

Recursion So, we want to create a way to solve the minimum # of coins problem with arbitrary coin denominations. A recursive strategy is: –BASE CASE: If the change K, we're trying to make is exactly equal to a coin denomination, then we only need 1 coin, which is the least # of coins. –RECURSIVE STEP: Otherwise, for all possible values of i, split the amount of change K into two sets i and K- i. Solve these two sets recursively and when done with all the i's, keep the minimum sum of the number of coins among all the i's.

Recursion split the total into parts and solve those parts recursively. –e.g. –.63 = –.63 = –.63 = –.63 = –. –.63 = –See example on page 288, figure 7.22 –Each time through, save the least number of coins.

Recursion split the total into parts and solve those parts recursively. –e.g. –.63 = –.63 = –.63 = –.63 = –. –.63 = –Each time through, save the least number of coins. –The base case of the recursion is when the change we are making is equal to one of the coins – hence 1 coin. –Otherwise recurse. –Why is this bad?

Recursion split the total into parts and solve those parts recursively. –e.g. –.63 = –.63 = –.63 = –.63 = –. –.63 = –Each time through, save the least coins. –The base case of the recursion is when the change we are making is equal to one of the coins – hence 1 coin. –Otherwise recurse. –Why is this bad? Let's see (let's try to make change for some amounts with a Java implementation of this.)‏

Recursion The major problem with that change making algorithm is that it makes so many recursive calls and it duplicates work already done. Example anyone? An idea called dynamic programming is a good solution in this case. Dynamic Programming is an idea that instead of making recursive calls to figure out something that we already figured out we compute it once and save the value in a table for lookup later.

Dynamic Programming Could we use dynamic programming for the fibonacci numbers? Yes. Let's write the fibonacci method to take advantage of the stored values when we can.