1 / 14

The 2-way Merge Problem

資工研二 翁靜美. The 2-way Merge Problem. An Example Of Linear Merge. L 1 : 1, 2, 5, 6. L 2 : 3, 4. 1. 2. 3. 4. 5. 6. Linear Merge Algorithm. Input : Two sorted lists, L 1 = ( a 1 , a 2 , ... , a n1 ) and L 2 = ( b 1 ,  b 2 , ... ,  b n2 ).

Download Presentation

The 2-way Merge Problem

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 資工研二 翁靜美 The 2-way Merge Problem

  2. An Example Of Linear Merge L1: 1, 2, 5, 6 L2: 3, 4 1 2 3 4 5 6

  3. Linear Merge Algorithm Input: Two sorted lists, L1 = (a1 , a2 , ... , an1) and L2 = (b1 , b2 , ... , bn2). Output: A sorted list consisting of elements in L1 and L2 . Begin i : = 1 j : = 1 do compare aiand bj if ai>bj then output bjand j : = j + 1 else output ai andi: = i + 1 while (i n1 and j n2) if i > n1 then output bj, bj+1 , ... , bn2 , else output ai, ai+1 , ... , an1 . End. • The worst case of merge sort • L1: 1 3 5 • L2: 2 4 6 • The number of comparison required is m+n-1

  4. 2-way Merge • If more than two sorted lists are to be merged, we can still apply the linear merge algorithm. • These merging processes are called 2-way merge because each merging step only merges two sorted lists.

  5. 2-way Merge 50+30-1 = 79 the number of comparisons required in this merging sequence is 168 80+10-1 = 89

  6. 2-way Merge 30+10-1 = 39 the number of comparisons required is only 128 40+50-1 = 89

  7. 2-way Merge Problem • There are m sorted lists. • Each of them consists of ni elements. • What is the optimal sequence of merging process to merge these sorted lists together by using the minimum number of comparisons?

  8. An Example OfOptimal 2-way Merge Problem 2 3 5 7 11 13 41 17 24 10 7 11 13 5 5 2 3

  9. Optimal 2-way Merge Algorithm Input: m sorted lists, Li, i = 1, 2, ... , m, each Liconsisting of ni elements. Output: An optimal 2-way merge tree. Step 1:Generate m trees, where each tree has exactly one node (external node) with weight ni. Step 2: Choose two trees T1 and T2 with minimal weights. Step 3: Create a new tree T whose root has T1 and T2 as its subtrees and weight is equal to the sum of weights T1 and T2. Step 4: Replace T1 and T2 by T. Step 5: If there is only one tree left, stop and return; otherwise, go to Step 2.

  10. The Time Complexity ofOptimal 2-way Merge • For the given m numbers n1, n2, …, nm, we can construct a min-heap to represent these numbers where the root value is smaller than the values of its sons. • The main loop is executed (n-1) times: • Tree reconstruction after removing the root, which has the smallest value, can be done in O(log n) time. • The insertion of a new node into a min-heap also can be done inO(log n) time. • The total time to generate an optimal extended binary tree is O(n log n).

  11. Huffman Codes • In telecommunication, how do we represent a set of messages, each with an access frequency, by a sequence of 0’s and 1’s? • To minimize the transmission and decoding costs, we may use short strings to represent more frequently used messages. • This problem can by solved by using the 2-way merge algorithm.

  12. An example ofHuffman algorithm 64 A:2 0 1 28 36 B:3 0 1 0 1 C:5 E:13 F:15 18 G:18 0 1 D:8 D:8 10 E:13 0 1 A Huffman Code Tree 5 C:5 F:15 0 1 G:18 A:2 B:3

  13. End.

More Related