CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.

Slides:



Advertisements
Similar presentations
§6 Leftist Heaps CHAPTER 5 Graph Algorithms  Heap: Structure Property + Order Property Target : Speed up merging in O(N). Leftist Heap: Order Property.
Advertisements

DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
COL 106 Shweta Agrawal and Amit Kumar
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Treaps.  Good (logarithmic) performance with random data  Linear performance if the data is sorted ◦ Solutions – Splay Trees Amortized O( lg n) performance.
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Heaps, Heap Sort, and Priority Queues. Sorting III / Slide 2 Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Heaps, Heap Sort, and Priority Queues
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Chapter 21 Binary Heap.
data ordered along paths from root to leaf
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Priority Queues (Heaps)
Review for Exam 2 Topics covered (since exam 1): –Splay Tree –K-D Trees –RB Tree –Priority Queue and Binary Heap –B-Tree For each of these data structures.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
Heaps Priority Queues. 1/6/2016Data Structure: Heaps2 Outline Binary heaps Binomial queues Leftist heaps.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
CS 367 Introduction to Data Structures Lecture 8.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
Heaps and Heap Sort. Sorting III / Slide 2 Background: Complete Binary Trees * A complete binary tree is the tree n Where a node can have 0 (for the leaves)
CS 201 Data Structures and Algorithms
Priority Queues (Heaps)
Topics covered (since exam 1):
Binary Heaps Priority Queues
Source: Muangsin / Weiss
Binary Heaps What is a Binary Heap?
Bohyung Han CSE, POSTECH
CMSC 341 Lecture 13 Leftist Heaps
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
Binary Heaps What is a Binary Heap?
Binary Heaps What is a Binary Heap?
7/23/2009 Many thanks to David Sun for some of the included slides!
Heaps, Heap Sort, and Priority Queues
CMSC 341: Data Structures Priority Queues – Binary Heaps
Binary Heaps Priority Queues
Heapsort Heap & Priority Queue.
Topics covered (since exam 1):
CMSC 341 Lecture 14 Priority Queues & Heaps
Heaps and the Heapsort Heaps and priority queues
Binary Heaps Priority Queues
Heap Sort The Heap Data Structure
CE 221 Data Structures and Algorithms
Heaps Priority Queues.
CMSC 341 Lecture 19.
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
Topics covered (since exam 1):
Heaps & Multi-way Search Trees
Presentation transcript:

CMSC 341 Binary Heaps Priority Queues

8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized with respect to other objects of the same type Min Priority Queue: homogeneous collection of Comparables with the following operations (duplicates are allowed). Smaller value means higher priority.  void insert (Comparable x)  void deleteMin( )  Comparable findMin( )  Construct from a set of initial values  boolean isEmpty( )  boolean isFull( )  void makeEmpty( )

8/3/2007 UMBC CSMC 341 PQueue 3 Priority Queue Applications Printer management:  The shorter document on the printer queue, the higher its priority. Jobs queue within an operating system:  Users’ tasks are given priorities. System priority high. Simulations  The time an event “happens” is its priority. Sorting (heap sort)  An elements “value” is its priority.

8/3/2007 UMBC CSMC 341 PQueue 4 Possible Implementations Use a sorted list. Sorted by priority upon insertion.  findMin( ) --> list.front( )  insert( ) --> list.insert( )  deleteMin( )--> list.erase( list.begin( ) ) Use ordinary BST  findMin( ) --> tree.findMin( )  insert( ) --> tree.insert( )  deleteMin( )--> tree.delete( tree.findMin( ) ) Use balanced BST  guaranteed O(lg n) for Red-Black

8/3/2007 UMBC CSMC 341 PQueue 5 Min Binary Heap A min binary heap is a complete binary tree with the further property that at every node neither child is smaller than the value in that node (or equivalently, both children are at least as large as that node). This property is called a partial ordering. As a result of this partial ordering, every path from the root to a leaf visits nodes in a non- decreasing order. What other properties of the Min Binary Heap result from this property?

8/3/2007 UMBC CSMC 341 PQueue 6 Min Binary Heap Performance Performance (n is the number of elements in the heap)  constructionO( n )  findMinO( 1 )  insertO( lg n )  deleteMinO( lg n ) Heap efficiency results, in part, from the implementation  Conceptually a complete binary tree  Implementation in an array/vector (in level order) with the root at index 1

8/3/2007 UMBC CSMC 341 PQueue 7 Min Binary Heap Properties For a node at index i  its left child is at index 2i  its right child is at index 2i+1  its parent is at index  i/2  No pointer storage Fast computation of 2i and  i/2  by bit shifting i << 1 = 2i i >> 1 =  i/2 

8/3/2007 UMBC CSMC 341 PQueue 8 Heap is a Complete Binary Tree

8/3/2007 UMBC CSMC 341 PQueue 9 Which satisfies the properties of a Heap?

8/3/2007 UMBC CSMC 341 PQueue 10 Min BinaryHeap Definition public class BinaryHeap > { public BinaryHeap( ) { /* See online code */ } public BinaryHeap( int capacity ){ /* See online code */ } public BinaryHeap( AnyType [ ] items ){/* Figure 6.14 */ } public void insert( AnyType x ) { /* Figure 6.8 */ } public AnyType findMin( ) { /* TBD */ } public AnyType deleteMin( ) { /* Figure 6.12 */ } public boolean isEmpty( ) { /* See online code */ } public void makeEmpty( ) { /* See online code */ } private static final int DEFAULT_CAPACITY = 10; private int currentSize; // Number of elements in heap private AnyType [ ] array; // The heap array private void percolateDown( int hole ){/* Figure 6.12 */ } private void buildHeap( ) { /* Figure 6.14 */ } private void enlargeArray(int newSize){/* code online */} }

8/3/2007 UMBC CSMC 341 PQueue 11 Min BinaryHeap Implementation public AnyType findMin( ) { if ( isEmpty( ) ) throw Underflow( ); return array[1]; }

8/3/2007 UMBC CSMC 341 PQueue 12 Insert Operation Must maintain  CBT property (heap shape): Easy, just insert new element at “the end” of the array  Min 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 of insert is O( lg n ) in the worst case because the height of a CBT is O( lg n )

8/3/2007 UMBC CSMC 341 PQueue 13 Min BinaryHeap Insert (cont.) /** * Insert into the priority queue, maintaining heap order. * Duplicates are allowed. x the item to insert. */ public void insert( AnyType x ) { if( currentSize == array.length - 1 ) enlargeArray( array.length * ); // Percolate up int hole = ++currentSize; for( ; hole > 1&& x.compareTo(array[hole/2]) < 0; hole/=2 ) array[ hole ] = array[ hole / 2 ]; array[ hole ] = x; }

8/3/2007 UMBC CSMC 341 PQueue 14 Insert 14

8/3/2007 UMBC CSMC 341 PQueue 15 Deletion Operation Steps  Remove min element (the root)  Maintain heap shape  Maintain min heap order To maintain heap shape, actual node removed is “last one” in the array  Replace root value with value from last node and delete last node  Sift-down the new root value Continually exchange value with the smaller child until no child is smaller.

8/3/2007 UMBC CSMC 341 PQueue 16 Min BinaryHeap Deletion(cont.) /** * Remove the smallest item from the priority queue. the smallest item, or throw UnderflowException, if empty. */ public AnyType deleteMin( ) { if( isEmpty( ) ) throw new UnderflowException( ); AnyType minItem = findMin( ); array[ 1 ] = array[ currentSize-- ]; percolateDown( 1 ); return minItem; }

8/3/2007 UMBC CSMC 341 PQueue 17 MinBinaryHeap percolateDown(cont.) /** * Internal method to percolate down in the heap. hole the index at which the percolate begins. */ private void percolateDown( int hole ) { int child; AnyType tmp = array[ hole ]; for( ; hole * 2 <= currentSize; hole = child ){ child = hole * 2; if( child != currentSize && array[ child + 1 ].compareTo( array[ child ] ) < 0 ) child++; if( array[ child ].compareTo( tmp ) < 0 ) array[ hole ] = array[ child ]; else break; } array[ hole ] = tmp; }

8/3/2007 UMBC CSMC 341 PQueue 18 deleteMin

8/3/2007 UMBC CSMC 341 PQueue 19 deleteMin (cont.)

8/3/2007 UMBC CSMC 341 PQueue 20 Constructing a Min BinaryHeap A BH can be constructed in O(n) time. Suppose we are given an array of objects in an arbitrary order. Since it’s an array with no holes, it’s already a CBT. It can be put into heap order in O(n) time.  Create the array and store n elements in it in arbitrary order. O(n) to copy all the objects.  Heapify the array starting in the “middle” and working your way up towards the root for (int index =  n/2  ; index > 0; index--) percolateDown( index );

8/3/2007 UMBC CSMC 341 PQueue 21 Constructing a Min BinaryHeap(cont.) //Construct the binary heap given an array of items. public BinaryHeap( AnyType [ ] items ){ currentSize = items.length; array = (AnyType[]) new Comparable[ (currentSize + 2)*11/10 ]; int i = 1; for( AnyType item : items ) array[ i++ ] = item; buildHeap( ); } // Establish heap order property from an arbitrary // arrangement of items. Runs in linear time. private void buildHeap( ){ for( int i = currentSize / 2; i > 0; i-- ) percolateDown( i ); }

8/3/2007 UMBC CSMC 341 PQueue 22 Performance of Construction A CBT has 2 h-1 nodes 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 = 2 h *0 + 2 h-1 *1 + 2 h-2 *2 + … *h = = h(2 h+1 -1) - ((h-1)2 h+1 +2) = 2 h+1 (h-(h-1))-h-2 = 2 h+1 -h-2

8/3/2007 UMBC CSMC 341 PQueue 23 Performance of Construction (cont.) But 2 h+1 -h-2= O(2 h ) But n = … + 2 h = Therefore, n = O(2 h ) So S = O(n) A heap of n nodes can be built in O(n) time.

8/3/2007 UMBC CSMC 341 PQueue 24 Heap Sort Given n values we can sort them in place in O(n log n) time  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 (high down to low) order. With a max heap, it sorts in normal (low up to high) order. Given an unsorted array A[ ] of size n for (i = n-1; i >= 1; i--) { x = findMin( ); deleteMin( ); A[i+1] = x; }

8/3/2007 UMBC CSMC 341 PQueue 25 Limitations MinBinary heaps support insert, findMin, deleteMin, and construct efficiently. They do not efficiently support the meld or merge operation in which 2 BHs are merged into one. If H 1 and H 2 are of size n 1 and n 2, then the merge is in O(n 1 + n 2 ).

8/3/2007 UMBC CSMC 341 PQueue 26 Leftist Min Heap Supports  findMin-- O( 1 )  deleteMin-- O( lg n )  insert-- O( lg n )  construct-- O( n )  merge-- O( lg n )

8/3/2007 UMBC CSMC 341 PQueue 27 Leftist Tree The null path length, npl(X), of a node, X, is defined as the length of the shortest path from X to a node without two children (a non-full node). Note that npl(NULL) = -1. A Leftist Tree is a binary tree in which at each node X, the null path length of X’s right child is not larger than the null path length of the X’s left child. I.E. the length of the path from X’s right child to its nearest non-full node is not larger than the length of the path from X’s left child to its nearest non-full node. An important property of leftist trees:  At every node, the shortest path to a non-full node is along the rightmost path. “Proof”: Suppose this was not true. Then, at some node the path on the left would be shorter than the path on the right, violating the leftist tree definition.

8/3/2007 UMBC CSMC 341 PQueue 28 Leftist Min Heap A leftist min heap is a leftist tree in which the values in the nodes obey heap order (the tree is partially ordered). Since a LMH 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 MBH  deleteMin -- implemented using meld operation  insert-- implemented using meld operation  construct -- implemented using meld operation

8/3/2007 UMBC CSMC 341 PQueue 29 Merge // Merge rhs into the priority queue. // rhs becomes empty. rhs must be different from this. rhs the other leftist heap. public void merge( LeftistHeap rhs ){ if( this == rhs ) return; // Avoid aliasing problems root = merge( root, rhs.root ); rhs.root = null; } // Internal method to merge two roots. // Deals with deviant cases and calls recursive merge1. private Node merge(Node h1, Node h2 ){ if( h1 == null ) return h2; if( h2 == null ) return h1; if( h1.element.compareTo( h2.element ) < 0 ) return merge1( h1, h2 ); else return merge1( h2, h1 ); }

8/3/2007 UMBC CSMC 341 PQueue 30 Merge (cont.) /** * Internal method to merge two roots. * Assumes trees are not empty, and h1's root contains smallest item. */ private Node merge1( Node h1, Node h2 ) { if( h1.left == null ) // Single node h1.left = h2; // Other fields in h1 already accurate else { h1.right = merge( h1.right, h2 ); if( h1.left.npl < h1.right.npl ) swapChildren( h1 ); h1.npl = h1.right.npl + 1; } return h1; }

8/3/2007 UMBC CSMC 341 PQueue 31 Merge (cont.) Performance:O( lg n )  The rightmost path of each tree has at most  lg(n+1)  nodes. So O( lg n ) nodes will be involved.

8/3/2007 UMBC CSMC 341 PQueue 32

8/3/2007 UMBC CSMC 341 PQueue 33

8/3/2007 UMBC CSMC 341 PQueue 34

8/3/2007 UMBC CSMC 341 PQueue 35 Student Exercise Show the steps needed to merge the Leftist Heaps below. The final result is shown on the next slide

8/3/2007 UMBC CSMC 341 PQueue 36 Student Exercise Final Result

8/3/2007 UMBC CSMC 341 PQueue 37 Min Leftist Heap Operations Other operations implemented using Merge( )  insert (item) Make item into a 1-node LH, X Merge(this, X)  deleteMin Merge(left subtree, right subtree)  construct from N items Make N LHs from the N values, one element in each Merge each in  one at a time (simple, but slow)  use queue and build pairwise (complex but faster)

8/3/2007 UMBC CSMC 341 PQueue 38 LH Construct Algorithm: Make n leftist heaps, H 1 ….H n each with one data value Instantiate Queue q; for (i = 1; i <= n; i++) q.enqueue(H i ); Leftist Heap h = q.dequeue( ); while ( !q.isEmpty( ) ) q.enqueue( merge( h, q.dequeue( ) ) ); h = q.dequeue( );