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

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

Transcript of CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

Page 1: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

CS 206Introduction to Computer Science II

02 / 23 / 2009

Instructor: Michael Eckmann

Page 2: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 206 - Spring 2009

Today’s Topics

• Questions? Comments?• General recursion ideas

–Definition of recursion–Several examples

Page 3: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

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.

Page 4: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

Recursion

• some definitions are inherently recursive• e.g.• sum of the first n integers, n>= 1.• S(1) = 1• S(N) = S(N-1) + N

• recursive code for this• iterative code for this• simpler code for this (based on our proof) N*(N+1)/2• any problems with the recursive code? n=0 or large n?

Page 5: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

Recursion

• The last example showed that recursion didn't really simplify our lives, but there are times when it does.

• e.g. If given an integer and you wanted to print the individual digits in order, but you didn't have the ability to easily convert an int >10 to a String.

• e.g. int n=35672;• If we wanted to print 3 first then 5 then 6 then 7 then 2,

we need to come up with a way to extract those digits via some mathematical computation.

• It's easy to get the last digit n%10 gives us that. • Notice: 35672 % 10 = 2 also 2 % 10 = 2.• Any ideas on a recursive way to display all the numbers in

order?

Page 6: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

Recursion

void printDigits(int n){

if (n>=10)printDigits((n/10));

System.out.println( n%10 );}

// what's the base case here?

// what's the recursive step here? Will it always approach the base case?

Page 7: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

Recursion

• Now that last problem was made up sort of, because Java (and most languages) allow us to print ints.

• However what if we wanted to print the int in a different base? Say base 2, 3, 4, 5, or some other base?

Page 8: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

Recursion

• The fibonacci sequence is:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

– Can you detect the pattern?

Page 9: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

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

F1 = 1

Fn = F

n-1 + F

n-2

• How could we convert this to code to calculate the ith fibonacci number?

Page 10: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

Recursion• Fibonacci numbers are simply defined recursively:

F0 = 0

F1 = 1

Fn = F

n-1 + F

n-2

• How could we convert this to code to calculate the ith fibonacci number?int fib(int i){

if (i <= 1)return i;

elsereturn ( fib(i-1) + fib(i-2) );

}

Page 11: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

Recursionint fib(int i){

if (i <= 1)return i;

elsereturn ( fib(i-1) + fib(i-2) );

}

Any problems with this code?

Page 12: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

Recursionint fib(int i){

if (i <= 1)return i;

elsereturn ( 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 4th idea of recursion stated earlier: in

different recursive calls, don't recalculate the same info.

Page 13: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

Recursion• We know what a tree is.

• Can anyone give a recursive definition of a tree?

Page 14: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

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.)

Page 15: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

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 C1 ... 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?

Page 16: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

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 C1 ... 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.

Page 17: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

Recursion• What if : make change for .63 and the coins you have are

C1 =.01, C

2 =.05, C

3 =.10, C

4 =.21 and C

5 =.25 only.

• A 21 cent piece comes into the picture.

Page 18: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

Recursion• What if : make change for .63 and the coins you have are

C1 =.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.

Page 19: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

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.

Page 20: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

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.

Page 21: CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.

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?