Category >> Algorithms  

Iteration vs. Recursion in Java

1. Recursion

Consider the factorial function: n!=n*(n-1)*(n-2)*...*1

There are many ways to compute factorials. One way is that n! is equal to n*(n-1)!. Therefore the program can be directly written as:

Program 1:

int factorial (int n) {
    if (n == 1) {
        return 1;
    } else {
        return n*factorial(n-1);
    }
}

In order to run this program, the computer needs to build up a chain of multiplications: factorial(n) → factorial(n-1) → factorial(n-2) → ... → factorial(1). Therefore, the computer has to keep track of the multiplications to be performed later on. This type of program, characterized by a chain of operations, is called recursion. Recursion can be further categorized into linear and tree recursion. When the amount of information needed to keep track of the chain of operations grows linearly with the input, the recursion is called linear recursion. The computation of n! is such a case, because the time required grows linearly with n. Another type of recursion, tree recursion, happens when the amount of information grows exponentially with the input. But we will leave it undiscussed here and go back shortly afterwards.

2. Iteration

A different perspective on computing factorials is by first multiplying 1 by 2, then multiplying the result by 3, then by 4, and so on until n. More formally, the program can use a counter that counts from 1 up to n and compute the product simultaneously until the counter exceeds n. Therefore the program can be written as:

Program 2:

int factorial (int n) {
    int product = 1;
    for(int i=2; i<n; i++) {
        product *= i;
    }
    return product;
}

This program, by contrast to program 2, does not build a chain of multiplication. At each step, the computer only need to keep track of the current values of the product and i. This type of program is called iteration, whose state can be summarized by a fixed number of variables, a fixed rule that describes how the variables should be updated, and an end test that specifies conditions under which the process should terminate. Same as recursion, when the time required grows linearly with the input, we call the iteration linear recursion.

3. Recursion vs Iteration

Compared the two processes, we can find that they seem almost same, especially in term of mathematical function. They both require a number of steps proportional to n to compute n!. On the other hand, when we consider the running processes of the two programs, they evolve quite differently.

In the iterative case, the program variables provide a complete description of the state. If we stopped the computation in the middle, to resume it only need to supply the computer with all variables. However, in the recursive process, information is maintained by the computer, therefore "hidden" to the program. This makes it almost impossible to resume the program after stopping it.

4. Tree recursion

As described above, tree recursion happens when the amount of information grows exponentially with the input. For instance, consider the sequence of Fibonacci numbers defined as follows:

recursion-iteration-java

By the definition, Fibonacci numbers have the following sequence, where each number is the sum of the previous two: 0, 1, 1, 2, 3, 5, 8, 13, 21, ...

A recursive program can be immediately written as:

Program 3:

int fib (int n) {
    if (n == 0) {
        return 0;
    } else if (n == 1) {
        return 1;
    } else {
        return fib(n-1) + fib(n-2);
    }
}

Therefore, to compute fib(5), the program computes fib(4) and fib(3). To computer fib(4), it computes fib(3) and fib(2). Notice that the fib procedure calls itself twice at the last line. Two observations can be obtained from the definition and the program:

  1. The ith Fibonacci number Fib(i) is equal to Φi/√5 rounded to the nearest integer, which indicates that Fibonacci numbers grow exponentially.
  2. This is a bad way to compute Fibonacci numbers because it does redundant computation. Computing the running time of this procedure is beyond the scope of this article, but one can easily find that in books of algorithms, which is O(Φn). Thus, the program takes an amount of time that grows exponentially with the input.

On the other hand, we can also write the program in an iterative way for computing the Fibonacci numbers. Program 4 is a linear iteration. The difference in time required by Program 3 and 4 is enormous, even for small inputs.

Program 4:

int fib (int n) {
    int fib = 0;
    int a = 1;
    for(int i=0; i<n; i++) {
       fib = fib + a;
       a = fib;
    }
    return fib;
}

However, one should not think tree-recursive programs are useless. When we consider programs that operate on hierarchically data structures rather than numbers, tree-recursion is a natural and powerful tool. It can help us understand and design programs. Compared with Program 3 and 4, we can easily tell Program 3 is more straightforward, even if less efficient. After that, we can most likely reformulate the program into an iterative way.

Reference:
1. Cornell cs211 lecture

Category >> Algorithms  
  • Sukesh

    Program 3 is wrong, it’s computing f(n-2) and f(n-1) twice!

  • congw

    program 4 is wrong. it needs to return 0, 1 when n is 0, 1

  • Java God

    Program 2 has one error. The for loop should be like this:

    for(int i=2; i<n+1; i++)

    NOT

    for(int i=2; i<n; i++)

  • Someguy

    Avoid recursion. Two simple reasons:

    1) Call the function enough times and you blow the stack. This can’t happen to you with iteration. You don’t know at compile time how often the recursive method will be entered. This is dangerous.

    2) If you need to check whether the result is getting too large (e.g. in the standard factorial example) then recursion (e.g. fac(500) ) will cause you to go through all recursions and then back up until you notice the numbers get too large. Iterations will throw you that error much more efficiently.

  • Hongtai

    The Program 4 is wrong, there should be one temp value to store the last execution result,

  • http://www.itoctopus.com/ itoctopus

    I love recursion… Thanks for this excellent post!

    The ability to write recursive functions and to think recursively is an intelligence trait. Additionally, recursion can make things much, much simpler if you know how to do it.
    On the other hand, recursive functions can be extremely costly especially when it comes to memory. That was the first thing that we learned back in college.

  • gusto2

    Very nice :) As a comment – I am not aware it is not a goal if this article, but there are implication when using one or another way (recursive or iterative) and they differ depending on what programming language you use. E.g. using the recursion you can run out of the stack space, unless you use tail recursion (see scala tail recursion), etc. Would be interesting to tackle this point of view too some time :)