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

What is the sum of the nth roots of unity? What is their product if n is odd? If n is even?

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?

In this problem we will develop a divide-and-conquer algorithm for the following geometric task.

CLOSEST PAIRInput: A set of points in the plane, {p1=(x1;y1),p2=(x2,y2),...,pn=(xn,yn)}

Output: The closest pair of points: that is, the pair PiPjfor which the distance between piand pj, that is,

(xi-xi)2+z(yi-yi)2,

is minimized.

For simplicity, assume that n is a power of two, and that all the x-coordinates role="math" localid="1659237354869" xi are distinct, as are the y-coordinates.

Here’s a high-level overview of the algorithm:

.Find a value for which exactly half the points have xi<x, and half have xi>x. On this basis, split the points into two groups, L and R.

• Recursively find the closest pair in L and in R. Say these pairs are pL·qLLand pR·qRRwith distances dLand dR respectively. Let d be the smaller of these two distances.

• It remains to be seen whether there is a point in Land a point in R that are less than distance dapart from each other. To this end, discard all points with xi<x-dor xi>x+d and sort the remaining points by y-coordinate.

• Now, go through this sorted list, and for each point, compute its distance to the seven subsequent points in the list. Let pM·qMbe the closest pair found in this way.

• The answer is one of the three pairs role="math" localid="1659237951608" {pL,qL},{pR,qR}{pM,qM}, whichever is closest.

(a) In order to prove the correctness of this algorithm, start by showing the following property: any square of size d×d in the plane contains at most four points of L.

(b) Now show that the algorithm is correct. The only case which needs careful consideration is when the closest pair is split between L and R.

(c) Write down the pseudocode for the algorithm, and show that its running time is given by the recurrence:

T(n)=2T(nl2)+0(nlogn)

Show that the solution to this recurrence is o(nlogzn).

(d) Can you bring the running time down to O(nlogn)?

Consider the task of searching a sorted array A[1,,n] for a given element x: a task we usually perform by binary search in time O(logn) . Show that any algorithm that accesses the array only via comparisons (that is, by asking questions of the form “is A[i]z 0?”), must take Ω(logn) steps.

Thesquare of a matrix A is its product with itself, AA.

(a) Show that five multiplications are sufficient to compute the square of a 2 x 2 matrix.

(b) What is wrong with the following algorithm for computing the square of an n x n matrix?

“Use a divide-and-conquer approach as in Strassen’s algorithm, except that instead of getting 7 subproblems of size n2, we now get 5 subproblems of size n2 thanks to part (a). Using the same analysis as in Strassen’s algorithm, we can conclude that the algorithm runs in time O (nc) .”

(c) In fact, squaring matrices is no easier than matrix multiplication. In this part, you will show that if n x n matrices can be squared in time S(n) = O(nc), then any two n x n matrices can be multiplied in time O(nc) .

  1. Given two n x n matrices A and B, show that the matrix AB + BA can be computed in time 3S(n) + O(n2 ) .
  2. Given two n x n matrices X and Y, define the 2n x 2n matrices A and B,L as follows:
    A=X000andB=0Y00
    What is AB + BA, in terms of X and Y?
  3. Using (i) and (ii), argue that the product XY can be computed in time 3S(2n) + O(n2 ). Conclude that matrix multiplication takes time O(nc ).
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