Computing the nth Fibonacci number using recursion (fib(n) = fib(n-1)+fib(n-2)) takes time at least 2n/2. We improve this by noting that this computation is highly redundant. For example, fib(3) is recomputed many times. To see this, look at the recursion tree for, say, fib(8):
Using the "show_call_graph" utility from /Prog1, applied to call graphs generated by running the fib() routine given to you there.
To improve this, we can use dynamic programming. This is implemented one of two ways:
For details, see S04_CS141:FibonacciByDP .
We also considered counting paths: S04_CS141:CountingPathsByDP, and did a class exercise about counting the number of subsets of {1,2,..,n} that have size k: S04_CS141:NChooseKByDP .
For more examples: S04_CS141:DynamicProgramming