Вы находитесь на странице: 1из 3

Recursion

Recursion is a method of solving a problem by solving a simpler version or versions


of the original problem (and perhaps doing some additional computations).
Arecursive solution must break a problem up into one or more simpler versions of the
original problem. These might be simpler in that they use a smaller value of a
parameter. Also, the chain of recursive calls that gets generated must always end in
a stopping case, a case where the answer is directly known so that further recursive
function calls are not used.

The Fibonacci Function

Another common mathematical function that can be defined recursively is the


fibonacci function. It can be defined as follows:
fib(0) = 1
fib(1) = 1
fib(n) = fib(n-1) + fib(n-2) for n > 1

This is a recursive definition, with the first two lines specifying stopping cases. The
last line is for the recursive case. It defines fib(n) in terms of fib(n-1) and fib(n-2), two
simpler versions of the problem of finding fib(n). One can easily use this definition to
figure out that fib(2) = 2, and then that fib(3) = 3, etc. Each new value of the fibonacci
function is the sum of the two previous values. The following table summarizes the
first several values of the fibonacci function. Somewhat like the factorial function, this
function starts to get large pretty quickly.
n fib(n)
0 1
1 1
2 2
3 3
4 5
5 8
6 13
7 21
8 34
9 55
10 89

A recursive C++ fibonacci function can be defined as follows. Note how closely it
follows the above mathematical definition.
/* Given: n A non-negative integer.
Task: To find the nth Fibonacci number.
Return: This Fibonacci number in the function name.
*/
long fib(int n)
{
if ((n == 0) || (n == 1)) // stopping cases
return 1;
else // recursive case
return fib(n - 1) + fib(n - 2);
}

Let's draw one of our simplified pictures to trace the operation of this fib function.
Let's say that we start by calling fib(4). We show this call in a box with an arrow to
the code that it generates. But as you see, the original call generates two more calls.
Then the recursive function calls keep splitting out, in the manner of a binary tree,
until we reach the stopping cases.

Notice how fib(2) gets computed twice! Plus, the stopping cases for the function get
called needlessly over and over. There is a lot of wasted effort in this computation of
fib(4). The tree of function calls would be even larger and more wasteful if we
computed fib(5). This is an instance where a recursive function is the wrong approach
to take.
In the complete program, fib.cpp, there is an iterative fibonacci function that is much
more efficient. It uses a simple loop to carry out the computations shown in our table
of fibonacci values above. There is also no needless recomputation of values. So, for
the fibonacci function, iteration is better than recursion. Whenever you see a treeful of
recursive function calls, ask yourself if you can compute the same thing with just a
linear sequence of actions. If so, try using a loop as a method that may well be faster
than the recursion. Note that sometimes an algorithm that gives a treeful of recursive
function calls is efficient. This is often true if the tree is very short, so that the
stopping cases are reached quickly.

A Recursive Binary Search

Binary search is often written using recursion, instead of iteration as we did earlier in
the array section. The key idea is that when the algorithm decides to search the right
or left half of the array, that is a simpler version of the original problem. In the
recursive BinarySearch function below note how the parameters to the recursive calls
are adjusted to specify either the right or left half of the array. See bsearch.cpp for the
complete program.
/* Given: IntArray Array of integers.
Low The low index of the range of integers to search.
High The top index of the range of integers to search.
Target The integer for which to search.
Task: To do a recursive binary search for Target in the specified
range of IntArray.
Return: In the function name, return the index of where Target was
found or -1 if it was not found.
*/
int BinarySearch(IntArrayType IntArray, int Low, int High, int Target)
{
int Mid, Difference;

if (Low > High)


return -1;
else
{
Mid = (Low + High) / 2;
Difference = IntArray[Mid] - Target;

if (Difference == 0) // IntArray[Mid] == Target


return Mid;
else if (Difference < 0) // IntArray[Mid] < Target
return BinarySearch(IntArray, Mid + 1, High, Target);
else
return BinarySearch(IntArray, Low, Mid - 1, Target);

Вам также может понравиться