CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf ·...
Transcript of CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf ·...
![Page 1: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/1.jpg)
CS161 Recursion Continued
![Page 2: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/2.jpg)
Tail recursion
n Tail recursion is a recursive call that occurs as the last action in a method.
![Page 3: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/3.jpg)
Tail recursion
n Tail recursion is a recursive call that occurs 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); }
![Page 4: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/4.jpg)
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?
![Page 5: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/5.jpg)
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 optimize memory usage when they detect that this is the case.
![Page 6: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/6.jpg)
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.
![Page 7: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/7.jpg)
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?
![Page 8: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/8.jpg)
Binary search binarySearch(dictionary, word){ if (dictionary has one page) {// base case scan the page for word }
else {// recursive case
open 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) } }
![Page 9: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/9.jpg)
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
![Page 10: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/10.jpg)
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); } } }
![Page 11: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/11.jpg)
11
Towers of Hanoi
Example: Towers of Hanoi, move all disks to third peg without ever placing a larger disk on a smaller one.
![Page 12: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/12.jpg)
Try to find the pattern by cases
n One disk is easy
n Two disks...
n Three disks...
n Four disk...
![Page 13: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/13.jpg)
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
![Page 14: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/14.jpg)
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
![Page 15: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/15.jpg)
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/
![Page 16: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/16.jpg)
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
old. 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
![Page 17: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/17.jpg)
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?
![Page 18: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/18.jpg)
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?
![Page 19: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/19.jpg)
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.
![Page 20: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/20.jpg)
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
![Page 21: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/21.jpg)
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
![Page 22: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/22.jpg)
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
![Page 23: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/23.jpg)
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
![Page 24: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/24.jpg)
Fractals – the Koch curve
![Page 25: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/25.jpg)
Simpler example: cCurve
Can you draw the next one???
![Page 26: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/26.jpg)
Other growth phenomenon: cCurve
The next one is: the previous one rotated left plus the previous one rotated right
![Page 27: CS161 Recursion Continued - Colorado State Universitycs161/Fall14/slides/05_recursion_cont.pdf · This is tail recursion: public int factorial(int n) { ... computes the nth Fibonacci](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b4c/html5/thumbnails/27.jpg)
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); } }