Is there a faster way to compute the nth Fibonacci number than by fib2 (page 13) One | StudySoup

Textbook Solutions for Algorithms

Chapter 0 Problem 0.4

Question

Is there a faster way to compute the nth Fibonacci number than by fib2 (page 4)? One idea involves matrices.

We start by writing the equations \(F_{1}=F_{1}\) and \(F_{2}=F_{0}+F_{1}\) in matrix notation:

\(\begin{array}{l}\left(\begin{array}{l}F_{1} \\ F_{2}\end{array}\right)=\left(\begin{array}{ll}0 & 1 \\ 1 & 1\end{array}\right) \cdot\left(\begin{array}{l}F_{0} \\ F_{1}\end{array}\right) .\\ \text{Similarly,}\\ \left(\begin{array}{l}F_{2} \\ F_{3}\end{array}\right)=\left(\begin{array}{ll}0 & 1 \\ 1 & 1\end{array}\right) \cdot\left(\begin{array}{l}F_{1} \\ F_{2}\end{array}\right)=\left(\begin{array}{ll}0 & 1 \\ 1 & 1\end{array}\right)^{2} \cdot\left(\begin{array}{l}F_{0} \\ F_{1}\end{array}\right)\\ \text{and in general}\\ \left(\begin{array}{c}F_{n} \\ F_{n+1}\end{array}\right)=\left(\begin{array}{ll}0 & 1 \\ 1 & 1\end{array}\right)^{n} \cdot\left(\begin{array}{l}F_{0} \\ F_{1}\end{array}\right) .\end{array}\)

So, in order to compute \(F_{n}\), it suffices to raise this \(2 \times 2\) matrix, call it \(X\), to the nth power.

(a) Show that two \(2 \times 2\) matrices can be multiplied using 4 additions and 8 multiplications. But how many matrix multiplications does it take to compute \(X^{n}\)?

(b) Show that \(O(\log n)\) matrix multiplications suffice for computing \(X^{n}\).

(Hint: Think about computing \(X^{8}\).)

Thus the number of arithmetic operations needed by our matrix-based algorithm, call it fib3, is just \(O(\log n)\), as compared to \(O(n)\) for fib2. Have we broken another exponential barrier?

The catch is that our new algorithm involves multiplication, not just addition; and multiplication of large numbers are slower than additions. We have already seen that, when the complexity of arithmetic operations is taken into account, the running time of fib2 becomes \(O\left(n^{2}\right)\).

(c) Show that all intermediate results of fib3 are \(O(n)\) bits long.

(d) Let \(M(n)\) be the running time of an algorithm for multiplying n-bit numbers, and assume that \(M(n)=O\left(n^{2}\right)\) (the school method for multiplication, recalled in Chapter 1, achieves this). Prove that the running time of fib3 is \(O(M(n) \log n)\).

(e) Can you prove that the running time of fib3 is \(O(M(n))\)? Assume \(M(n)=\Theta\left(n^{a}\right)\) for some \(1 \leq a \leq 2\) (Hint: The lengths of the numbers being multiplied get doubled with every squaring.)

In conclusion, whether fib3 is faster than fib2 depends on whether we can multiply n-bit integers faster than \(O\left(n^{2}\right)\). Do you think this is possible? (The answer is in Chapter 2.)

Finally, there is a formula for the Fibonacci numbers:

\(F_{n}=\frac{1}{\sqrt{5}}\left(\frac{1+\sqrt{5}}{2}\right)^{n}-\frac{1}{\sqrt{5}}\left(\frac{1-\sqrt{5}}{2}\right)^{n}\) .

So, it would appear that we only need to raise a couple of numbers to the nth power in order to compute \(F_{n}\). The problem is that these numbers are irrational, and computing them to sufficient accuracy is nontrivial. In fact, our matrix method fib3 can be seen as a roundabout way of raising these irrational numbers to the nth power. If you know your linear algebra, you should see why. (Hint: What are the eigenvalues of the matrix \(X\)?)

Solution

Step 1 of 5

(a). Consider two \(2 \times 2\) matrices A,B where,

\(A=\left(\begin{array}{ll} a & b \\ c & d \end{array}\right) \text { and } B=\left(\begin{array}{ll} e & f \\ g & h \end{array}\right)\)

Then the formula to multiply two \(2 \times 2\) matrices is:

\(\left(\begin{array}{ll} a & b \\ c & d \end{array}\right)\left(\begin{array}{ll} e & f \\ g & h \end{array}\right)=\left(\begin{array}{ll} a e+b g & a f+b h \\ c e+d g & c f+d h \end{array}\right)\)

Thus we have 4 additions and 8 multiplications for multiplying two \(2 \times 2\) matrices. Hence proved.

Computing \(X^{n}\) requires \(\log n\) matrix multiplications. Since each matrix multiplication has at most 4 additions and 8 multiplications(as given above) this becomes \(4(\log n)+8(\log n)=12(\log n)=O(\log n)\), logarithmic growth.

Subscribe to view the
full solution

Title Algorithms  1 
Author Sanjoy Dasgupta Algorithms, Christos H. Papadimitriou Algorithms, Umesh Vazirani Algorithms
ISBN 9780073523408

Is there a faster way to compute the nth Fibonacci number than by fib2 (page 13) One

Chapter 0 textbook questions

×

Login

Organize all study tools for free

Or continue with
×

Register

Sign up for access to all content on our site!

Or continue with

Or login if you already have an account

×

Reset password

If you have an active account we’ll send you an e-mail for password recovery

Or login if you have your password back