Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion https://sites.google.com/a/cabrillo.edu/cs-11m/schedule/recursion.

Similar presentations


Presentation on theme: "Recursion https://sites.google.com/a/cabrillo.edu/cs-11m/schedule/recursion."— Presentation transcript:

1 Recursion

2 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

3 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

4 How many people are in this column?
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?

5 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? Hey, behind me, can you help me? Hey, behind me, can you help me?

6 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.

7 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.

8 Writing Recursive Functions
Base case: Always have at least one case that is solved directly without using recursion Know when to stop Make progress: Any recursive call must progress towards base case. Call function on smaller occurrence of the problem 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.

9 N! The classic first recursion example Example: 5! = 5*4*3*2*1 = 120
Recursive definition: 0! = 1 base case N! = N * (N-1)! recursive case Iterative function: int fact(int n) { int res = 1; for(int i = 2; i <= n; i++) { res *= i; } return res;

10 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); }

11 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) fact(4) calls fact(3) fact(3) calls fact(2) fact(2) calls fact(1) fact(1) calls fact(0) fact(0) returns fact(1) returns 1 * 1 = fact(2) returns 1 * 2 = fact(3) returns 2 * 3 = 6 fact(4) returns 6 * 4 = 24 n = 0 return 1 to caller n = 1 return 1 * fact(0) = 1 * 1 to caller n = 2 return 2 * fact(1) = 2 * 1 to caller n = 3 return 3 * fact(2) = 3 * 2 to caller n = 4 return 4 * fact(3) = 4 * 3 to caller

12 Evaluate Recursive Functions
int mystery(int n) { if (n == 0) return 1; else return 3 * mystery(n-1); } mystery(5) returns ? mystery(5) = 3 * mystery(4) = 3 * 3 * mystery(3) = 3^3 * mystery(2) = 3^4 * mystery(1) = 3^5 * mystery(0) = 3^5 * 1 = 3^5

13 Evaluate Recursive Functions
Draw the program stack: m(5) = 3 * m(4) m(4) = 3 * m(3) m(3) = 3 * m(2) m(2) = 3 * m(1) m(1) = 3 * m(0) m(0) = 1  3^5 = 243

14 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

15 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) b(0) = 2 b(-1) = 2 int bar(int n) { if (n <= 0) return 2; else return 3 + bar(n-1) + bar(n-2); }

16 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) = = 7 b(0) = 2 b(-1) = 2 int bar(int n) { if (n <= 0) return 2; else return 3 + bar(n-1) + bar(n-2); }

17 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) = = 12 b(1) = 7 b(0) = 2 b(-1) = 2 int bar(int n) { if (n <= 0) return 2; else return 3 + bar(n-1) + bar(n-2); }

18 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) = = 22 b(2) = 12 b(1) = 7 b(0) = 2 b(-1) = 2 int bar(int n) { if (n <= 0) return 2; else return 3 + bar(n-1) + bar(n-2); }

19 Evaluating Recursive Functions
What's returned by bar(5)? b(5) = 3 + b(4) + b(3) b(4) = = 37 b(3) = 22 b(2) = 12 b(1) = 7 b(0) = 2 b(-1) = 2 int bar(int n) { if (n <= 0) return 2; else return 3 + bar(n-1) + bar(n-2); }

20 Evaluating Recursive Functions
What's returned by bar(5)? b(5) = = 62 b(4) = 37 b(3) = 22 b(2) = 12 b(1) = 7 b(0) = 2 b(-1) = 2 int bar(int n) { if (n <= 0) return 2; else return 3 + bar(n-1) + bar(n-2); }

21 Recursion Practice Write a function raiseToPower(int base, int power)
// pre: power >= 0 Simple or tail recursion: recursive function call occurs at end of function int raiseToPower(int base, int power) { if(n == 0) return 1; else return base * raiseToPower(base, power-1); }

22 Recursion Practice Here's an iterative function that prints a line of asterisks: // Print a line of n stars // pre: n >= 1 void printStars(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? base case: print one star (n == 1) recursive case: (n > 1) print one star, call printStars(n-1) void printStars(int n) { if(n ==1) {putchar("*"); putchar("\n");} else { putchar("*"); printStars(n-1); }

23 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)? mystery(648): a = 64, b = 8 return mystery(72): a = 7, b = 2 return mystery(9): return 9

24 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

25 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); }

26 Common Recurrence Relations
T(N) = T(N/2) + O(1)  O(logN) binary search T(N) = T(N-1) + O(1)  O(N) sequential search, factorial T(N) = T(N-1) + T(N-1) + O(1)  O(2^N) Fibonacci


Download ppt "Recursion https://sites.google.com/a/cabrillo.edu/cs-11m/schedule/recursion."

Similar presentations


Ads by Google