/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Q4E Is there a faster way to compute... [FREE SOLUTION] | 91影视

91影视

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 22 matrix, call it X, to the nth power.

a. Show that two 22matrices 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 22matrices 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 鈥渇ib3鈥 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 91影视!

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

How long does the recursive multiplication algorithm (page 25) take to multiply an n -bit number by an m -bit number? Justify your answer.

Here鈥檚 a problem that occurs in automatic program analysis. For a set of variablesx1,......,xn, you are given some equality constraints, of the form 鈥 xi=xj鈥 and some disequality constraints, of the form 鈥 xixj.鈥 Is it possible to satisfy all of them?

For instance, the constraints.

x1=x2,x2=x3,x3=x4,x1x4

cannot be satisfied. Give an efficient algorithm that takes as input m constraints over n variables and decides whether the constraints can be satisfied.

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}.

Question: 0.1. In each of the following situations, indicate whether 蹿=翱(驳),辞谤蹿=惟(驳),or both (in which case f=(g))

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鈥檒l call1

鈥 The mean, which we鈥檒l call2

(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).

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.