Optimal Merge Pattern

When more than two sorted files are merged together, the merge can be accomplish by repeatedly merging sorted files in pairs. Thus if the files x1, x2, x3 and x4 are to be merged then we would have to merge first x1 and x2 to get result y1 then y1 and x3 to get y2 and then y2 to x4 to get result y3 desired sorted files. Alternatively, we also do the merging of files x1 and x2 to get the result y1 then merge file x3 and x4 to get y2 and at last merge y1 and y2 to get sorted files. For example the files x1, x2, and x3 are the three sorted files with the length 30, 20 and 10 records each. If we merge x1 and x2 then we get 50 moves and then if we merge with x3 we get another 60 moves.
       Alternatively by this method we give the merging file of about 110 moves.

optimal merge pattern, data structure and algorithm
At another method if we merged x2 and x3 and then x1 then total record moves is only 90.
optimal merge pattern, data structure and algorithm
Hence the second merge pattern is faster than the first one.

Algorithm for optimal merge pattern

Algorithm Tree (n)
{
For i=1 to n-1 do
{
pt = new treenode ;    // get tree node

( pt →  lchild ) = least ( list ) ;  // merge two tree with smallest length s
( pt →  rchild ) = least ( list ) ;
( pt →  weight ) = ( ( pt→ lchild ) → weight ) + ( ( pt →  rchild ) → weight ) ;
Insert ( list, pt ) ;
}

// Tree left in list is merge tree
return least ( list ) ;
}









Comments

Popular posts from this blog

Articulation point of graph / Biconnected components and DFS traversal

Brief shortnote on Requirement elicitation and requirement analysis

Requirement Engineering