Lecture 15 Recursion
Announcements for Today Prelim 1 Other Announcements • Reading: 5.8 – 5.10 • Tonight at 7:30-9pm • Assignment 3 now graded § A–J (Uris G01) § Mean 94, Median 99 § K-Z (Statler Auditorium) § Time : 7 hrs, StdDev : 3 hrs • Graded by noon on Sun § Unchanged from last year § Scores will be in CMS • Assignment 4 posted Friday § In time for drop date § Parts 1-3: Can do already • Make-ups were e-mailed § Part 4: material from today § If not, e-mail Jessica NOW § Due two weeks from today 10/15/15 Recursion 2
Recursion • Recursive Definition : A definition that is defined in terms of itself • Recursive Function : A function that calls itself (directly or indirectly) • Recursion : If you understand the definition, stop; otherwise, see Recursion • Infinite Recursion : See Infinite Recursion 10/15/15 Recursion 3
A Mathematical Example: Factorial • Non-recursive definition: n! = n × n-1 × … × 2 × 1 = n (n-1 × … × 2 × 1) • Recursive definition: n! = n (n-1)! for n ≥ 0 Recursive case 0! = 1 Base case What happens if there is no base case? 10/15/15 Recursion 4
Factorial as a Recursive Function def factorial(n): • n! = n (n-1)! """Returns: factorial of n. • 0! = 1 Pre: n ≥ 0 an int""" if n == 0: return 1 Base case(s) return n*factorial(n-1) Recursive case What happens if there is no base case? 10/15/15 Recursion 5
Example: Fibonnaci Sequence • Sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ... a 0 a 1 a 2 a 3 a 4 a 5 a 6 § Get the next number by adding previous two § What is a 8 ? A: a 8 = 21 B: a 8 = 29 C: a 8 = 34 D: None of these. 10/15/15 Recursion 6
Example: Fibonnaci Sequence • Sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ... a 0 a 1 a 2 a 3 a 4 a 5 a 6 § Get the next number by adding previous two § What is a 8 ? A: a 8 = 21 B: a 8 = 29 correct C: a 8 = 34 D: None of these. 10/15/15 Recursion 7
Example: Fibonnaci Sequence • Sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ... a 0 a 1 a 2 a 3 a 4 a 5 a 6 § Get the next number by adding previous two § What is a 8 ? • Recursive definition: § a n = a n -1 + a n -2 Recursive Case § a 0 = 1 Base Case § a 1 = 1 (another) Base Case Why did we need two base cases this time? 10/15/15 Recursion 8
Fibonacci as a Recursive Function def fibonacci(n): """Returns: Fibonacci no. a n Precondition: n ≥ 0 an int""" if n <= 1: Base case(s) return 1 return (fibonacci(n-1)+ Recursive case fibonacci(n-2)) Note difference with base case conditional. 10/15/15 Recursion 9
Fibonacci as a Recursive Function def fibonacci(n): • Function that calls itself """Returns: Fibonacci no. a n § Each call is new frame Precondition: n ≥ 0 an int""" § Frames require memory if n <= 1: § ∞ calls = ∞ memory return 1 fibonacci 3 return (fibonacci(n-1)+ 5 n fibonacci(n-2)) fibonacci 1 fibonacci 1 4 3 n n 10/15/15 Recursion 10
Fibonacci: # of Frames vs. # of Calls • Fibonacci is very inefficient. § fib( n ) has a stack that is always ≤ n § But fib( n ) makes a lot of redundant calls fib(5) fib(4) fib(3) fib(3) fib(2) fib(2) fib(1) fib(2) fib(1) fib(1) fib(0) fib(1) fib(0) fib(1) fib(0) Recursion 11
Fibonacci: # of Frames vs. # of Calls • Fibonacci is very inefficient. § fib( n ) has a stack that is always ≤ n § But fib( n ) makes a lot of redundant calls fib(5) Path to end = fib(4) fib(3) the call stack fib(3) fib(2) fib(2) fib(1) fib(2) fib(1) fib(1) fib(0) fib(1) fib(0) fib(1) fib(0) Recursion 12
Two Major Issues with Recursion • How are recursive calls executed? § We saw this with the Fibonacci example § Use the call frame model of execution • How do we understand a recursive function (and how do we create one)? § You cannot trace the program flow to understand what a recursive function does – too complicated § You need to rely on the function specification 10/15/15 Recursion 13
How to Think About Recursive Functions 1. Have a precise function specification. 2. Base case(s): § When the parameter values are as small as possible § When the answer is determined with little calculation. 3. Recursive case(s): § Recursive calls are used. § Verify recursive cases with the specification 4. Termination: § Arguments of calls must somehow get “smaller” § Each recursive call must get closer to a base case 10/15/15 Recursion 14
Understanding the String Example def num_es(s): • Break problem into parts """Returns: # of 'e's in s""" number of e’s in s = # s is empty number of e’s in s[0] if s == '': Base case + number of e’s in s[1:] return 0 # s has at least one 'e' • Solve small part directly if s[0] == 'e': Recursive case return 1+num_es(s[1:]) number of e’s in s = number of e’s in s[1:] return num_es(s[1:])) (+1 if s[0] is an 'e') 0 1 len(s) (+0 is s[0] not an 'e') s H ello World! 10/15/15 Recursion 15
Understanding the String Example • Step 1: Have a precise specification def num_es(s): """Returns: # of 'e's in s""" “Write” your return # s is empty statement using the if s == '': specification Base case return 0 # return # of 'e's in s[0]+# of 'e's in s[1:] if s[0] == 'e': return 1+num_es(s[1:]) Recursive case return num_es(s[1:])) • Step 2: Check the base case § When s is the empty string, 0 is (correctly) returned. 10/15/15 Recursion 16
Understanding the String Example • Step 3: Recursive calls make progress toward termination def num_es(s): parameter s """Returns: # of 'e's in s""" argument s[1:] is smaller than # s is empty parameter s, so there is progress if s == '': toward reaching base case 0 return 0 # return # of 'e's in s[0]+# of 'e's in s[1:] if s[0] == 'e': return 1+num_es(s[1:]) argument s[1:] return num_es(s[1:])) • Step 4: Check the recursive case § Does it match the specification? 10/15/15 Recursion 17
Exercise: Remove Blanks from a String 1. Have a precise specification def deblank(s): """Returns: s but with its blanks removed""" 2. Base Case : the smallest String s is ''. if s == '': return s 3. Other Cases : String s has at least 1 character. return (s[0] with blanks removed) + (s[1:] with blanks removed) 10/15/15 Recursion 18
Exercise: Remove Blanks from a String 1. Have a precise specification def deblank(s): """Returns: s but with its blanks removed""" 2. Base Case : the smallest String s is ''. if s == '': return s 3. Other Cases : String s has at least 1 character. return (s[0] with blanks removed) + (s[1:] with blanks removed) ('' if s[0] == ' ' else s[0]) 10/15/15 Recursion 19
What the Recursion Does deblank a b c 10/15/15 Recursion 20
What the Recursion Does deblank a b c deblank a b c 10/15/15 Recursion 21
What the Recursion Does deblank a b c deblank a b c a b c deblank 10/15/15 Recursion 22
What the Recursion Does deblank a b c deblank a b c a b c deblank b c deblank 10/15/15 Recursion 23
What the Recursion Does deblank a b c deblank a b c a b c deblank b c deblank b c deblank 10/15/15 Recursion 24
What the Recursion Does deblank a b c deblank a b c a b c deblank b c deblank b c deblank c deblank 10/15/15 Recursion 25
What the Recursion Does deblank a b c deblank a b c a b c deblank b c deblank b c deblank c deblank c 10/15/15 Recursion 26
What the Recursion Does deblank a b c deblank a b c a b c deblank b c deblank b c deblank c deblank c c 10/15/15 Recursion 27
What the Recursion Does deblank a b c deblank a b c a b c deblank b c deblank b c deblank ✗ c c deblank c c 10/15/15 Recursion 28
What the Recursion Does deblank a b c deblank a b c a b c deblank b c deblank b c b c deblank ✗ c c deblank c c 10/15/15 Recursion 29
What the Recursion Does deblank a b c deblank a b c a b c deblank ✗ b c b c deblank b c b c deblank ✗ c c deblank c c 10/15/15 Recursion 30
What the Recursion Does deblank a b c deblank a b c a b c a b c deblank ✗ b c b c deblank b c b c deblank ✗ c c deblank c c 10/15/15 Recursion 31
What the Recursion Does deblank a b c ✗ deblank a b c a b c a b c a b c deblank ✗ b c b c deblank b c b c deblank ✗ c c deblank c c 10/15/15 Recursion 32
What the Recursion Does deblank a b c a b c ✗ deblank a b c a b c a b c a b c deblank ✗ b c b c deblank b c b c deblank ✗ c c deblank c c 10/15/15 Recursion 33
Recommend
More recommend