0% found this document useful (0 votes)
3K views9 pages

3.5 Optimal Merge Patterns

The optimal merge pattern relates to merging sorted files into a single sorted file with the fewest number of record moves. It can be represented as a binary tree where leaf nodes are the original files and internal nodes represent the result of merging child nodes. An algorithm finds the optimal pattern by repeatedly merging the two smallest files until a single file remains, taking O(n log n) time when a heap data structure is used.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views9 pages

3.5 Optimal Merge Patterns

The optimal merge pattern relates to merging sorted files into a single sorted file with the fewest number of record moves. It can be represented as a binary tree where leaf nodes are the original files and internal nodes represent the result of merging child nodes. An algorithm finds the optimal pattern by repeatedly merging the two smallest files until a single file remains, taking O(n log n) time when a heap data structure is used.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

OPTIMAL MERGE PATTERNS

 Optimal merge pattern is a pattern that relates to the merging


of two or more sorted files into a single sorted file.

 This type of merging can be done by two way merging method.

 If we have two sorted files containing ‘m’ and ‘n’ records then they
could be merged together to obtain one sorted file in time O(m+n).

 When more than two sorted files are to be merged together, it


can be done by repeatedly merging sorted files in pairs.
OPTIMAL MERGE PATTERNS
Thus if files x1, x2, x3, x4 are to be merged we can follow different

pairing methods.
Method-1 Method-2
Merge x1 and x2 to get a file y1. Merge x1 and x2 to get a file y1.
Merge y1 and x3 to get y2. Merge x3 and x4 to get a file y2.
Merge y2 and x4 to get desired sorted file. Merge y1 and y2 to get desired sorted file.

 Different pairings require different amounts of computing time.


OPTIMAL MERGE PATTERNS
For example, x1, x2, x3 are three sorted files of length 30, 20 and 10

records each.
Method-1 Method-2
Operation Moves Operation Moves
Merge x1 and x2 to get file y1 50 Merge x2 and x3 to get file y1 30
Merge y1 with x3 to get 60 Merge y1 with x1 to get 60
desired sorted file desired sorted file
Total Record moves 110 Total Record moves 90

 A greedy attempt to obtain optimal merge pattern is , at each stage


merge the two smallest files together.
What is optimal merge pattern? Find optimal merge pattern for ten files whose record
lengths are 28, 32, 12, 5, 84, 53, 91, 35, 3, and 11.[7M][R16 III-II SET-1 Regular
April/May - 2019]
Z9 354
X1 = 28 X2 = 32 X3 = 12 X4 = 5 X5 = 84

X6= 53 X7 = 91 X8 = 35 X9 = 3 X10 = 11
Z7 179 175 Z8

Operation Moves
Z6 126 53 X6 84 91
Merge X9 with X4 to get Z1 8 X5 X7
Merge Z1 with X10 to get Z2 19 Z5 91
35 X8
Merge Z2 with X3 to get Z3 31
Merge Z3 with X1 to get Z4 59 Z4 59 32 X2
Merge Z4 with X2 to get Z5 91 Z3 31
Merge Z5 with X8 to get Z6 126
28 X1
Z2 19
Merge Z6 with X6 to get Z7 179 12 X3 Fig: Binary Tree
Merge X5 with X7 to get Z8 175 representing Merge
Z1 8 Pattern
Merge Z7 with Z8 to get Z9 354 11 X10
Total Moves 1042
X9 3 5 X4
Find optimal merge pattern for five files whose record lengths are 20, 30, 10, 5, 30.
X1 = 20 X2 = 30 X3 = 10 X4 = 5 X5 = 30 95 Z4

Operation Moves
Merge X4 with X3 to get Z1 15 Z2 35 Z3 60
Merge Z1 with X1 to get Z2 35
Merge X2 with X5 to get Z3 60
Z1 15 20 30 30
Merge Z2 with Z3 to get Z4 95 X1 X2 X5
Total Moves 205
5 10
X4 X3
Fig: Binary Tree representing Merge Pattern

 In the above binary tree representation, leaf nodes are drawn as


squares and represent the given five files. These nodes are called as
external nodes.
 The remaining nodes are represented with circles are called internal
nodes.
 Each internal node has exactly two children and it represents the file
obtained by merging the files represented by its two children.
 The number in each node is the length of file represented by that node.
 The external node X4 is at a distance of ‘3’ from the root node Z4.
 Hence, the records of file X4 will be moved three times, once to get Z1,
once again to get Z2, and finally one more time to get Z4.
 If di is the distance from the root to the external node for file Xi and qi
is length of Xi then the total number of record moves for this binary
merge tree is

 This sum is called weighted external path length of the tree.


OPTIMAL MERGE PATTERN ALGORITHM
Algorithm Tree(L,N)
//L is a list of ‘n’ single node Binary tree.
{
for i:=1 to n-1 do
{
GETNODE(T);
LCHILD(T)LEAST(L);
RCHILD(T)LEAST(L);
WEIGHT(T) WEIGHT(LCHILD(T))+ WEIGHT(RCHILD(T));
INSERT(L,T);
}
return LEAST(L)
}
 The algorithm has a list of ‘L’ trees as input.
 Each node in a tree has three fields: LCHILD, RCHILD, WEIGHT.
 Initially each tree in ‘L’ has exactly one node.
 This node is an external node and has LCHILD and RCHLD fields,
while the WEIGHT is the length of one of the ‘n’ files to be merged.
 GETNODE(T) provides a new node for use in building the tree.
 LEAST(L) finds a tree in ‘L’ whose root has least WEIGHT. This tree
is removed from ‘L’.
 INSERT(L,T) inserts the tree with root ‘T’ into the list ‘L’.
ANALYSIS OF ALGORITHM
 The main loop is executed ‘n-1’ times. If ‘L’ is kept in non-decreasing
order according to the WEIGHT value in the roots, then the
LEAST(L) requires only O(1) time and INSERT(L,T) can be done in
O(n) time. Hence the total time taken is O(n2).
 In case ‘L’ is represented as a minimum heap where the root values
are less than or equal to the values of its children then LEAST(L) and
INSERT(L,T) can be done in O(log n) time. In this case the
computing time is O(n log n).

You might also like