The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998

18.04.2013 Views

Fibonacci numbers Figure: The computation tree for computing Fibonacci numbers recursively How much time does this algorithm take to compute Fibonacci[n]? Since , this means that . Since our recursion tree, illustrated in Figure , has only 0 and 1 as leaves, summing up to such a large number means we must have at least leaves or procedure calls! This humble little program takes exponential time to run! In fact, we can do much better. We can calculate in linear time by storing all values. We trade space for time in the algorithm below: Fibonacci[n] For i=1 to n, Because we evaluate the Fibonacci numbers from smallest to biggest and store all the results, we know that we have and ready whenever we need to compute . Thus each of the n values is computed as the simple sum of two integers in total O(n) time, which is quite an improvement over exponential time. file:///E|/BOOK/BOOK2/NODE44.HTM (2 of 3) [19/1/2003 1:28:42]

Fibonacci numbers Next: The Partition Problem Up: Dynamic Programming Previous: Dynamic Programming Algorithms Mon Jun 2 23:33:50 EDT 1997 file:///E|/BOOK/BOOK2/NODE44.HTM (3 of 3) [19/1/2003 1:28:42]

Fibonacci numbers<br />

Next: <strong>The</strong> Partition Problem Up: Dynamic Programming Previous: Dynamic Programming<br />

<strong>Algorithm</strong>s<br />

Mon Jun 2 23:33:50 EDT 1997<br />

file:///E|/BOOK/BOOK2/NODE44.HTM (3 of 3) [19/1/2003 1:28:42]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!