Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.

Similar presentations


Presentation on theme: "Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful."— Presentation transcript:

1 Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful to have functions call themselves A recursive function is a function that calls itself directly or indirectly through another function

2 Recursion A recursive function is called to solve a problem.
The function actually knows how to solve only the simplest case(s), called the base case(s) . If the function is called with a base case, the function simply returns a result. If a function is called with a more complex problem, the function divides the problem into two conceptual pieces the piece that the function knows how to do a piece that the function does not know how to do

3 Recursion To make recursion feasible, the latter piece must resemble the original problem, but be a slightly simpler or slightly smaller version of the original problem. Because this new problem looks like the original problem, the function calls a fresh copy of itself to work on the smaller problem -- this is referred to as a recursive call and also the recursive step. The recursive step executes while the original call to the function has not finished executing. Every recursive function must include a mechanism for halting the recursion!!

4 Recursion The recursive step can result in many more recursive calls.
In order for recursion to eventually terminate, each time the function calls itself with a slightly simpler version of the original problem, the sequence of smaller problems must converge on the base case. When the sequence reaches the base case, the function returns a result to the previous copy of the function. This previous copy then returns a result to the copy that called it, etc. until the original function returns a final result to the caller

5 Example A simple, but not useful, application The "traditional" example of recursion involves computing mathematical recurrences, commonly the factorial function. The factorial (and other recurrences) are trivial to calculate in a single for loop in a way that is much more CPU and memory efficient! Nevertheless, the simple example provides a useful first step in understanding the recursive approach.

6 Example Computing n! (n factorial) n! = n*(n-1)*(n-2)*(n-3)*...*1 0! = 1 1! = ! = 5*4*3*2*1 = 120 We can use iteration to compute n! as follows: int factorial = 1; for (int counter = n; counter >= 0; counter--) factorial *= counter;

7 Example By observing the following relationship, we can develop a recursive definition: n! = n*(n-1)! The recursive function definition is int factorial (int number) { if (number == 0) // base case return 1; else return (number * factorial (number - 1)); // Every recursive function must include a mechanism // for halting the recursion!! }

8 Properties of Recursive Problems and Solutions
Problems that can be solved by recursion have the following characteristics: One or more stopping cases (base cases) have a simple, non-recursive solution. The other cases of the problem can be reduced to problems that are closer to the base cases. Eventually the problem can be reduced to only base cases, which are relatively easy to solve.

9 To solve a problem recursively:
Try to express the problem as a simpler version of itself. Determine the base cases. Determine the recursive steps.

10 Solving a problem recursively:
Our recursive algorithms will generally consist of an if statement of the form:   if (base case is reached) Solve it else Split the problem into simpler cases using recursion

11 Example - Computing the nth Fibonacci number
The Fibonacci numbers were originally intended to model the growth of a rabbit colony. The Fibonacci sequence is 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The Fibonacci sequence is defined as follows: fib(1) = 1 fib(2) = 1 fib(n) = fib(n-1) + fib(n-2), n > 2 // A method for computing the nth Fibonacci number int fibonacci(int n) { // This method computes the nth Fibonacci number // pre: n is defined and n > 0 // post: returns the nth Fibonacci number if (n <= 2) return 1; else return fibonacci(n - 2) + fibonacci(n - 2); }

12 Greatest common divisor (GCD) of two positive integers
gcd(m,n) is gcd(n,m) if m < n gcd(m, n) is n if n <= m and n divides m gcd(m, n) is gcd(n, m%n), otherwise // This method finds the greatest common divisor of m and n. // pre: m and n are defined. m > 0, n > 0 // post: returns the greatest common divisor of m & n int gcd(int m, int n) { if (m < n) return gcd(n,m) else if (m % n == 0) // n divides m return n; else return gcd(n, m%n) }

13 Example - Raising x to the power y
// This method raises x to the power y // pre: x and y are defined and y > 0 // post: returns x raised to the power y int power(int x, int y) { if (y <= 1) return x; // Base case else return x * power(x, y - 1); // Recursive step }


Download ppt "Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful."

Similar presentations


Ads by Google