Tuesday 11 March 2014

Selection Trees



selection tree is a complete binary tree in which the leaf nodes hold a set of keys, and each internal node holds the “winner” key among its children.

Modifying a Key

It takes O(log n) time to modify a selection tree in response to a change of a key in a leaf.
        4o
   |---------|
   o         o
   2        4|
 |----|   |----|
 o    o   o    o
1    2    3    4         4o
   |---------|

   2o        4o
 |----|   |----|

1o   2o   3oo5    4o         4o
   |---------|

   2o        45oo
 |----|   |----|

1o   2o   3oo5    4o         oo
        45
   |---------|
   2o        45oo
   |         |
 |----|   |----|
1o   2o   3oo5    4o

Initialization

The construction of a selection tree from scratch takes O(n) time by traversing it level-wise from bottom up.
        o
        |
   |---------|
   o         o
   |         |
 |----|   |----|
1o   2o   3o    4o         o
        |
   |---------|
   o         o
   |        4|
 |----|   |----|
 o    o   o    o
1    2    3    4         o
   |---------|

   2o        4o
 |----|   |----|
 o    o   o    o
1    2    3    4         4o
   |---------|

   2o        4o
 |----|   |----|

1o   2o   3o    4o

Application: External Sort

Given a set of values16 9 10 8 6 11 12 1 4 7 14 13 2 15 5 3= 16
divide it into chuncks,16 9 10 86 11 12 14 7 14 132 15 5 3= 4
internally sort each chunk,8 9 10 161 6 11 124 7 13 142 3 5 15
construct complete binary tree of leaves with the chunks attached to the leaves.                   o
         |--------------------|
         o                    o
         |                    |
    |---------|          |---------|
    o         o          o         o
    |         |          |         |
    |         |          |         |
8-9 10-16 1-6-11 12 -4 7-13-14 2-3 5-15
--------- --------- ---------  --------
Convert the tree into a selection tree with the keys being fed to the leaves from the chunks                   0o
         |--------------------|
         o                    o
         1                   0|
    |---------|          |---------|
    o         o          o         o
   8|         1         0|        2|
    |         |          |         |
--9 10-16 --6-11 12 --7-13-14| --3 5-15
--------- --------- ---------  --------
Remove the winner from the tree                   o
         |--------------------|
         o                    o
         1                    |
    |---------|          |---------|
    o         o          o         o
   8|         1          |        2|
    |         |          |         |
--9 10-16 --6-11 12 --7-13-14| --3 5-15
--------- --------- ---------  --------
Feed to the empty leaf the next value from its corresponding chunk                   o
         |--------------------|
         o                    o
         1                    |
    |---------|          |---------|
    o         o          o         o
   8|         1         7|        2|
    |         |          |         |
--9 10-16 --6-11 12 ----13-14| --3 5-15
--------- --------- ---------  --------
Adjust the selection tree to the change in the leaf                   1o
         |--------------------|
         o                    o
         1                   2|
    |---------|          |---------|
    o         o          o         o
   8|         1         7|        2|
    |         |          |         |
--9 10-16 --6-11 12 ----13-14| --3 5-15
--------- --------- ---------  --------
Repeat the deletion subprocess until all the values are consumed.
  • The algorithm takes O(M -nlog-n)
    M    M time to internally sort the elements of the chunks, O(M) to initialize the selection tree, and O(n log M) to perform the selection sort. For « the total time complexity is O(n logn).
  • To reduce I/O operations, inputs from the chunks to the selection tree should go through buffers.

No comments:

Post a Comment

 

FACEBOOK PAGE

SKETCHES & PAINTINGS