CSE332: Data Abstractions Lecture 4: Priority Queues

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

CSE332: Data Abstractions Lecture 4: Priority Queues Dan Grossman Spring 2010.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
CSE373: Data Structures & Algorithms Lecture 5: Dictionaries; Binary Search Trees Aaron Bauer Winter 2014.
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.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Dan Grossman Fall 2013.
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:
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.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
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.
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.
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.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
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 Structures & Algorithms
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Heaps And Priority Queues
October 30th – Priority QUeues
Hashing Exercises.
Source: Muangsin / Weiss
March 31 – Priority Queues and the heap
Bohyung Han CSE, POSTECH
Heaps, Heap Sort, and Priority Queues
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
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
Priority Queue & Heap CSCI 3110 Nan Chen.
Instructor: Lilian de Greef Quarter: Summer 2017
CSE 373: Data Structures and Algorithms
CMSC 341 Lecture 14 Priority Queues & Heaps
B-Tree Insertions, Intro to Heaps
CE 221 Data Structures and Algorithms
CS Data Structure: Heaps.
Priority Queues (Chapter 6.6):
CSE 326: Data Structures Priority Queues & Binary Heaps
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
Priority Queues & Heaps
CSE 12 – Basic Data Structures
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 (Heaps)
Priority Queues CSE 373 Data Structures.
Priority Queues & Heaps
Priority Queues (Chapter 6):
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

CSE332: Data Abstractions Lecture 4: Priority Queues Tyler Robison Summer 2010

A new ADT: Priority Queue Textbook Chapter 6: Priority Queues Will go back to binary search trees (4) and hashtables (5) later 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 Numbers: numeric ordering Strings: lexicon ordering Employee profile: lexicon ordering on name? Id? Much of course will require comparable items: Sorting Binary Search Trees Integers are comparable, so will use them in examples But the priority queue ADT is much more general

Priority Queues Assume each item has a “priority” Operations: The lesser value item is the one with the greater priority So “priority 1” is more important than “priority 4” (Just a convention) Operations: insert deleteMin create, is_empty, destroy Key property: deleteMin returns and deletes from the queue the item with greatest priority (lowest priority value) Can resolve ties arbitrarily insert deleteMin 6 2 15 23 12 18 45 3 7

Focusing on the numbers For simplicity in lecture, we’ll often suppose items are just ints and the int is the priority So an operation sequence could be insert 6 insert 5 x = deleteMin int priorities are common, but really just need comparable Not having “other data” is very rare Example: print job is a priority and the file

Example insert 5 insert 3 insert 4 a = deleteMin b = deleteMin c = deleteMin d = deleteMin Analogy: insert is like enqueue, deleteMin is like dequeue But the whole point is to use priorities instead of FIFO 3 4 2 5

Applications Like all good ADTs, the priority queue arises often 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 Much like Project 1 uses a stack to implement reverse

More applications for Priority Queues “Greedy” algorithms Perform 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”

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; but 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 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)

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) Only really feasible for small k But we are about to see a data structure called a “binary heap” O(log n) insert and O(log n) deleteMin worst-case Very good constant factors If items arrive in random order, then insert is O(1) on average!

Tree terms (review?) Tree T The binary heap data structure implementing the priority queue ADT will be a tree, so worth establishing some terminology A E B D F C G I H L J M K N root(tree) children(node) parent(node) leaves(tree) siblings(node) ancestors(node) descendents(node) subtree(node) depth(node) height(tree) degree(node) branching factor(tree)

Kinds of trees Certain terms define trees with specific structure Binary tree: Each node has at most 2 children n-ary tree: Each node as at most n children Complete tree: Each row is completely full except maybe the bottom row, which is filled from left to right Later we’ll learn a tree is a kind of directed graph with specific structure

Binary Heap: Priority Queue DS Finally, then, a binary min-heap (aka binary heap or just heap) has the following 2 properties: Structure property : A complete tree Heap ordering property: For every (non-root) node the parent node’s value is less than the node’s value 15 30 80 20 10 99 60 40 50 700 85 not a heap a heap So: Where is the highest-priority item? What is the height of a heap with n items? root O(logn)

Operations: basic idea findMin: return root.data deleteMin: answer = root.data Move right-most node in last row to root to restore structure property “Percolate down” to restore heap property insert: Put new node in next position on bottom row to restore structure property “Percolate up” to restore heap property 99 60 40 80 20 10 50 700 85

DeleteMin 1. Delete (and return) value at root node 4 3 7 5 8 9 11 9 6 10

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 4 3 7 5 8 9 11 9 6 10 3 4 9 8 5 7 10 6 11

3. Restore the Heap Property 10 ? 4 9 8 5 7 6 11 3 10 ? 8 4 9 10 5 7 6 11 3 4 3 7 5 8 9 11 9 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 What is the run time? Why not swap with larger of children, if it’s smaller than both? O(logn)

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

Insert: Maintain the Structure Property 2 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 1 4 8 7 5 10 9 11 9 6

Maintain the heap property 2 5 8 4 9 10 7 6 11 1 ? 2 5 8 9 10 4 7 6 11 1 ? 1 4 8 7 5 10 9 ? 11 9 6 2 Percolate up: Put new data in new location If parent larger, swap with parent, and continue Done if parent  item or reached root Run time? At the end, how do we know 2 is going to be less than its left child (here, 7) which it wasn’t compared against?

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); what about the promised O(1) on average (assuming random arrival of items)? There’s a “trick”: don’t represent complete trees as nodes with pointers to children