600 likes | 709 Views
Advanced Recursion. Recursive Function Example: Factorial. Problem: calculate n! (n factorial) n! = 1 if n = 0 n! = 1 * 2 * 3 *...* n if n > 0 Recursively: if n = 0 , then n! = 1 if n > 0, then n! = n * ( n-1 ) !. Solving Recursive Problems.
E N D
Recursive Function Example: Factorial Problem: calculate n! (n factorial) n! = 1 if n = 0 n! = 1 * 2 * 3 *...* n if n > 0 Recursively: if n = 0, then n! = 1 if n > 0, then n! = n * (n-1)!
Solving Recursive Problems • See recursive solutions as two sections: • Current • Rest N! = N * (N-1)! 7! = 7 * 6! 7! = 7 * (6 * 5 * 4 * 3 * 2 * 1 * 1)
Factorial Function function Factorial returnsa Num (n isoftype in Num) // Calculates n factorial, n! // Precondition: n is a non-negative // integer if (n = 0) then Factorial returns 1 else Factorial returns n * Factorial(n-1) endif endfunction //Factorial
if (0 = 0) then Fact returns 1 else Fact returns 1 endif endfunction //Fact if (1 = 0) then Fact returns 1 else Fact returns 1 * Fact(0) endif endfunction //Fact if (2 = 0) then Fact returns 1 else Fact returns 2 * Fact(1) endif endfunction //Fact if (3 = 0) then Fact returns 1 else Fact returns 3 * Fact(2) endif endfunction //Fact algorithm Test ans <- Fact(3) endalgorithm
1. Actual parameters stored on the stack 5. Return value and release stack frame 3. Create a new Stack Frame 4. Unfinished Business 2. Recursive call to Fact Tracing Details function Fact returnsa Num (2) if (2 = 0) then Fact returns 1 else Fact returns 2 * Fact(1) endif endfunction //Fact
Activation Stack for Factorial Call the function: answer <- Fact(5) Main Algorithm: Unfinished: answer <- Fact (5)
Activation Stack for Factorial Fact. 1st: N=5, Unfinished: 5*Fact(4) Main Algorithm: Unfinished: answer <- Fact (5)
Activation Stack for Factorial Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Main Algorithm: Unfinished: answer <- Fact (5)
Activation Stack for Factorial Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Main Algorithm: Unfinished: answer <- Fact (5)
Activation Stack for Factorial Fact. 4th: N=2, Unfinished: 2*Fact(1) Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Main Algorithm: Unfinished: answer <- Fact (5)
Activation Stack for Factorial Fact. 5th: N=1, Unfinished: 1*Fact(0) Fact. 4th: N=2, Unfinished: 2*Fact(1) Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Main Algorithm: Unfinished: answer <- Fact (5)
Activation Stack for Factorial Fact. 6th: N=0, Finished: returns 1 Fact. 5th: N=1, Unfinished: 1*Fact(0) Fact. 4th: N=2, Unfinished: 2*Fact(1) Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Main Algorithm: Unfinished: answer <- Fact (5)
Activation Stack for Factorial Fact. 5th: N=1, Finished: returns 1*1 Fact. 4th: N=2, Unfinished: 2*Fact(1) Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Main Algorithm: Unfinished: answer <- Fact (5)
Activation Stack for Factorial Fact. 4th: N=2, Finished: returns 2*1 Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Main Algorithm: Unfinished: answer <- Fact (5)
Activation Stack for Factorial Fact. 3rd: N=3, Finished: returns 3*2 Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Main Algorithm: Unfinished: answer <- Fact (5)
Activation Stack for Factorial Fact. 2nd: N=4, Finished: returns 4*6 Fact. 1st: N=5, Unfinished: 5*Fact(4) Main Algorithm: Unfinished: answer <- Fact (5)
Activation Stack for Factorial Fact. 1st: N=5, Finished: returns 5*24 Main Algorithm: Unfinished: answer <- Fact (5)
Activation Stack for Factorial Main Algorithm: Finished: answer <- 120
LB Exponentiation baseexponent e.g. 53 Could be written as a function Power(base, exp)
LB Can we write it recursively? be = b * b(e-1) What’s the limiting case? When e = 0 we have b0 which always equals? 1
Another Recursive Function function Power returnsa Num (base, exp isoftype in Num) // Computes the value of BaseExp // Pre: exp is a non-negative integer if (exp = 0) then Power returns 1 else Power returns base * Power(base, exp-1) endif endfunction //Power
Power base = 3 exp = 0 Finished:1 1 Power base = 3 exp = 1 3 *Power(3,0) 3 Power base = 3 exp = 2 3 *Power(3,1) 9 Power base = 3 exp = 3 3 *Power(3,2) 27 Power base = 3 exp = 4 3 *Power(3,3) 81 Algo: total <- Power(3,4) Function Power returnsa Num (base, exp isoftype in Num) //Computes the value of BaseExp //Preconditions: exp is a non-negative integer if(exp = 0 ) then Power returns 1 else Power returns (base * Power(base, exp – 1)) endif endfunction //Power Activations Stack Example 1 3 9 27 Total <- 81
LB Bunnies? • The Time: 13th Century • The Place: Italy • The Man: Fibonacci • The Problem: We start with a pair of newborn rabbits. At the end of the 2nd month, and each month thereafter the female gives birth to a new pair of rabbits: one male and one female. The babies mature at the same rate as the parents and begin to produce offspring on the same schedule. So how many rabbits do we have at the end of one year?
A More Complex Recursive Function Fibonacci Number Sequence if n = 1, then Fib(n) =1 if n = 2, then Fib(n) = 1 if n > 2, then Fib(n) = Fib(n-2) + Fib(n-1) Numbers in the series: 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Fibonacci Sequence Function function Fib returnsa Num (n iot in Num) // Calculates the nth Fibonacci number // Precondition: N is a positive integer if ((n = 1) OR (n = 2)) then Fib returns 1 else Fib returnsFib(n-2) + Fib(n-1) endif endfunction //Fibonacci
Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Fib(5): Fib returns Fib(3) + Fib(4) Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Fib(3): Fib returns Fib(1) + Fib(2) Fib(5): Fib returns Fib(3) + Fib(4) Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Fib(1): Fib returns 1 Fib(3): Fib returns Fib(1) + Fib(2) Fib(5): Fib returns Fib(3) + Fib(4) Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Fib(3): Fib returns 1 + Fib(2) Fib(5): Fib returns Fib(3) + Fib(4) Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Fib(2): Fib returns 1 Fib(3): Fib returns 1 + Fib(2) Fib(5): Fib returns Fib(3) + Fib(4) Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Fib(3): Fib returns 1 + 1 Fib(5): Fib returns Fib(3) + Fib(4) Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Fib(5): Fib returns 2 + Fib(4) Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Fib(4): Fib returns Fib(2) + Fib(3) Fib(5): Fib returns 2 + Fib(4) Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Fib(2): Fib returns 1 Fib(4): Fib returns Fib(2) + Fib(3) Fib(5): Fib returns 2 + Fib(4) Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Fib(4): Fib returns 1 + Fib(3) Fib(5): Fib returns 2 + Fib(4) Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Fib(3): Fib returns Fib(1) + Fib(2) Fib(4): Fib returns 1 + Fib(3) Fib(5): Fib returns 2 + Fib(4) Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Fib(1): Fib returns 1 Fib(3): Fib returns Fib(1) + Fib(2) Fib(4): Fib returns 1 + Fib(3) Fib(5): Fib returns 2 + Fib(4) Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Fib(3): Fib returns 1 + Fib(2) Fib(4): Fib returns 1 + Fib(3) Fib(5): Fib returns 2 + Fib(4) Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Fib(2): Fib returns 1 Fib(3): Fib returns 1 + Fib(2) Fib(4): Fib returns 1 + Fib(3) Fib(5): Fib returns 2 + Fib(4) Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Fib(3): Fib returns 1 + 1 Fib(4): Fib returns 1 + Fib(3) Fib(5): Fib returns 2 + Fib(4) Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Fib(4): Fib returns 1 + 2 Fib(5): Fib returns 2 + Fib(4) Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Fib(5): Fib returns 2 + 3 Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Main Algorithm: answer <- 5
Mutual Recursion Recursion doesn’t always occur because a routine calls itself... Mutual Recursion occurs when two routines call each other. A B A B
Mutual Recursion Example Problem: Determine whether a number, N, is odd or even. • If N is equal to 0, then n is even • N is odd if N-1 is even
Example Implementation function Odd returnsa Boolean (n iot in Num) if (n = 0) then Odd returns FALSE else Odd returns Even (n - 1) endif endfunction //Odd function Even returnsa Boolean (n iot in Num) if (n = 0) then Even returns TRUE else Even returns Odd (n - 1) endif endfunction //Even
Building Recursive Modules • Decide on the terminating condition • Decide the final actions when you terminate • There may be none • If you are computing something, this usually starts a “ripple” of returned data • Decide how to take a step closer to terminating • think about how to make your original, complex problem simpler • Decide how to call a “clone” of this module • Decide what work to do at this step in recursion