Presentation is loading. Please wait.

Presentation is loading. Please wait.

Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.

Similar presentations


Presentation on theme: "Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using."— Presentation transcript:

1

2 Welcome to Recursion! Say what?!?

3 Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using loops. elegant, once you understand it! not necessarily efficient, it can be memory intensive! able to handle big problems with ease. Let’s take a look…

4 What does (4!) evaluate to? 4! = 4 * 3 * 2 * 1 = 24 (But that’s too easy!) Recursively, 4! equals 4 * 3! Now we have a simpler problem to solve. What is 3! equal to? Recursively, 3! = 3 * 2! What is 2! equal to? Recursively, 2! = 2 * 1 = 2 Once we know what the last product is we can now multiply back to find the answer.

5 To reiterate: Keep repetitively simplifying until we get to a stopping point: 4 * 3! = ?  keep finding (n – 1)! 4 * 3 * 2! = ? 2! = 2 * 1 = 2  base case 3 * 2 = 6  work backwards 4 * 6 = 24  the final solution!

6 Recursive Factorial Discussion: This is a good introduction to the basic concept of recursion but not the best use of it. Each value had to be temporarily stored until the final answer was calculated. We couldn’t complete 4! until we knew value of 3! and so on. This seems like a lot of work for a simple problem. It is, but recursion can do many more things!

7 A recursive method needs: A call to the method itself. A base case or “recursive out” to end the call to itself.

8 Let’s look at the C++ method: int Factorial(int num) //Finds the factorial of num { if (num == 1)//base case return 1;//return base value else return num * Factorial(num - 1); } //recursive call

9 What’s going on in memory? 4! = 4 * 3! Store the 4 and Calculate 3! = 3 * 2! Store the 3 and Calculate 2! = 2 * 1  base is reached now backtrack  use stored 3 * 2  use stored 4 * 6 The answer = 4 * 3 * 2 = 24 Memory can fill up fast!

10 Now let’s try another one: Write a recursive function to find a base raised to a power. i.e. 24 = 2 * 2 * 2 * 2 = 16 Look for the recursion! How can 24 be simplified? 24 = 2 * 23 23 = 2 * 22 22 = 2 * 21 What is the base case? 21 = 2  when the exponent equals 1

11 Now let’s write the code: cout << Power(2, 4) int Power(int base, int exp) {//which value will be reduced? if (exp == 1)//base case return base;//21 = 2 else//recursive call return base * Power(base, exp-1); }

12 Recursion Definitions Recursion: If you understand the concept, stop; otherwise, see Recursion. Infinite recursion: See infinite recursion. If there is no base case infinite recursion will occur and you will run out of memory. Recursion is a difficult concept at first but it can be used to do amazing things!

13 Recursion & Graphics (Fractals) Images from: http://www.enchgallery.com/fractals/fracthumbs.htm

14 Recursive output: What is the output of the following code with a call to example(3)? void example (int N) { cout << (“Entering method, N = “ + N); if (N > 1) example(N – 1); cout << (“Leaving method, N = “ + N); }//Be careful, this has tail recursion! What is the base case? Base case is when N = 0 Entering function, N = 3 Entering function, N = 2 Entering function, N = 1 N is now 0 so the method is not called again! But there are still parts of the function not completed! Leaving function, N = 1  Tail recursion happens Leaving function, N = 2  after the conclusion of Leaving function, N = 3  the recursive calls!

15 Let’s practice: Plan a method called SumUp() that takes one parameter and finds the sum of all the numbers from one up to the parameter. i.e. SumUp(4) = 1 + 2 + 3 + 4 = 10 Write the code to do it iteratively and recursively.

16 Iterative Solution: int SumUpI(int n) { int intSum = 0, x; for (x = 1; x <= n; x++) intSum += x; return intSum; }

17 Recursive Solution: int SumUpR(int n) {//What is the base case? if (n == 1) //Return what? return 1; else return n + SumUpR(n – 1); }

18 Closing Questions: What control structure is recursion similar to? LOOPING What are the two basic requirements in a recursive method? BASE CASE, METHOD CALLS ITSELF What will happen if there is no base case? INFINITE RECURSION What can be a drawback to using recursion? MEMORY INTENSIVE CHALLENGE YOURSELF TO THINK RECURSIVELY!

19 More Practice: Write a recursive method FIBO that returns the nth Fibonacci #: 1 123581321… Something to think about: f(1)=1 f(2)=1 f(3)=2f(n) = f(n-1) + f(n-2) Here’s the solution: int FIBO(int n) { if (n <= 2) //Why <= ? return 1; else //otherwise FIBO(2) returns 2! return f(n - 1) + f(n - 2); } This one can be memory intensive due to the two recursive calls!


Download ppt "Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using."

Similar presentations


Ads by Google