In Section 1.2.3, we studied Euclid’s 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’s 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.)

Short Answer

Expert verified

(a) The rule can be proved true by the even-odd substitution.

(b) The efficient algorithm is as follows:

Procedure gcd (a,b)

ifa=b

return a

else if a%2=0b%2=0:

return 2.gcda2,b2

else if a%20b%2=0:

return gcda,b2

else if a%20b%20a>b:

return gcda-b2,b

else if a%20b%20a<b:

returngcda,b-a2

(c) The running time of the divide-and-conquer algorithm is O(n2)comparatively faster than the Euclid’s algorithm.

Step by step solution

01

Explain divide-and-conquer algorithm

Divide-and-conquer algorithm solves the large problem by dividing it into the smaller sub-problems. The sub-problems are solved individually and the results of the sub-problems are combined to get the output of the larger problem.

02

Show that the given rule is true.

(a)

Consider the following given rule,

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

If aand b are even numbers, 2 is a common divisor of aand b. Thus , the greatest common divisor will be 2 times the gcd of numbers a2and b2.

If is odd and b is even, then b is divisible by 2. Thus, the gcd of numbers will be same as gcd of a and b2 .

The third property follows from the fact that if a and b are odd, then (a-b) will be even. Since gcd(a,b)=gcd(a,-b,b) and a-b is even . Now applying the second property will prove that the gcd of a-b2,b

Therefore, it has been proved that the given rule is true.

03

Give the efficient algorithm for divide-and conquer.

(b)

The efficient recursive algorithm for solving divide-and-conquer problem is as follows:

Procedure gcd(a,b)

if a=b:

return a

else if a%2=0b%2=0:

return 2.gcda2,b2

else if a%20b%2=0:

returngcda,b2gcda,b2

else if a%20b%20a>b:

return gcda-b2,b

else if a%20b%20a<b:

return gcda,b-a2

04

Compare the efficiency of the algorithm with Euclid’s algorithm.

(c)

Consider that a and b are n-bit numbers. Size of a and b is 2n bits. Out of 4 if conditions, every on except the case when is odd and is even, decreases the size of aand b to 2n-2 bits. Each of the operation is constant time operation as the dividing and multiplying the numbers by 2. For two subtractions of two n-bit numbers is the number of bits of the operand. Consider the running time complexity in worst case as follows:

T(2n)=T(2n-1)+cnT(2n-1)=T(2n-2)+cnKT(2)=T(1)+c

By substitution,

T(2n)=2c.i=1ni

This results in the running time O(n2)for the divide-and-conquer algorithm. The running time of the Euclid’s algorithm is O(n3).

Therefore, the divide-and conquer algorithm is faster with the running time complexity of O(n2)in worst case.

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

A kway merge operation. Suppose you have ksorted arrays, each with nelements, and you want to combine them into a single sorted array ofkn elements.

(a)Here’s one strategy: Using the merge procedure from Section 2.3, merge the first two arrays, then merge in the third, then merge in the fourth, and so on. What is the time complexity of this algorithm, in terms of kand n?

(b) Give a more efficient solution to this problem, using divide-and-conquer.

Practice with polynomial multiplication by FFT.

(a) Suppose that you want to multiply the two polynomials x + 1 and x2+1using the FFT. Choose an appropriate power of two, find the FFT of the two sequences, multiply the results component wise, and compute the inverse FFT to get the final result.

(b) Repeat for the pair of polynomials 1+x+2x2and 2 + 3x.

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

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

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.

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