Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 7 – Recursion (A Very Quick Look). CISC 105 – Topic 7 What is Recursion? A recursive function is a function that calls itself. Recursive functions.

Similar presentations


Presentation on theme: "Topic 7 – Recursion (A Very Quick Look). CISC 105 – Topic 7 What is Recursion? A recursive function is a function that calls itself. Recursive functions."— Presentation transcript:

1 Topic 7 – Recursion (A Very Quick Look)

2 CISC 105 – Topic 7 What is Recursion? A recursive function is a function that calls itself. Recursive functions can be used to replace loops, under certain circumstances.

3 CISC 105 – Topic 7 Recursive Problems Problems that can be solved using recursion typically: Have one or more simple cases (or base case) that have simple, non-recursive solutions The other cases can be redefined in terms of problems that are closer to the base cases. By applying this redefinition, the entire problem can be reduced entirely to simple cases, which are relatively easy to solve. This relationship between cases is known as the recurrence relation.

4 CISC 105 – Topic 7 THE Classic Recursive Problem The most-common recursive problem is that of the Fibonacci numbers. Each Fibonacci number can be defined as the sum of the two previous Fibonacci numbers. The first two Fibonacci numbers are exempt from this definition, as they are both equal to one (1).

5 CISC 105 – Topic 7 Fibonacci Numbers Therefore, the first two Fibonacci numbers (the ones) are the base cases. All other Fibonacci numbers are the sum of the previous two Fibonacci numbers. This is the recurrence relation of the Fibonacci numbers. Therefore, Fib(3) = Fib(2) + Fib(1). As Fib(1) = 1 and Fib(2) = 1, Fib(3) = 1 + 1 = 2

6 CISC 105 – Topic 7 Fibonacci Numbers Similarily, Fib(4) = Fib(3) + Fib(2). Fib(3) = Fib(2) + Fib(1) Fib(2) = 1 Fib(1) = 1 Therefore, Fib(4) = 1 + 1 + 1 = 3

7 CISC 105 – Topic 7 Fibonacci Numbers Similarily, Fib(5) = Fib(4) + Fib(3). Fib(4) = Fib(3) + Fib(2) Fib(3) = Fib(2) + Fib(1) Fib(4) = Fib(2) + Fib(1) + Fib(2) Fib(2) = 1 Fib(1) = 1 Therefore, Fib(5) = 1 + 1 + 1 + 1 + 1 = 5

8 CISC 105 – Topic 7 Fibonacci Numbers Function So…a recursive Fibonacci function would look like: int fib(int n) { int ans; if (n == 1 || n == 2) ans = 1; else ans = fib(n - 2) + fib(n - 1); return ans; } Base Cases Here, fib(1) and fib(2) are the base cases. They both have simple, non-recursive solution; they are both equal to one. Recurrence Relation The program redefined all cases other than the base case as a problem closer to the base case(s). This will continue to occur until all problems are simply a function of the base case(s).

9 CISC 105 – Topic 7 A Common Error in Recursive Function Design A common error in the writing of recursive function is to not have a problem which breaks down (using the recurrence relation) into base cases. If a function does not break down to base cases, the function may continue forever. For example, if the Fibonacci function did not include the Fib(1) and Fib(2) base cases, it would continue forever and never return an answer.

10 CISC 105 – Topic 7 A Common Error in Recursive Function Design For example, this Fibonacci function would never return a correct answer: int fib(int n) { int ans; ans = fib(n - 2) + fib(n - 1); return ans; }

11 CISC 105 – Topic 7 A Common Error in Recursive Function Design Likewise, this function will also never return an answer: int fib(int n) { int ans; if (n == 1 || n == 2) ans = 1; else ans = fib(n) + fib(n - 1); return ans; }

12 CISC 105 – Topic 7 A Test of Recursion Write a recursive function which can be used to compute the factorial of a certain number.


Download ppt "Topic 7 – Recursion (A Very Quick Look). CISC 105 – Topic 7 What is Recursion? A recursive function is a function that calls itself. Recursive functions."

Similar presentations


Ads by Google