Priority Queues (Heaps)

Slides:



Advertisements
Similar presentations
DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
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.
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.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
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
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
CS 315 Lecture 18 Nov 15 Goals Complete heap operations review insert, deletemin decreaseKey, increaseKey heap building Heap sorting Some applications.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
§3 Binary Heap 1. Structure Property: 【 Definition 】 A binary tree with n nodes and height h is complete iff its nodes correspond to the nodes numbered.
Chapter 21 Binary Heap.
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.
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Priority Queues (Heaps)
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
8 January Heap Sort CSE 2011 Winter Heap Sort Consider a priority queue with n items implemented by means of a heap  the space used is.
ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΡΧΕΙΩΝ
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.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
1 Priority Queues (Heaps)  Sections 6.1 to Priority Queues  Regular queues which supports –First In, First Out –Enqueue(): add a new element.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
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)
"Teachers open the door, but you must enter by yourself. "
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues (Heaps)
Priority Queues and Heaps
October 30th – Priority QUeues
Source: Muangsin / Weiss
Heaps, Heap Sort, and Priority Queues
Binary Heaps What is a Binary Heap?
ADT Heap data structure
Heaps, Heap Sort, and Priority Queues
CMSC 341: Data Structures Priority Queues – Binary Heaps
Binary Heaps Priority Queues
Data Structures & Algorithms Priority Queues & HeapSort
CMSC 341 Lecture 14 Priority Queues & Heaps
Priority Queue and Binary Heap Neil Tang 02/12/2008
Heaps and the Heapsort Heaps and priority queues
Binary Heaps Priority Queues
Hassan Khosravi / Geoffrey Tien
CE 221 Data Structures and Algorithms
Heap Sort CSE 2011 Winter January 2019.
CS Data Structure: Heaps.
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
Fundamental Structures of Computer Science II
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
Data Structures and Algorithm Analysis Priority Queues (Heaps)
CSE 373: Data Structures and Algorithms
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
Priority Queues (Heaps)
Presentation transcript:

Priority Queues (Heaps)

Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted order. Often we collect a set of items and process the one with the current minimum value. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments An appropriate data structure is called a priority queue.

Definition A priority queue is a data structure that supports two basic operations: insert a new item and remove the minimum item. deleteMin insert Priority Queue

Simple Implementations A simple linked list: Insertion at the front (O(1)); find minimum (O(N)), or Keep list sorted; insertion O(N), findMin O(1) A binary search tree: This gives an O(log N) average for both operations. But BST class supports a lot of operations that are not required. Self-balancing BSTs O(log N) worst for both operations. An array: Binary Heap Does not require links and will support both operations in O(logN) wost-case time. findMin in O(1) at worst.

Binary Heap The binary heap is the classic method used to implement priority queues. We use the term heap to refer to the binary heap. Heap is different from the term heap used in dynamic memory allocation. Heap has two properties: Structure property Ordering property

Structure Property A heap is a complete binary tree, represented as an array. A complete binary tree is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.

Properties of a complete binary tree A complete binary tree of height h has between 2h and 2h+1 – 1 nodes The height of a complete binary tree is log N. It can be implemented as an array such that: For any element in array position i : the left child is in position 2i, the right child is in the cell after the left child (2i + 1), and the parent is in position  i/2 .

Figure 21.1 A complete binary tree and its array representation

Heap-Order Property In a heap, for every node X with parent P, the key in P is smaller than or equal to the key in X. Thus the minimum element is always at the root. Thus we get the extra operation findMin in constant time. A max heap supports access of the maximum element instead of the minimum, by changing the heap property slightly.

Figure 21.3 Two complete trees: (a) a heap; (b) not a heap

Binary Heap Class template <class Comparable> class BinaryHeap { public: BinaryHeap( int capacity = 100 ); bool isEmpty( ) const; const Comparable & findMin( ) const; void insert( const Comparable & x ); void deleteMin( ); void deleteMin( Comparable & minItem ); void makeEmpty( ); private: int theSize; // Number of elements in heap vector<Comparable> array; // The heap array void buildHeap( ); void percolateDown( int hole ); };

Basic Heap Operations: Insert To insert an element X into the heap: We create a hole in the next available location. If X can be placed there without violating the heap property, then we do so and are done. Otherwise we bubble up the hole toward the root by sliding the element in the hole’s parent down. We continue this until X can be placed in the hole. This general strategy is known as a percolate up.

Figure 21.7 Attempt to insert 14, creating the hole and bubbling the hole up

Figure 21.8 The remaining two steps required to insert 14 in the original heap shown in Figure 21.7

Insert procedure // Insert item x into the priority queue, maintaining heap order. // Duplicates are allowed. template <class Comparable> void BinaryHeap<Comparable>::insert( const Comparable & x ) { array[ 0 ] = x; // initialize sentinel if( theSize + 1 == array.size( ) ) array.resize( array.size( ) * 2 + 1 ); // Percolate up int hole = ++theSize; for( ; x < array[ hole / 2 ]; hole /= 2 ) array[ hole ] = array[ hole / 2 ]; array[ hole ] = x; }

Delete Minimum deleteMin is handled in a similar manner as insertion: Remove the minimum; a hole is created at the root. The last element X must move somewhere in the heap. If X can be placed in the hole then we are done. Otherwise, We slide the smaller of the hole’s children into the hole, thus pushing the hole one level down. We repeat this until X can be placed in the hole. deleteMin is logarithmic in both the worst and average cases.

Figure 21.10 Creation of the hole at the root

Figure 21.11 The next two steps in the deleteMin operation

Figure 21.12 The last two steps in the deleteMin operation

deleteMin procedure // Remove the smallest item from the priority queue. // Throw UnderflowException if empty. template <class Comparable> void BinaryHeap<Comparable>::deleteMin( ) { if( isEmpty( ) ) throw UnderflowException( ); array[ 1 ] = array[ theSize-- ]; percolateDown( 1 ); }

// Internal method to percolate down in the heap. // hole is the index at which the percolate begins. template <class Comparable> void BinaryHeap<Comparable>::percolateDown( int hole ) { int child; Comparable tmp = array[ hole ]; for( ; hole * 2 <= theSize; hole = child ) child = hole * 2; if( child != theSize && array[child + 1] < array[child]) child++; if( array[ child ] < tmp ) array[ hole ] = array[ child ]; else break; } array[ hole ] = tmp;

Building a Heap Take as input N items and place them into an empty heap. Obviously this can be done with N successive inserts: O(NlogN) worst case. However buildHeap operation can be done in linear time (O(N)) by applying a percolate down routine to nodes in reverse level order.

buildHeap method // Establish heap-order property from an arbitrary // arrangement of items. Runs in linear time. template <class Comparable> void BinaryHeap<Comparable>::buildHeap( ) { for( int i = theSize / 2; i > 0; i-- ) percolateDown( i ); }

After percolatedown(7) Figure 21.17 Implementation of the linear-time buildHeap method Initial complete tree After percolatedown(7) CENG 213 Data Structures

Figure 21.18 (a) After percolateDown(6); (b) after percolateDown(5)

Figure 21.19 (a) After percolateDown(4); (b) after percolateDown(3)

Figure 21.20 (a) After percolateDown(2); (b) after percolateDown(1) and buildHeap terminates

Analysis of buildHeap The linear time bound of buildHeap, can be shown by computing the sum of the heights of all the nodes in the heap, which is the maximum number of dashed lines. For the perfect binary tree of height h containing N = 2h+1 – 1 nodes, the sum of the heights of the nodes is N – H – 1. Thus it is O(N). Please see this for a cool derivation of O(N) http://www.cs.umd.edu/~meesh/351/mount/lectures/lect14-heapsort-analysis-part.pdf

Heapsort The priority queue can be used to sort N items by inserting every item into a binary heap and extracting every item by calling deleteMin N times, thus sorting the result. An algorithm based on this idea is heapsort. It is an O(N logN) worst-case sorting algorithm.

Heapsort The main problem with this algorithm is that it uses an extra array for the items exiting the heap. We can avoid this problem as follows: After each deleteMin, the heap shrinks by 1. Thus the cell that was last in the heap can be used to store the element that was just deleted. Using this strategy, after the last deleteMin, the array will contain all elements in decreasing order. If we want them in increasing order we must use a max heap.

Figure 21.25 Max heap after the buildHeap phase for the input sequence 59,36,58,21,41,97,31,16,26,53

Figure 21.26 Heap after the first deleteMax operation

Figure 21.27 Heap after the second deleteMax operation

Implementation In the implementation of heapsort, the ADT BinaryHeap is not used. Everything is done in an array. The root is stored in position 0. Thus there are some minor changes in the code: Since we use max heap, the logic of comparisons is changed from > to <. For a node in position i, the parent is in (i-1)/2, the left child is in 2i+1 and right child is next to left child. Percolating down needs the current heap size which is lowered by 1 at every deletion.

The heapsort routine /** * Standard heapsort. */ template <class Comparable> void heapsort( vector<Comparable> & a ) { for( int i = a.size( ) / 2; i >= 0; i-- ) /* buildHeap */ percDown( a, i, a.size( ) ); for( int j = a.size( ) - 1; j > 0; j-- ) swap( a[ 0 ], a[ j ] ); /* deleteMax */ percDown( a, 0, j ); }

Analysis of Heapsort It is an O(N log N) algorithm. First phase: Build heap O(N) Second phase: N deleteMax operations: O(NlogN). Detailed analysis shows that, the average case for heapsort is poorer than quick sort. Quicksort’s worst case however is far worse. An average case analysis of heapsort is very complicated, but empirical studies show that there is little difference between the average and worst cases. Heapsort usually takes about twice as long as quicksort. Heapsort therefore should be regarded as something of an insurance policy: On average, it is more costly, but it avoids the possibility of O(N2).

Heap vs. BST

k largest elements in an array Run outer loop of Bubble Sort (ascending) k times Print the last k elements O(nk) for n-size input array Similarly, run outer loop of Selection Sort (descending) k times Print the first k elements O(nk) Similarly, run Quick Sort (descending) O(nlogn)  Find kth largest in O(n) using order statistic algorithm Use Quick Sort partition() algo around the kth largest number O(n)

k largest elements in an array Build a max heap Call deleteMax k times O(n + klogn) Build a min heap (MH) of the first k elements (A[0..k-1]) Compare each element after the kth element (A[k..n-1]) w/ MH.root If element > MH.root, MH.root = element and percolateDown(1) In the end, MH has k largest elements, e.g., root is the kth largest O(k + (n-k)logk)

Check if given array is min heap

Convert max heap to min heap in O(n) Trick question: same as convert normal array to a min heap Call buildHeap() O(n)

Merge M sorted lists Naïve solution is to put them all in an array and sort it: O(nlogn) Cool solution is to use a min heap Build a min heap (MH) of size M using the first elements of each list deleteMin from MH and insert next element from same list as deleted elmnt O(NlogM)

Stack using a max heap Assign priorities, i.e., push time, to the elements being pushed. O(logn) for push and pop.

Queue using a min heap Just use a min heap with the same priority, time, as previous slide. O(logn) for push and pop.