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

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

CSE373: Data Structures & Algorithms Priority Queues

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

Administration CSE 326: Data Structures

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

CSE 373: Data Structures and Algorithms

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

Sorting and Searching

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Sorting and Searching

Recall: Properties of B-Trees

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

9. Heap : Priority Queue

Data Structures and Algorithms

CMSC 341 Lecture 14: Priority Queues, Heaps

Binary Heaps. CSE 373 Data Structures Lecture 11

CS350: Data Structures Heaps and Priority Queues

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

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

Priority Queues and Binary Heaps

Priority Queues Heaps Heapsort

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

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

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

CSCI2100B Data Structures Heaps

CE 221 Data Structures and Algorithms

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

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

Module 2: Priority Queues

Module 2: Priority Queues

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

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

Algorithms and Data Structures

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

Heaps, Heap Sort, and Priority Queues.

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

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

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

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

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

Priority Queues. Binary Heaps

Programming Abstractions

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

Priority Queues and Heaps

Data Structures Lesson 9

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

Priority Queues Heaps Heapsort

Section 4 SOLUTION: AVL Trees & B-Trees

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

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

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

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

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

CSE 214 Computer Science II Heaps and Priority Queues

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

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

Figure 1: A complete binary tree.

UNIT III BALANCED SEARCH TREES AND INDEXING

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

Priority Queues (Heaps)

Figure 1: A complete binary tree.

Priority Queues (Heaps)

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

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Heaps and Priority Queues

Data structures. Priority queues, binary heaps. Dr. Alex Gerdes DIT961 - VT 2018

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

Heaps. Heaps Priority Queue Revisit HeapSort

Heap sort. Carlos Moreno uwaterloo.ca EIT

CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees. Linda Shapiro Spring 2016

CS301 - Data Structures Glossary By

Unit #2: Priority Queues

Programming II (CS300)

COMP : Trees. COMP20012 Trees 219

CSE 332, Spring 2010, Midterm Examination 30 April 2010

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

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

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

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

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

CompSci 201 Tree Traversals & Heaps

Lecture 13: AVL Trees and Binary Heaps

CSE 332: Data Structures & Parallelism Lecture 10:Hashing. Ruth Anderson Autumn 2018

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

Course Review. Cpt S 223 Fall 2009

Data Structures and Algorithms

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

Heaps. 2/13/2006 Heaps 1

Programming II (CS300)

CSE 373: Data Structures and Algorithms

Lecture Notes on Priority Queues

CS S-08 Priority Queues Heaps 1

Basic Data Structures (Version 7) Name:

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

Trees. A tree is a directed graph with the property

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

CS 310: Priority Queues and Binary Heaps

Chapter 6 Heapsort 1

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

Transcription:

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

Today Finish up Intro to Asymptotic Analysis New ADT! Priority Queues 1/11/201 2

Scenario What is the difference between waiting for service at a pharmacy versus an ER? Pharmacies usually follow the rule First Come, First Served Emergency Rooms assign priorities based on each individual's need 1/11/201 3

A new ADT: Priority Queue Textbook Chapter 6 We will go back to binary search trees (ch4) and hash tables (ch5) later Nice to see a new and surprising data structure first A priority queue holds compare-able data Unlike stacks and queues need to compare items Given x and y, is x less than, equal to, or greater than y What this means can depend on your data Much of course will require comparable data: e.g. sorting 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 1/11/201 5

Priority Queue ADT Assume 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 convention, could also do a maximum priority Main Operations: insert deletemin insert 6 2 15 23 12 18 45 3 7 deletemin Key property: deletemin returns and deletes from the queue the item with greatest priority (lowest priority value) Can resolve ties arbitrarily 1/11/201 6

Aside: We will use ints as data and priority For simplicity in lecture, we ll often suppose items are just ints and the int is also the priority So an operation sequence could be insert 6 insert 5 x = deletemin // Now x = 5. int priorities are common, but really just need comparable Not having other data is very rare Example: print job has a priority and the file to print is the data 1/11/201 7

Priority Queue Example To simplify our examples, we will just use the priority values from now on insert a with priority 5 insert b with priority 3 insert c with priority 4 w = deletemin x = deletemin insert d with priority 2 insert e with priority 6 y = deletemin z = deletemin after execution: Analogy: insert is like enqueue, deletemin is like dequeue But the whole point is to use priorities instead of FIFO 1/11/201 8

Priority Queue Example To simplify our examples, we will just use the priority values from now on insert a with priority 5 insert b with priority 3 insert c with priority 4 w = deletemin x = deletemin insert d with priority 2 insert e with priority 6 y = deletemin z = deletemin after execution: w = b x = c y = d z = a Analogy: insert is like enqueue, deletemin is like dequeue But the whole point is to use priorities instead of FIFO 1/11/201

Applications Like all good ADTs, the priority queue arises often Sometimes directly, sometimes less obvious Run multiple programs in the operating system critical before interactive before compute-intensive Maybe let users set priority level Treat hospital patients in order of severity (or triage) Select print jobs in order of decreasing length? Forward network packets in order of urgency Select most frequent symbols for data compression (cf. CSE143) Sort: insert all, then repeatedly deletemin 1/11/201 10

More applications Greedy algorithms Select the best-looking choice at the moment Will see an example when we study graphs in a few weeks Discrete event simulation (system modeling, virtual worlds, ) Simulate how state changes when events fire Each event e happens at some time t and generates new events e1,, en at times t+t1,, t+tn Naïve approach: advance clock by 1 unit at a time and process any events that happen then Better: Pending events in a priority queue (priority = time happens) Repeatedly: deletemin and then insert new events Effectively, set clock ahead to next event 1/11/201 11

Preliminary Implementations of Priority Queue ADT insert deletemin Unsorted Array Unsorted Linked-List Sorted Circular Array Sorted Linked-List Binary Search Tree (BST) Notes: Worst case, Assume arrays have enough space 1/11/201 12

Aside: More on possibilities Note: If priorities are inserted in random order, binary search tree will likely do better than O(n) O(log n) insert and O(log n) deletemin on average Could get same performance from a balanced binary search tree (e.g. AVL tree we will study later) One more idea: if priorities 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) 1/11/201 14

Our Data Structure: The Heap The Heap: Worst case: O(log n) for insert Worst case: O(log n) for deletemin If items arrive in random order, then the average-case of insert is O(1) Very good constant factors Key idea: Only pay for functionality needed We need something better than scanning unsorted items But we do not need to maintain a full sorted list We will visualize our heap as a tree, so we need to review some tree terminology 1/11/201 15

Q: Reviewing Some Tree Terminology root(t): leaves(t): children(b): parent(h): siblings(e): ancestors(f): descendents(g): subtree(g): A B D E F H J K L Tree T C G I M N 1/11/201 16

Q: Some More Tree Terminology depth(b): height(g): height(t): degree(b): branching factor(t): D B E A F H Tree T C G I J K L M N 1/11/201 17

Types of Trees Binary tree: n-ary tree: Perfect tree: Complete tree: Every node has 2 children Every node has n children Every row is completely full All rows except possibly the bottom are completely full, and it is filled from left to right Perfect Tree Complete Tree 1/11/201 20

Some Basic Tree Properties Nodes in a perfect binary tree of height h? Leaf nodes in a perfect binary tree of height h? Height of a perfect binary tree with n nodes? Height of a complete binary tree with n nodes? 1/11/201 21

Properties of a Binary Min-Heap More commonly known as a binary heap or simply a heap Structure Property: A complete [binary] tree Heap Property: Every non-root node has a priority value larger than (or possibly equal to) the priority of its parent How is this different from a binary search tree? 1/11/201 23

Properties of a Binary Min-Heap More commonly known as a binary heap or simply a heap Structure Property: A complete [binary] tree Heap Order Property: Every non-root node has a priority value larger than (or possibly equal to) the priority of its parent A Heap 10 Not a Heap 30 20 80 20 80 40 60 85 13 25 50 700 1/11/201 24

Properties of a Binary Min-Heap Where is the minimum priority item? What is the height of a heap with n items? A Heap 10 20 80 40 60 85 50 700 1/11/201 25

Heap Operations findmin: deletemin: percolate down. insert(val): percolate up. 10 20 80 40 60 85 50 700 65 1/11/201 27

Operations: 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 order property insert: 1. Put new node in next position on bottom row to restore structure property 2. Percolate up to restore heap order property 40 20 50 700 60 Overall strategy: 10 85 80 Preserve complete tree structure property This may break heap order property Percolate to restore heap order property 1/11/201 28

DeleteMin Implementation 1. Delete value at root node (and store it for later return) 2. There is now a "hole" at the root. We must "fill" the hole with another value, must have a tree with one less node, and it must still be a complete tree 3. The "last" node is the is obvious choice, but now the heap order property is violated 4. We percolate down to fix the heap order: While greater than either child 11 11 7 7 4 5 6 10 10 4 5 6 10 8 8 3 3 Swap with smaller child 1/11/201 2

Percolate Down? 10 3? 10 3 4 3 4 4 8 7 5 8 7 5 8 7 5 10 11 6 11 6 11 6 Percolate down: Keep comparing with both children Move smaller child up and go down one level Done if both children are item or reached a leaf node Why does this work? What is the run time? 1/11/201 30

DeleteMin: Run Time Analysis Run time 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 time of deletemin is O(log n) 1/11/201 31

Insert Add a value to the tree 2 Structure and heap order properties must still be correct afterwards 1 4 8 7 5 10 11 6 1/11/201 32

Insert: Maintain the Structure Property There is only one valid tree shape after we add one more node! 2 1 So put our new data there and then focus on restoring the heap order property 7 4 5 10 8 11 6 1/11/201 33

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

A Clever Trick for Storing the Heap Clearly, insert and deletemin are worst-case O(log n) But we promised average-case O(1) insert (how??) Insert requires access to the next to use position in the tree Walking the tree from root to leaf requires O(log n) steps Insert and Deletemin would have to update the next to use reference each time: O(log n) We should only pay for the functionality we need!! Why have we insisted the tree be complete? All complete trees of size n contain the same edges So why are we even representing the edges? Here comes the really clever bit about implementing heaps!!! 1/11/201 35

Array Representation of a Binary Heap From node i: left child: right child: parent: H D I B J E 1 K A 2 3 4 5 6 8 10 11 12 L F C 7 G A B C D E F G H I J K L 0 1 2 3 4 5 6 7 8 10 11 12 13 We skip index 0 to make the math simpler Actually, it can be a good place to store the current size of the heap 1/11/201 36