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

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

Module 2: Priority Queues

Module 2: Priority Queues

Module 2: Priority Queues

Algorithms and Data Structures

Algorithms, Spring 2014, CSE, OSU Lecture 2: Sorting

Appendix A and B are also worth reading.

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

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

Sorting and Searching

Sorting and Searching

Heapsort. Heap data structure

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

CSC263 Week 2. Larry Zhang

Basic Data Structures and Heaps

Algorithms and Data Structures

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

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

Programming II (CS300)

Partha Sarathi Manal

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

Topic: Heaps and priority queues

BM267 - Introduction to Data Structures

Lecture: Analysis of Algorithms (CS )

Heaps and Priority Queues

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

Design and Analysis of Algorithms

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

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

Priority Queues. Priority Queues Trees and Heaps Representations of Heaps Algorithms on Heaps Building a Heap Heapsort.

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

Chapter 6 Heap and Its Application

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

Introduction to Algorithms 3 rd edition

Priority Queues (Heaps)

The Heap Data Structure

Data Structures and Algorithms

CSE 214 Computer Science II Heaps and Priority Queues

Heaps. 2/13/2006 Heaps 1

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

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

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

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Heaps Goodrich, Tamassia. Heaps 1

Outline. Computer Science 331. Heap Shape. Binary Heaps. Heap Sort. Insertion Deletion. Mike Jacobson. HeapSort Priority Queues.

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

Next. 1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

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

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

Priority queues. Priority queues. Priority queue operations

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

CS2 Algorithms and Data Structures Note 6

Priority queue ADT Heaps. Lecture 21

Priority Queues. Binary Heaps

9. Heap : Priority Queue

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

CS2 Algorithms and Data Structures Note 6

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

CS165: Priority Queues, Heaps

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

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

CS 241 Analysis of Algorithms

Module 4: Priority Queues

Binary Heaps in Dynamic Arrays

CMSC 341 Lecture 14: Priority Queues, Heaps

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

Recall: Properties of B-Trees

CS350: Data Structures Heaps and Priority Queues

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

Basic Data Structures (Version 7) Name:

1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

Heaps, Heap Sort, and Priority Queues.

CS301 - Data Structures Glossary By

Heaps, Heapsort, Priority Queues

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn

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

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

403: Algorithms and Data Structures. Heaps. Fall 2016 UAlbany Computer Science. Some slides borrowed by David Luebke

Properties of a heap (represented by an array A)

CSE332: Data Abstractions Lecture 4: Priority Queues; Heaps. James Fogarty Winter 2012

Priority Queues and Binary Heaps

Data Structures and Algorithms for Engineers

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

A data structure and associated algorithms, NOT GARBAGE COLLECTION

csci 210: Data Structures Priority Queues and Heaps

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

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

Programming II (CS300)

Priority Queues Heaps Heapsort

CSCI2100B Data Structures Heaps

Heaps. A complete binary tree can be easily stored in an array - place the root in position 1 (for convenience)

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Heapsort. Algorithms.

Heaps Outline and Required Reading: Heaps ( 7.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

CH 8. HEAPS AND PRIORITY QUEUES

Lecture 5: Sorting Part A

CS S-08 Priority Queues Heaps 1

Chapter 6 Heapsort 1

CH. 8 PRIORITY QUEUES AND HEAPS

Transcription:

CS 240 Data Structures and Data Management Module 2: Priority Queues A. Biniaz A. Jamshidpey É. Schost Based on lecture notes by many previous cs240 instructors David R. Cheriton School of Computer Science, University of Waterloo Spring 2018 References: Sedgewick 9.1-9.4 version 2018-05-07 15:07 Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 1 / 23

Outline 1 Priority Queues Abstract Data Types ADT Priority Queue Binary Heaps Operations in Binary Heaps PQ-Sort and Heapsort Intro for the Selection Problem Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018

Outline 1 Priority Queues Abstract Data Types ADT Priority Queue Binary Heaps Operations in Binary Heaps PQ-Sort and Heapsort Intro for the Selection Problem Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018

Abstract Data Types Abstract Data Type (ADT): A description of information and a collection of operations on that information. The information is accessed only through the operations. We can have various realizations of an ADT, which specify: How the information is stored (data structure) How the operations are performed (algorithms) Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 2 / 23

Stack ADT Stack: an ADT consisting of a collection of items with operations: push: inserting an item pop: removing the most recently inserted item Items are removed in LIFO (last-in first-out) order. We can have extra operations: size, isempty, and top Applications: Addresses of recently visited sites in a Web browser, procedure calls Realizations of Stack ADT using arrays using linked lists Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 3 / 23

Queue ADT Queue: an ADT consisting of a collection of items with operations: enqueue: inserting an item dequeue: removing the least recently inserted item Items are removed in FIFO (first-in first-out) order. Items enter the queue at the rear and are removed from the front. We can have extra operations: size, isempty, and front Realizations of Queue ADT using (circular) arrays using linked lists Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 4 / 23

Outline 1 Priority Queues Abstract Data Types ADT Priority Queue Binary Heaps Operations in Binary Heaps PQ-Sort and Heapsort Intro for the Selection Problem Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018

Priority Queue ADT Priority Queue: An ADT consisting of a collection of items (each having a priority) with operations insert: inserting an item tagged with a priority deletemax: removing the item of highest priority deletemax is also called extractmax or getmax. The priority is also called key. The above definition is for a maximum-oriented priority queue. A minimum-oriented priority queue is defined in the natural way, by replacing the operation deletemax by deletemin. Applications: typical todo list, simulation systems, sorting Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 5 / 23

Using a Priority Queue to Sort PQ-Sort(A[0..n 1]) 1. initialize PQ to an empty priority queue 2. for k 0 to n 1 do 3. PQ.insert(A[k]) 4. for k n 1 down to 0 do 5. A[k] PQ.deleteMax() Note: Run-time depends on how we implement the priority queue. Sometimes written as: O(n + n insert + n deletemax) Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 6 / 23

Realizations of Priority Queues Attempt 1: Use unsorted arrays insert: O(1) deletemax: O(n) Note: We assume dynamic arrays, i. e., expand by doubling as needed. (Amortized over all insertions this takes O(1) extra time.) Using unsorted linked lists is identical. This realization used for sorting yields selection sort. Attempt 2: Use sorted arrays insert: O(n) deletemax: O(1) Using sorted linked-lists is identical. This realization used for sorting yields insertion sort. Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 7 / 23

Outline 1 Priority Queues Abstract Data Types ADT Priority Queue Binary Heaps Operations in Binary Heaps PQ-Sort and Heapsort Intro for the Selection Problem Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018

Third Realization: Heaps A (binary) heap is a certain type of binary tree. You should know: A binary tree is either empty, or consists of three parts: a node and two binary trees (left subtree and right subtree). Terminology: root, leaf, parent, child, level, sibling, ancestor, descendant, etc. Any binary tree with n nodes has height at least log(n + 1) 1 Ω(log n). Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 8 / 23

Example Heap 50 29 34 27 15 8 10 23 26 ( In our examples we only show the priorities, and we show them directly in ) the node. A more accurate picture would be priority = 50, <other info> Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 9 / 23

Heaps Definition A max-heap is a binary tree with the following two properties: 1 Structural Property: All the levels of a heap are completely filled, except (possibly) for the last level. The filled items in the last level are left-justified. 2 Heap-order Property: For any node i, the key of parent of i is larger than or equal to key of i. A min-heap is the same, but with opposite order property. Lemma: The height of a heap with n nodes is Θ(log n). Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 10 / 23

Storing Heaps in Arrays Heaps should not be stored as binary trees! Let H be a heap of n items and let A be an array of size n. Store root in A[0] and continue with elements level-by-level from top to bottom, in each level left-to-right. A[0] 50 A[1] 29 34 A[2] A[3] 27 15 A[4] A[5] 8 10 A[6] A[7] 23 26 A[8] Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 11 / 23

Heaps in Arrays Navigation It is easy to navigate the heap using this array representation: the root node is A[0] The textbook puts it at A[1] instead. This gives prettier formulas but more complicated heapsort code. the left child of A[i] (if it exists) is A[2i + 1], the right child of A[i] (if it exists) is A[2i + 2], the parent of A[i] (i 1) is A[ i 1 2 ] the last node is A[n 1] Should hide implementation details using helper-functions! functions root(), parent(i), last(n), etc. Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 12 / 23

Outline 1 Priority Queues Abstract Data Types ADT Priority Queue Binary Heaps Operations in Binary Heaps PQ-Sort and Heapsort Intro for the Selection Problem Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018

Insertion in Heaps Place the new key at the first free leaf The heap-order property might be violated: perform a fix-up: fix-up(a, k) k: an index corresponding to a node of the heap 1. while parent(k) exists and A[parent(k)] < A[k] do 2. swap A[k] and A[parent(k)] 3. k parent(k) The new item bubbles up until it reaches its correct place in the heap. Time: O(height of heap) = O(log n). Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 13 / 23

fix-up example 50 29 34 27 15 8 10 23 26 Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 14 / 23

deletemax in Heaps The maximum item of a heap is just the root node. We replace root by the last leaf (last leaf is taken out). The heap-order property might be violated: perform a fix-down: fix-down(a, n, k) A: an array that stores a heap of size n k: an index corresponding to a node of the heap 1. while k is not a leaf do 2. // Find the child with the larger key 3. j left child of k 4. if (j is not last(n) and A[j + 1] > A[j]) 5. j j + 1 6. if A[k] A[j] break 7. swap A[j] and A[k] 8. k j Time: O(height of heap) = O(log n). Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 15 / 23

fix-down example 50 48 34 27 29 8 10 23 26 15 Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 16 / 23

Priority Queue Realization Using Heaps Store items in priority queue in array A and keep track of size insert(x) 1. increase size 2. l last(size) 3. A[l] x 4. fix-up(a, l) deletemax() 1. l last(size) 2. swap A[root()] and A[l] 3. decrease size 4. fix-down(a,size,root()) 5. return (A[l]) insert and deletemax: O(log n) Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 17 / 23

Outline 1 Priority Queues Abstract Data Types ADT Priority Queue Binary Heaps Operations in Binary Heaps PQ-Sort and Heapsort Intro for the Selection Problem Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018

Sorting using heaps Recall: Any priority queue can be used to sort in time O(n + n insert + n deletemax) Using the binary-heaps implementation of PQs, we obtain: PQ-SortWithHeaps(A) 1. initialize H to an empty heap 2. for k 0 to n 1 do 3. H.insert(A[k]) 4. for k n 1 down to 0 do 5. A[k] H.deleteMax() both operations run in O(log n) time for heaps PQ-Sort using heaps takes O(n log n) time. Can improve this with two simple tricks: 1 Heaps can be built faster if we know all input in advance. 2 Can use the same array for input and heap. O(1) additional space! Heapsort Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 18 / 23

Building Heaps by Bubble-up Problem statement: Given n items (in A[0 n 1]) build a heap containing all of them. Solution 1: Start with an empty heap and insert items one at a time: simpleheapbuilding(a) A: an array 1. initialize H as an empty heap 2. for i 0 to size(a) 1 do 3. H.insert(A[i]) This corresponds to doing fix-ups Worst-case running time: Θ(n log n). Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 19 / 23

Building Heaps by Bubble-down Problem statement: Given n items (in A[0 n 1]) build a heap containing all of them. Solution 2: Using fix-downs instead: heapify(a) A: an array 1. n A.size() 2. for i parent(last(n)) downto 0 do 3. fix-down(a, n, i) A careful analysis yields a worst-case complexity of Θ(n). A heap can be built in linear time. Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 20 / 23

heapify example 10 80 50 30 20 60 10 40 70 Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 21 / 23

HeapSort Idea: PQ-Sort with heaps. But: Use same input-array A for storing heap. HeapSort(A, n) 1. // heapify 2. n A.size() 3. for i parent(last(n)) downto 0 do 4. fix-down(a, n, i) 5. // repeatedly find maximum 6. while n > 1 7. // do deletemax 8. swap items at A[root()] and A[last(n)]) 9. decrease n 10. fix-down(a, n, root()) The for-loop takes Θ(n) time and the while-loop takes O(n log n) time. Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 22 / 23

Outline 1 Priority Queues Abstract Data Types ADT Priority Queue Binary Heaps Operations in Binary Heaps PQ-Sort and Heapsort Intro for the Selection Problem Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018

Selection Problem Statement: The kth-max problem asks to find the kth largest item in an array A of n numbers. Solution 1: Make k passes through the array, deleting the maximum number each time. Complexity: Θ(kn). Solution 2: First sort the numbers. Then return the kth largest number. Complexity: Θ(n log n). Solution 3: Scan the array and maintain the k largest numbers seen so far in a min-heap Complexity: Θ(n log k). Solution 4: Make a max-heap by calling heapify(a). Call deletemax(a) k times. Complexity: Θ(n + k log n). Biniaz, Jamshidpey, Schost (SCS, UW) CS240 Module 2 Spring 2018 23 / 23