CSE373: Data Structures & Algorithms Priority Queues

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

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

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

CSE 373: Data Structures and Algorithms

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

9. Heap : Priority Queue

Administration CSE 326: Data Structures

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

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

CS350: Data Structures Heaps and Priority Queues

Priority Queues Heaps Heapsort

Recall: Properties of B-Trees

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

CMSC 341 Lecture 14: Priority Queues, Heaps

CE 221 Data Structures and Algorithms

Sorting and Searching

Priority Queues. Binary Heaps

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

Data Structures and Algorithms

Heaps, Heap Sort, and Priority Queues.

Binary Heaps. CSE 373 Data Structures Lecture 11

Sorting and Searching

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

CSCI2100B Data Structures Heaps

CSE 373: Data Structures and Algorithms More Asympto<c Analysis; More Heaps

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

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

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

Programming Abstractions

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

Priority Queues Heaps Heapsort

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

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

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

CSE 214 Computer Science II Heaps and Priority Queues

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues. Aaron Bauer Winter 2014

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

CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019

Data Structures Lesson 9

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

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

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

Priority Queues and Heaps

Algorithms and Data Structures

AMORTIZED ANALYSIS. Announcements. CSE 332 Data Abstractions: Priority Queues, Heaps, and a Small Town Barber. Today.

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

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

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014

Programming II (CS300)

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

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

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

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

Module 2: Priority Queues

Programming II (CS300)

Module 2: Priority Queues

Subject : Computer Science. Paper: Data Structures. Module: Priority Queue and Applications. Module No: CS/DS/14

CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees. Kevin Quinn Fall 2015

Figure 1: A complete binary tree.

Figure 1: A complete binary tree.

Operations. Priority Queues & Heaps. Prio-Q as Array. Niave Solution: Prio-Q As Array. Prio-Q as Sorted Array. Prio-Q as Sorted Array

Priority Queues (Heaps)

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

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

Heaps and Priority Queues

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

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

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

Heaps. 2/13/2006 Heaps 1

Priority Queues (Heaps)

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

Chapter 2: Basic Data Structures

Heaps Goodrich, Tamassia. Heaps 1

Heaps. Heaps Priority Queue Revisit HeapSort

CSE 373: Data Structures and Algorithms

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

CS 310: Priority Queues and Binary Heaps

UNIT III BALANCED SEARCH TREES AND INDEXING

Unit #2: Priority Queues

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

Adam Blank Lecture 3 Winter 2017 CSE 332. Data Structures and Parallelism

COMP : Trees. COMP20012 Trees 219

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

Priority Queues and Binary Heaps

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

Properties of a heap (represented by an array A)

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

Priority queues. Priority queues. Priority queue operations

Heap sort. Carlos Moreno uwaterloo.ca EIT

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

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

Data Structures and Algorithms

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

PRIORITY QUEUES AND HEAPS

DATA STRUCTURES AND ALGORITHMS

CSE 373: Data Structures and Algorithms

csci 210: Data Structures Priority Queues and Heaps

Basic Data Structures (Version 7) Name:

CSE373: Data Structures & Algorithms Lecture 5: Dictionaries; Binary Search Trees. Aaron Bauer Winter 2014

Lecture 17 Notes Priority Queues

Transcription:

Priority Queues Hunter Zahn Summer 2016 Summer 2016 1

A Quick Note: Homework 3 out! Start early! Summer 2016 2

A new ADT: Priority Queue Textbook Chapter 6 Nice to see a new and surprising data structure A priority queue holds compare- able data Like diclonaries and unlike stacks and queues, need to compare items Given x and y, is x less than, equal to, or greater than y Meaning of the ordering can depend on your data Many data structures require this: diclonaries, sorlng Integers are comparable, so will use them in examples But the priority queue ADT is much more general Typically two fields, the priority and the data Summer 2016 3

PrioriLes Each item has a priority The lesser item is the one with the greater priority So priority 1 is more important than priority 4 (Just a convenlon, think first is best ) OperaLons: insert deletemin is_empty insert 6 2 15 23 12 18 45 3 7 deletemin Key property: deletemin returns and deletes the item with greatest priority (lowest priority value) Can resolve Les arbitrarily Summer 2016 4

Example insert e1 with priority 5 insert e2 with priority 3 insert e3 with priority 4 a = deletemin // a = e2 b = deletemin // b = e3 insert e4 with priority 2 insert e5 with priority 6 c = deletemin // c = e4 d = deletemin // d = e1 Analogy: insert is like enqueue, deletemin is like dequeue But the whole point is to use prioriles instead of FIFO Summer 2016 5

ApplicaLons Like all good ADTs, the priority queue arises oben SomeLmes blatant, somelmes less obvious Run mullple programs in the operalng system crilcal before interaclve before compute- intensive Maybe let users set priority level Treat hospital palents in order of severity (or triage) Select print jobs in order of decreasing length? Summer 2016 6

More applicalons Greedy algorithms May see an example when we study graphs in a few weeks Forward network packets in order of urgency Select most frequent symbols for data compression (cf. CSE143) SorLng (first insert all, then repeatedly deletemin) Much like Homework 1 uses a stack to implement reverse Summer 2016 7

Finding a good data structure Will show an efficient, non- obvious data structure for this ADT But first let s analyze some obvious ideas for n data items All Lmes worst- case; assume arrays have room data insert algorithm / =me deletemin algorithm / =me unsorted array unsorted linked list sorted array sorted linked list binary search tree AVL tree (our) hash table Summer 2016 8

Need a good data structure! Will show an efficient, non- obvious data structure for this ADT But first let s analyze some obvious ideas for n data items All Lmes worst- case; assume arrays have room data insert algorithm / =me deletemin algorithm / =me unsorted array add at end O(1) search O(n) unsorted linked list add at front O(1) search O(n) sorted array search / shib O(n) stored in reverse O(1) sorted linked list put in right place O(n) remove at front O(1) binary search tree put in right place O(n) lebmost O(n) AVL tree put in right place O(log n) lebmost O(log n) (our) hash table add O(1) iterate over keys O(n) Summer 2016

More on possibililes If prioriles are random, binary search tree will likely do beper O(log n) insert and O(log n) deletemin on average One more idea: if prioriles are 0, 1,, k can use array of lists insert: add to front of list at arr[priority], O(1) deletemin: remove from lowest non- empty list O(k) We are about to see a data structure called a binary heap O(log n) insert and O(log n) deletemin worst- case Possible because we don t support unneeded operalons; no need to maintain a full sort If items arrive in random order, then insert is O(1) on average Summer 2016

Our data structure A binary min- heap (or just binary heap or just heap) is: Structure property: A complete binary tree Heap property: The priority of every (non- root) node is greater than the priority of its parent Not a binary search tree Summer 2016 11

Structure Property: Completeness A Binary Heap is a complete binary tree: A binary tree with all levels full, with a possible exceplon being the bopom level, which is filled leb to right Examples: complete incomplete 20 80 20 80 40 60 85 30 40 400 50 700 are these trees complete? Summer 2016 12

Heap Order Property The priority of every (non- root) node is greater than (or equal to) that of it s parent. Examples: heap not a heap 1 20 80 30 33 20 40 400 40 20 40 800 which of these are heaps? Summer 2016 13

Our data structure A binary min- heap (or just binary heap or just heap) is: Structure property: A complete binary tree Heap property: The priority of every (non- root) node is greater than (or equal to) the priority of its parent Not a binary search tree Summer 2016 14

Our data structure A binary min- heap (or just binary heap or just heap) is: Structure property: A complete binary tree Heap property: The priority of every (non- root) node is greater than (or equal to) the priority of its parent Not a binary search tree 30 20 15 80 450 50 75 1 60 3 8 40 20 50 700 60 85 80 Summer 2016 15

Our data structure A binary min- heap (or just binary heap or just heap) is: Structure property: A complete binary tree Heap property: The priority of every (non- root) node is greater than (or equal to) the priority of its parent Not a binary search tree not a heap 30 20 15 80 450 not a heap 50 75 So: Where is the highest- priority item? What is the height of a heap with n items? 1 60 3 8 a heap 20 40 50 700 60 85 80 Summer 2016 16

OperaLons: basic idea findmin: return root.data deletemin: 1. answer = root.data 2. Move right- most node in last row to root to restore structure property 3. Percolate down to restore heap property insert: 1. Put new node in next posilon on bopom row to restore structure property 2. Percolate up to restore heap property Summer 2016 17 40 20 50 700 60 Overall strategy: 85 80 Preserve structure property Break and restore heap property

DeleteMin 1. Delete (and later return) value at root node 11 7 4 6 5 8 3 Summer 2016

2. Restore the Structure Property We now have a hole at the root 4 3 Need to fill the hole with another value 11 7 6 5 8 When we are done, the tree will have one less node and must slll be complete 7 4 5 8 3 11 6 Summer 2016

3. Restore the Heap Property? 3? 3 4 3 4 4 8 7 5 8 7 5 8 7 5 11 6 11 6 11 6 Percolate down: Keep comparing with both children Swap with lesser child and go down one level What happens if we swap with the larger child? Done if both children are item or reached a leaf node Summer 2016 Why is this correct? What is the run time?

DeleteMin: Run Time Analysis We will percolate down at most (height of heap) Lmes So run Lme is O(height of heap) A heap is a complete binary tree Height of a complete binary tree of n nodes? height = log 2 (n) Run Lme of deletemin is O(log n) Summer 2016

Insert Add a value to the tree 2 Aberwards, structure and heap properles must slll be correct 7 4 5 1 8 Where do we insert the new value? 11 6 Summer 2016

Insert: Maintain the Structure Property There is only one valid tree shape aber we add one more node So put our new data there and then focus on restoring the heap property 4 8 2 1 7 5 11 6 Summer 2016

Maintain the heap property 1 1 2? 1 4 8 4 8 8 11 7 6 5? 2 11 7 6 5? 2 11 7 6 4 5 Percolate up: Put new data in new location If parent larger, swap with parent, and continue Done if parent item or reached root Why is this correct? What is the run time? Summer 2016

Insert: Run Time Analysis Like deletemin, worst- case Lme proporlonal to tree height O(log n) But deletemin needs the last used complete- tree posilon and insert needs the next to use complete- tree posilon If keep a reference to there then insert and deletemin have to adjust that reference: O(log n) in worst case Could calculate how to find it in O(log n) from the root given the size of the heap But it s not easy And then insert is always O(log n), promised O(1) on average (assuming random arrival of items) There s a trick : don t represent complete trees with explicit edges! Summer 2016

Array RepresentaLon of Binary Trees 1 A From node i: 2 3 B 4 D 5 6 E F 8 11 12 H I J K L implicit (array) implementalon: C 7 G leb child: i*2 right child: i*2+1 parent: i/2 (waslng index 0 is convenient for the index arithmelc) A B C D E F G H I J K L 0 1 2 3 4 5 6 7 8 11 12 13 Summer 2016 26

Judging the array implementalon Plusses: Less wasted space Just index 0 and unused space on right In convenlonal tree representalon, one edge per node (except for root), so n- 1 wasted space (like linked lists) Array would waste more space if tree were not complete MulLplying and dividing by 2 is very fast (shib operalons in hardware) Last used posilon is just index size Minuses: Same might- be- empty or might- get- full problems we saw with stacks and queues (resize by doubling as necessary) Plusses outweigh minuses: this is how people do it Summer 2016 27