CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Heaps, Heap Sort, and Priority Queues. Sorting III / Slide 2 Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two.
CSE332: Data Abstractions Lecture 4: Priority Queues Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued Tyler Robison Summer
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Heaps and heapsort COMP171 Fall Sorting III / Slide 2 Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. Sizes: Job.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
1 Binary heaps binary tree that satisfy two properties –structural property (is a complete tree) –heap-ordering property (minimum item on top) Can have.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Lecture 7 Heaps and Priority Queues. Motivating Example 3 jobs have been submitted to a printer, the jobs have sizes 100, 10, 1 page. Average waiting.
Lecture 7 Heaps and Priority Queues. Motivating Example 3 jobs have been submitted to a printer, the jobs have sizes 100, 10, 1 page. Average waiting.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Chapter 21 Binary Heap.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Dan Grossman Fall 2013.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued Dan Grossman Spring 2012.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CSE 373: Data Structures and Algorithms Lecture 11: Priority Queues (Heaps) 1.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
CSE373: Data Structures & Algorithms Lecture 8: Priority Queues Aaron Bauer Winter 2014.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps Linda Shapiro Spring 2016.
CSE332: Data Abstractions Lecture 4: Priority Queues Dan Grossman Spring 2012.
Nov 2, 2001CSE 373, Autumn Hash Table example marking deleted items + choice of table size.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Heaps and Heap Sort. Sorting III / Slide 2 Background: Complete Binary Trees * A complete binary tree is the tree n Where a node can have 0 (for the leaves)
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structure & Algorithms
CSE373: Data Structures & Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
October 30th – Priority QUeues
Source: Muangsin / Weiss
Cse 373 April 26th – Exam Review.
Heaps, Heap Sort, and Priority Queues
7/23/2009 Many thanks to David Sun for some of the included slides!
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
CMSC 341: Data Structures Priority Queues – Binary Heaps
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued
Instructor: Lilian de Greef Quarter: Summer 2017
CSE 373: Data Structures and Algorithms
CSE332: Data Abstractions Lecture 4: Priority Queues
Instructor: Lilian de Greef Quarter: Summer 2017
CS Data Structure: Heaps.
CSE 326: Data Structures Priority Queues & Binary Heaps
CSE373: Data Structures & Algorithms Lecture 7: Binary Heaps, Continued Dan Grossman Fall 2013.
CSE 373 Priority queue implementation; Intro to heaps
CSC 380: Design and Analysis of Algorithms
Priority Queues CSE 373 Data Structures.
Presentation transcript:

CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015

A Quick Note: Homework 2 due tonight at 11pm! Fall 20152CSE373: Data Structures & Algorithms

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 dictionaries 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: dictionaries, 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 Fall 20153CSE373: Data Structures & Algorithms

Priorities 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, think “first is best”) Operations: –insert –deleteMin –is_empty Key property: deleteMin returns and deletes the item with greatest priority (lowest priority value) –Can resolve ties arbitrarily Fall 20154CSE373: Data Structures & Algorithms insert deleteMin

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 priorities instead of FIFO Fall 20155CSE373: 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? Fall 20156CSE373: Data Structures & Algorithms

More applications “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) Sorting (first insert all, then repeatedly deleteMin ) –Much like Homework 1 uses a stack to implement reverse Fall 20157CSE373: 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 array sorted linked list binary search tree AVL tree (our) hash table Fall 20158CSE373: Data Structures & Algorithms

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 times worst-case; assume arrays “have room” data insert algorithm / time deleteMin algorithm / time unsorted array add at end O(1) search O(n) unsorted linked list add at front O(1) search O(n) sorted array search / shift 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)leftmost O(n) AVL tree put in right place O( log n)leftmost O( log n) (our) hash table add O(1) iterate over keys O(n) Fall 20159CSE373: Data Structures & Algorithms

More on possibilities If priorities are random, binary search tree will likely do better –O( log n) insert and O( log n) deleteMin on average 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) 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 operations; no need to maintain a full sort –If items arrive in random order, then insert is O(1) on average Fall CSE373: Data Structures & Algorithms

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 Fall CSE373: Data Structures & Algorithms

Structure Property: Completeness A Binary Heap is a complete binary tree: –A binary tree with all levels full, with a possible exception being the bottom level, which is filled left to right Examples: Fall CSE373: Data Structures & Algorithms incomplete complete are these trees complete?

Heap Order Property The priority of every (non-root) node is greater than (or equal to) that of it’s parent. Examples: Fall CSE373: Data Structures & Algorithms which of these are heaps? heapnot a heap 20

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 Fall CSE373: Data Structures & Algorithms

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 Fall CSE373: Data Structures & Algorithms

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 Fall CSE373: Data Structures & Algorithms not a heap a heap So: Where is the highest-priority item? What is the height of a heap with n items? not a heap 10

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 property insert: 1.Put new node in next position on bottom row to restore structure property 2.“Percolate up” to restore heap property Fall CSE373: Data Structures & Algorithms Overall strategy: Preserve structure property Break and restore heap property

18 DeleteMin Delete (and later return) value at root node Fall 2015CSE373: Data Structures & Algorithms

19 2. Restore the Structure Property We now have a “hole” at the root –Need to fill the hole with another value When we are done, the tree will have one less node and must still be complete Fall 2015CSE373: Data Structures & Algorithms

20 3. Restore the Heap Property 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 Why is this correct? What is the run time? ? ? Fall 2015CSE373: Data Structures & Algorithms

21 DeleteMin: Run Time Analysis We will percolate down at most (height of heap) times –So 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) Fall 2015CSE373: Data Structures & Algorithms

22 Insert Add a value to the tree Afterwards, structure and heap properties must still be correct Where do we insert the new value? Fall 2015 CSE373: Data Structures & Algorithms

23 Insert: Maintain the Structure Property There is only one valid tree shape after we add one more node So put our new data there and then focus on restoring the heap property Fall 2015CSE373: Data Structures & Algorithms

24 Maintain the heap property 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? ? ? ? Fall 2015CSE373: Data Structures & Algorithms

25 Insert: Run Time Analysis Like deleteMin, worst-case time proportional to tree height –O( log n) But… deleteMin needs the “last used” complete-tree position and insert needs the “next to use” complete-tree position –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! Fall 2015CSE373: Data Structures & Algorithms

Fall Array Representation of Binary Trees G ED CB A JKHI F L From node i : left child: i*2 right child: i*2+1 parent: i/2 (wasting index 0 is convenient for the index arithmetic) ABCDEFGHIJKL implicit (array) implementation: CSE373: Data Structures & Algorithms

Judging the array implementation Plusses: Less “wasted” 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” Fall CSE373: Data Structures & Algorithms