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.

Short Answer

Expert verified

(a) Time complexity:O(k2n)

(b) Time complexity:O(knlogk)

Step by step solution

01

Introduction to array

There are different kinds of Array: 1,2,3 -dimensional and multi-dimensional arrays. In the above question there will be sorted array technique through we can combine single sorted array in knelements. To check that see below detail related to combine theory of array.

02

Step 2:Give the time complexity of the given algorithm.

(a)

Combine each of thek arrays with the preceding array one after the other, for each array containing 1to nentries.

Suppose that merging the array takes linear time.

Originally, the first and second arrays are combined, with each array having a size of n.

As a result, it takesn×n time, which equals 2n. The next array of sizen is then combined with both the earlier merged array2n .

As a result, merging three arrays takes3n time. It is then combined with the fourth array, which takes4n time.

As a result, the total time required to combine the entire array is

Timetaken=O(2n+3n++kn)=O(k2n)

Therefore, thetime complexity used to merge array is O(k2n).

03

                                                                                                                     Step 3:Efficient solution to perform k-way merge operation:

(b)

To begin, all narrays get separated into two equal groups, each withk2 arrays.

After that, every format's arrays were recursive combined one by one, and both sets merging array were merged together.

The issue is solved by breaking it into two smaller problems of size k2and combining them in a constant time nk.

ThusT(k),can be expressed as,

T(k)=2T(k2)+O(nk)……(1)

Consider the master theorem for the following recurrence equation,

T(k)=aT(kb)+kc……(2)

CompareEquations (1) and (2),kc equalsnk ,cequals 1,bequals2and aequals2 . If,c=logba then perhaps the running speed is, according to master's thesis.

T(k)=O(kclogk)……(3)

Here, the value of c=1and the value oflogba=log22=1which is equal to .Thus, substitute kcasnk in Equation (3). So, the running time of algorithm is,

T(k)=O(nklogk)

Therefore, the time complexity of the algorithm in terms of kand n is T(k)=O(nklogk).

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

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.

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

You are given two sorted lists of size mandn. Give an O(logm+logn)time algorithm for computing the k th smallest element in the union of the two lists.

Suppose we want to evaluate the polynomial P(x) = a0 + a1x + a2x2 + ... + anxn at point x.

  1. Show that the following simple routine, known as Horner’s rule, does the job and leaves the answer in z.
    z = an
    for I = n-1 down to 0 :
    z = zx + ai
  2. How many additions and multiplications does this routine use, as a function of n ? Can you find a polynomial for which an alternative method is substantially better?

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

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