K-way merge algorithm

This is an old revision of this page, as edited by Happysailor (talk | contribs) at 18:42, 23 November 2015 (Added {{orphan}} and {{uncategorized}} tags to article). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computer science, K-Way Merge Algorithms are a specific type of Sequence Merge Algorithms that specialize in taking in multiple sorted lists and merging them into a single sorted list. These merge algorithms generally refer to merge algorithms that take in a number of sorted lists greater than two. 2-Way Merges are referred to as binary merges on the other hand and are also utilized in k-way merge algorithms.

2-Way Merge

A 2-Way Merge, or a binary merge, has been studied extensively due to its key role in Merge sort. An example of such is the classic merge that appears frequently in merge sort examples. The classic merge outputs the data item with the lowest key at each step; given some sorted lists, it produces a sorted list containing all the elements in any of the input lists, and it does so in time proportional to the sum of the lengths of the input lists. There are algorithms that exist that can operate in better than linear times such as the Hwang-Lin Merging Algorithm.[1]

Example[2]

Assume, that we have two arrays A[0..m-1] and B[0..n-1] that are sorted in ascending order and we want to merge them into an array C[0..m+n-1] with the same order. 

  1. Introduce read-indices ij to traverse arrays A and B, accordingly. Introduce write-index k to store position of the first free cell in the resulting array. By default i = j = k = 0.
  2. At each step: if both indices are in range (i < m and j < n), choose minimum of (A[i], B[j]) and write it to C[k]. Otherwise go to step 4.
  3. Increase k and index of the array, algorithm located minimal value at, by one. Repeat step 2.
  4. Copy the rest values from the array, which index is still in range, to the resulting array.

Applying 2-Way Merge when k > 2

If the number of sorted lists k is greater than 2, the 2-Way Merge function found in merge sort can still be used to merge everything into a single sorted list.

 
An example of Huffman Coding, which uses the same technique as in the optimal merge. The values shown would represent each list length.

Non-optimal

Let D={n1, ... , nk} be the set of sequences to be merged. Pick ni, nj∈ D and then merge them together using the merge function. The new set D is then D' = (D - {ni, nj}) ∪ {ni+nj}. This process is repeated until |D| = 1. The question then becomes how to pick ni and nj. How the merge algorithm picks ni and nj determines the cost of the overall algorithm. The worst case running time for this algorithm reaches O(m2 • n).

optimal merge pattern

The optimal merge pattern is found by utilizing a Greedy algorithm that selects the two shortest lists at each time to merge. This technique is similar to the one used in Huffman coding. The algorithm picks  ni, nj∈ D such that |n| ≥ |ni| and |n| ≥ |nj| ∀ n∈ D.[3]

On the optimal merge pattern on the hand can reduce the running time to O(m • n • log m)[4]. By choosing the shortest lists to merge each time, this lets the algorithm minimize how many times it is necessary to copy the same value into each new merged list.

Ideal Merge

The Ideal Merge technique is another merge method for merging greater than two lists except it does not use the 2-Way merge technique. The ideal merging technique was discussed and saw use as a part of UnShuffle Sort.[5]

Given a group of sorted lists S that we want to merge into list S', the algorithm is as follows:

  1. Each list is sorted by the value of its head element
  2. Then the head element of the first list is removed and placed into S'
  3. After the head element of the first list is removed, it is resorted according to the value of its new head element
  4. This repeats until all lists are empty

The head elements are generally stored in a priority queue. Depending on how the priority queue is implemented, the running time can vary. If ideal merge keeps its information in a sorted list, then inserting a new head element to the list would be done through a Linear search and the running time will be Θ(M • N) where N is the total number of elements in the sorted lists, and M is the total number of sorted lists.

On the other hand, if the sorted items in a heap, then the running time becomes Θ(N log M).

Binary Tree Implementation

As an example, the above technique can be implemented using a binary tree for its priority queue[6]. This can help limit the number of comparisons between the element heads. The binary tree is built containing the results of comparing the head of each array. The topmost node of the binary tree is then popped off and its leaf is refilled with the next element in its array.

As an example, let's say we have 4 sorted arrays:[7]

{5, 10, 15, 20}

{10, 13, 16, 19}

{2, 19, 26, 40}

{18, 22, 23, 24}

We start with the heads of each array and then build a binary tree from there.

 

The nodes from each array are compared to each other, before the value 2 is found as the lowest list head element. That value is then popped off, and its leaf is refilled with the next value in the list.

 

The value 2 is repopulated by the next value in the sorted list, 19. The comparisons end with 5 being the smallest value, and thus the next value to be popped off. This continues until all of the sorted lists are empty.

References

  1. ^ F.K.Hwang and S. Lin, \A Simple Algorithm for Merging Two Disjoint Linearly Ordered Sets", SIAM Journal on Computing 1 (1972), 31-39.
  2. ^ "ALGORITHM TO MERGE SORTED ARRAYS (Java, C++) | Algorithms and Data Structures". www.algolist.net. Retrieved 2015-11-19.
  3. ^ KABAT, MANAS RANJAN (2013-08-21). DESIGN AND ANALYSIS OF ALGORITHMS. PHI Learning Pvt. Ltd. ISBN 9788120348066.
  4. ^ Discrete Mathematics. Addison-Wesley. 1997-01-01. ISBN 9780673980397.
  5. ^ Art S. KagelUnshuffle Algorithm, Not Quite a Sort?, Computer Language Magazine, 3(11), November 1985.
  6. ^ Horowitz, Ellis; Sahni, Sartaj (1983-01-01). Fundamentals of data structures. Computer Science Press. ISBN 9780914894209.
  7. ^ "kway - Very fast merge sort - Google Project Hosting". code.google.com. Retrieved 2015-11-22.

Further reading