18.04.2013 Views

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

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

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

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Fibonacci numbers<br />

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

Fibonacci numbers<br />

<strong>The</strong> tradeoff between space and time exploited in dynamic programming is best illustrated in evaluating<br />

recurrence relations, such as the Fibonacci numbers. <strong>The</strong> Fibonacci numbers were originally defined by<br />

the Italian mathematician Fibonacci in the thirteenth century to model the growth of rabbit populations.<br />

Rabbits breed, well, like rabbits. Fibonacci surmised that the number of pairs of rabbits born in a given<br />

year is equal to the number of pairs of rabbits born in each of the two previous years, if you start with<br />

one pair of rabbits in the first year. To count the number of rabbits born in the nth year, he defined the<br />

following recurrence relation:<br />

with basis cases and . Thus , , and the series continues<br />

. As it turns out, Fibonacci's formula didn't do a very good job of counting<br />

rabbits, but it does have a host of other applications and interesting properties.<br />

Since they are defined by a recursive formula, it is easy to write a recursive program to compute the nth<br />

Fibonacci number. Most students have to do this in one of their first programming courses. Indeed, I<br />

have particularly fond memories of pulling my hair out writing such a program in 8080 assembly<br />

language. In pseudocode, the recursive algorithm looks like this:<br />

Fibonacci[n]<br />

if (n=0) then return(0)<br />

else if (n=1) then return(1)<br />

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

else return(Fibonacci[n-1]+Fibonacci[n-2])

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

Saved successfully!

Ooh no, something went wrong!