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

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

CSE 373: Data Structures and Algorithms

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

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

CSE373: Data Structures & Algorithms Priority Queues

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

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

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

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

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

Heaps, Heap Sort, and Priority Queues.

CS350: Data Structures Heaps and Priority Queues

CE 221 Data Structures and Algorithms

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

Administration CSE 326: Data Structures

Priority Queues Heaps Heapsort

CSCI2100B Data Structures Heaps

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

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

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

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

CMSC 341 Lecture 14: Priority Queues, Heaps

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

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

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

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

Priority Queues Heaps Heapsort

Data Structures and Algorithms

Priority Queues and Heaps

Binary Heaps. CSE 373 Data Structures Lecture 11

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

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

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

CSE 214 Computer Science II Heaps and Priority Queues

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

Sorting and Searching

Figure 1: A complete binary tree.

Sorting and Searching

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

COMP : Trees. COMP20012 Trees 219

Priority Queues. Binary Heaps

CS 315 April 1. Goals: Heap (Chapter 6) continued review of Algorithms for Insert DeleteMin. algoritms for decrasekey increasekey Build-heap

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

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

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

Programming II (CS300)

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

Course Review. Cpt S 223 Fall 2010

Recall: Properties of B-Trees

Algorithms and Data Structures

Figure 1: A complete binary tree.

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

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

9. Heap : Priority Queue

Priority Queues (Heaps)

CS 310: Priority Queues and Binary Heaps

Priority Queues (Heaps)

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

CSE 326: Data Structures Lecture #4 Heaps more Priority Qs. Today s Outline

Data Structures Lesson 9

Algorithms and Data Structures

Course Review. Cpt S 223 Fall 2009

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

CS2 Algorithms and Data Structures Note 6

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Binary Heaps in Dynamic Arrays

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

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

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

Priority Queues and Binary Heaps

Programming II (CS300)

Module 2: Priority Queues

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

Lecture 13: AVL Trees and Binary Heaps

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

Module 2: Priority Queues

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

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

Lecture 17 Priority Queues

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Lecture Notes on Priority Queues

CSE 332, Spring 2010, Midterm Examination 30 April 2010

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

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

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

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

CS2 Algorithms and Data Structures Note 6

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

Design and Analysis of Algorithms

Lecture 17 Notes Priority Queues

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

CSE 373: Data Structures and Algorithms

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

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

Programming Abstractions

Basic Data Structures (Version 7) Name:

Chapter 2: Basic Data Structures

Priority Queues. Chapter 9

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

Transcription:

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

A new ADT: Priority Queue A priority queue holds compare-able data Like dictionaries, we 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 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 Winter 2016 CSE373: Data Structures & Algorithms 2

Priorities Each item has a priority In our examples, the lesser item is the one with the greater priority So priority 1 is more important than priority (Just a convention, think first is best ) Operations: insert deletemin insert 6 2 1 23 12 18 3 7 deletemin is_empty Key property: deletemin returns and deletes the item with greatest priority (lowest priority value) Can resolve ties arbitrarily Winter 2016 CSE373: Data Structures & Algorithms 3

Example insert x1 with priority insert x2 with priority 3 insert x3 with priority a = deletemin // x2 b = deletemin // x3 insert x with priority 2 insert x with priority 6 c = deletemin // x d = deletemin // x1 (x1,) (x1,) (x2,3) (x1,) (x3,) (x2,3) (x1,) (x3,) (x1,) Analogy: insert is like enqueue, deletemin is like dequeue But the whole point is to use priorities instead of FIFO Winter 2016 CSE373: Data Structures & Algorithms

Applications Like all good ADTs, the priority queue arises often Sometimes blatant, 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 Sort (first insert all, then repeatedly deletemin) Winter 2016 CSE373: Data Structures & Algorithms

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 times worst-case; assume arrays have room data insert algorithm / time deletemin algorithm / time unsorted array unsorted linked list sorted circular array sorted linked list binary search tree AVL tree add at end O(1) search O(n) add at front O(1) search O(n) search / shift O(n) move front O(1) put in right place O(n) remove at front O(1) put in right place O(n) leftmost O(n) put in right place O(log n) leftmost O(log n) Winter 2016 CSE373: Data Structures & Algorithms 6

Our data structure: the Binary Heap A binary min-heap (or just binary heap or just heap) has: Structure property: A complete binary tree Heap property: The priority of every (non-root) node is less than the priority of its parent Not a binary search tree not a heap a heap So: 30 20 1 80 20 0 0 700 60 8 80 Where is the most important item? What is the height of a heap with n items? Winter 2016 CSE373: Data Structures & Algorithms 7

Operations: basic idea deletemin: 1. Remove root node 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 position on bottom row to restore structure property 2. Percolate up to restore heap property 0 20 0 700 60 8 80 Overall strategy: Preserve structure property Break and restore heap property 8

DeleteMin Delete (and later return) value at root node 1 3 7 8 6 Spring 2016 CSE 373 Data Structures & Algorithms

DeleteMin: Keep the Structure Property We now have a hole at the root Need to fill the hole with another value 3 Keep structure property: When we are done, the tree will have one less node and must still be complete 7 6 8 Pick the last node on the bottom row of the tree and move it to the hole 3 7 8 6 Spring 2016 CSE 373 Data Structures & Algorithms

DeleteMin: Restore the Heap Property Percolate down: Keep comparing priority of item with both children If priority is less important, swap with the most important child and go down one level Done if both children are less important than the item or we ve reached a leaf node? 3? 3 3 8 7 8 7 8 7 6 Run time? Runtime is O(height of heap) O(log n) Height of a complete binary tree of n nodes = log 2 (n) Spring 2016 6 CSE 373 Data Structures & Algorithms 6

Insert Add a value to the tree 2 Afterwards, structure and heap properties must still be correct 1 8 7 6 Spring 2016 CSE 373 Data Structures & Algorithms 12

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 property 7 8 6 Spring 2016 CSE 373 Data Structures & Algorithms 13

Insert: Restore the heap property Percolate up: Put new data in new location If parent is less important, swap with parent, and continue Done if parent is more important than item or reached root 1 1 2? 1 7 6? 8 2 7 6? What is the running time? Like deletemin, worst-case time proportional to tree height: O(log n) 8 2 7 2 6 8 Spring 2016 CSE 373 Data Structures & Algorithms 1

Array Representation of Binary Trees 1 A From node i: 2 3 B D 6 E F 8 12 H I J K L implicit (array) implementation: C 7 G left child: i*2 right child: i*2+1 parent: i/2 (wasting index 0 is convenient for the index arithmetic) A B C D E F G H I J K L 0 1 2 3 6 7 8 12 13 Spring 2016 CSE 373 Data Structures 1 & Algorithms

Judging the array implementation Plusses: Non-data space: just index 0 and unused space on right In conventional tree representation, 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 Multiplying and dividing by 2 is very fast (shift operations in hardware) Last used position 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 16

Pseudocode: insert into binary heap void insert(int val) { if(size==arr.length-1) resize(); size++; i=percolateup(size,val); arr[i] = val; } This pseudocode uses ints. In real use, you will have data nodes with priorities. int percolateup(int hole, int val) { while(hole > 1 && val < arr[hole/2]) arr[hole] = arr[hole/2]; hole = hole / 2; } return hole; } 20 80 0 60 8 700 0 20 80 0 60 8 700 0 0 1 2 3 6 7 8 12 13 17

This pseudocode uses ints. In real use, you will have data nodes with priorities. insert(30) percolateup(, 30) void insert(int val) { if(size==arr.length-1) resize(); size++; i=percolateup(size,val); arr[i] = val; } int percolateup(int hole, int val) { while(hole > 1 && val < arr[hole/2]) arr[hole] = arr[hole/2]; hole = hole / 2; } return hole; } 0 20 60 8 80 30 700 0 60 30 20 80 0 60 8 700 0 0 1 2 3 6 7 8 12 13 18 60

Pseudocode: deletemin from binary heap int deletemin() { if(isempty()) throw ans = arr[1]; hole = percolatedown (1,arr[size]); arr[hole] = arr[size]; size--; return ans; } 0 700 0 20 60 8 80 20 80 0 60 8 700 0 int percolatedown(int hole, int val) { while(2*hole <= size) { left = 2*hole; right = left + 1; if(right > size arr[left] < arr[right]) target = left; else target = right; if(arr[target] < val) { arr[hole] = arr[target]; hole = target; } else break; } return hole; } 0 1 2 3 6 7 8 12 13 1

Pseudocode: deletemin from binary heap 20 0 0 20 80 0 60 8 700 0 0 1 2 3 6 7 8 12 13 0? 0 20 60 8 80 20 is smaller than 80 20 0 0 700 0 20

Example 1. insert: 16, 32,, 67,, 3, 2 2. deletemin 0 1 2 3 6 7 21

Example 1. insert: 16, 32,, 67,, 3, 2 2. deletemin 16 0 1 2 3 6 7 16 22

Example 1. insert: 16, 32,, 67,, 3, 2 2. deletemin 16 32 0 1 2 3 6 7 16 32 23

Example 1. insert: 16, 32,, 67,, 3, 2 2. deletemin 32 16 0 1 2 3 6 7 32 16 2

Example 1. insert: 16, 32,, 67,, 3, 2 2. deletemin 32 16 67 0 1 2 3 6 7 32 16 67 2

Example 1. insert: 16, 32,, 67,, 3, 2 2. deletemin 32 16 67 0 1 2 3 6 7 32 16 67 26

Example 1. insert: 16, 32,, 67,, 3, 2 2. deletemin 32 16 67 3 0 1 2 3 6 7 32 16 67 3 27

Exercise 1. insert: 16, 32,, 67,, 3, 2 2. deletemin 2 32 67 3 16 0 1 2 3 6 7 2 32 67 3 16 28

Other operations decreasekey: given pointer to object in priority queue (e.g., its array index), lower its priority value by p Change priority and percolate up increasekey: given pointer to object in priority queue (e.g., its array index), raise its priority value by p Change priority and percolate down remove: given pointer to object in priority queue (e.g., its array index), remove it from the queue decreasekey with p =, then deletemin Running time for all these operations? 2

Build Heap Suppose you have n items to put in a new (empty) priority queue Call this operation buildheap n inserts works Only choice if ADT doesn t provide buildheap explicitly O(n log n) Why would an ADT provide this unnecessary operation? Convenience Efficiency: an O(n) algorithm called Floyd s Method Common issue in ADT design: how many specialized operations 30

Floyd s Method 1. Use n items to make any complete tree you want That is, put them in array indices 1,,n 2. Treat it as a heap and fix the heap-order property Bottom-up: leaves are already in heap order, work up toward the root one level at a time void buildheap() { for(i = size/2; i>0; i--) { val = arr[i]; hole = percolatedown(i,val); arr[hole] = val; } } 31

Example 12/2 = 6 In tree form for readability Purple for node not less than descendants heap-order problem 6 12 Notice no leaves are purple Check/fix each non-leaf bottom-up (6 steps here) 3 3 2 1 2 8 1 7 6 32

Example 12 Step 1 12 3 2 3 2 8 1 7 6 8 1 7 6 Happens to already be less than children (er, child) 33

Example 12 Step 2 12 3 2 3 1 2 8 1 7 6 8 7 6 Percolate down (notice that moves 1 up) 3

Example 12 Step 3 12 3 1 2 3 1 2 8 7 6 8 7 6 Another nothing-to-do step 3

Example 12 Step 12 2 3 1 2 3 1 6 8 7 6 8 7 Percolate down as necessary (steps a and b) 36

Example 12 Step 12 2 1 2 3 1 6 3 6 8 7 8 7 37

Example 12 Step 6 1 1 2 3 2 3 6 6 8 7 12 8 7 38

But is it right? Seems to work Let s prove it restores the heap property (correctness) Then let s prove its running time (efficiency) void buildheap() { for(i = size/2; i>0; i--) { val = arr[i]; hole = percolatedown(i,val); arr[hole] = val; } } 3

Correctness void buildheap() { for(i = size/2; i>0; i--) { val = arr[i]; hole = percolatedown(i,val); arr[hole] = val; } } Loop Invariant: For all j>i, arr[j] is less than its children True initially: If j > size/2, then j is a leaf Otherwise its left child would be at position > size True after one more iteration: loop body and percolatedown make arr[i] less than children without breaking the property for any descendants So after the loop finishes, all nodes are less than their children 0

Efficiency void buildheap() { for(i = size/2; i>0; i--) { val = arr[i]; hole = percolatedown(i,val); arr[hole] = val; } } Easy argument: buildheap is O(n log n) where n is size size/2 loop iterations Each iteration does one percolatedown, each is O(log n) This is correct, but there is a more precise ( tighter ) analysis of the algorithm 1

Efficiency Better argument: buildheap is O(n) where n is size size/2 total loop iterations: O(n) 1/2 the loop iterations percolate at most 1 step 1/ the loop iterations percolate at most 2 steps 1/8 the loop iterations percolate at most 3 steps void buildheap() { for(i = size/2; i>0; i--) { val = arr[i]; hole = percolatedown(i,val); arr[hole] = val; } } ((1/2) + (2/) + (3/8) + (/16) + (/32) + ) < 2 (page of Weiss) So at most 2*(size/2) total percolate steps: O(n) 2

Lessons from buildheap Without buildheap, our ADT already let clients implement their own in O(n log n) worst case By providing a specialized operation internal to the data structure (with access to the internal data), we can do O(n) worst case Intuition: Most data is near a leaf, so better to percolate down Can analyze this algorithm for: Correctness: Non-trivial inductive proof using loop invariant Efficiency: First analysis easily proved it was O(n log n) Tighter analysis shows same algorithm is O(n) 3

Exercise: Build the Heap using Floyd s method 3 2 6 2 1 80 3 1 2 3 6 7 8 2 6 2 1 80 3 3