Docsity

Recursion and Iteration - Lecture Slides | CS 162, Study notes of Computer Science

Partial preview of the text

Download Recursion and Iteration - Lecture Slides | CS 162 and more Study notes Computer Science in PDF only on Docsity!

1

CS162: Introduction to

Computer Science II

Recursion III

2

Outline

1. Recursion vs Iteration

2. Tail Recursion

3. More recursion examples: base

conversion

3

The Fibonacci sequence

Recursive mathematical definition:

Fib(1) = 1

Fib(2) = 1

Fib(n) = Fib(n-1) + Fib(n-2), if n > 2

4

Coding Fibonacci

• Recursive:

public long fibonacci(int n){

if ((n == 1) || (n == 2))

return 1;

else

return fibonacci(n-1) + fibonacci(n-2);

• Notice the elegance of writing the code

directly from the mathematical definition

7

Activation Records for

fibonnaci(6)

# of activation records grows exponentially because it

computes the same values over and over!

8

Coding Fibonacci

• Non-recursive (iterative):

public long fibonacci1(int n){

long a = 0, b = 1, c = 1;

for (int k = 2; k <= n; k++){

c = a + b;

a = b;

b = c;

return c;

• How many times does the loop execute?

n-

9

Comparing iterative and recursive

calculation of fibonacci(n)

Iterative Recursive

n = 10 uses 9 iterations n = 10 uses 109

activations

n = 20 uses 19 iterations n = 20 uses 13,

activations

n = 50 uses 49 iterations n = 50 uses 25,172,478,

activations

10

So … why use recursion?

• Simplicity: For some problems, the

iterative solution is much too complex.

• Proof of correctness: Recursive

solutions can be proved by

mathematical induction.

• Elegance: "To iterate is human, to

recurse, divine." – L. Peter Deutsch

• Use it selectively: Consider the ease of

coding vs. runtime efficiency.

13

Outline

1. Recursion vs Iteration

2. Tail Recursion

3. More recursion examples: base

conversion

14

Tail Recursion

• A recursive function is said to be tail

recursive if there are no pending

operations to be performed on return

from a recursive call

• Tail recursive functions return the value

of the last recursive call as the value of

the function

15

Factorial Example

public long rfactorial (int n){

if (n == 1)

return 1;

else

return n * rfactorial(n-1);

  • Not tail recursive! There is a pending operation of

multiplication.

  • Information about each pending operation must be stored

16

Tail Recursive Factorial

public long factorialHelper (int n, int result){ if (n == 1) return result; else return factorialHelper(n-1,nresult); }*

public long factorial(int n){ return factorialHelper(n,1); }

The trick to making factorial tail recursive: use a recursive

helper function and add an extra argument

19

Base Conversion

Write a recursive function to convert a base 10

number to binary. Let’s convert 14 in base 10

to binary as an example.

First, we need some Java operators:

1. % is called the mod operator. It returns the

remainder eg. 16 % 3 = 1

2. / is integer division if the denominator and

numerator are ints. It returns the quotient

and ignores the remainder eg. 16 / 3 = 5

20

Base Conversion

Let’s remind ourselves how base conversion works.

To convert 14 to binary:

1. First take 14 % 2 = 0. This is the right most (least

significant) digit.

2. Take 14 / 2 = 7. Repeat step (1) except using 7

instead of 14.

21

Base Conversion

The overall set of mod and division operations look

like the following:

22

Base Conversion

To write out the binary number, start at the bottom of

the left column and print out the digits.

14 in binary is 1110

25

Base Conversion (Tail Recursive)

public String baseConversionTRHelper(int n, String result) { int base = 2; // binary if( n < base ) { return Integer. toString (n)+result; } else { int quotient = n / base; String remainder = Integer. toString (n % base); return baseConversionTRHelper(quotient,remainder+result); } }

public String baseConversionTR(int n) { return baseConversionTRHelper(n,""); }