Heaps and Priority Queues

Similar documents
Heaps and Priority Queues

Heaps and Priority Queues

BM267 - Introduction to Data Structures

Sorting and Searching

Sorting and Searching

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

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

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

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

CH 8. HEAPS AND PRIORITY QUEUES

CH. 8 PRIORITY QUEUES AND HEAPS

Heapsort. Heap data structure

Heapsort. Why study Heapsort?

CS165: Priority Queues, Heaps

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

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

9. Heap : Priority Queue

Priority queues. Priority queues. Priority queue operations

Heaps Goodrich, Tamassia. Heaps 1

Stores a collection of elements each with an associated key value

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

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

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

CS350: Data Structures Heaps and Priority Queues

Binary heaps (chapters ) Leftist heaps

Data Structures and Algorithms for Engineers

Module 2: Priority Queues

Programming II (CS300)

Basic Data Structures (Version 7) Name:

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

Partha Sarathi Manal

Module 2: Priority Queues

Properties of a heap (represented by an array A)

Data Structures and Algorithms

Algorithms and Data Structures

Module 2: Priority Queues

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

Heaps. Heaps Priority Queue Revisit HeapSort

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

CS 240 Data Structures and Data Management. Module 2: Priority Queues

Tables and Priority Queues

Heaps & Priority Queues. (Walls & Mirrors - Remainder of Chapter 11)

CS106X Programming Abstractions in C++ Dr. Cynthia Bailey Lee

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used

CS 240 Data Structures and Data Management. Module 2: Priority Queues

Balanced Search Trees

Lecture 7. Transform-and-Conquer

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

ADT Priority Queue. Heaps. A Heap Implementation of the ADT Priority Queue. Heapsort

Tables, Priority Queues, Heaps

Data Structures and Algorithms

Topic: Heaps and priority queues

COS 226 Midterm Exam, Spring 2009

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

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

DATA STRUCTURES AND ALGORITHMS

Priority Queues and Binary Heaps

Priority Queues Heaps Heapsort

Binary Heaps in Dynamic Arrays

Algorithms and Data Structures

Programming II (CS300)

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

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

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

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

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

Data Structures Lesson 9

CSCI2100B Data Structures Heaps

Priority Queues. Reading: 7.1, 7.2

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Adding a Node to (Min) Heap. Lecture16: Heap Sort. Priority Queue Sort. Delete a Node from (Min) Heap. Step 1: Add node at the end

Data Structures and Algorithms

Figure 1: A complete binary tree.

Priority Queues Heaps Heapsort

CMSC 341 Lecture 14: Priority Queues, Heaps

Module 2: Classical Algorithm Design Techniques

Examination Questions Midterm 2

Recall: Properties of B-Trees

Priority Queues. T. M. Murali. January 23, T. M. Murali January 23, 2008 Priority Queues

Priority Queues. T. M. Murali. January 29, 2009

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

COMP Data Structures

Partha Sarathi Mandal

CSE 214 Computer Science II Heaps and Priority Queues

CS 10: Problem solving via Object Oriented Programming Winter 2017

Data Structures and Algorithms (DSA) Course 12 Trees & sort. Iulian Năstac

Collection of priority-job pairs; priorities are comparable.

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

Priority Queues. Binary Heaps

Chapter 6 Heap and Its Application

quiz heapsort intuition overview Is an algorithm with a worst-case time complexity in O(n) data structures and algorithms lecture 3

Binary Heaps. CSE 373 Data Structures Lecture 11

EST Solutions. Ans 1(a): KMP Algorithm for Preprocessing: KMP Algorithm for Searching:

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

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

COMP250: Priority queue ADT, Heaps. Lecture 23 Jérôme Waldispühl School of Computer Science McGill University

Heaps. 2/13/2006 Heaps 1

csci 210: Data Structures Priority Queues and Heaps

Heaps and Priority Queues

Transcription:

Unit 9, Part Heaps and Priority Queues Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Priority Queue A priority queue (PQ) is a collection in which each item has an associated number known as a priority. ("Ann Cudd", ), ("Robert Brown", 1), ("Dave Sullivan", ) use a higher priority for items that are "more important" Example application: scheduling a shared resource like the CPU give some processes/applications a higher priority, so that they will be scheduled first and/or more often Key operations: insert: add an item (with a position based on its priority) remove: remove the item with the highest priority One way to implement a PQ efficiently is using a type of binary tree known as a heap.

Complete Binary Trees A binary tree of height h is complete if: levels 0 through h 1 are fully occupied there are no gaps to the left of a node in level h Complete: Not complete ( = missing node): Representing a Complete Binary Tree A complete binary tree has a simple array representation. The nodes of the tree are stored in the array in the order in which they would be visited by a level-order traversal (i.e., top to bottom, left to right). a[1] a[0] a[2] a[] a[4] Examples: 17 14 2 4 1 2 17 2 4 1 2 14

Navigating a Complete Binary Tree in Array Form The root node is in a[0] Given the node in a[i]: its left child is in a[2*i + 1] its right child is in a[2*i + 2] its parent is in a[(i - 1)/2] (using integer division) a[0] a[1] a[2] a[] a[4] a[] a[6] Examples: the left child of the node in a[1] is in a[2*1 + 1] = a[] the left child of the node in a[2] is in a[2*2 + 1] = a[] the right child of the node in a[] is in a[2* + 2] = a[] the right child of the node in a[2] is in a[2*2 + 2] = a[6] the parent of the node in a[4] is in a[(4-1)/2] = a[1] the parent of the node in a[7] is in a[(7-1)/2] = a[] a[7] a[] What is the left child of 24? Assume that the following array represents a complete tree: 0 1 2 4 6 7 2 24 1 2 47 9

Heaps Heap: a complete binary tree in which each interior node is greater than or equal to its children Examples: 2 1 2 7 7 The largest value is always at the root of the tree. The smallest value can be in any leaf node - there s no guarantee about which one it will be. We're using max-at-top heaps. can also have a min-at-top heap, with every interior node <= its children Which of these is a heap? A. 2 B. 1 C. 2 7 1 7 2 D. more than one (which ones?) E. none of them

Removing the Largest Item from a Heap Remove and return the item in the root node. In addition, we need to move the largest remaining item to the root, while maintaining a complete tree with each node >= children Algorithm: 1. make a copy of the largest item 2. move the last item in the heap to the root. sift down the new root item until it is >= its children (or it s a leaf) 4. return the largest item 2 sift down the : Sifting Down an Item To sift down item x (i.e., the item whose key is x): 1. compare x with the larger of the item s children, y 2. if x < y, swap x and y and repeat Other examples: sift down the : 1 7 1 7 6 6 sift down the 7: 7 2 1 1

Inserting an Item in a Heap Algorithm: 1. put the item in the next available slot (grow array if needed) 2. sift up the new item until it is <= its parent (or it becomes the root item) Example: insert put it in place: sift it up: Converting an Arbitrary Array to a Heap To convert an array (call it contents) with n items to a heap: 1. start with the parent of the last element: contents[i], where i = ((n 1) 1)/2 = (n 2)/2 2. sift down contents[i] and all elements to its left Example: 0 1 2 4 6 14 1 14 1 Last element s parent = contents[(7 2)/2] = contents[2]. Sift it down: 14 1 14 1

Converting an Array to a Heap (cont.) Next, sift down contents[1]: 14 1 14 1 Finally, sift down contents[0]: 14 1 14 1 14 1 Time Complexity of a Heap 14 1 A heap containing n items has a height <= log 2 n. Why? Thus, removal and insertion are both O(log n). remove: go down at most log 2 n levels when sifting down; do a constant number of operations per level insert: go up at most log 2 n levels when sifting up; do a constant number of operations per level This means we can use a heap for a O(log n)-time priority queue.

Using a Heap for a Priority Queue Recall: a priority queue (PQ) is a collection in which each item has an associated number known as a priority. ("Ann Cudd", ), ("Robert Brown", 1), ("Dave Sullivan", ) use a higher priority for items that are "more important" To implement a PQ using a heap: order the items in the heap according to their priorities every item in the heap will have a priority >= its children the highest priority item will be in the root node get the highest priority item by calling heap.remove()! Using a Heap to Sort an Array Recall selection sort: it repeatedly finds the smallest remaining element and swaps it into place: 0 1 2 4 6 14 1 0 1 2 4 6 1 14 0 1 2 4 6 1 14 It isn t efficient, because it performs a linear scan to find the smallest remaining element (O(n) steps per scan). Heapsort is a sorting algorithm that repeatedly finds the largest remaining element and puts it in place. It is efficient, because it turns the array into a heap. it can find/remove the largest remaining in O(log n) steps!

Pseudocode: Heapsort heapsort(arr) { // Turn the array into a max-at-top heap. heap = new Heap(arr); endunsorted = arr.length - 1; while (endunsorted > 0) { // Get the largest remaining element and put it // at the end of the unsorted portion of the array. largestremaining = heap.remove(); arr[endunsorted] = largestremaining; } } endunsorted--; Sort the following array: Heapsort Example Here s the corresponding complete tree: 1 0 1 2 4 6 1 6 4 22 6 4 22 Begin by converting it to a heap:

Heapsort Example (cont.) Here s the heap in both tree and array forms: 4 22 0 1 2 4 6 4 22 6 1 endunsorted: 6 6 1 Remove the largest item and put it in place: remove() copies 4; moves to root 4 22 remove() sifts down ; returns 4 22 heapsort() puts 4 in place; decrements endunsorted 1 22 1 6 1 6 6 toremove: 4 0 1 2 4 6 22 1 6 endunsorted: 6 largestremaining: 4 0 1 2 4 6 22 1 6 4 endunsorted: copy 22; move to root 22 1 Heapsort Example (cont.) sift down ; return 22 1 put 22 in place 1 6 6 6 copy 1; move to root toremove: 22 1 sift down ; return 1 0 1 2 4 6 1 6 4 endunsorted: largestremaining: 22 6 put 1 in place 0 1 2 4 6 1 6 22 4 endunsorted: 4 6 6 toremove: 1 0 1 2 4 6 6 22 4 endunsorted: 4 largestremaining: 1 0 1 2 4 6 6 1 22 4 endunsorted:

copy ; move to root 6 Heapsort Example (cont.) sift down ; return 6 put in place 6 copy 6; move to root toremove: 6 sift down ; return 6 0 1 2 4 6 6 1 22 4 endunsorted: largestremaining: put 6 in place 0 1 2 4 6 6 1 22 4 endunsorted: 2 toremove: 6 0 1 2 4 6 1 22 4 endunsorted: 2 largestremaining: 6 0 1 2 4 6 6 1 22 4 endunsorted: 1 copy ; move to root Heapsort Example (cont.) sift down ; return put in place toremove: 0 1 2 4 6 6 1 22 4 endunsorted: 1 largestremaining: 0 1 2 4 6 6 1 22 4 endunsorted: 0

Time Complexity of Heapsort 14 1 Time complexity of creating a heap from an array? Time complexity of sorting the array? How Does Heapsort Compare? algorithm best case avg case worst case extra memory selection sort O(n 2 ) O(n 2 ) O(n 2 ) O(1) insertion sort O(n) O(n 2 ) O(n 2 ) O(1) Shell sort O(n log n) O(n 1. ) O(n 1. ) O(1) bubble sort O(n 2 ) O(n 2 ) O(n 2 ) O(1) quicksort O(n log n) O(n log n) O(n 2 ) O(1) mergesort O(n log n) O(n log n) O(nlog n) O(n) heapsort O(n log n) O(n log n) O(nlog n) O(1) Heapsort matches mergesort for the best worst-case time complexity, but it has better space complexity. Insertion sort is still best for arrays that are almost sorted. heapsort will scramble an almost sorted array before sorting it Quicksort is still typically fastest in the average case.