Recursion

Excerpts taken from Data Abstraction and Problem Solving w/C++ by Carrano


Recursion is a way to break up a large problem into smaller sub-problems. The strategy of recursion is one of divide and conquer.

An example could be looking up a word in the dictionary, where the pseudocode is:

search(dictionary, word){
    if( the dictionary contains only one page )
        Scan the page for the word
    else{
        Open the dictionary in the middle
        Determine the location of the word (before or after page)
        if( the word is in the first half )
            search(first half of dictionary, word)
        else
            search(second half, word)
    }

At some point you will reach the base case where you will examine one page of the dictionary. A base case is a special case whose solution you know.

Notice that in the pseudocode above, the function is making a call to itself. This is a recursive call.

Some examples of recursive functions are the factorial function, fibinacci function and binary search algorithm. Let's take a look at some of these as well as a few others.

Factorial of n

Factorial of n [or n!] is calculated as:

    fact(n) = n *[(n-1)*(n-2)*(n-3)*....*1]
            = n * fact( n-1 )

The base case of fact( n ) is fact( 0 ) which is 1. So if we break this up, we get:

                 _
    fact(n) =   | 1                     if n == 0
                |
                | n * fact( n-1 )       if n > 0
                |_

Let's do a runthrough with n=5:

    fact(5) = 5*fact(5-1)
    fact(4) = 4*fact(4-1)
    fact(3) = 3*fact(3-1)
    fact(2) = 2*fact(2-1)
    fact(1) = 1*fact(1-1)
    fact(0) = 1

Now we have to substitute back for all the answers that we have obtained. For now we know that fact(0)==1 so we substitite that back on the line with fact(1):

    ...
    ...
    fact(1) = 1 * 1
    fact(0) = 1

With all substitutions we get:

    fact(5) = 5 * 24 = 120
    fact(4) = 4 * 6 = 24
    fact(3) = 3 * 2 = 6
    fact(2) = 2 * 1 = 2
    fact(1) = 1 * 1 = 1
    fact(0) = 1

The actual code for fact(n) is here:

    int fact(int n){
        if( n == 0 ){   /* base case! */
            return 1;
        }else{
            return n * fact( n - 1 );
        }
    }

Backwards String

[More detail to be filled in later]

    void writeBackwards(string str, int size){
        if( size > 0 ){
            cout << str.substr( size-1, 1 );
            writeBackwards(str, size-1);
        }
    }

Fibonacci

    References:
  1. animation
  2. Wolfram Mathworld
  3. Wikipedia
    int fib( int n ){
        if( n == 0 || n == 1 )
            /* Base Case! */
            return 1;
         else
             return fib( n - 1 ) + fib( n - 2 );
    }

Choosing k out of n things:

    int c( int n, int k ){
        if(( k == 0 ) || (k == n ){
            /* Base Case! */
            return 0;
        }else if( k > n ){
            return 0;
        }else{
            return c( n-1, k-1 ) + c( n-1, k );
        }
    }

Binary Search

    References
  1. Binary Search
    int bsearch(const int arr[], int first, int last, int value){
        int index;
        if( first > last ){
            /* value not found! */
            return -1;
        }else{
            int mid = (first + last)/2;
            if( value == arr[mid] )
                index = mid;
            else if( value < arr[mid] )
                index = bsearch(arr, first, mid-1, value);
            else
                index = bsearch(arr, mid+1, last, value);
        }

        return index;
    }

Towers of Hanoi

    References:
  1. Wolfram Mathworld
  2. Wikipedia
    void towers(int count, char source, char destination, char spare){
        if( count == 1 ){
            /* Base Case! */
            cout << "Move top disk from pole " << source
                 << " to pole " << destination << endl;
        }else{
            towers( count-1, source, spare, destination );
            towers( 1, source, destination, spare );
            towers( count-1, spare, destination, source );
        }

    }

Actual program can be found here.



QC > QC CS Dept > J. Levy Homepage > CS 340

Last Modified:

Jamie L. Levy
Computer Science Department
Queens College, CUNY