Presentation is loading. Please wait.

Presentation is loading. Please wait.

The 2-Way Merge Problem 學生:曾羽銘.

Similar presentations


Presentation on theme: "The 2-Way Merge Problem 學生:曾羽銘."— Presentation transcript:

1 The 2-Way Merge Problem 學生:曾羽銘

2 The 2-Way Merge Problem 2-Way Merge Sort是先將資料分成幾個排序好的串列,然後把串列兩兩合併成更大的已排序串列,直到合併成包含所有資料的已排序串列為止。 Liner Merge 則是只有兩個字串或串列,作排序。 2 way merge sort

3 An Example Of Linear Merge
B: 3, 4 C: 1 2 3 4 5 6 當兩個字串要做排序時我們稱作是linear merge 有兩個字串,A與B A是1256 B是34

4 The 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.

5 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 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 sort L1: 1 3 5 L2: 2 4 6 The number of comparisons required is m+n-1

6 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. 如果要合併兩個以上的排序列表,可以應用線性合併算法 這些合併過程被稱為2路合併,合併,因為每個步驟只能合併兩個已排序的列表。

7 2-way Merge 80+70-1=149 150+50-1=199 L1 80 elements L1+L2 150 elements
L1+L2+L3 200 elements L3 50 elements 80+70-1=149 the number of comparisons required in this merging sequence is 348 150+50-1=199

8 2-way Merge 70+50-1=119 120+80-1=199 L1 80 elements L2+L3 120 elements
L1+L2+L3 200 elements L3 50 elements 70+50-1=119 the number of comparisons required is only 318 120+80-1=199

9 2-way Merge Problem We 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? 什麼是最佳的序列合併過程合併排序的列表,通過使用最少的比較?

10 The 2-Way Merge Problem We 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 comparisons Merge Z1 and L3 to produce Z2 with 25+8=33 comparisons Merge Z2 and L4 to produce Z3 with 33+7=40 comparisons Merge Z3 and L5 to produce Z4 with 40+4=44 comparisons Total = 142 comparisons

11 Illustration of Merging Sequences
Z4 Z3 L5 1*4 4 Z2 L4 2*7 7 Z1 L3 3*8 8 L1 L2 4*20 + 4*5 20 5

12 The 2-Way Merge Problem Suppose 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 comparisons Merge L3 and L4 to produce Z2 with 8+7=15 comparisons Merge Z1 and Z2 to produce Z3 with 9+15=24 comparisons Merge Z3 and L1 to produce Z4 with 24+20=44 comparisons Total = 92 comparisons (20,5,8,7,4)

13 Illustration of Merging Sequences
Z4 Z3 L1 1*20 20 Z1 Z2 L2 L5 L3 L4 8 7 5 4 3*( )

14 An Example Of Optimal 2-way Merge Problem
41 17 10 5 24 2 3 5 7 11 13

15 Optimal 2-way Merge Algorithm
Input: 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 列表

16 The Time Complexity of Optimal 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 in O(log n) time. The total time to generate an optimal extended binary tree is O(n log n).

17 Huffman Codes

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

19 An example of Huffman algorithm
64 A:2 1 28 36 B:3 1 1 C:5 E:13 F:15 18 G:18 Huffman Codes A 10100 B 10101 C 1011 D 100 E 00 F 01 G 11 1 D:8 D:8 10 E:13 A Huffman Code Tree 1 5 C:5 F:15 1 G:18 A:2 B:3

20 Symbols frequencies Huffman ASCII Reduced Bits A 2 10100 4 B 3 10101 6 C 5 1011 15 D 8 100 32 E 13 00 65 F 01 75 G 18 11 90

21 Thanks for listening


Download ppt "The 2-Way Merge Problem 學生:曾羽銘."

Similar presentations


Ads by Google