Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Data Structures CSCI 132, Spring 2014 Lecture 15 Recursion.

Similar presentations


Presentation on theme: "1 Data Structures CSCI 132, Spring 2014 Lecture 15 Recursion."— Presentation transcript:

1 1 Data Structures CSCI 132, Spring 2014 Lecture 15 Recursion

2 2 Recall Divide and Conquer Play DicePoker Roll DiceCompute Rank Find Winner

3 3 Recursion Skate 4 laps Skate 1 lapSkate 3 laps Skate 1 lapSkate 2 laps When the subproblems are smaller versions of the original problem, we can use recursion to solve the problem.

4 4 Example: Skate n laps How to skate n laps using recursion: 1) Skate 1 lap 2) Skate (n - 1) laps The first step is trivial, and we can do it in one step The second step is a smaller version of the original problem. A function that can skate n laps can also skate n-1 laps.

5 5 Recursive Skating void skateLaps( int n) { if ( n < = 0 ) { //do nothing } else { cout << "Skating 1 lap." << endl; skateLaps( n - 1); } } Base Case General Case

6 6 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making the call. In other words, recursion occurs when a function calls itself! But we need to avoid making an infinite sequence of function calls (infinite recursion).

7 7 Finding a Recursive Solution A recursive solution to a problem must be written carefully. The idea is for each successive recursive call to bring you one step closer to a situation in which the problem can easily be solved. This easily solved situation is called the base case. Each recursive algorithm must have at least one base case, as well as the general case.

8 8 General Format for Recursion if (some easily-solved condition) { // base case solution statement(s) } else { // general case (Possibly other statements here, too). recursive function call (Possibly other statements here, too). }

9 9 Example: Factorial Definition of Factorial n! = 1 * 2 * 3 * 4 *... * (n-1) * n; 0! = 1 In other words: fact(n) = 1, n = 0 n * fact( n-1), n > 0

10 10 Writing Factorial in C++ int Fact (int n) { }

11 11 Writing Factorial in C++ int Fact (int n) { if ( n <= 0) { return 1; } else { return n*Fact(n - 1); }

12 12 Execution Model for Factorial Fact(2) n if( n <= 0) return 1; } else { return n * Fact(n - 1); } n if( n <= 0) return 1; } else { return n * Fact(n - 1); } n if( n <= 0) return 1; } else { return n * Fact(n - 1); } 210

13 13 Invocation Tree for Factorial Fact( 3) : 6 Fact( 2) : 2 Fact( 1) : 1 Fact( 0) : 1 Trace of Factorial (we will do in class) nFact(n - 1)Fact( n )

14 14 Invocation Tree for Factorial Fact( 3) : 6 Fact( 2) : 2 Fact( 1) : 1 Fact( 0) : 1 Trace of Factorial (we will do in class) nFact(n - 1)Fact( n ) 3* 2= 6 2* 1= 2 1 * 1= 1 0 ---- 1

15 15 Rabbit Populations: Fibonacci

16 16 Math Teachers Shouldn't knit From "Frazz" by Jeff Mallett

17 17 Fibonacci in C++ int Fib( int n ) { }

18 18 Fibonacci in C++ int Fib( int n ) { if (n <= 1) { return n; } else { return Fib(n-1) + Fib(n-2); }

19 19 Fibonacci Invocation Tree

20 20 How long will this take? For large numbers, each time you increment by 1, it takes about twice as long to compute the Fibonacci result: Fib(32) Fib(31)Fib(30) Suppose our computer can process 10 9 operations per second. How long will it take to compute Fib(100)? (Approximate answer will be given in class).


Download ppt "1 Data Structures CSCI 132, Spring 2014 Lecture 15 Recursion."

Similar presentations


Ads by Google