Download presentation
Presentation is loading. Please wait.
1
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann
2
Michael Eckmann - Skidmore College - CS 206 - Spring 2009 Today’s Topics Questions? Comments? More recursion –Examples Fibonacci Recursive definition of a tree Make change –Dynamic Programming
3
Recursion Read –Chapter 7 the first 3 sections and 7.6.
4
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.
5
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?
6
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) ); }
7
Recursion int fib(int i) { if (i <= 1) return i; else return ( fib(i-1) + fib(i-2) ); } Any problems with this code?
8
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.
9
Recursion We know what a tree is. Can anyone give a recursive definition of a tree?
10
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.)
11
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?
12
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.
13
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.
14
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.
15
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.
16
Recursion split the total into parts and solve those parts recursively. –e.g. –.63 =.01 +.62 –.63 =.02 +.61 –.63 =.03 +.60 –.63 =.04 +.59 –. –.63 =.31 +.32 –See example on page 288, figure 7.22 –Each time through, save the least number of coins.
17
Recursion split the total into parts and solve those parts recursively. –e.g. –.63 =.01 +.62 –.63 =.02 +.61 –.63 =.03 +.60 –.63 =.04 +.59 –. –.63 =.31 +.32 –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?
18
Recursion split the total into parts and solve those parts recursively. –e.g. –.63 =.01 +.62 –.63 =.02 +.61 –.63 =.03 +.60 –.63 =.04 +.59 –. –.63 =.31 +.32 –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.)
19
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.
20
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.