## Thursday, April 27, 2006

### Recursion Part 4: Tree Recursion and Dynamic Programming

The following is a part of a series of articles I had written on recursion.

This part introduces tree recursion, a form of recursion that occurs when a function calls itself more than once in a path. We then introduce dynamic programming, a class of algorithms to tackle certain types of tree recursive problems.

Some recursive functions call themselves more than once. Consider the function to calculate the nth term of a fibonacci series:

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

In order to calculate fibonacci(4), we need to calculate fibonacci(2) and fibonacci(3). For each call, there are two more recursive calls. This proceeds until n becomes 0 or 1, which are the terminating points of the recursion. We see that execution follows a tree form like this:

`fibonacci(4)   +------------------------------   |                             |fibonacci(2)                 fibonacci(3)   +-----------------            +---------------   |                |            |              |fibonacci(0)   fibonacci(1)  fibonacci(1)  fibonacci(2)                                                +-----------                                                |          |                                           fibonacci(0) fibonacci(1)`

Each node has two children, one for each recursive call. A function with 'n' recursive calls will have 'n' children at each node. Since program execution follows a tree like structure, we call this form of recursion tree recursion

Is there any way to convert tree recursion to iteration?

The short answer is no1. There is no general algorithm to convert tree recursion to iteration. However, specialised algorithms do exists for certain problems. These algorithms (if they exist) are usually specific to each problem.

And it just so happens that our fibonacci algorithm falls into a class of tree recursive problems that can be converted to iteration using a technique called dynamic programming!

Look at the execution pattern for fibonacci(4) again. Notice how fibonacci(2) is calculated twice: Once in the computation of fibonacci(4) and once again in the computation of fibonacci(3). fibonacci(1) is computed thrice!

What if we could save the values of previous fibonacci terms when they are first computed and reuse the values later on without having to compute them again? This optimisation is the cornerstone of dynamic programming.

Here is how we can proceed:
1. Store the initial values of fibonacci(0) and fibonacci(1)
2. Compute fibonacci(2) from fibonacci(0) and fibonacci(1). The saved value of fibonacci(0) is now no longer required, so we can discard it.
2a. In general, compute fibonacci(n + 1) from the saved values of fibonacci(n - 1) and fibonacci(n). Discard the value of fibonacci(n - 1).
4. Repeat step 2a until we have computed the desired term

Look at the execution tree again. Notice how we start the computation from the leaf nodes of the tree and work our way up the tree computing each node, until we reach the root, which is the value we desire.

Here is the implementation using iteration:

`int fibonacci(int n){    int a = 0, b = 1, temp = 0;    int i = 0;    for (i=1; i<=n; i++) {        temp = a + b;        a = b;        b = temp;    }    return a;}`

We have just used dynamic programming to convert a tree recursive algorithm into an iterative algorithm. The basic idea of dynamic programming is to look at the execution tree, identify repeated computations and perform the calculations 'bottom-up' from leaf to root. At each step, the computed values of the subproblems are stored to be reused later.

Dynamic programming cannot be used on every problem. Some problems can be converted to iteration using other methods, and some problems cannot be converted to iteration at all. Nevertheless, dynamic programming works on a large class of problems, and it is a useful tool to have in the toolbox

1 You can write an iterative loop that manages it's own stack, but that is basically equivalent to recursion.

This post is a part of the selected archive.

Ravi said...

Excellent (series of ) posts if I may say so.
:-)

Siddhi said...

Thanks :) Anonymous said...

Hello!
Thank you for this post, I was Googeing a lot for this!

Your 2nd solution is dynamic programming isn't it?

-thetrojan01