CS350: Data Structures Heaps and Priority Queues

Similar documents
Data Structures Lesson 9

CE 221 Data Structures and Algorithms

CSCI2100B Data Structures Heaps

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

Data Structures and Algorithms

Binary Heaps. CSE 373 Data Structures Lecture 11

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

Algorithms and Data Structures

Priority Queues. Binary Heaps

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

Priority Queues (Heaps)

CMSC 341 Lecture 14: Priority Queues, Heaps

The priority is indicated by a number, the lower the number - the higher the priority.

Figure 1: A complete binary tree.

CS165: Priority Queues, Heaps

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

Priority Queues (Heaps)

Implementations. Priority Queues. Heaps and Heap Order. The Insert Operation CS206 CS206

Figure 1: A complete binary tree.

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Priority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments

Priority Queues Heaps Heapsort

Priority Queues and Binary Heaps

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

Heap Model. specialized queue required heap (priority queue) provides at least

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Priority Queues Heaps Heapsort

9. Heap : Priority Queue

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Linda Shapiro Spring 2016

Recall: Properties of B-Trees

CS350: Data Structures Red-Black Trees

CS 315 April 1. Goals: Heap (Chapter 6) continued review of Algorithms for Insert DeleteMin. algoritms for decrasekey increasekey Build-heap

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Heaps, Heap Sort, and Priority Queues.

Programming II (CS300)

Design and Analysis of Algorithms

Heaps Goodrich, Tamassia. Heaps 1

CS350: Data Structures Binary Search Trees

COMP 103 RECAP-TODAY. Priority Queues and Heaps. Queues and Priority Queues 3 Queues: Oldest out first

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

Height of a Heap. Heaps. 1. Insertion into a Heap. Heaps and Priority Queues. The insertion algorithm consists of three steps

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

CS350: Data Structures B-Trees

Heaps. 2/13/2006 Heaps 1

CSE 214 Computer Science II Heaps and Priority Queues

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

Programming II (CS300)

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

CS 350 : Data Structures Binary Search Trees

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Nicki Dell Spring 2014

Heapsort. Heap data structure

Sorting and Searching

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Priority Queues and Heaps

CS350: Data Structures AVL Trees

Heaps. Heaps Priority Queue Revisit HeapSort

Priority queues. Priority queues. Priority queue operations

CS 350 : Data Structures B-Trees

Sorting and Searching

CS 240 Fall Mike Lam, Professor. Priority Queues and Heaps

CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues. Ruth Anderson Winter 2019

Heaps. Heaps. A heap is a complete binary tree.

CMSC 341. Binary Heaps. Priority Queues

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

COSC160: Data Structures Heaps. Jeremy Bolton, PhD Assistant Teaching Professor

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

COMP : Trees. COMP20012 Trees 219

CSE373: Data Structures & Algorithms Priority Queues

CMSC 341 Leftist Heaps

The heap is essentially an array-based binary tree with either the biggest or smallest element at the root.

Trees & Tree-Based Data Structures. Part 4: Heaps. Definition. Example. Properties. Example Min-Heap. Definition

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci2000, Bowdoin College

1 Tree Sort LECTURE 4. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

UNIT III BALANCED SEARCH TREES AND INDEXING

CSE 373: Data Structures and Algorithms

CS350: Data Structures Dijkstra s Shortest Path Alg.

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Priority Queues and Heaps. Heaps of fun, for everyone!

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT

CMSC 341 Lecture 15 Leftist Heaps

Data Structures Week #8. Heaps (Priority Queues)

Heaps and Priority Queues

CMSC 341 Lecture 15 Leftist Heaps

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

Properties of a heap (represented by an array A)

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Administration CSE 326: Data Structures

Heap: A binary heap is a complete binary tree in which each, node other than root is smaller than its parent. Heap example: Fig 1. NPTEL IIT Guwahati

CS 10: Problem solving via Object Oriented Programming. Priori9zing 2

Heaps in C. CHAN Hou Pong, Ken CSCI2100 Data Structures Tutorial 7

Data Structures Question Bank Multiple Choice

Transcription:

Heaps and Priority Queues James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola

Priority Queue An abstract data type of a queue that associates a priority with each of the elements inserted Elements are enqueued with some priority Elements are dequeued in priority order - Highest priority elements are dequeued first 2

Binary Heaps Great for an implementation of a priority queue Similar in structure to a binary search tree Sorting of elements in a heap is much weaker than in a BST, however it is sufficient to implement a priority queue A binary heap can be either a Min Heap or a Max Heap - Min Heap - smallest key values have highest priority - Max Heap - largest key values have highest priority 3

Complete Binary Tree A heap is implemented as a complete binary tree which can be stored efficiently in an array A complete binary tree has the following properties: - Tree is filled in level-order from left to right - Tree has the maximum number of nodes at every level except for possibly the bottom level - There are no holes allowed in the tree 4

Complete Binary Tree A D B E F C G This IS a complete binary tree H I J K L A B C D E F G This IS NOT a complete binary tree H I J K L N O 5

Complete Binary Tree Using a complete binary tree to represent a heap makes traversing the heap easy The complete binary tree can easily be stored in an array A B C D E F G H I J K L left child = 2 * i right child = 2 * i + 1 A B C D E F G H I J K L 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 parent = i / 2 6

Binary Heap Properties Just as with all other data structures, there are properties that must be maintained for the binary heap Elements in the heap must maintain the heap-order property - Heap-order property (for a Min Heap): In a heap, for every node X with parent P, the key in P is less than or equal to the key in X (i.e. the parent s key is less than a child s key) P X P X 7

Binary Heap Operations: insert The basic idea for insert: - Create a hole (an empty node) in the next available complete tree location - If the new element can be inserted into the hole without violating the heap-order property, then insert the new element into the hole - If inserting the new element into the hole will violate the heap-order property, then move the hole up the heap until a location is found where the new element can be inserted without violating the heaporder property This operation is called percolateup 8

Binary Heap Operations: percolateup Select the node that is to be percolated up the heap Compare the node with its parent If the node is less than it s parent, then swap the node with its parent Compare the node to its new parent Continue moving the node up the tree until the node is greater than or equal to its parent node (i.e. it s parent is smaller) 9

Binary Heap Operations: insert Example Insert node 14 First create a hole in the next available heap location 13 21 16 24 31 19 68 65 26 32 Can 14 be inserted into new hole without violating heap the property? 10

Binary Heap Operations: insert Example Insert node 14 Swap the hole with its parent to move it up the heap 13 21 16 24 19 68 65 26 32 31 Can 14 be inserted into this location without violating heap the property? 11

Binary Heap Operations: insert Example Insert node 14 Continually swap the hole with its parent to move the hole up the heap until a valid location is found to insert the new node 13 This is typically referred to as percolateup 16 24 21 19 68 65 26 32 31 Can 14 be inserted into this location without violating heap the property? 12

Binary Heap Operations: insert Example Insert node 14 Done with insertion 13 14 16 24 21 19 68 65 26 32 31 Insert node 14 into this location Done with insertion since (14 > 13) 13

Binary Heap Operations: insert Time required to do insertion could be as much as O(log N) if the value getting inserting is the new minimum value in the heap - A newly inserted value that is the minimum value must percolate all the way up the tree 14

Binary Heap Operations: deletemin The basic idea for deletemin: - Delete the root node of the heap (will be the minimum node) - Create a hole in the location where the root node was... this hole will eventually be filled with the last node in the heap (the rightmost node on the bottom level) - Move the hole down the tree until a location is found that permits the last node in the heap to get moved while still maintaining the heaporder property This operation is called percolatedown 15

Binary Heap Operations: percolatedown Select the node that is to be percolated down the heap Compare the node the lesser of its two children If the node is greater than the lesser of its two children, then swap the node with that child Compare the node to its new children Continue moving the node down the tree until the node is less than or equal to both of its children 16

Binary Heap Operations: deletemin Example Delete the minimum node The minimum node is node 13 13 In a minheap, the minimum node will always be at the top of the heap 14 16 19 21 19 68 65 26 32 31 17

Binary Heap Operations: deletemin Example Delete the minimum node A hole is created where the minimum value was removed The size of the heap decreases After node 13 is removed, a hole is created in its place 14 16 19 21 19 68 65 26 32 31 Because the size of the heap is decremented after a node is deleted, the last node in the heap is removed. Must find a new location to store the value that was in the last node 18

Binary Heap Operations: deletemin Example Delete the minimum node Swap the hole with the smaller of its children to move it down the heap Move the hole down the tree 14 Node 14 < Node 16 16 19 21 19 68 65 26 32 31 Can the 31 we re trying to place be moved to this location without violating the heap property? 19

Binary Heap Operations: deletemin Example Delete the minimum node Continually swap the hole with its smaller child to move the hole down the heap until a valid location is found to place the last value 14 This is typically referred to as percolatedown 19 16 21 19 68 65 26 32 31 Can the 31 we re trying to place be moved to this location without violating the heap property? 20

Binary Heap Operations: deletemin Example Delete the minimum node Continually swap the hole with its smaller child to move the hole down the heap until a valid location is found to place the last value 14 This is typically referred to as percolatedown 19 16 26 21 19 68 65 32 31 Can the 31 we re trying to place be moved to this location without violating the heap property? 21

Binary Heap Operations: deletemin Example Delete the minimum node Once a location is found for the 31, fill the hole with that value 14 19 16 26 21 19 68 65 31 32 Fill the hole with the value that was last in the heap prior to the deletemin operation 22

Binary Heap Operations: buildheap The buildheap operation takes a tree that is not in heap-order and puts it into heap-order Idea: - Call percolatedown on all non-leaf nodes in reverse level-order No need to call percolatedown on leaf nodes since they cannot be moved down The first non-leaf node is located in the array index currentsize/2 Easily implemented by iteratively visiting each node in the heap array in reverse order starting at the first non-leaf node 23

Binary Heap Operations: buildheap Example Fixing the heap order No need to call percolatedown on the leaf nodes 1 92 2 3 47 21 4 5 6 7 20 12 45 63 8 9 10 11 12 13 14 15 61 17 55 37 25 64 83 73 24

Binary Heap Operations: buildheap Example Heap size is 15, so first non-leaf node is 15/2 = 7 Processing node at array index 7 1 92 2 3 47 21 Compare with lesser of children, no swap needed 4 5 6 7 20 12 45 63 8 9 10 11 12 13 14 15 61 17 55 37 25 64 83 73 25

Binary Heap Operations: buildheap Example Processing node at array index 6 1 92 2 3 47 21 4 5 6 7 20 12 45 63 8 9 10 11 12 13 14 15 61 17 55 37 25 64 83 73 Compare with lesser of children, need to swap 26

Binary Heap Operations: buildheap Example 1 92 2 3 47 21 4 5 6 7 20 12 25 63 8 9 10 11 12 13 14 15 61 17 55 37 45 64 83 73 Swapped with child 27

Binary Heap Operations: buildheap Example Processing node at array index 5 1 92 2 3 47 21 4 5 6 7 20 12 25 63 8 9 10 11 12 13 14 15 61 17 55 37 45 64 83 73 Compare with lesser of children, no swap needed 28

Binary Heap Operations: buildheap Example Processing node at array index 4 1 92 2 3 47 21 4 5 6 7 20 12 25 63 8 9 10 11 12 13 14 15 61 17 55 37 45 64 83 73 Compare with lesser of children, need to swap 29

Binary Heap Operations: buildheap Example 1 92 2 3 47 21 4 5 6 7 17 12 25 63 8 9 10 11 12 13 14 15 61 20 55 37 45 64 83 73 Swapped with child 30

Binary Heap Operations: buildheap Example Processing node at array index 3 1 92 2 3 47 21 Compare with lesser of children, no swap needed 4 5 6 7 17 12 25 63 8 9 10 11 12 13 14 15 61 20 55 37 45 64 83 73 31

Binary Heap Operations: buildheap Example Processing node at array index 2 Compare with lesser of children, need to swap 17 12 1 92 2 3 47 21 4 5 6 7 8 9 10 11 12 13 14 15 25 63 61 20 55 37 45 64 83 73 32

Binary Heap Operations: buildheap Example 1 92 2 3 12 21 Swapped with child 4 5 6 7 17 47 25 63 8 9 10 11 12 13 14 15 61 20 55 37 45 64 83 73 33

Binary Heap Operations: buildheap Example 1 92 2 3 12 21 4 5 6 7 17 47 25 63 8 9 10 11 12 13 14 15 61 20 55 37 45 64 83 73 Continue to compare with lesser of children, Need another swap 34

Binary Heap Operations: buildheap Example 1 92 2 3 12 21 4 5 6 7 17 37 25 63 8 9 10 11 12 13 14 15 61 20 55 47 45 64 83 73 Swapped with child 35

Binary Heap Operations: buildheap Example Processing node at array index 1 1 92 Compare with lesser of children, need to swap 2 3 12 21 4 5 6 7 17 37 25 63 8 9 10 11 12 13 14 15 61 20 55 47 45 64 83 73 36

Binary Heap Operations: buildheap Example 1 Swapped with child 12 2 3 92 21 4 5 6 7 17 37 25 63 8 9 10 11 12 13 14 15 61 20 55 47 45 64 83 73 37

Binary Heap Operations: buildheap Example Continue to compare with lesser of children, Need another swap 17 37 1 12 2 3 92 21 4 5 6 7 8 9 10 11 12 13 14 15 25 63 61 20 55 47 45 64 83 73 38

Binary Heap Operations: buildheap Example 1 12 Swapped with child 2 3 17 21 4 5 6 7 92 37 25 63 8 9 10 11 12 13 14 15 61 20 55 47 45 64 83 73 39

Binary Heap Operations: buildheap Example 1 12 2 3 17 21 4 5 6 7 92 37 25 63 8 9 10 11 12 13 14 15 61 20 55 47 45 64 83 73 Continue to compare with lesser of children, Need another swap 40

Binary Heap Operations: buildheap Example 1 12 2 3 17 21 4 5 6 7 20 37 25 63 8 9 10 11 12 13 14 15 61 92 55 47 45 64 83 73 Swapped with child 41

Binary Heap Operations: buildheap Example Fixing the heap order Now in heap order 1 12 2 3 17 21 4 5 6 7 20 37 25 63 8 9 10 11 12 13 14 15 61 92 55 47 45 64 83 73 42