Module 2: Priority Queues

Similar documents
Module 2: Priority Queues

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

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

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.

Module 4: Priority Queues

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

Sorting and Searching

Priority queues. Priority queues. Priority queue operations

Sorting and Searching

Data Structures and Algorithms

Algorithms and Data Structures

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

Heapsort. Heap data structure

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

Algorithms and Data Structures

Basic Data Structures and Heaps

Heaps Goodrich, Tamassia. Heaps 1

Partha Sarathi Manal

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

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

BM267 - Introduction to Data Structures

Topic: Heaps and priority queues

Heaps. 2/13/2006 Heaps 1

Appendix A and B are also worth reading.

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

Programming II (CS300)

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

Design and Analysis of Algorithms

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

Chapter 6 Heap and Its Application

CSC263 Week 2. Larry Zhang

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

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

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

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

Heaps and Priority Queues

Data Structures and Algorithms

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

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

The Heap Data Structure

Module 8: Range-Searching in Dictionaries for Points

Introduction to Algorithms 3 rd edition

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

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

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?

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

CMSC 341 Lecture 14: Priority Queues, Heaps

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

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

CS2 Algorithms and Data Structures Note 6

Priority Queues. Binary Heaps

CSE 214 Computer Science II Heaps and Priority Queues

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

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

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

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

Lecture: Analysis of Algorithms (CS )

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

Binary Heaps in Dynamic Arrays

CSCI2100B Data Structures Heaps

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

CS711008Z Algorithm Design and Analysis

CS711008Z Algorithm Design and Analysis

CS2 Algorithms and Data Structures Note 6

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

CS165: Priority Queues, Heaps

CS350: Data Structures Heaps and Priority Queues

CH 8. HEAPS AND PRIORITY QUEUES

Describe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods

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

CH. 8 PRIORITY QUEUES AND HEAPS

Properties of a heap (represented by an array A)

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

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

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

Programming II (CS300)

Priority Queues and Binary Heaps

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

CS S-08 Priority Queues Heaps 1

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

9. Heap : Priority Queue

CS 241 Analysis of Algorithms

Priority Queues (Heaps)

CS 10: Problem solving via Object Oriented Programming Winter 2017

CS301 - Data Structures Glossary By

Chapter 6 Heapsort 1

Heapsort. Algorithms.

Heaps, Heapsort, Priority Queues

Data Structures and Algorithms for Engineers

Basic Data Structures (Version 7) Name:

Stores a collection of elements each with an associated key value

Priority queue ADT Heaps. Lecture 21

Tirgul 4. Order statistics. Minimum & Maximum. Order Statistics. Heaps. minimum/maximum Selection. Overview Heapify Build-Heap

CSE 373: Data Structures and Algorithms

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

Stacks, Queues, and Priority Queues. Inf 2B: Heaps and Priority Queues. The PriorityQueue ADT

Transform & Conquer. Presorting

A data structure and associated algorithms, NOT GARBAGE COLLECTION

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

Transcription:

Module 2: Priority Queues CS 240 Data Structures and Data Management T. Biedl K. Lanctot M. Sepehri S. Wild Based on lecture notes by many previous cs240 instructors David R. Cheriton School of Computer Science, University of Waterloo Winter 2018 version 2018-01-15 12:23 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 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 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 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 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 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) Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 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 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 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 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 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 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 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. 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 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 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 i 0 to n 1 do 3. create an item x with key A[i] 4. PQ.insert(x) 5. for i 0 to n 1 do 6. A[n 1 i] key(pq.deletemax()) Note: Run-time depends on how we implement the priority queue. Sometimes written as: O(n + n insert + n deletemax) Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 6 / 23

Realizations of Priority Queues Attempt 1: Use unsorted arrays Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 7 / 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.) Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 7 / 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. Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 7 / 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 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 7 / 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. Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 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 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018

Third Realization: Heaps A (binary) heap is a certain type of binary tree. Recall binary trees: 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. Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 8 / 23

Example Heap 50 29 34 27 15 8 10 23 26 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 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. Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 10 / 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: Height of a heap with n nodes is Θ(log n). Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 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. Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 11 / 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] Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 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 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 0) is A[ i 1 2 ] Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 12 / 23

Heaps in Arrays Navigation It is easy to navigate the heap using this array representation: the root node is A[0] 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 0) is A[ i 1 2 ] Should hide implementation details using helper-functions! Allow to pass node v (identified by array-index) functions root(), parent(v), etc. Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 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 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018

Insertion in Heaps Place the new key at the first free leaf The heap-order property might be violated: perform a bubble-up: Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 13 / 23

Insertion in Heaps Place the new key at the first free leaf The heap-order property might be violated: perform a bubble-up: bubble-up(v) v: a node of the heap 1. while parent(v) exists and key(parent(v)) < key(v) do 2. swap v and parent(v) 3. v parent(v) The new item bubbles up until it reaches its correct place in the heap. Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 13 / 23

Insertion in Heaps Place the new key at the first free leaf The heap-order property might be violated: perform a bubble-up: bubble-up(v) v: a node of the heap 1. while parent(v) exists and key(parent(v)) < key(v) do 2. swap v and parent(v) 3. v parent(v) The new item bubbles up until it reaches its correct place in the heap. Time: O(height of heap) = O(log n). Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 13 / 23

bubble-up example 50 29 34 27 15 8 10 23 26 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 14 / 23

bubble-up example 50 29 34 27 15 8 10 23 26 48 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 14 / 23

bubble-up example 50 29 34 27 48 8 10 23 26 15 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 14 / 23

bubble-up example 50 48 34 27 29 8 10 23 26 15 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 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 bubble-down: Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 15 / 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 bubble-down: bubble-down(v) v: a node of the heap 1. while v is not a leaf do 2. u child of v with largest key 3. if key(u) > key(v) then 4. swap v and u 5. v u 6. else 7. break Time: O(height of heap) = O(log n). Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 15 / 23

bubble-down example 50 48 34 27 29 8 10 23 26 15 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 16 / 23

bubble-down example 15 48 34 27 29 8 10 23 26 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 16 / 23

bubble-down example 48 15 34 27 29 8 10 23 26 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 16 / 23

bubble-down example 48 29 34 27 15 8 10 23 26 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 16 / 23

Priority Queue Realization Using Heaps Store items in priority queue in array A and keep track of size We assume existence of accessor functions root() and last() insert(x) 1. increase size 2. v last() 3. create item with priority x and assign it to v 4. bubble-up(v) deletemax() 1. x key(root()) 2. swap(root(),last()) 3. decrease size 4. bubble-down(root()) 5. return (x) Insert and deletemax: O(log n) Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 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 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018

Sorting using heaps Heaps are one implementation of priority queues. PQ-Sort using Heaps takes O(n log n) time. PQ-SortWithHeaps(A) 1. initialize H to an empty heap 2. for i 0 to n 1 do 3. H.insert(A[i]) 4. for i 0 to n 1 do 5. A[n 1 i] H.deleteMax() Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 18 / 23

Sorting using heaps Heaps are one implementation of priority queues. PQ-Sort using Heaps takes O(n log n) time. PQ-SortWithHeaps(A) 1. initialize H to an empty heap 2. for i 0 to n 1 do 3. H.insert(A[i]) 4. for i 0 to n 1 do 5. A[n 1 i] H.deleteMax() 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 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 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. Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 19 / 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]) Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 19 / 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 bubble-ups Worst-case running time: Θ(n log n). Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 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. Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 20 / 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 bubble-downs instead: heapify(a) A: an array 1. n size(a) 1 2. use A as the array storing the heap 3. // start at parent(last()), which has index (n 2)/2 4. for i (n 2)/2 downto 0 do 5. let v be the node corresponding to array-index i 6. bubble-down(v) Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 20 / 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 bubble-downs instead: heapify(a) A: an array 1. n size(a) 1 2. use A as the array storing the heap 3. // start at parent(last()), which has index (n 2)/2 4. for i (n 2)/2 downto 0 do 5. let v be the node corresponding to array-index i 6. bubble-down(v) A careful analysis yields a worst-case complexity of Θ(n). A heap can be built in linear time. Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 20 / 23

heapify example 10 80 50 30 20 60 10 40 70 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 21 / 23

heapify example 10 80 50 70 20 60 10 40 30 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 21 / 23

heapify example 10 80 50 70 20 60 10 40 30 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 21 / 23

heapify example 10 80 60 70 20 50 10 40 30 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 21 / 23

heapify example 10 80 60 70 20 50 10 40 30 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 21 / 23

heapify example 10 80 60 70 20 50 10 40 30 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 21 / 23

heapify example 80 10 60 70 20 50 10 40 30 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 21 / 23

heapify example 80 70 60 10 20 50 10 40 30 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 21 / 23

heapify example 80 70 60 40 20 50 10 10 30 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 21 / 23

HeapSort Idea: PQ-Sort with heaps. But: Use same input-array A for storing heap. Need heapdeletemax: Same as deletemax except pass array A of heap as parameter HeapSort(A) 1. heapify(a) 2. for i 0 to n 1 do 3. A[n 1 i] heapdeletemax(a) Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 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 Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 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). Biedl, Lanctot, Sepehri, Wild (SCS, UW) CS240 Module 2 Winter 2018 23 / 23