Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

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 F1=F1 and F2=F0+F1 in matrix notation:


role="math" localid="1659767046297" (F1F2)=(0111).(F0F1).

Similarly,

F2F3=(0111).(F1F2)=(0111)2.(F0F1)

And in general

(FnFn+1)=(0111)n.(F0F1)

So, in order to compute Fn, it suffices to raise this 2×2 matrix, call it X, to the nth power.

a. Show that two 2×2matrices can be multiplied using 4additions and 8multiplications.

But how many matrix multiplications does it take to compute Xn?

b. Show that O(logn) matrix multiplications suffice for computing Xn. (Hint: Think about computing X8.)

Thus, the number of arithmetic operations needed by our matrix-based algorithm, call it fib3, is just O(logn), 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 multiplications 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 offib2becomes O(n).

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(n2) (the school method for multiplication, recalled in Chapter 1, achieves this). Prove that the running time of fib3 is O(M(n)logn).


e. Can you prove that the running time of fib3 is O(M(n))? Assume M(n)=Θ(na)for some 1a2. (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 thanO(n2) . Do you think this is possible? (The answer is in Chapter 2.) Finally, there is a formula for the Fibonacci numbers:

role="math" localid="1659768125292" Fn=15(1+52)n15(152)n.

So, it would appear that we only need to raise a couple of numbers to the nth power in order to computeFn . 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?)

Short Answer

Expert verified
  1. Number of multiplications is required to compute “Xn
  2. The “logn” are squares and most of “logn” are multiplications by “X”.
  3. The multiplication operation does not change the size of intermediate result.
  4. It shows that the algorithm runs in the timeO(M(n)logn).
  5. Proved the running time of the algorithm.

Step by step solution

01

Required Number of multiplications.

a).

Consider the two 2×2matrices as “X” and “Y”.

The multiplication of two matrices “ X” and “Y” can be written as follows:

XY=x11x12x21x22y11y12y21y22

When multiplying the above two matrices, the following will be resulted:

XY=x11y11+x12y21x11y12+x12y22x21y11+x22y21x21y12+x22y22

In the above matrix, the multiplication of two matrix matrices has been performed by using 4additions and 8 multiplications.

Hence, it is proved.

Therefore, Number of multiplications is required to compute “ Xn”:

02

Solution of part (b)

b).

Initially, consider the instance where “n=2k” for some positive integers “k”.

For “Xn” can be written as “X2k”.

To compute X2k, recursively compute “Y=X2k+1”.

After the recursive computation, square the “Y” value.

Y2=X2k

The repeated square of “X” can be written as “ X2,X4,...,X2k=Xn”.

Double the exponent of “X” for each square of “X”.

To produce “Xn”, it takes k=logn matrix multiplications.

The above method can be written as the following recursion:

Xn=(X(n/2))2X×(X(n/2))2ifnisevenifnisodd

This algorithm involves “O(logn) ” matrix multiplications.

The “ logn” are squares and most of “ logn” are multiplications by “ X”.

03

Solution of part (c)

c).

In the matrix, the entries are the addition of the products of entries in the given matrix.

While performing addition operation, the number of bits in the entries are unchanged (at maximum one bit).

When performing multiplication operation, number of bits of the operands are added.

Thus, whenever square the matrix “X”, the number of bits of its entries are doubled.

When referring the part (b), the matrix has been squared for “Xlogn” times.

Thus, all intermediate results should be of length less than equal to “2logn=O(n)”.

The multiplication operation does not change the size of intermediate result.

04

Solution of part (d)

d).

Note: Assume that “ M(n)=nc” for some “ c1”.

In this algorithm, “O(logn)” matrix multiplications are performed; each multiplication performed in the matrix consists a constant number of arithmetic operations.

Each arithmetic operation performed in this algorithm contains size of O(n).

Thus it takes at most time O(M(n)); because M(n)=nc.

It shows that the algorithm runs in the timeO(M(n)logn).

05

Solution of part (e)

e).

Note: Assume that “M(n)=nc” for some “c1 ”.

Consider that the running time of the algorithm on input size of “n” is “T(n)”.

At the first level of recursion, execute the algorithm for input size “n/2”; it takes “T(n/2)” time to execute.

To get the final answer, square the results:

The last operation takes time “M(n/2)” and the result of the operation has bit size “O(n/2)”.

This result, “ T(n)=T(n/2)+M(n/2)”; while expanding the recursion, and applying geometric series results the following:

T(n)=Mn2+Mn4+....+M(1)n2c+n4c+....+n8c

=nci=112ic

=O(nc)

=O(M(n))

Therefore, the running time of algorithm “fib3” is “O(M(n)) ”.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Alice wants to throw a party and is deciding whom to call. She has n people to choose from, and she has made up a list of which pairs of these people know each other. She wants to pick as many people as possible, subject to two constraints: at the party, each person should have at least five other people whom they know and five other people whom they don’t know. Give an efficient algorithm that takes as input the list of n people and the list of pairs who know each other and outputs the best choice of party invitees. Give the running time in terms of n

The Fibonacci numbers F0,F1,F2,... are defined by the rule

F0=0,F1=1,Fn=Fn1+Fn2.

In this problem we will confirm that this sequence grows exponentially fast and obtain some bounds on its growth.

(a) Use induction to prove that Fn20.5nfor n6.

(b) Find a constant c<1such thatFn2cn for all n0. Show that your answer is correct.

(c) What is the largestc you can find for which Fn=Ω(2cn)?

|ψ=12|00+12|11is one of the famous “Bell states,” a highly entangled state of its two qubits. In this question we examine some of its strange properties. (a) Suppose this Bell state could be decomposed as the (tensor) product of two qubits (recall the box on page ), the first in state α0|0+α1|1and the second in stateβ0|0+β1|1. Write four equations that the amplitudes α0,α1,β0andβ1must satisfy. Conclude that the Bell state cannot be so decomposed.

(b) What is the result of measuring the first qubit of |ψ?

(c) What is the result of measuring the second qubit after measuring the first qubit? (d) If the two qubits in state|ψ are very far from each other, can you see why the answer to (c) is surprising?

Mean and median. One of the most basic tasks in statistics is to summarize a set of observations x1,x2,,xnR by a single number. Two popular choices for this summary statistic are:

• The median, which we’ll callμ1

• The mean, which we’ll callμ2

(a) Show that the median is the value of μthat minimizes the function

i|xi-μ|

You can assume for simplicity that is odd. (Hint: Show that for any , the function decreases if you move either slightly to the left or slightly to the right.)

(b) Show that the mean is the value of μ that minimizes the function

i(xi-μ)2

One way to do this is by calculus. Another method is to prove that for any μR,

i(xi-μ)2=i(xi-μ2)2+n(μ-μ2)2

Notice how the function for μ2 penalizes points that are far from much more heavily than the function for μ1 . Thus μ2 tries much harder to be close to all the observations. This might sound like a good thing at some level, but it is statistically undesirable because just a few outliers can severely throw off the estimate of μ2 . It is therefore sometimes said that μ1 is a more robust estimator than μ2 . Worse than either of them, however, is μ , the value of μthat minimizes the function

maxi|xi-μ|

(c) Show that μ can be computed in O(n) time (assuming the numbers are xismall enough that basic arithmetic operations on them take unit time).

A vertex cover of a graph G=(V,E)is a subset of vertices SVthat includes at least one endpoint of every edge in E. Give a linear-time algorithm for the following task.

Input: An undirected tree T=(V,E).

Output: The size of the smallest vertex cover of T. For instance, in the following tree, possible vertex covers include{A,B,C,D,E,F,G}and{A,C,D,F}but not{C,E,F}.The smallest vertex cover has size 3: {B,E,G}.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free