Tuesday 11 March 2014

Heaps


heap is a complete tree with an ordering-relation R holding between each node and its descendant.
Examples for R: smaller-than, bigger-than
Assumption: In what follows, is the relation ‘bigger-than’, and the trees have degree 2.
HeapNot a heap
      8o
   |------|
   o      o
  5|     6
|----|
o    o
3    2      8o
   |------|
   o      o
  3|     6
|----|
o    o
5    2

Adding an Element

  1. Add a node to the tree
          8o
   |------|
   o      o
  5|     6|
|----|    |
o    o    o
3    2
  2. Move the elements in the path from the root to the new node one position down, if they are smaller than the new element
    new element479
    modified tree      8o
   |------|

  5o     6o
|----|    |

3o    2o    o      8o
   |------|

  5o      o
|----|    |

3o    2o   6o      o
   -------
   |      |
  5o     8o
|----|    |

3o    2o   6o
  3. Insert the new element to the vacant node
          8o
   |------|
   o      o
  5|     6|
|----|    |
o    o    o
3    2   4      8o
   |------|

  5o     7o
|----|    |

3o    2o   6o      9o
   |------|

  5o     8o
|----|    |

3o    2o   6o
  4. A complete tree of nodes has depth  |~ log n ~| , hence the time complexity is O(log n)

Deleting an Element

  1. Delete the value from the root node, and delete the last node while saving its value.
    beforeafter
          9o
   -------
   |      |
  8o     5o
|----|    |

3o    6o   2o      o
   |------|
   o      o
  8|     5
|----|
o    o
3    6  2
  2. As long as the saved value is smaller than a child of the vacant node, move up into the vacant node the largest value of the children.
          8o
   |------|
   o      o
   |     5
|----|
o    o
3    6  2       8o
   |------|

  6o     5o
|----|

3o    o  2
  3. Insert the saved value into the vacant node
          8o
   |------|
   o      o
  6|     5
|----|
o    o
3    2
  4. The time complexity is O(log n)

Initialization

Brute Force

Given a sequence of values e1, ..., en, repeatedly use the insertion module on the given values.
  • Level in a complete tree has at most 2h-1 = O(2n) elements
  • Levels 1, ..., h 1 have 20 + 21 + ... + 2h-2 = O(2h) elements
  • Each element requires O(log n) time. Hence, brute force initialization requires O(n log n) time.
    elements.

Efficient

  • Insert the elements e1, ..., en into a complete tree
          1o
   |------|
   o      o
  2|     3|
|----|    |
o    o    o
4    5   6
  • For each node, starting from the last one and ending at the root, reorganize into a heap the subtree whose root node is given. The reorganization is performed by interchanging the new element with the child of greater value, until the new element is greater than its children.
          1o
   |------|
   o      o
  2|     6|
|----|    |
o    o    o
4    5   3
          1o
   |------|
   o      o
  5|     6|
|----|    |
o    o    o
4    2   3
          6o
   |------|
   o      o
  5|     1|
|----|    |
o    o    o
4    2   3
          6o
   |------|
   o      o
  5|     3|
|----|    |
o    o    o
4    2   1
  • The time complexity is O(0 (n/2) + 1 (n/4) + 2 (n/8) + ... + (log n1) = O(n(0 . 2-1 + 1 . 2-2 + 2 . 2-3 + ... + (log n. 2- log n)) = O(n)
    since the following equalities holds.  sum  = 1 oo (1)2-k =2[ sum  = 1 oo (1)2-k[ sum  = 1 oo (1)2-k] =[ sum  = 1 oo k2-k[ sum  = 1 oo (1)2-k] = sum  = 1 oo [(1)]2-k = sum  = 1 oo 2-k =1

Applications

Priority Queue A dynamic set in which elements are deleted according to a given ordering-relation.
Heap Sort Build a heap from the given set (O(n)) time, then repeatedly remove the elements from the heap (O(n log n)).

Implementation

An array. The root is at location 1. Location i > 1 is a child of  |_ i/2 _| .

No comments:

Post a Comment

 

FACEBOOK PAGE

SKETCHES & PAINTINGS