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

In Section 2.1 we described an algorithm that multiplies two n-bit binary integers x and y in time na, where a=log23. Call this procedure fast multiply (x,y).

(a) We want to convert the decimal integer 10n(a 1 followed by n zeros) into binary. Here is the algorithm (assume n is a power of 2):

function pwr2bin(n)

if n = 1: return10102

else:

z= ???

return fastmultiply(z,z)

Fill in the missing details. Then give a recurrence relation for the running time of the algorithm, and solve the recurrence.

(b) Next, we want to convert any decimal integer x with n digits (where n is a power of 2) into binary. The algorithm is the following:

function dec2bin(x)

if n=1: return binary [ x ]

else:

split x into two decimal numbers xt,xRwith n/2 digits each

return ???

Here binary [.] is a vector that contains the binary representation of all one-digit integers. That is, binary role="math" localid="1659333641173" [0]=02, binary [1]=12, up to binary [9]=10012. Assume that a lookup in binary takes 0(1) time. Fill in the missing details. Once again, give a recurrence for the running time of the algorithm, and solve it.

Short Answer

Expert verified

a).Filling the missing details of the algorithm.

b). proving the recurrence for the running time of the algorithm.

Step by step solution

01

Solution of part (a)  

Filling the missing details of the algorithm:

function pwr2bin(n)

if n=1 :

return 10102

else:

z = pwr2bin (n/2)

return fastmultiply(z,z)

The highlighted part of the algorithm is the missing part.

Description: • "pwr2bin" is a function that takes a decimal integer and transforms it to binary using the power of ten as an input argument.

• It first validates whether n is equal to 1 and then returns the binary number 10102.

• If not, it calls the same function "pwr2bin" with the argument (n/2).

• It then returns the result of the function "fastmultiply" with arguments as the result of the function "pwr2bin."

o The "fastmultiply" technique returns the product of two binary digits.

The textbook contains the algorithm. Figure 2.1 shows the "fastmultiply" algorithm's execution time. is 0nlog3.

Thus, it takes T(n/2) time to check the power of 0nkand a time of to perform the multiplication. So the recurrence relation is,

Tn=Tn/2+0(nlog3) …… (1)

Consider the master theorem for the following recurrence equation,

Tn=aTn/b+0(nc) …… (2)

By comparing equation (1) and (2), the value of “nc” is “n”, “c” is “ log23”, “b” is “2” and “a” is 1.

By master’s theorem, if the value of c>logba, then the running time is,

Tn=0nc …… (3)

Check if c>logba:

logba …… (4)

Substitute the value of “b” as 2 and “a” as 1 in equation (4).

logba=log21=0

Thus, the value of logbais less than “c” which is log23=1.58.

Substitute nkas nlog23in equation (3). The running time of algorithm is shown below:

Tn=0nlog23

Therefore, the algorithm takes a run time of 0nlog23.

02

Solution of part (b)

Filling the missing details of the algorithm:

function dec2bin(x )

if n = 1:

return binary [x]

else:

split x into two decimal numbers xL,xRwith n/2digits each

return ( fastmultiply ( pwr2bin(n/2), dec2bin(localid="1659334894381" xL)))+ dec2bin(localid="1659334900087" xR)

The highlighted part of the algorithm is the missing part.

Explanation:

• “dec2bin” is the function which accepts the decimal value “x” as the input parameter and it converts the decimal integer “x” with “n” digits into binary.

• Initially, it checks whether the number of digits “n” is equal to 1 and returns the binary value.

• Otherwise, it splits the decimal into two halves.

• Then, it returns the value returned by the function “fastmultiply” with parameters as the value of the function “ pwr2bin(n/2) ” and “dec2bin(xL)” along with the addition of dec2bin(xR).

o Here, “pwr2bin” function convert the decimal integer (n/2) into binary.

o “dec2bin” function convert the decimal integer “xL” into binary.

o Then, finally the value returned by the fastmultiply is added with the value returned by dec2bin(xR)

The algorithm “dec2bin” splits the number into two halves and process which takes a time of 2T(n/2) and as seen already the running time of “fastmultiply” algorithm is 0nlog23.

So the recurrence relation is,

T(n)=2T(n/2)+0nlog23 …… (1)

Consider the master theorem for the following recurrence equation,

role="math" localid="1659335661292" T(n)=aT(n/b)+nc …… (2)

By comparing equation (1) and (2), the value of “n” is “ nlog23”, “c” is log23, “b” is “2” and “a” is 2.

By master’s theorem, if the value of c > logba, then the running time is,

Tn=0nc …… (3)

Check if c>logba:

logba …… (4)

Substitute the value for “b” as 2 and “a” as 2, in equation (4).

logba=log22=1

Thus, the value of logbais less than “c” which is role="math" localid="1659335310024" log23=1.58.

Substitute “c” as log23in equation (3). The running time of algorithm is shown below:

Tn=0nlog23

Therefore, the algorithm takes a run time of 0nlog23.

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

This problem illustrates how to do the Fourier Transform (FT) in modular arithmetic, for example, modulo .(a) There is a number such that all the powers ω,ω2,...,ω6 are distinct (modulo ). Find this role="math" localid="1659339882657" ω, and show that ω+ω2+...+ω6=0. (Interestingly, for any prime modulus there is such a number.)

(b) Using the matrix form of the FT, produce the transform of the sequence (0,1,1,1,5,2) modulo 7; that is, multiply this vector by the matrix M6(ω), for the value of ωyou found earlier. In the matrix multiplication, all calculations should be performed modulo 7.

(c) Write down the matrix necessary to perform the inverse FT. Show that multiplying by this matrix returns the original sequence. (Again all arithmetic should be performed modulo 7.)

(d) Now show how to multiply the polynomials and using the FT modulo 7.

Suppose you are choosing between the following three algorithms: • Algorithm A solves problems by dividing them into five sub-problems of half the size, recursively solving each sub-problem, and then combining the solutions in linear time. •

Algorithm B solves problems of size n by recursively solving two sub-problems of size n-1and then combining the solutions in constant time. • Algorithm C solves problems of size n by dividing them into nine sub-problems of size n/3, recursively solving each sub-problem, and then combining the solutions in O(n2)time.

What are the running times of each of these algorithms (in big- O notation), and which would you choose?

Find the unique polynomial of degree 4 that takes on values p(1)=2,p(2)=1,p(3)=0,p(4)=4,andp(5)=0. Write your answer in the coefficient representation.

You are given an array of nelements, and you notice that some of the elements are duplicates; that is, they appear more than once in the array. Show how to remove all duplicates from the array in time O(nlogn) .

A binary tree is full if all of its vertices have either zero or two children. Let Bndenote the number of full binary trees with n vertices. (a)By drawing out all full binary trees with 3, 5, or 7 vertices, determine the exact values of B3, B5, and B7. Why have we left out even numbers of vertices, like B4?

(b) For general n, derive a recurrence relation for Bn.

(c) Show by induction that Bnis Ω(2n).

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