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

Question: 0.1. In each of the following situations, indicate whether f=O(g),orf=Ω(g),or both (in which case f=(g))

Here’s 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.

The tramp steamer problem. You are the owner of a steamship that can apply between a group of port cities V . You make money at each port: a visit to city i earns you a profit of pi dollars. Meanwhile, the transportation cost from port i to port j is cij>0 .You want to find a cyclic route in which the ratio of profit to cost is maximized.

To this end, consider a directed graph G=(V,E) whose nodes are ports, and which has edges between each pair of ports. For any cycle C in this graph, the profit-to-cost ratio is

role="math" localid="1658920675878" r(c)=i,jicPiji,jicCij

Let r' be the maximum ratio achievable by a simple cycle. One way to determine r' is by binary search: by first guessing some ratio r , and then testing whether it is too large or too small. Consider any positive r>0 . Give each edge (i,j) a weight of wij=rcij-pj .

  1. Show that if there is a cycle of negative weight, then .
  2. Show that if all cycles in the graph have strictly positive weight, then r<r*.
  3. Give an efficient algorithm that takes as input a desired accuracy >0 and returns a simple cycle c for which r(C)3r*- Justify the correctness of your algorithm and analyze its running time in terms of |V|, and R=max(i,j)iE(PJCIJ) .

Consider the following game. A “dealer” produces a sequence s1···sn of “cards,” face up, where each card si has a value vi. Then two players take turns picking a card from the sequence, but can only pick the first or the last card of the (remaining) sequence. The goal is to collect cards of largest total value. (For example, you can think of the cards as bills of different denominations.) Assume n is even. (a) Show a sequence of cards such that it is not optimal for the first player to start by picking up the available card of larger value. That is, the natural greedy strategy is suboptimal. (b) Give an O(n2) algorithm to compute an optimal strategy for the first player. Given the initial sequence, your algorithm should precompute in O(n2) time some information, and then the first player should be able to make each move optimally in O(1) time by looking up the precomputed information.

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

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