21
The 2-Way Merge Problem 學學 : 學學學

學生 : 曾羽銘. The 2-Way Merge Problem 2-Way Merge Sort 是先將資料分成幾個排序好的串列, 然後把串列兩兩合併成更大的已排序串列,直到合併

Embed Size (px)

Citation preview

The 2-Way Merge Problem

The 2-Way Merge Problem :1The 2-Way Merge Problem2-Way Merge Sort

Liner Merge 2 way merge sort 2An Example Of Linear MergeA: 1, 2, 5, 6 B: 3, 4124365C:linear merge,AB A1256 B34

3The Linear Merge Problem L1 and L2, L1 = (a1 , a2 , ... , an1) and L2 = (b1 ,b2 ,... ,bn2)L1 and L2 can be merged into one sorted list by applying the Linear merge algorithm.Linear Merge AlgorithmInput: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 ai and bj if ai > bj then output bj and j : = j + 1 else output ai and i : = 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 sortL1: 1 3 5L2: 2 4 6The number of comparisons required is m+n-12-way MergeIf 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.

,

262-way MergeL1 elementsL1+L2 elementsL1+L2+L3 elementsL2 elementsL3 elementsthe number of comparisons required in this merging sequence is 72-way MergeL1 elementsL2+L3 elementsL1+L2+L3 elementsL2 elementsL3 elementsthe number of comparisons required is only 82-way Merge ProblemWe are now concerned with the following 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?

9The 2-Way Merge ProblemWe have (L1,L2,L3,L4,L5) with sizes (20,5,8,7,4).Imagine that we merge these lists as follow:

Merge L1 and L2 to produce Z1 with 20+5=25 comparisonsMerge Z1 and L3 to produce Z2 with 25+8=33 comparisonsMerge Z2 and L4 to produce Z3 with 33+7=40 comparisonsMerge Z3 and L5 to produce Z4 with 40+4=44 comparisonsTotal = 142 comparisonsIllustration of Merging SequencesZ4Z1Z2Z3L1L2L3L4L5205847420 + 45142738The 2-Way Merge ProblemSuppose that we use a greedy method in which we all always merge two presently shortest lists. Then the merging pattern will be

Merge L2 and L5 to produce Z1 with 5+4=9 comparisonsMerge L3 and L4 to produce Z2 with 8+7=15 comparisonsMerge Z1 and Z2 to produce Z3 with 9+15=24 comparisonsMerge Z3 and L1 to produce Z4 with 24+20=44 comparisonsTotal = 92 comparisons

(20,5,8,7,4)Illustration of Merging SequencesZ4Z1Z2Z3L2L5L3L4L15482073(5+4+8+7)120An Example OfOptimal 2-way Merge Problem23571113510172441Optimal 2-way Merge AlgorithmInput:m sorted lists, Li, i = 1, 2, ... , m, each Li consisting 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.M sort 15The Time Complexity ofOptimal 2-way MergeFor 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).

Huffman Codes

Huffman CodesIn telecommunication, how do we represent a set of messages, each with an access frequency, by a sequence of 0s and 1s?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.An example ofHuffman algorithmA:2B:3E:13G:18C:5D:8F:15A:2B:35C:510D:818E:13F:1528G:183664A Huffman Code Tree010101010101Huffman CodesA10100B10101C1011D100E00F01G11SymbolsfrequenciesHuffmanASCIIReduced BitsA21010010000014B31010110000106C51011100001115D8100100010032E1300100010165F1501100011075G1811100011190