CMSC 341 Lecture 14 Priority Queues & Heaps

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

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)
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.
Heaps, Heap Sort, and Priority Queues
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
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.
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.
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.
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.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
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.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
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.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
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.
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)
Priority Queues A priority queue is an ADT where:
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structures & Algorithms
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues (Heaps)
Binary Heaps Priority Queues
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
CMSC 341 Lecture 13 Leftist Heaps
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
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
Part-D1 Priority Queues
Binary Heaps Priority Queues
Heapsort Heap & Priority Queue.
CSE 373: Data Structures and Algorithms
ITEC 2620M Introduction to Data Structures
Heaps and the Heapsort Heaps and priority queues
Tree Representation Heap.
Binary Heaps Priority Queues
Ch. 8 Priority Queues And Heaps
Heap Sort The Heap Data Structure
CE 221 Data Structures and Algorithms
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Priority Queues & Heaps
Heap code in C++ template <class eType>
CSE 373 Priority queue implementation; Intro to heaps
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Priority Queues & Heaps
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
Priority Queues (Heaps)
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

CMSC 341 Lecture 14 Priority Queues & Heaps Prof. Neary Based on slides from previous iterations of this course (Dr. Gibson)

UMBC CMSC 341 Priority Queues (Heaps) Today’s Topics Priority Queues Abstract Data Type Implementations of Priority Queues: Lists BSTs Heaps Properties Insertion Deletion UMBC CMSC 341 Priority Queues (Heaps)

Priority Queues

UMBC CMSC 341 Priority Queues (Heaps) Priority Queue ADT A priority queue stores a collection of entries Typically, an entry is a pair (key, value) where the key indicates the priority Smaller value, higher priority Keys in a priority queue can be arbitrary objects on which an order is defined UMBC CMSC 341 Priority Queues (Heaps)

Priority Queue vs Queue Priority queue is a specific type of queue Queues are FIFO The element in the queue for the longest time is the first one we take out Priority queues: most important, first out The element in the priority queue with the highest priority is the first one we take out Examples: emergency rooms, airline boarding UMBC CMSC 341 Priority Queues (Heaps)

Implementing Priority Queues Priority queues are an Abstract Data Type They are a concept, and hence there are many different ways to implement them Possible implementations include A sorted list An ordinary BST A balanced BST Run time will vary based on implementation UMBC CMSC 341 Priority Queues (Heaps)

Implementing a Priority Queue

Priority Queue: Sorted List We can implement a priority queue with a sorted list (array, vector, etc.) Sorted by priority upon insertion To find the highest priority, simply find the minimum, in O(1) time findMin() --> list.front() Insertion can take O(n) time, however Could also use an unsorted list – run time??? UMBC CMSC 341 Priority Queues (Heaps)

UMBC CMSC 341 Priority Queues (Heaps) Priority Queue: BST A BST makes a bit more sense than a list Sorted like a regular BST upon insertion To find the minimum, just go to the left call findMin() And removal will be easy, because it will always be a leaf node! Insertion should take no more than O(log n) time call Insert() UMBC CMSC 341 Priority Queues (Heaps)

Priority Queue: BST Downsides Unfortunately, a BST Priority Queue can become unbalanced very easily, and the actual run time will suffer If we have a low priority (high value) instance as our root, nearly everything will be to its left findMin() is now O(n) time  UMBC CMSC 341 Priority Queues (Heaps)

UMBC CMSC 341 Priority Queues (Heaps) Priority Queue: Heap The most common way to implement a priority queue is using a heap A heap is a binary tree (not a BST!!!) that satisfies the “heap condition”: Nodes in the tree are sorted based in relation to their parent’s value, such that if A is a parent node of B, then the key of node A is ordered with respect to the key of node B with the same ordering applying across the heap Additionally, the tree must be complete UMBC CMSC 341 Priority Queues (Heaps)

Heaps

UMBC CMSC 341 Priority Queues (Heaps) Min Binary Heap A min binary heap is a… Complete binary tree Neither child is smaller than the value in the parent Both children are at least as large as the parent In other words, smaller items go above larger ones UMBC CMSC 341 Priority Queues (Heaps)

UMBC CMSC 341 Priority Queues (Heaps) Min Binary Heap This property is called a partial ordering There is no set relation between siblings, cousins, etc. – only that the values grow as we increase our distance from the root As a result of this partial ordering, every path from the root to a leaf visits nodes in a non-decreasing order UMBC CMSC 341 Priority Queues (Heaps)

Min Binary Heap Performance (n is the number of elements in the heap) construction O( n ) findMin() O( 1 ) insert() O( lg n ) deleteMin() O( lg n ) UMBC CMSC 341 Priority Queues (Heaps)

Convert a Heap to an Array Level-order traversal UMBC CMSC 341 Priority Queues (Heaps)

Min Binary Heap Performance Heap efficiency results, in part, from the implementation Conceptually a complete binary tree But implemented by using an array/vector (in level order) with the root at index 1 UMBC CMSC 341 Priority Queues (Heaps)

Min Binary Heap Performance 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⌋ UMBC CMSC 341 Priority Queues (Heaps)

Min Binary Heap: Exercises How to find the parent of E? The left child of D? The right child of A? UMBC CMSC 341 Priority Queues (Heaps)

Building a Heap

UMBC CMSC 341 Priority Queues (Heaps) Insert Operation Must maintain 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 (“percolate up”) Performance of insert is O(log n) in the worst case because the height of a complete binary tree (CBT) is at most log n UMBC CMSC 341 Priority Queues (Heaps)

UMBC CMSC 341 Priority Queues (Heaps) Insert Code void insert( const Comparable & x ) { if( currentSize == array.size( ) - 1 ) { array.resize( array.size( ) * 2 ); } // percolate up int hole = ++currentSize; for( ; hole > 1 && x < array[ hole / 2 ]; hole /= 2 ) // swap, from child to parent array[ hole ] = array[ hole / 2 ]; } array[ hole ] = x; UMBC CMSC 341 Priority Queues (Heaps)

UMBC CMSC 341 Priority Queues (Heaps) Insert Example: 14 UMBC CMSC 341 Priority Queues (Heaps)

UMBC CMSC 341 Priority Queues (Heaps) Delete 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. UMBC CMSC 341 Priority Queues (Heaps)

UMBC CMSC 341 Priority Queues (Heaps) Delete Code void deleteMin( ) { if( isEmpty() ) throw UnderflowException( ); } array[ 1 ] = array[currentSize-1]; array[currentSize-1] = 0; percolateDown( 1 ); UMBC CMSC 341 Priority Queues (Heaps)

Percolate Down Function void percolateDown( int hole ) { int child; Comparable tmp = array[ hole ]; for( ; hole * 2 <= currentSize; hole = child ) child = hole * 2; if( child != currentSize && array[child + 1] < array[child] ) { child++; } if( array[ child ] < tmp ) { array[ hole ] = array[ child ]; } else { break; } } array[ hole ] = tmp; UMBC CMSC 341 Priority Queues (Heaps)

UMBC CMSC 341 Priority Queues (Heaps) Example: Delete Min UMBC CMSC 341 Priority Queues (Heaps)

UMBC CMSC 341 Priority Queues (Heaps) Example: Delete Min UMBC CMSC 341 Priority Queues (Heaps)