0% found this document useful (0 votes)
26 views24 pages

Lecture Chapter 2 Part 3

Uploaded by

De Shad Bostic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views24 pages

Lecture Chapter 2 Part 3

Uploaded by

De Shad Bostic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Lecture Chapter 2

Section 2.5

Computing the nth Fibonacci Number


• We consider the Fibonacci numbers, a famous
sequence
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . .
• This sequence can be defined by the simple
recurrence
F(n) = F(n − 1) + F(n − 2) for n > 1
• and two initial conditions
F(0) = 0, F(1) = 1.
Historical context
• The Fibonacci numbers
– introduced by Leonardo Fibonacci in 1202 as a solution
to the rabbit population problem
• The Problem
• A man put a pair of rabbits in a place surrounded by
a wall. How many pairs of rabbits will be there in a
year if the initial pair of rabbits (male and female)
are newborn and all rabbit pairs are not fertile
during their first month of life but thereafter give
birth to one new pair (male and female) at the end
of every month?
• Many examples of Fibonacci-like numbers have
since been discovered in the natural world,
– Even used in predicting the prices of stocks and
commodities.
• Some interesting applications of the Fibonacci
numbers in computer science.
• For example, worst-case inputs for Euclid’s
algorithm earlier happen to be consecutive
elements of the Fibonacci sequence.
• Eg GCD(34,21)
• We will consider algorithms for computing the
nth element of this sequence.
• This discussion will provide us with an
opportunity to introduce another method for
solving recurrence relations useful for analysis
of recursive algorithms.
• Now, let us get an explicit formula for F(n).
• The method of backward substitutions to
solve recurrence fails to get an easily
discernible pattern.
• Hence, we make use of a theorem that describes solutions
to a homogeneous second-order linear recurrence with
constant coefficients
ax(n) + bx(n − 1) + cx(n − 2) = 0
where a, b, and c are some fixed real numbers (a = 0)
called the coefficients of the recurrence and x(n) is the
generic term of an unknown sequence to be found.
• Applying this theorem (as given in Discrete Mathematics) to
our recurrence with the initial conditions given, we get
• ax2 +bx + c = 0
• (see appendix B of text)
• This gives F(n) =  (n) which
• One of the benefits of formula is that it
immediately implies that F(n) grows
exponentially (remember Fibonacci’s
rabbits?), i.e., F(n) =  (n).
• In the algorithms that follow (next slide), we consider,
for the sake of simplicity, such operations as additions
and multiplications at unit cost.
• Since the Fibonacci numbers grow infinitely large (and
grow very rapidly), a more detailed analysis than the
one given so far is necessary.
• In fact, it is the size of the numbers rather than a
time-efficient method for computing them is of
primary concern here.
• To begin, we can use recurrence F(n) = F(n − 1) + F(n −
2) for n > 1 and two initial conditions
F(0) = 0, F(1) = 1.
Algorithm Fibonacci
• We begin a formal analysis.
• The algorithm’s basic operation is clearly addition,
• So let A(n) be the number of additions performed
by the algorithm in computing F(n).
• Then the numbers of additions needed for
computing F(n − 1) and F(n − 2) are A(n − 1) and
A(n − 2), respectively, and the algorithm needs
one more addition to compute their sum.
Thus, we get the following recurrence for
A(n):
• A(n) = A(n − 1) + A(n − 2) + 1 for n > 1,
A(0) = 0, A(1) = 0.
• The recurrence A(n) - A(n − 1) − A(n − 2) = 1 is quite similar to
recurrence
F(n) − F(n − 1) − F(n − 2) = 0, but its right-hand side is not
equal to zero.
• Such recurrences are called inhomogeneous.
• There are general techniques for solving inhomogeneous
recurrences (see Appendix B or any textbook on discrete
mathematics),
• For this particular recurrence, a special trick leads to a faster
solution.
• We can reduce our inhomogeneous
recurrence to a homogeneous one by
rewriting it as
• [A(n) + 1]− [A(n − 1) + 1]− [A(n − 2) + 1]= 0
and substituting B(n) = A(n) + 1:
• B(n) − B(n − 1) − B(n − 2) = 0,
• B(0) = 1, B(1) = 1.
• This homogeneous recurrence can be solved
exactly
• We note that B(n) is, in fact, the same
recurrence as F(n) except that it starts with
two 1’s and thus runs one step ahead of F(n).
• So B(n) = F(n + 1), and
• The poor efficiency class of the algorithm could be
anticipated by the nature of recurrence
• It contains two recursive calls with the sizes of
smaller instances only slightly smaller than size n.
• We can also see the reason behind the algorithm’s
inefficiency by looking at a recursive tree of calls
tracing the algorithm’s execution.
• An example of such a tree for n = 5 is given in the
next slide
• Note that the same values of the function are being
evaluated here again and again, which is clearly
extremely inefficient.
• We can obtain a much faster algorithm by
simply computing the successive elements of
the Fibonacci sequence iteratively, as is done
in the following algorithm (next slide).
• Recall: F(n) = F(n − 1) + F(n − 2) for n > 1
and F(0) = 0, F(1) = 1.
• This algorithm clearly makes n − 1 additions.
• Hence, it is linear as a function of n and “only”
exponential as a function of the number of bits
b in n’s binary representation.
• Note that using an extra array for storing all
the preceding elements of the Fibonacci
sequence can be avoided: storing just two
values is necessary to accomplish the task
• The third alternative for computing the nth
Fibonacci number lies in using formula.


• The efficiency of the algorithm will obviously
be determined by the efficiency of an
exponentiation algorithm used for computing
φn.
• If it is done by simply multiplying φ by itself
n − 1 times
• There are faster algorithms for the exponentiation
problem.
For example, using divide and conquer and transform and
conquer
• Note also that special care should be exercised in
implementing this approach to computing the nth
Fibonacci number.
• Since all its intermediate results are irrational numbers,
we would have to make sure that their approximations
in the computer are accurate enough so that the final
round-off yields a correct result.
• Read Chapter 2.5

You might also like