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

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

In justifying our matrix multiplication algorithm (Section 2.5), we claimed the following block wise property: if X and Y are n×nn matrices, and

X=[ABCD],Y=[EFGH],

where A,B,C,D,E,F,G, and H are n/2×n/2 sub-matrices, then the product XY can be expressed in terms of these blocks:

XY=[ABCD][EFGH]=[AE+BGAF+BHCE+DGCF+DH]

Prove this property.

Question: You are given an infinite array A[·]in which the first n cells contain integers in sorted order and the rest of the cells are filled with . You are not given the value of n. Describe an algorithm that takes an integer x as input and finds a position in the array containing x, if such a position exists, in O(log n) time. (If you are disturbed by the fact that the array A has infinite length, assume instead that it is of length n, but that you don’t know this length, and that the implementation of the array data type in your programming language returns the error message whenever elements A[i]withi>n are accessed.)

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

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

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