/*! 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} Q25E In Section 2.1 we described an a... [FREE SOLUTION] | 91影视

91影视

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 鈥渘鈥, 鈥渃鈥 is 鈥 log23鈥, 鈥渂鈥 is 鈥2鈥 and 鈥渁鈥 is 1.

By master鈥檚 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 鈥渂鈥 as 2 and 鈥渁鈥 as 1 in equation (4).

logba=log21=0

Thus, the value of logbais less than 鈥渃鈥 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:

鈥 鈥渄ec2bin鈥 is the function which accepts the decimal value 鈥渪鈥 as the input parameter and it converts the decimal integer 鈥渪鈥 with 鈥渘鈥 digits into binary.

鈥 Initially, it checks whether the number of digits 鈥渘鈥 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 鈥渇astmultiply鈥 with parameters as the value of the function 鈥 pwr2bin(n/2) 鈥 and 鈥渄ec2bin(xL)鈥 along with the addition of dec2bin(xR).

o Here, 鈥減wr2bin鈥 function convert the decimal integer (n/2) into binary.

o 鈥渄ec2bin鈥 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 鈥渄ec2bin鈥 splits the number into two halves and process which takes a time of 2T(n/2) and as seen already the running time of 鈥渇astmultiply鈥 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 鈥渘鈥 is 鈥 nlog23鈥, 鈥渃鈥 is log23, 鈥渂鈥 is 鈥2鈥 and 鈥渁鈥 is 2.

By master鈥檚 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 鈥渂鈥 as 2 and 鈥渁鈥 as 2, in equation (4).

logba=log22=1

Thus, the value of logbais less than 鈥渃鈥 which is role="math" localid="1659335310024" log23=1.58.

Substitute 鈥渃鈥 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 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

Question: Solve the following recurrence relations and give a bound for each of them.

(a)T(n)=2T(n/3)+1(b)T(n)=5T(n/4)+n(c)T(n)=7T(n/7)+n(d)T(n)=9T(n/3)+n2(e)T(n)=8T(n/2)+n3(f)T(n)=49T(n/25)+n(3/2)logn(g)T(n)=T(n-1)+2(h)T(n)=T(n-1)+nc,whereisaconstant(i)T(n)=T(n-1)+cn,whereissomeconstant(j)T(n)=2T(n-1)+1(k)T(n)=T(n)+1

Question: On page 66 there is a high-level description of the quicksort algorithm.

(a) Write down the pseudocode for quicksort.

(b) Show that its worst - case running time on an array of size n is (n)2.

(c) Show that its expected running time satisfies the recurrence relation.

T(n)O(n)+1ni=1n-1(Ti+Tn-i)

Then, show that the solution to this recurrence is O(nlogn).

How many lines, as a function of n (in (.)form), does the following program print? Write a recurrence and solve it. You may assume is a power of . function f (n) if n > 1:

print_line (鈥樷榮till going鈥欌)

f (n/2)

f (n/2)

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.

In Section 1.2.3, we studied Euclid鈥檚 algorithm for computing the greatest common divisor (gcd) of two positive integers: the largest integer which divides them both. Here we will look at an alternative algorithm based on divide-and-conquer.

(a) Show that the following rule is true.

gcd(a,b)={2gcd(a2,b2)ifa,bareevengcd(ab2)ifaisodd,bisevengcd(a-b2,b)ifa,bareodd

(b) Give an efficient divide-and-conquer algorithm for greatest common divisor.

(c) How does the efficiency of your algorithm compare to Euclid鈥檚 algorithm if a and b are n-bit -bit integers? (In particular, since n might be large you cannot assume that basic arithmetic operations like addition take constant 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.