Note: this is not working on IE. Please use Firefox while
I work out ignore the problems.
Take for instance the fibonacci series, defined by taking "old" values and creating from them "new" values. If the values are denoted f0, f1, ..., then we set,
and for the rest, A sort of introduction to dynamic programming can be given by considering the calculation of the fibonacci series, or any recurrence equation, that is, where we have a formula, and initial values for f0 through fk-1Two methods immediately come to mind for this calculation. One is to keep a table f where f[i] gets filled in with value fi. Very clever! This is called dynamic programming. The other method is to keep just the portion of the table that will be needed in the future, either discarding or printing out values of fi outside (smaller i) this window. This second manner is the typical calculation of the i-th fibonacci number, as it seems wasteful (of storage, not time!) to keep around the j-th fibonacci number for all j less than i - only the previous two values are needed for future calculations, and in certain instances, we have not other future use of the other values. However, we will stay with the filling out the table variant.
The other method is straight recursion. This method will be very slow. Dynamic programming is involved in going from the slow recursive calculation to the fast table-based calculation.
Why is the recursive version slow? Because evaluting F(i) will call F(i-2) twice. Since this is true, F(i-4) will be called at least four times (actually, it will be called 5 times), and F(i-6) at least 8 times (actually it will be called 13 times). If we look at F(1), it will be called an exponential number of times in the value i.
The table version avoids this by,
In our fibonacci series problem, it is obvious how to arrange the sub-problems. Order the collection of sub-problems that need to be solved in order to solve fib(i) in order of increasing i. Solve them in this order, so that when fib(i) needs the value of fib(i-2), it is already solved, and likewise for fib(i-1).
You can also let the computer figure out automatically the order of solutions, in some cases, by a process called memoization. For memoization, you fill in the table on-demand. First the table must be filled in with "unsolved" marks. For fibonacci, initialize the table to zeros. Then when the value of fib(i) needs to be consulted, look first in the table. If the table location has a value, it is the value of fib(i) and it should be used. If not, calculate fib(i) and place the result in the table for future reference.
Note that the number of calls for the recursive version is almost exactly twice the output value of the fibonacci function. The number of calls for memoize is twice the value of the input to the fibonacci function.
Note that the size of problem that can be solved as been increasing even though I have not changed my machine or browser. It seems that Javascript has been getting very much faster.