CSE 373 Data Structures and Algorithms. Lecture 15: Priority Queues (Heaps) III

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

Examination Questions Midterm 2

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

Priority Queues (Heaps)

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

Algorithms and Data Structures

Lower Bound on Comparison-based Sorting

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

Sorting and Searching

CS350: Data Structures Heaps and Priority Queues

Sorting and Searching

CE 221 Data Structures and Algorithms

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

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

Priority Queues (Heaps)

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

Programming II (CS300)

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

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

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

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

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

Priority Queues Heaps Heapsort

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Binary Heaps. CSE 373 Data Structures Lecture 11

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

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

Data Structures Lesson 9

Heaps, Heap Sort, and Priority Queues.

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

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

Tables, Priority Queues, Heaps

Priority Queues and Heaps

CSCI2100B Data Structures Heaps

Heap sort. Carlos Moreno uwaterloo.ca EIT

CSC 1700 Analysis of Algorithms: Heaps

CSC263 Week 2. Larry Zhang

07 B: Sorting II. CS1102S: Data Structures and Algorithms. Martin Henz. March 5, Generated on Friday 5 th March, 2010, 08:31

Heaps and Priority Queues

Data Structures and Algorithms

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

COMP Data Structures

BM267 - Introduction to Data Structures

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

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

Priority queues. Priority queues. Priority queue operations

Priority Queues. Binary Heaps

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

COMP : Trees. COMP20012 Trees 219

Heaps and Priority Queues

Heaps and Priority Queues

Design and Analysis of Algorithms

Today s Outline. The One Page Cheat Sheet. Simplifying Recurrences. Set Notation. Set Notation. Priority Queues. O( 2 n ) O( n 3 )

CSE 373 Autumn 2010: Midterm #2 (closed book, closed notes, NO calculators allowed)

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

Programming II (CS300)

Recall: Properties of B-Trees

Chapter 6 Heapsort 1

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

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSC 8301 Design and Analysis of Algorithms: Heaps

COS 226 Algorithms and Data Structures Fall Midterm Solutions

CS165: Priority Queues, Heaps

Draw the resulting binary search tree. Be sure to show intermediate steps for partial credit (in case your final tree is incorrect).

PRIORITY QUEUES AND HEAPS

Appendix A and B are also worth reading.

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

Priority Queues. 04/10/03 Lecture 22 1

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

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

HEAPS & PRIORITY QUEUES

Sorting (I) Hwansoo Han

Priority Queues. Chapter 9

Lecture 13: AVL Trees and Binary Heaps

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

Introduction. e.g., the item could be an entire block of information about a student, while the search key might be only the student's name

Building Java Programs

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

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

Course Review for Finals. Cpt S 223 Fall 2008

Heap sort. Carlos Moreno uwaterloo.ca EIT

Tables and Priority Queues

COS 226 Algorithms and Data Structures Fall Midterm

Comparison Based Sorting Algorithms. Algorithms and Data Structures: Lower Bounds for Sorting. Comparison Based Sorting Algorithms

Priority Queues Heaps Heapsort

CSE 373: Data Structures and Algorithms

11/8/2016. Chapter 7 Sorting. Introduction. Introduction. Sorting Algorithms. Sorting. Sorting

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

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

Algorithms and Data Structures: Lower Bounds for Sorting. ADS: lect 7 slide 1

Binary heaps (chapters ) Leftist heaps

University of Waterloo CS240, Winter 2010 Assignment 2

Midterm solutions. n f 3 (n) = 3

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

Lecture: Analysis of Algorithms (CS )

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

COS 226 Midterm Exam, Spring 2009

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

COMP 250 Fall priority queues, heaps 1 Nov. 9, 2018

Module 2: Priority Queues

Transcription:

CSE 373 Data Structures and Algorithms Lecture 15: Priority Queues (Heaps) III

Generic Collections

Generics and arrays public class Foo<T> { private T myfield; // ok } public void method1(t param) { myfield = new T(); T[] a = new T[10]; } // error // error You cannot create objects or arrays of a parameterized type. Why not? 3

Generics/arrays, fixed public class Foo<T> { private T myfield; // ok } public void method1(t param) { myfield = param; T[] a2 = (T[])(new Object[10]); } // ok // ok But you can declare variables of that type, accept them as parameters, return them, or create arrays by casting Object[]. 4

The compareto method The standard way for a Java class to define a comparison function for its objects is to define a compareto method. Example: in the String class, there is a method: public int compareto(string other) A call of A.compareTo(B) will return: a value < 0 if A comes "before" B a value > 0 if A comes "after" B or 0 if A and B are "equal 5

Comparable public interface Comparable<E> { public int compareto(e other); } A class can implement the Comparable interface to define a natural ordering function for its objects. A call to the compareto method should return: a value < 0 if the other object comes "before" this one a value > 0 if the other object comes "after" this one or 0 if the other object is considered "equal" to this 6

Comparable template public class name implements Comparable<name> {... } public int compareto(name other) {... } Exercise: Add a compareto method to the PrintJob class such that PrintJobs are ordered according to their priority (ascending lower priorities are more important than higher ones). 7

Comparable example public class PrintJob implements Comparable<PrintJob> { private String user; private int number; private int priority; public PrintJob(int number, String user, int priority) { this.number = number; this.user = user; this.priority = priority; } public int compareto(printjob otherjob) { return priority - otherjob.priority; } } public String tostring() { return number + " (" + user + "):" + priority; } 8

d-heaps

Generalization: d-heaps Each node has d children Still can be represented by array Good choices for d are a power of 2 Only because multiplying and dividing by powers of 2 is fast on a computer How does height compare to binary heap? 4 1 1 2 7 3 8 5 12 11 10 6 9 2 7 3 4 8 5 12 11 10 6 9 10

Operations on d-heap insert: runtime = depth of tree decreases, Θ(log d n) remove: runtime = bubbledown requires more comparisons to find min, Θ(d log d n) Does this help insert or remove more? 11

Other Priority Queue Operations

More Min-Heap Operations decreasepriority: reduce the priority value of an element in the queue Solution: change priority and increasepriority: increase the priority value of an element in the queue Solution: change priority and How do we find the element in the queue? What about duplicates? Need a reference to the element! 13

More Min-Heap Operations remove: given a reference to an object in the queue, remove the object from the queue Solution: set priority to negative infinity, percolate up to root and deletemin findmax Solution: Can look at all leaves, but not really the point of a min-heap! 14

Building a Heap Given a list of numbers, how would you build a heap? At every point, the new item may need to percolate all the way through the heap Adding the items one at a time is Θ(n log n) in the worst case A more sophisticated algorithm does it in Θ(n) 15

O(N) buildheap First, add all elements arbitrarily maintaining the completeness property Then fix the heap order property by performing a "bubble down" operation on every node that is not a leaf, starting from the rightmost internal node and working back to the root 45 6 21 18 14 18 14 60 32 6 21 60 32 45 16

buildheap practice problem Each element in the list [12, 5, 11, 3, 10, 6, 9, 4, 8, 1, 7, 2] has been inserted into a heap such that the completeness property has been maintained. Now, fix the heap's order property by "bubbling down" every internal node. 12 5 11 3 10 6 9 4 8 1 7 2 17

12 12 5 11 5 11 3 10 2 9 3 1 2 9 4 8 1 7 6 4 8 10 7 6 12 12 5 2 1 2 3 1 6 9 3 5 6 9 4 8 10 7 11 4 8 10 7 11 18

Final State of the Heap 1 3 2 4 5 6 9 12 8 10 7 11 19

Different Heaps Successive inserts Θ(n log n): 1 buildheap Θ(n): 1 3 2 3 2 5 4 6 9 4 5 6 9 12 8 10 7 11 12 8 10 7 11 But it doesn't maker because they are both heaps. 20

Heap Sort

Heap sort heap sort: an algorithm to sort an array of N elements by turning the array into a heap, then doing a remove N times The elements will come out in sorted order! What is the runtime? This algorithm is not very space-efficient. Why not? 22

Improved heap sort The heap sort shown requires a second array We can use a max-heap to implement an improved version of heap sort that needs no extra storage Useful on low-memory devices Still only O(n log n) runtime Elegant 23

Improved heap sort 1 Use an array heap, but with 0 as the root index max-heap state after buildheap operation: 24

Improved heap sort 2 State after one remove operation: Modified remove that moves element to end 25

Improved heap sort 3 State after two remove operations: Notice that the largest elements are at the end (becoming sorted!) 26

Sorting algorithms review Best case Average case ( ) Worst case Bubble sort n n 2 n 2 Selection sort n 2 n 2 n 2 Insertion sort n n 2 n 2 Mergesort n log 2 n n log 2 n n log 2 n Heapsort n log 2 n n log 2 n n log 2 n Quicksort n log 2 n n log 2 n n 2 According to Knuth, the average growth rate of Insertion sort is about 0.9 times that of Selection sort and about 0.4 times that of Bubble Sort. The average growth rate of Quicksort is about 0.74 times that of Mergesort and about 0.5 times that of Heapsort. 27