CS161 Recursion Continued Tail recursion n Tail recursion is a - - PowerPoint PPT Presentation

cs161 recursion continued tail recursion
SMART_READER_LITE
LIVE PREVIEW

CS161 Recursion Continued Tail recursion n Tail recursion is a - - PowerPoint PPT Presentation

CS161 Recursion Continued Tail recursion n Tail recursion is a recursive call that occurs as the last action in a method. Tail recursion n Tail recursion is a recursive call that occurs as the last action in a method. n This is tail


slide-1
SLIDE 1

CS161 Recursion Continued

slide-2
SLIDE 2

Tail recursion

n Tail recursion is a recursive call that occurs as

the last action in a method.

slide-3
SLIDE 3

Tail recursion

n Tail recursion is a recursive call that

  • ccurs as the last action in a method.

n This is tail recursion: public int factorial(int n) { return factorialTail(n, 1); } int factorialTail(int n, int product) { if(n == 0) return product; return factorialTail(n-1, product*n); }

slide-4
SLIDE 4

Tail recursion

n Tail recursion is a recursive call that occurs as

the last action in a method.

n This is not tail recursion:

public int factorial(int n){ if (n==0) return 1; return n* factorial(n-1); }

q WHY NOT?

slide-5
SLIDE 5

Tail recursion

n This is tail recursion: public int factorial(int n) { return factorialTail(n, 1); } int factorialTail(int n, int product) { if(n == 0) return product; return factorialTail(n-1, product*n); }

n But why would you care? Turns out that compilers can

  • ptimize memory usage when they detect that this is the

case.

slide-6
SLIDE 6

Tail recursion

n This is tail recursion: public int factorial(int n) { return factorialTail(n, 1); } int factorialTail(int n, int product) { if(n == 0) return product; return factorialTail(n-1, product*n); }

n When making a recursive call, you no longer need to

save the information about the local variables within the calling method.

slide-7
SLIDE 7

Dictionary lookup

n Suppose you’re looking up a word in the

dictionary (paper one, not online!)

n You probably won’t scan linearly thru the

pages – inefficient.

n What would be your strategy?

slide-8
SLIDE 8

Binary search

binarySearch(dictionary, word){ if (dictionary has one page) {// base case scan the page for word } else {// recursive case

  • pen the dictionary to a point near the middle

determine which half of the dictionary contains word if (word is in first half of the dictionary) { binarySearch(first half of dictionary, word) } else { binarySearch(second half of dictionary, word) } }

slide-9
SLIDE 9

Binary search

n Let’s write a method called binarySearch that

accepts a sorted array of integers and a target integer and returns the index of an occurrence of that value in the array.

q If the target value is not found, return -1

int index = binarySearch(data, 42); // 10 int index2 = binarySearch(data, 66); // -1 index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 2 7 10 15 20 22 25 30 36 42 50 56 68 85 92 103

slide-10
SLIDE 10

Binary search

// Returns the index of an occurrence of the given // value in the given array, or -1 if not found. // Precondition: a is sorted public int binarySearch(int[] a, int target) { return binarySearch(a, target, 0, a.length - 1); } // Recursive helper to implement search. private int binarySearch(int[] a, int target, int first, int last) { if (first > last) { return -1; // not found } else { int mid = (first + last) / 2; if (a[mid] == target) { return mid; // found it! } else if (a[mid] < target) { // middle element too small; search right half return binarySearch(a, target, mid+1, last); } else { // a[mid] < target // middle element too large; search left half return binarySearch(a, target, first, mid-1); } } }

slide-11
SLIDE 11

11

Towers of Hanoi

Example: Towers of Hanoi, move all disks to third peg without

ever placing a larger disk on a smaller one.

slide-12
SLIDE 12

Try to find the pattern by cases

n One disk is easy n Two disks... n Three disks... n Four disk...

slide-13
SLIDE 13

13

Towers of Hanoi

Example: Towers of Hanoi, move all disks to third peg without

ever placing a larger disk on a smaller one.

  • 1. Move n-1 discs to middle peg
slide-14
SLIDE 14

14

Towers of Hanoi

Example: Towers of Hanoi, move all disks to third peg without

ever placing a larger disk on a smaller one.

  • 2. Move largest (bottom) disc to right peg
slide-15
SLIDE 15

15

Towers of Hanoi

Example: Towers of Hanoi, move all disks to third peg without

ever placing a larger disk on a smaller one.

  • 3. Move n-1 discs from middle to right peg

See http://www.brandonevans.org/hanoi/

slide-16
SLIDE 16

Fibonacci’s Rabbits

n Suppose a newly-born pair of

rabbits, one male, one female, are put on an island.

q

A pair of rabbits doesn’t breed until 2 months

  • ld.

q

Thereafter each pair produces another pair each month

q

Rabbits never die.

n How many pairs will there be after n

months?

16

image from: http://www.jimloy.com/algebra/fibo.htm

slide-17
SLIDE 17

Do some cases, see a pattern?

m0: 1 young 1 m1: 1 mature 1 m2: 1 mature 1 young 2 m3: 2 mature 1 young 3 m4: 3 mature 2 young 5 m5: 5 mature 3 young 8 m6?

slide-18
SLIDE 18

The pattern...

m0: 1 young 1 m1: 1 mature 1 m2: 1 mature 1 young 2 m3: 2 mature 1 young 3 m4: 3 mature 2 young 5 mn = mn-1 (rabbits never die) + mn-2 (newborn pairs) How fast does this rabbit population grow?

slide-19
SLIDE 19

Fibonacci numbers

n The Fibonacci numbers are a sequence of

numbers F0, F1, ... Fn defined by: F0 = F1 = 1 Fi = Fi-1 + Fi-2 for any i > 1

n Write a method that, when given an integer i,

computes the nth Fibonacci number.

slide-20
SLIDE 20

Fibonacci numbers

n Let's run it for n = 1,2,3,... 10, ... , 20,... n If n is large the computation takes a long time! Why?

F5 F3 F2 F0 F1 F4 F1 F3 F2 F0 F1 F1 F2 F0 F1

slide-21
SLIDE 21

21

Fibonacci numbers

n recursive Fibonacci was expensive because it

made many, recursive calls

q fibonacci(n) recomputed fibonacci(n-1),…,fibonacci(1)

many times in finding its answer!

q this is a case, where the sub-tasks handled by the

recursion are redundant and get recomputed

slide-22
SLIDE 22

Fibonacci numbers

n Every time n is incremented by 2, the call tree more than

doubles.

F5 F3 F2 F0 F1 F4 F1 F3 F2 F0 F1 F1 F2 F0 F1

slide-23
SLIDE 23

Growth of rabbit population

1 1 2 3 5 8 13 21 34 ... The fibonacci numbers themselves also grow rapidly: every 2 months the population at least DOUBLES

slide-24
SLIDE 24

Fractals – the Koch curve

slide-25
SLIDE 25

Simpler example: cCurve

Can you draw the next one???

slide-26
SLIDE 26

Other growth phenomenon: cCurve

The next one is: the previous one rotated left plus the previous one rotated right

slide-27
SLIDE 27

CCurve code

/** Recursive function which draws a CCurve

* @param rank of the CCurve * @param angle initial angle of the CCurve */ public static void cCurve(int rank, int angle) { if (rank <= 0) { addLine(angle); } else { cCurve(rank - 1, angle - 45); cCurve(rank - 1, angle + 45); } }