For example, Look at the code that calculates the n-th Fibonacci number:
fib(int n) { if(n==0 || n==1) return 1; return fib(n-1) + fib(n-2); }
The problem with this code is that it will generate stack overflow error for any number greater than 15 (in most computers).
Assume that we are calculating fib(10). In this process, say fib(5) is calculated a lot of times. Is there some way to store this in memory for fast retrieval and thereby increase the speed of recursion?
I am looking for a generic technique that can be used in almost all problems.
Yes your insight is correct. This is called dynamic programming. It is usually a common memory runtime trade-off.
In the case of fibo, you don’t even need to cache everything :
[edit] The author of the question seems to be looking for a general method to cache rather than a method to compute Fibonacci. Search wikipedia or look at the code of the other poster to get this answer. Those answers are linear in time and memory.
**Here is a linear-time algorithm O(n), constant in memory **
This perform in linear time. But log is actually possible !!! Roo’s program is linear too, but way slower, and use memory.
Here is the log algorithm O(log(n))
Now for the log-time algorithm (way way way faster), here is a method : If you know u(n), u(n-1), computing u(n+1), u(n) can be done by applying a matrix:
So that you have :
Computing the exponential of the matrix has a logarithmic complexity. Just implement recursively the idea :
You can also just diagonalize it (not to difficult), you will find the gold number and its conjugate in its eigenvalue, and the result will give you an EXACT mathematical formula for u(n). It contains powers of those eigenvalues, so that the complexity will still be logarithmic.
Fibo is often taken as an example to illustrate Dynamic Programming, but as you see, it is not really pertinent.
@John: I don’t think it has anything to do with do with hash.
@John2: A map is a bit general don’t you think? For Fibonacci case, all the keys are contiguous so that a vector is appropriate, once again there are much faster ways to compute fibo sequence, see my code sample over there.