Recursion https sites google comacabrillo educs11 mschedulerecursion Recursion
Recursion https: //sites. google. com/a/cabrillo. edu/cs-11 m/schedule/recursion
Recursion • Recursion: Definition of operation in terms of itself • Solve a problem in terms of the solution to smaller occurrences of same problem • Recursive functions: functions that call themselves • Substitute for iteration • Suited to certain types of problems
Why Recursion? • A different way of thinking about problems • A better way of solving some problems than iteration • Can produce elegant, short code • Some functional programming languages (Scheme, Haskell) use recursion exclusively – no loops
Exercise • For student in front row: How many students total are directly behind you in the classroom (i. e. , in your column in the room)? • Assumptions: • Your vision is poor. You cannot look back and count. • But you can ask questions of people sitting next to you (in any direction. ) • How do we solve this problem? How many people are in this column?
Exercise • Recursion involves breaking a big problem into smaller occurrences of the same problem. • Each person can solve a small part of the problem. • What's a small version of the problem that's easy to answer? • What information might the person behind me provide? Hey, behind me, can you help me?
Exercise • To find the number of people behind me: • If there's a person behind me: ask how many people are behind him/her. • If that person answers N, then my answer is N+1 • If there's nobody behind me, I answer 0.
Recursive Cases • A recursive algorithm has at least two cases • base case: a simple occurrence that can be answered directly • recursive case: an occurrence of the problem that is answered in terms of smaller occurrences of the problem • There may be more than one base or recursive case.
Writing Recursive Functions 1. Base case: Always have at least one case that is solved directly without using recursion • Know when to stop 2. Make progress: Any recursive call must progress towards base case. • Call function on smaller occurrence of the problem 3. You "gotta have faith. " Always assume that the recursive call works. (And of course, design and test to ensure that it does!) Note: A recursive solution solves a small occurrence of the problem directly, and leaves the rest of the problem in the same form as the original.
N! • The classic first recursion example • Example: 5! = 5*4*3*2*1 = 120 • Recursive definition: 0! = 1 N! = N * (N-1)! base case recursive case • Iterative function: int fact(int n) { int res = 1; for(int i = 2; i <= n; i++) { res *= i; } return res; }
Recursive Factorial • Recursive definition of factorial: 0! = 1 N! = N * (N-1)! // pre: n >= 0 int fact(int n) { if(n == 0) return 1; else return n * fact(n-1); }
Program Stack • For every recursive call, an activation record is pushed onto the runtime stack • Every recursive call must simplify the computation in some way • To compute fact(4) n=0 fact(4) calls fact(3) return 1 to caller fact(3) calls fact(2) n=1 return 1 * fact(0) fact(2) calls fact(1) = 1 * 1 to caller fact(1) calls fact(0) n=2 fact(0) returns 1 return 2 * fact(1) = 2 * 1 to caller fact(1) returns 1 * 1 = 1 n=3 fact(2) returns 1 * 2 = 2 return 3 * fact(2) fact(3) returns 2 * 3 = 6 = 3 * 2 to caller fact(4) returns 6 * 4 = 24 n=4 return 4 * fact(3) = 4 * 3 to caller
Evaluate Recursive Functions int mystery(int n) { if (n == 0) return 1; else return 3 * mystery(n-1); } mystery(5) returns ?
Evaluate Recursive Functions • Draw the program stack: m(5) = 3 * m(4) = 3 * m(3) = 3 * m(2) = 3 * m(1) = 3 * m(0) = 1 3^5 = 243
Multiple Recursive Calls int bar(int n) { if (n <= 0) return 2; else return 3 + bar(n-1) + bar(n-2); } What does bar(5) return? A. 2 B. 5 C. 13 D. 62 E. 127
Evaluating Recursive Functions • What's returned by bar(5)? b(5) = 3 + b(4) + b(3) b(4) = 3 + b(3) + b(2) b(3) = 3 + b(2) + b(1) b(2) = 3 + b(1) + b(0) b(1) = 3 + b(0) + b(-1) int bar(int n) { b(0) = 2 if (n <= 0) return 2; b(-1) = 2 else return 3 + bar(n-1) + bar(n-2); }
Evaluating Recursive Functions • What's returned by bar(5)? b(5) = 3 + b(4) + b(3) b(4) = 3 + b(3) + b(2) b(3) = 3 + b(2) + b(1) b(2) = 3 + b(1) + b(0) // substitute in results b(1) = 3 + 2 = 7 int bar(int n) { b(0) = 2 if (n <= 0) b(-1) = 2 return 2; else return 3 + bar(n-1) + bar(n-2); }
Evaluating Recursive Functions • What's returned by bar(5)? b(5) = 3 + b(4) + b(3) b(4) = 3 + b(3) + b(2) b(3) = 3 + b(2) + b(1) b(2) = 3 + 7 + 2 = 12 b(1) = 7 int bar(int n) { b(0) = 2 if (n <= 0) b(-1) = 2 return 2; else return 3 + bar(n-1) + bar(n-2); }
Evaluating Recursive Functions • What's returned by bar(5)? b(5) = 3 + b(4) + b(3) b(4) = 3 + b(3) + b(2) b(3) = 3 + 12 + 7 = 22 b(2) = 12 b(1) = 7 int bar(int n) { if (n <= 0) b(0) = 2 return 2; b(-1) = 2 else return 3 + bar(n-1) + bar(n-2); }
Evaluating Recursive Functions • What's returned by bar(5)? b(5) = 3 + b(4) + b(3) b(4) = 3 + 22 + 12 = 37 b(3) = 22 b(2) = 12 b(1) = 7 int bar(int n) { if (n <= 0) b(0) = 2 return 2; b(-1) = 2 else return 3 + bar(n-1) + bar(n-2); }
Evaluating Recursive Functions • What's returned by bar(5)? b(5) = 3 + 37 + 22 = 62 b(4) = 37 b(3) = 22 b(2) = 12 int bar(int n) { b(1) = 7 if (n <= 0) b(0) = 2 return 2; else b(-1) = 2 return 3 + bar(n-1) + bar(n-2); }
Recursion Practice • Write a function raise. To. Power(int base, int power) // pre: power >= 0 • Simple or tail recursion: recursive function call occurs at end of function
Recursion Practice • Here's an iterative function that prints a line of asterisks: // Print a line of n stars // pre: n >= 1 void print. Stars(int n) { for(int i = 0; i < n; i++) { putchar("*"); } printf("n"); } • Write a recursive version of this function. Your function should print only one star at a time. • What's the base case? • What's the recursive case?
Recursion Practice int mystery(int n) { if(n < 10) return n; else { int a = n / 10; int b = n % 10; return mystery(a + b); } • What is returned by mystery(648)?
Recursion • Recursion is a tool. It is not a good tool for all problems. • We'll implement several algorithms and functions where an iterative solution would work fine. • After learning how recursion works, the real skill is knowing when to use it
Big-O and Recursion • Determining big-O of recursive functions: tricky • T(N), the actual run time, is a recurrence relation. • For fact() function: • T(N) = T(N-1) + O(1) • There are N steps • T(N) = O(N) int fact(int n) { if(n == 0) return 1; else return n * fact(n-1); }
Common Recurrence Relations • T(N) = T(N/2) + O(1) O(log. N) • binary search • T(N) = T(N-1) + O(1) O(N) • sequential search, factorial • T(N) = T(N-1) + O(1) O(2^N) • Fibonacci
- Slides: 26