CS 3343/3341
  Introduction to Algorithms  
Weird Topic
  Fibonacci Matrices, Part 1  


Definition of Fibonacci Numbers: The Fibonacci Numbers consist of the sequence of integers: F0 = 0, F1 = 1, F2 = 1, F3 = 2, F4 = 3, F5 = 5, F6 = 8, F7 = 13, F8 = 21, F9 = 34, F10 = 55, ... . These are defined recursively by the following formula:

These numbers are also entries of successive powers of the matrix shown below, which can be proved by induction:

For a program that shows powers and successive squares of this matrix, see Fibonacci Matrix Programs.


Performance of Programs that Calculate Fibonacci Numbers: Here are three approachesp to calculate Fn (see details in separate sections below):

  1. Recursive, with performance O(φn), where φ = 1.61803....

  2. Straightforward, using a loop, with performance O(n).

  3. Fibonacci matrix approach, with performance O(log n).


1. Recursive Program: The first function definition we used to calculate Fibonacci Numbers was the recursive one, based on the definition above:

int f(int n) {
   if (n <= 1) return n;
   return f(n-1) + f(n-2);
}

A program that uses this definition is at Cascade of Recursive Calls. Denote by An the total number of function calls needed to calculate Fn. (These are called "Leonardo numbers".) Then the previous program gives values to An:

n01234 5678910...
An11359 15254167109177...

It's pretty easy to guess from the numbers that An can be defined by the recurrence:

A0 = 1
A1 = 1
An = An-1 + An-2 + 1

It's also easy to see that this recurrence is correct. Picture the tree of recursive calls for Fn. The two children from the root are Fn-1 and Fn-2, and these each have An-1 and An-2 calls. Then add one more call for the root.

It might be hard to solve the recurrence, but if one guesses that

An = 2 * Fn+1 -1

then it's easy to prove this by induction.

Now all we have to do is see how big Fn is as a function of n. Your text on pages 108-109 gives a long exercise to lead a student through a standard argument (discovered in 1730) that uses a power series as a generating function to give an exact formula for Fn (see your book for this), which implies in particular that Fn gets arbitrarily close to φn/√5 for large enough n, where φ is the "golden ratio", φ = (1+√5)/2 = 1.61803.... (The value is a good approximation, as you can see from the table below.) This gives the main result of this subsection:

Recursive calculation of Fn
is an exponential algorithm,
with performance:
Θ(φn)

At the end of this page is a tableof powers of φ. These powers divided by √5 give Fibonacci numbers.


2. Standard Θ(n) Program: The following simple program computes f(n) with n-2 additions (and some assignments, which we're not counting). Thus it is obviously Θ(n).

int f(int n) {
   int f0 = 0, f1 = 1, f2 = -1;
   for (int i = 1; i < n; i++) {
      f2 = f0 + f1;
      f0 = f1;
      f1 = f2;
   }
   return f2;
}

Standard (looping) calculation
of Fn is a linear algorithm,
with performance:
Θ(n)


3. Fibonacci Matrix Approach: Use the 2-by-2 matrix at the start of this page. We have a faster way to raise anything to an integer power -- the earlier algorithm for integers to an integer power also works for 2-by-2 matrices to an integer power. This case requires at most 2*log2(n) matrix multiplications.

Fibonacci Matrix calculation
of Fn is an unexpected algorithm,
with performance:
Θ(log n)


Table of Powers of φ, the Golden Ratio Below, a real number enclosed in curly brackets ( {} ) means to take the integer closest to the number. The integers n} are called "Lucus numbers" and are denoted by Ln. Dividing the powers of φ by √5 and taking the nearest integer gives the Fibonacci numbers.

nφnn}
= Ln
φn/√5 n/√5}
= Fn
01.0 (1) 0.44721359549995790
11.618033988749895(2) 0.72360679774997891
22.6180339887498953 1.17082039324993681
34.23606797749979 4 1.89442719099991572
46.8541019662496867 3.065247584249853 3
511.09016994374947611 4.959674775249769 5
617.94427190999916318 8.024922359499623 8
729.0344418537486429 12.984597134749393 13
846.97871376374780547 21.00951949424901621
9 76.0131556174964576 33.99411662899841 34
10122.99186938124426123 55.00363612324743 55
151364.00073313743661364 609.9996721309716 610
2015126.999933893048151276765.000029563936 6765
25167761.00000596116776175024.99999733428 75025
301860497.99999946451860498832040.0000002412832040
3520633239.000000075206332399227464.9999999899227465
40228826127.0000003228826127102334155.00000013102334155


Revision date: 2012-09-17. (Please use ISO 8601, the International Standard Date and Time Notation.)