/* Dynamic Programming: Simple Examples Consider the naive fibonacci function: int fib(int n) { if (n<2) return 1; else return fib(n-1)+fib(n-2); } The time complexity of this function is T(n) = T(n-1) + T(n-2) + C, which is in fact bounded by: T(n) = T(n-1) + T(n-1) +C, which we know gives us O(2^n) time. Calling fib(100) will take something on the order of a million years to complete. But now consider a way to optimize this algorithm, without changing its mathematical elegance (that is, without changing the way recursion is used). The idea is that we use a background array to memorize the result of computing fib numbers, so that redundant computation is avoided. For example, the first time we call fib(5), we have to also call fib(4) and fib(3). But then we store the result of fib(5) in the array, so that the next time it's needed (such as when fib(6) is called), we can return it right away. */ using namespace std; #include /* define the max size of array */ #define SIZE 1000 class dynamicfib { public: int M[SIZE]; dynamicfib() // constructor initializes matrix { for (int i=0;i