CSE 326: Data Structures Lecture #17 Priority Queues Alon Halevy Spring Quarter 2001.

Slides:



Advertisements
Similar presentations
§6 Leftist Heaps CHAPTER 5 Graph Algorithms  Heap: Structure Property + Order Property Target : Speed up merging in O(N). Leftist Heap: Order Property.
Advertisements

DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
COL 106 Shweta Agrawal and Amit Kumar
CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Priority Queues And the amazing binary heap Chapter 20 in DS&PS Chapter 6 in DS&AA.
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued Tyler Robison Summer
1 CSE 326: Data Structures Priority Queues – Binary Heaps.
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
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
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
1 COSC 2P03 Lecture #5 – Trees Part III, Heaps. 2 Today Take up the quiz Assignment Questions Red-Black Trees Binary Heaps Heap sort D-Heaps, Leftist.
CSE 373 Data Structures Lecture 12
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
Dijkstra/Prim 1 make-heap |V| insert |V| delete-min |E| decrease-key Priority Queues make-heap Operation insert find-min delete-min union decrease-key.
CS 315 Lecture 18 Nov 15 Goals Complete heap operations review insert, deletemin decreaseKey, increaseKey heap building Heap sorting Some applications.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
CSE 326: Data Structures Lecture #13 Extendible Hashing and Splay Trees Alon Halevy Spring Quarter 2001.
Cpt S 223 – Advanced Data Structures Course Review Midterm Exam # 2
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
§3 Binary Heap 1. Structure Property: 【 Definition 】 A binary tree with n nodes and height h is complete iff its nodes correspond to the nodes numbered.
CSE 326: Data Structures Lecture #13 Priority Queues and Binary Heaps Nick Deibel Winter Quarter 2002.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2010W2 1.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Cpt S 223 – Advanced Data Structures Priority Queues
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
1 CSE 326 Data Structures Part 6: Priority Queues, AKA Heaps Henry Kautz Autumn 2002.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2014W1 1.
Priority Queues (Heaps)
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
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.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003.
CSE373: Data Structures & Algorithms Lecture 8: Priority Queues Aaron Bauer Winter 2014.
CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps Linda Shapiro Spring 2016.
CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.
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.
1 Chapter 6: Priority Queues, AKA Heaps. 2 Queues with special properties Consider applications –ordering CPU jobs –searching for the exit in a maze (or.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
CSE 326: Data Structures Priority Queues (Heaps)
CS 201 Data Structures and Algorithms
Hashing Exercises.
Source: Muangsin / Weiss
CMSC 341 Lecture 13 Leftist Heaps
CMSC 341: Data Structures Priority Queues – Binary Heaps
CSE 326 Heaps and the Priority Queue ADT
CSE 326: Data Structures Lecture #4 Heaps more Priority Qs
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued
Instructor: Lilian de Greef Quarter: Summer 2017
CSE 326: Data Structures Lecture #4 Mind Your Priority Queues
CSE 332: Data Structures Priority Queues – Binary Heaps
Heaps and the Heapsort Heaps and priority queues
CE 221 Data Structures and Algorithms
CSE 326: Data Structures Lecture #5 Political Heaps
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
CSE373: Data Structures & Algorithms Lecture 7: Binary Heaps, Continued Dan Grossman Fall 2013.
CSE 326: Data Structures Lecture #3 Mind your Priority Qs
Priority Queues CSE 373 Data Structures.
CSE 326: Data Structures Lecture #5 Heaps More
Presentation transcript:

CSE 326: Data Structures Lecture #17 Priority Queues Alon Halevy Spring Quarter 2001

Binary Heap Priority Q Data Structure Heap-order property –parent’s key is less than children’s keys –result: minimum is always at the top Structure property –complete tree with fringe nodes packed to the left –result: depth is always O(log n); next open location always known

DeleteMin ? pqueue.deleteMin()

Percolate Down ? ? ?

Finally…

DeleteMin Code Object deleteMin() { assert(!isEmpty()); returnVal = Heap[1]; size--; newPos = percolateDown(1, Heap[size+1]); Heap[newPos] = Heap[size + 1]; return returnVal; } int percolateDown(int hole, Object val) { while (2*hole <= size) { left = 2*hole; right = left + 1; if (right <= size && Heap[right] < Heap[left]) target = right; else target = left; if (Heap[target] < val) { Heap[hole] = Heap[target]; hole = target; } else break; } return hole; } runtime:

Insert pqueue.insert(3) ?

Percolate Up ? ? ?

Insert Code void insert(Object o) { assert(!isFull()); size++; newPos = percolateUp(size,o); Heap[newPos] = o; } int percolateUp(int hole, Object val) { while (hole > 1 && val < Heap[hole/2]) Heap[hole] = Heap[hole/2]; hole /= 2; } return hole; } runtime:

Performance of Binary Heap In practice: binary heaps much simpler to code, lower constant factor overhead Binary heap worst case Binary heap avg case AVL tree worst case AVL tree avg case InsertO(log n)O(1) percolates 1.6 levels O(log n) Delete Min O(log n)

Changing Priorities In many applications the priority of an object in a priority queue may change over time –if a job has been sitting in the printer queue for a long time increase its priority –unix “renice” –Alon’s story Must have some (separate) way of find the position in the queue of the object to change (e.g. a hash table)

Other Priority Queue Operations decreaseKey –given the position of an object in the queue, reduce its priority value increaseKey –given the position of an an object in the queue, increase its priority value remove –given the position of an object in the queue, remove it buildHeap –given a set of items, build a heap

DecreaseKey, IncreaseKey, and Remove void decreaseKey(int pos, int delta){ temp = Heap[pos] - delta; newPos = percolateUp(pos, temp); Heap[newPos] = temp; } void increaseKey(int pos, int delta) { temp = Heap[pos] + delta; newPos = percolateDown(pos, temp); Heap[newPos] = temp; } void remove(int pos) { percolateUp(pos, NEG_INF_VAL); deleteMin(); }

BuildHeap Floyd’s Method. Thank you, Floyd pretend it’s a heap and fix the heap-order property! Easy worst case bound: Easy average case bound:

Build(this)Heap

Finally… runtime?

Complexity of Build Heap Note: size of a perfect binary tree doubles (+1) with each additional layer At most n/4 percolate down 1 level at most n/8 percolate down 2 levels at most n/16 percolate down 3 levels… O(n)

Thinking about Heaps Observations –finding a child/parent index is a multiply/divide by two –operations jump widely through the heap –each operation looks at only two new nodes –inserts are at least as common as deleteMins Realities –division and multiplication by powers of two are fast –looking at one new piece of data terrible in a cache line –with huge data sets, disk accesses dominate

Solution: d-Heaps Each node has d children Still representable by array Good choices for d: –optimize performance based on # of inserts/removes –choose a power of two for efficiency –fit one set of children on a memory page/disk block What do d-heaps remind us of???

New Operation: Merge Given two heaps, merge them into one heap –first attempt: insert each element of the smaller heap into the larger. runtime: –second attempt: concatenate heaps’ arrays and run buildHeap. runtime: How about O(log n) time?

Idea: Hang a New Tree = ? 10 Now, just percolate down! Note: we just gave up the nice structural property on binary heaps!

Problem? Need some other kind of balance condition… = 2 ?

Leftist Heaps Idea: make it so that all the work you have to do in maintaining a heap is in one small part Leftist heap: –almost all nodes are on the left –all the merging work is on the right

the null path length (npl) of a node is the number of nodes between it and a null in the tree Random Definition: Null Path Length npl(null) = -1 npl(leaf) = 0 npl(single-child node) =

Leftist Heap Properties Heap-order property –parent’s priority value is  to childrens’ priority values –result: minimum element is at the root Leftist property –null path length of left subtree is  npl of right subtree –result: tree is at least as “heavy” on the left as the right Are leftist trees complete? Balanced?

Leftist tree examples NOT leftistleftist every subtree of a leftist tree is leftist, comrade!

Right Path in a Leftist Tree is Short If the right path has length at least r, the tree has at least 2 r - 1 nodes Proof by induction Basis: r = 1. Tree has at least one node: = 1 Inductive step: assume true for r’ < r. The right subtree has a right path of at least r - 1 nodes, so it has at least 2 r nodes. The left subtree must also have a right path of at least r - 1 (otherwise, there is a null path of r - 3, less than the right subtree). Again, the left has 2 r nodes. All told then, there are at least: 2 r r = 2 r - 1 So, a leftist tree with at least n nodes has a right path of at most log n nodes

Merging Two Leftist Heaps merge(T 1,T 2 ) returns one leftist heap containing all elements of the two (distinct) leftist heaps T 1 and T 2 a L1L1 R1R1 b L2L2 R2R2 merge T1T1 T2T2 a < b a L1L1 recursive merge b L2L2 R2R2 R1R1

Merge Continued a L1L1 R’R’ R ’ = Merge(R 1, T 2 ) a R’R’ L1L1 npl(R ’ ) > npl(L 1 ) constant work at each merge; recursively traverse RIGHT right path of each tree; total work = O(log n)

Operations on Leftist Heaps merge with two trees of total size n: O(log n) insert with heap size n: O(log n) –pretend node is a size 1 leftist heap –insert by merging original heap with one node heap deleteMin with heap size n: O(log n) –remove and return root –merge left and right subtrees merge

Example merge ? merge 10 5 ? 0 merge

Sewing Up the Example ? ? ? Done?

Finally…

Skew Heaps Problems with leftist heaps –extra storage for npl –two pass merge (with stack!) –extra complexity/logic to maintain and check npl Solution: skew heaps –blind adjusting version of leftist heaps –amortized time for merge, insert, and deleteMin is O(log n) –worst case time for all three is O(n) –merge always switches children when fixing right path –iterative method has only one pass What do skew heaps remind us of?

Merging Two Skew Heaps a L1L1 R1R1 b L2L2 R2R2 merge T1T1 T2T2 a < b a L1L1 merge b L2L2 R2R2 R1R1 Notice the old switcheroo!

Example merge merge merge

Skew Heap Code void merge(heap1, heap2) { case { heap1 == NULL: return heap2; heap2 == NULL: return heap1; heap1.findMin() < heap2.findMin(): temp = heap1.right; heap1.right = heap1.left; heap1.left = merge(heap2, temp); return heap1; otherwise: return merge(heap2, heap1); }

Comparing Heaps Binary Heaps d-Heaps Binomial Queues Leftist Heaps Skew Heaps