recursion - purdue universitycs180/spring2010web/recitation_slides/week12.pdffibonacci numbers can...
TRANSCRIPT
![Page 1: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/1.jpg)
![Page 2: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/2.jpg)
RecursionJohn Ross Wallrabenstein
![Page 3: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/3.jpg)
![Page 4: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/4.jpg)
Recursion: The Black Magic of Programming
![Page 5: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/5.jpg)
![Page 6: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/6.jpg)
Learning the Dark ArtTheory
Definition
Constructing a Recursive Function Definition
Practice
Implementation and Data Structures
![Page 7: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/7.jpg)
Theory
![Page 8: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/8.jpg)
DefinitionRecursion: See recursion
But I spelled ‘recursion’ correctly...
![Page 9: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/9.jpg)
Formal Definition
Recursive Function:
a function that is defined in terms of itself
![Page 10: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/10.jpg)
Black Magic
You have seen such dark mathemagic before!
![Page 11: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/11.jpg)
The Fibonacci Numbers
![Page 12: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/12.jpg)
Fibonacci NumbersHow are the Fibonacci Numbers defined?
Fib(0)=0
Fib(1)=1
Fib(2)=1
Fib(3)=2
The Dark Sequence: 0,1,1,2,3,5,8,13,21...
![Page 13: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/13.jpg)
Fibonacci Numbers
Can we generalize this progression?
Fib(n)=?
![Page 14: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/14.jpg)
Fibonacci Numbers
Can we generalize this progression?
Fib(n)=Fib(n-1)+Fib(n-2)
However, I claim that this alone is a poorly cast spell!
![Page 15: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/15.jpg)
Casting Recursive Spells
The novice witch or wizard is overly concerned with the recursive power of their spell and they forget:
A strong base for their spell!
![Page 16: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/16.jpg)
Casting Recursive Spells
A proper recursive spell is composed of:
A series of one or more base cases
A series of one or more recursive spells
![Page 17: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/17.jpg)
Fibonacci NumbersThe Correct Fibonacci Spell
A strong spell base
Fib(0)=0
Fib(1)=1
The recursive spell
Fib(n)=Fib(n-1)+Fib(n-2)
![Page 18: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/18.jpg)
Conjuring Practice
![Page 19: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/19.jpg)
Conjuring PracticeA crafty wizard has proposed the following challenge: “What is the sum of the first n integers greater than zero?”
Unfortunately, your wand does not have the ability to perform loop iterations or multiplication/division, only addition
Assumption: You are not the powerful mathemagician Carl Frederich Gauss
![Page 20: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/20.jpg)
Conjuring Practice
We must divide this challenge up into smaller challenges that we can solve easily:
GaussSum(n)=n+GaussSum(n-1)
Lower your wands! What have we forgotten?
![Page 21: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/21.jpg)
Conjuring Practice
Remember: A powerful recursive spell must have one or more base cases
What is the GaussSum(n) base case?
GaussSum(0)=0
![Page 22: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/22.jpg)
![Page 23: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/23.jpg)
Gauss’ SolutionRewrite the problem as follows:
(100+1)+(99+2)+(98+3)+...
Note that each quantity sums to 101, which is n+1
Note that there are 50, or n/2, quantities
In general, GaussSum(n)=(n+1)(n/2)
![Page 24: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/24.jpg)
Practice
![Page 25: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/25.jpg)
StacksA stack is a “last-in first-out” data structure similar to a stack of papers
i.e. The bottom sheet can only be removed after all sheets above it have been removed
Method calls use stacks with entries called activation records, rather than sheets
![Page 26: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/26.jpg)
Stacks
Consider what happens when a call to MethodB is made from MethodA
The information relevant to MethodA must be saved so that when we return from MethodB, we can resume execution
![Page 27: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/27.jpg)
Stacks
In order to gain insight into how recursion correctly arrives at the solution, we will trace through a sample execution of the GaussSum spell
![Page 28: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/28.jpg)
GaussSpell Trace
!public static int GaussSum(int n){! if(n == 0)! ! return 0;! return (n + GaussSum(n-1));}
return (n + GaussSum(n-1));
In: n=Q
![Page 29: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/29.jpg)
return (3 + GaussSum(3-1))
In: n=3return (3 + GaussSum(3-1))
In: n=3return (3 + GaussSum(3-1))
In: n=3return (3 + GaussSum(3-1))
In: n=3
return (2 + GaussSum(2-1))
In: n=2return (2 + GaussSum(2-1))
In: n=2return (2+ GaussSum(2-1))
In: n=2
return (1 + GaussSum(1-1))
In: n=1return (1 + GaussSum(1-1))
In: n=1
return 0
In: n=0
return (3 + GaussSum(3-1))
In: n=3
return (2 + GaussSum(2-1))
In: n=2
return (1 + 0GaussSum(1-1))
In: n=1
return (3 + GaussSum(3-1))
In: n=3
return (2 + 1GaussSum(2-1))
In: n=2
return (3 + 3GaussSum(3-1))
In: n=3
Time
![Page 30: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/30.jpg)
When Spells Go Wrong
![Page 31: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/31.jpg)
Infinite Recursion
When a recursive spell is cast without properly defined base cases, the recursion continues ad infinitum
![Page 32: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/32.jpg)
Infinite Recursion!
public static int Fib(int n){! fib = Fib(n-1)+Fib(n-2) ! if(n == 0)! ! return 0;! if(n == 1)! ! return 1;! return fib;} What is
wrong?
Stack Overflow
Note: Space Turtles!
![Page 33: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/33.jpg)
Efficiency
![Page 34: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/34.jpg)
Efficiency
A skilled wizard or witch knows that their spell must manifest quickly to defeat their opponent
As fate would have it, both the Fibonacci Sequence and the Gaussian Sum should NEVER be computed recursively!
![Page 35: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/35.jpg)
EfficiencyThe Fibonacci Sequence, in particular, generates too many recursive calls to be useful
Solution?
Iterative Approach
Always consider switching to an iterative solution when recursion is too costly
![Page 36: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/36.jpg)
When To Use Recursion
When is recursion useful?
Recursive solutions are often more concise
Some problems have natural recursive solutions
Iterative solution would be convoluted
![Page 37: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/37.jpg)
A Proper Recursion Application
![Page 38: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/38.jpg)
The Challenge
A crafty wizard has proposed the following challenge: “Given an array of sorted numbers, find the location of element X”
To check if element I = X, you tap I with your wand and yell “Prae Quam”!
Note: “Prae Quam” is Latin for “Comparison”
![Page 39: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/39.jpg)
The Challenge
However, to defeat the wizard, you must GUARANTEE to use less than N uses of your wand to perform a comparison (lest your magic be exhausted)
How do we win the challenge?
![Page 40: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/40.jpg)
The Challenge
Key Observation: If X is greater than the middle element of the list, it is in the right half. Otherwise, it is in the left half.
Can anyone think of a recursive solution?
![Page 41: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/41.jpg)
Binary Search
Solution:
Compare X with the middle element of the list
Recursively search the proper half of the list
![Page 42: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/42.jpg)
Binary Search15
20
35
41
57
63
75
80
85
90
X=63
15
20
35
41
57
63
75
80
85
90
PraeQuam!
PraeQuam!
15
20
35
41
57
63
75
80
85
90
PraeQuam!DONE!
![Page 43: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/43.jpg)
!
public int BinS(int[] a, int f, int l, int k){ if( f > l) return -1; else{ int mid = ( f + l) / 2; if( key == a[mid] ) return mid; if( key < a[mid] ) return BinS(a, f, mid-1, k); if( key > a[mid] ) return BinS(a, mid+1, l, k);} In O(log n)
Time!
![Page 44: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/44.jpg)
![Page 45: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/45.jpg)
You have Mastered Recursive Spells
No Infinite Recursion!
Who has actually seen Fantasia?
![Page 46: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this](https://reader035.vdocuments.net/reader035/viewer/2022070822/5f24ca700cffd456050878b7/html5/thumbnails/46.jpg)