23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Unfortunately, <strong>in</strong> spite of the Fibonacci def<strong>in</strong>ition look<strong>in</strong>g like a b<strong>in</strong>ary recursion,<br />

us<strong>in</strong>g this technique is <strong>in</strong>efficient <strong>in</strong> this case. In fact, it takes an exponential<br />

number of calls to compute the kth Fibonacci number <strong>in</strong> this way. Specifically, let<br />

n k denote the number of calls performed <strong>in</strong> the execution of B<strong>in</strong>aryFib(k).<br />

Then, we have the follow<strong>in</strong>g values for the n k 's:<br />

n 0 = 1<br />

n 1 = 1<br />

n 2 = n 1 + n 0 + 1 = 1 + 1 + 1 = 3<br />

n 3 = n 2 + n 1 + 1 = 3 + 1 + 1 = 5<br />

n 4 = n 3 + n2 + 1 = 5 + 3 + 1 = 9<br />

n 5 = n 4 + n 3 + 1 = 9 + 5 + 1 = 15<br />

n 6 = n 5 + n 4 + 1 = 15 + 9 + 1 = 25<br />

n 7 = n 6 + n 5 + 1 = 25 + 15 + 1 = 41<br />

n 8 = n 7 + n 6 + 1 = 41 + 25 + 1 = 67.<br />

If we follow the pattern forward, we see that the number of calls more than<br />

doubles for each two consecutive <strong>in</strong>dices. That is, n 4 is more than twice n 2 n 5 is<br />

more than twice n 3 , n 6 is more than twice n 4 , <strong>and</strong> so on. Thus, n k > 2 k/2 , which<br />

means that B<strong>in</strong>aryFib(k) makes a number of calls that are exponential <strong>in</strong> k. In<br />

other words, us<strong>in</strong>g b<strong>in</strong>ary recursion to compute Fibonacci numbers is very<br />

<strong>in</strong>efficient.<br />

Comput<strong>in</strong>g Fibonacci Numbers via L<strong>in</strong>ear Recursion<br />

The ma<strong>in</strong> problem with the approach above, based on b<strong>in</strong>ary recursion, is that the<br />

computation of Fibonacci numbers is really a l<strong>in</strong>early recursive problem. It is not<br />

a good c<strong>and</strong>idate for us<strong>in</strong>g b<strong>in</strong>ary recursion. We simply got tempted <strong>in</strong>to us<strong>in</strong>g<br />

b<strong>in</strong>ary recursion because of the way the kth Fibonacci number, F k , depends on the<br />

two previous values, F k−1 <strong>and</strong> F k−2 . But we can compute F k much more efficiently<br />

us<strong>in</strong>g l<strong>in</strong>ear recursion.<br />

In order to use l<strong>in</strong>ear recursion, however, we need to slightly redef<strong>in</strong>e the<br />

problem. One way to accomplish this conversion is to def<strong>in</strong>e a recursive function<br />

that computes a pair of consecutive Fibonacci numbers (F k ,F k −1) us<strong>in</strong>g the<br />

convention F−1 = 0. Then we can use the l<strong>in</strong>early recursive algorithm shown <strong>in</strong><br />

Code Fragment 3.36.<br />

201

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

Saved successfully!

Ooh no, something went wrong!