CMSC 341 - PowerPoint PPT Presentation

About This Presentation
Title:

CMSC 341

Description:

Heap efficiency results, in part, from the implementation ... put in heap order in O ... Binary heaps support insert, findMin, deleteMin, and ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 31
Provided by: csU59
Category:
Tags: cmsc | heaps

less

Transcript and Presenter's Notes

Title: CMSC 341


1
CMSC 341
  • Binary Heaps
  • Priority Queues

2
Priority Queues
  • Priority some property of an object that allows
    it to be prioritized WRT other objects (of the
    same type)
  • Priority Queue homogeneous collection of
    Comparables with the following operations
    (duplicates are allowed)
  • void insert (const Comparable x)
  • void deleteMin()
  • void deleteMin ( Comparable min)
  • const Comparable findMin() const
  • Construct from set of initial values
  • bool isEmpty() const
  • bool isFull() const
  • void makeEmpty()

3
Priority Queue Applications
  • Printer management the shorter document on the
    printer queue, the higher its priority.
  • Jobs queue users tasks are given priorities.
    System priority high.
  • Simulations
  • Sorting

4
Possible Implementations
  • Use sorted list. Sort by priority upon insertion.
  • findMin() --gt Itr.retrieve()
  • insert() --gt list.insert()
  • deleteMin() --gt list.delete(1)
  • Use ordinary BST
  • findMin() --gt tree.findMin()
  • insert() --gt tree.insert()
  • deleteMin() --gt tree.delete(tree.findMin())
  • Use balanced BST
  • guaranteed O(lg n) for AVL, Red-Black

5
Binary Heap
  • A binary heap is a CBT with the further property
    that at every vertex neither child is smaller
    than the vertex, called partial ordering.
  • Every path from the root to a leaf visits
    vertices in a non-decreasing order.

6
Binary Heap Properties
  • For a node at index i
  • its left child is at index 2i
  • its right child is at index 2i1
  • its parent is at index ?i/2?
  • No pointer storage
  • Fast computation of 2i and ?i/2?
  • i ltlt 1 2i
  • i gtgt 1 ?i/2?

7
Binary Heap Performance
  • Performance
  • construction O(n)
  • findMin O(1)
  • insert O(lg n)
  • deleteMin O(lg n)
  • Heap efficiency results, in part, from the
    implementation
  • conceptually a binary tree
  • implementation in an array (in level order),
    root at index 1

8
BinaryHeap.H
  • template ltclass Comparablegt
  • class Binary Heap
  • public
  • explicit BinaryHeap(int capacity BIG)
  • bool isEmpty() const
  • bool isFull() const
  • const Comparable findMin() const
  • void insert (const Comparable x)
  • void deleteMin()
  • void deleteMin(Comparable min_item)
  • void makeEmpty()
  • private
  • int currentSize
  • vectorltComparablegt array
  • void buildHeap()
  • void percolateDown(int hole)

9
BinaryHeap.C
  • template ltclass Comparablegt
  • const Comparable BinaryHeapfindMin( )
  • if ( isEmpty( ) ) throw Underflow( )
  • return array1

10
Insert Operation
  • Must maintain
  • CBT property (heap shape)
  • easy, just insert new element at the right of the
    array
  • Heap order
  • could be wrong after insertion if new element is
    smaller than its ancestors
  • continuously swap the new element with its parent
    until parent is not greater than it
  • called sift up or percolate up
  • Performance O(lg n) worst case because height of
    CBT is O(lg n)

11
BinaryHeap.C (cont)
  • template ltclass Comparablegt
  • void BinaryHeapltComparablegt
  • insert(const Comparable x)
  • if (isFull()) throw OverFlow()
  • int hole currentSize
  • // percolate up
  • for ( hole gt 1 x lt arrayhole/2 hole / 2)
  • arrayhole arrayhole/2
  • // put x in hole
  • arrayhole x

12
Deletion Operation
  • Steps
  • remove min element (the root)
  • maintain heap shape
  • maintain heap order
  • To maintain heap shape, actual vertex removed is
    last one
  • replace root value with value from last vertex
    and delete last vertex
  • sift-down the new root value
  • continually exchange value with the smaller child
    until no child is smaller

13
BinaryHeap.C (cont)
  • template ltclass Comparablegt
  • void BinaryHeapltComparablegt
  • deleteMin(Comparable minItem)
  • if ( isEmpty( ) ) throw Underflow( )
  • minItem array1
  • array1 arraycurrentSize--
  • percolateDown(1)

14
BinaryHeap.C (cont)
  • template ltclass Comparablegt
  • void BinaryHeapltComparablegtpercolateDown(int
    hole)
  • int child
  • Comparable tmp arrayhole
  • for ( hole 2 lt currentSize hole child)
  • child hole 2
  • if (child ! currentSize
  • arraychild 1 lt array child )
  • child
  • if (array child lt tmp)
  • array hole array child
  • else break
  • arrayhole tmp

15
Constructing a Binary Heap
  • A BH can be constructed in O(n) time.
  • Suppose an array in arbitrary order. It can be
    put in heap order in O(n) time.
  • Create the array and store n elements in it in
    arbitrary order. O(n)
  • Heapify the array
  • start at vertex i ?n/2?
  • percolateDown(i)
  • repeat for all vertices down to i

16
BinaryHeap.C (cont)
  • template ltclass Comparablegt
  • void BinaryHeapltComparablegt
  • buildHeap( )
  • for(int i currentSize/2 i gt0 i--)
  • percolateDown(i)

17
Performance of Construction
  • A CBT has 2h-1 vertices on level h-1.
  • On level h-l, at most 1 swap is needed per node.
  • On level h-2, at most 2 swaps are needed.
  • On level 0, at most h swaps are needed.
  • Number of swaps S
  • 2h0 2h-11 2h-22 20h
  • h(2h1-1) - ((h-1)2h12)
  • 2h1(h-(h-1))-h-2
  • 2h1-h-2

18
Performance of Construction (cont)
  • But 2h1-h-2 O(2h)
  • But n 1 2 4 2h
  • Therefore, n O(2h)
  • So S O(n)
  • A heap of n vertices can be built in O(n) time.

19
Heap Sort
  • Given n values, can sort in O(n log n) time (in
    place).
  • Insert values into array -- O(n)
  • heapify -- O(n)
  • repeatedly delete min -- O(lg n) n times
  • Using a min heap, this code sorts in reverse
    order. With a max heap, it sorts in normal order.
  • for (i n-1 i gt 1 i--)
  • x findMin()
  • deleteMin()
  • Ai1 x

20
Limitations
  • Binary heaps support insert, findMin, deleteMin,
    and construct efficiently.
  • They do not efficiently support the meld or merge
    operation in which 2 PQs are merged into one. If
    P1 and P2 are of size n1 and n2, then the merge
    is in O(n1 n2)

21
Leftist Heap
  • Supports
  • findMin -- O(1)
  • deleteMin -- O(lg n)
  • insert -- O(lg n)
  • construct -- O(n)
  • merge -- O(lg n)

22
Leftist Tree
  • A LT is a binary tree in which at each vertex v,
    the path length, dr, from vs right child to the
    nearest non-full vertex is not larger than that
    from the vertexs left child to the nearest
    non-full vertex.
  • An important property of leftist trees
  • At every vertex, the shortest path to a non-full
    vertex is along the rightmost path.
  • Suppose this was not true. Then, at the same
    vertex the path on the left would be shorter than
    the path on the right.

23
Leftist Heap
  • A leftist heap is a leftist tree in which the
    values in the vertices obey heap order (the tree
    is partially ordered).
  • Since a LH is not necessarily a CBT we do not
    implement it in an array. An explicit tree
    implementation is used.
  • Operations
  • findMin -- return root value, same as BH
  • deleteMin -- done using meld operation
  • insert -- done using meld operation
  • construct -- done using meld operation

24
Meld
  • Algorithm
  • Meld (H1, H2)
  • if (!root(H1) (root_value(H1) gt
    root_value(H2) )
  • swap (H1, H2)
  • if (root(H1) ! NULL))
  • right(H1) lt-- Meld(right(H1),H2)
  • if (left_length(H1) lt right_length(H1)
  • swap(left(H1), right(H1)

25
Meld (cont)
  • Performance O(lg n)
  • the rightmost path of each tree has at most
    ?lg(n1)? vertices. So O(lg n) vertices will be
    involved.

26
(No Transcript)
27
(No Transcript)
28
(No Transcript)
29
Leftist Heap Operations
  • Other operations implemented in terms of Meld
  • insert (item)
  • make item into a 1-vertex LH, X
  • Meld(this, X)
  • deleteMin
  • Meld(left subtree, right subtree)
  • construct from N items
  • make N LH from the N values, one element in each
  • meld each in
  • one at a time
  • use queue and build pairwise

30
LH Construct
  • Algorithm
  • make N heaps each with one data value
  • Queue Q
  • for (I1 I lt N I)
  • Q.Enqueue(Hi)
  • Heap H Q.Dequeue()
  • while (!Q.IsEmpty())
  • Q.Enqueue(meld(H,Q.Dequeue())
  • H Q.Dequeue()
Write a Comment
User Comments (0)
About PowerShow.com