1 CSE 326 Data Structures Part 6: Priority Queues, AKA Heaps Henry Kautz Autumn 2002.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
Advertisements

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.
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.
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 4: Priority Queues Dan Grossman Spring 2010.
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.
CSE 373 Data Structures Lecture 12
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.
CSE 326: Data Structures Lecture #17 Priority Queues Alon Halevy Spring Quarter 2001.
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
CSE 326: Data Structures Lecture #13 Priority Queues and Binary Heaps Nick Deibel Winter Quarter 2002.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
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
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:
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2014W1 1.
Priority Queues (Heaps)
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.
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.
CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.
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.
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.
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
CSE 326: Data Structures Priority Queues (Heaps)
CS 201 Data Structures and Algorithms
October 30th – Priority QUeues
Hashing Exercises.
CMSC 341 Lecture 13 Leftist Heaps
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.
Heaps, Heap Sort, and Priority Queues
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
Instructor: Lilian de Greef Quarter: Summer 2017
CSE332: Data Abstractions Lecture 4: Priority Queues
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
Data Structures and Analysis (COMP 410)
CSE 326: Data Structures Lecture #3 Mind your Priority Qs
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 12
Presentation transcript:

1 CSE 326 Data Structures Part 6: Priority Queues, AKA Heaps Henry Kautz Autumn 2002

2 Not Quite Queues Consider applications –ordering CPU jobs –searching for the exit in a maze –emergency room admission processing Problems? –short jobs should go first –most promising nodes should be searched first –most urgent cases should go first

3 Priority Queue ADT Priority Queue operations –create –destroy –insert –deleteMin –is_empty Priority Queue property: for two elements in the queue, x and y, if x has a lower priority value than y, x will be deleted before y F(7) E(5) D(100) A(4) B(6) insert deleteMin G(9)C(3)

4 Applications of the Priority Q Hold jobs for a printer in order of length Store packets on network routers in order of urgency Simulate events Anything greedy

5 Discrete Event Simulation An event is a pair (x,t) where x describes the event and t is time it should occur A discrete event simulator (DES) maintains a set S of events which it intends to simulate in time order repeat { Find and remove (x 0,t 0 ) from S such that t 0 is minimum; Do whatever x 0 says to do, in the process new events (x 2,t 2 )…(x k,t k ) may be generated; Insert the new events into S; }

6 Emergency Room Simulation Patient arrive at time t with injury of criticality C –If no patients waiting and a free doctor, assign them to doctor and create a future departure event; else put patient in the Criticality priority queue Patient departs at time t –If someone in Criticality queue, pull out most critical and assign to doctor; create a future departure event time queue arrive(t,c) criticality (triage) queue assign patient to doctor patient generator depart(t) arrive(t,c)

7 Naïve Priority Queue Data Structures Unsorted list: –insert: –deleteMin: Sorted list: –insert: –deleteMin:

8 BST Tree Priority Queue Data Structure Regular BST: –insert: –deleteMin: AVL Tree: –insert: –deleteMin: Can we do better?

9 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 How do we find the minimum?

Nifty Storage Trick Calculations: –child: –parent: –root: –next free: 0

Nifty Storage Trick Calculations: –child: left = 2*node right=2*node+1 –parent: floor(node/2) –root: 1 –next free: length+1 0

12 DeleteMin pqueue.deleteMin()

13 Percolate Down

14 DeleteMin Code Comparable deleteMin(){ x = A[1]; A[1]=A[size--]; percolateDown(1); return x; } percolateDown(int hole) { tmp=A[hole]; while (2*hole <= size) { left = 2*hole; right = left + 1; if (right <= size && A[right] < A[left]) target = right; else target = left; if (A[target] < tmp) { A[hole] = A[target]; hole = target; } else break; } A[hole] = tmp; } runtime: Trick to avoid repeatedly copying the value at A[1] Move down

15 Insert pqueue.insert(3) 3

16 Percolate Up

17 Insert Code void insert(Comparable x) { // Efficiency hack: we won’t actually put x // into the heap until we’ve located the position // it goes in. This avoids having to copy it // repeatedly during the percolate up. int hole = ++size; // Percolate up for( ; hole>1 && x < A[hole/2] ; hole = hole/2) A[hole] = A[hole/2]; A[hole] = x; } runtime:

18 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 BST tree avg case Insert O(log n)O(1) percolates 1.6 levels O(log n) Delete Min O(log n)

19 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” Must have some (separate) way of find the position in the queue of the object to change (e.g. a hash table)

20 Other Priority Queue Operations decreaseKey –Given the position of an object in the queue, increase its priority (lower its key). Fix heap property by: increaseKey –given the position of an an object in the queue, decrease its priority (increase its key). Fix heap property by: remove –given the position of an an object in the queue, remove it. Do increaseKey to infinity then …

21 BuildHeap Task: Given a set of n keys, build a heap all at once Approach 1: Repeatedly perform Insert(key) Complexity:

22 BuildHeap Floyd’s Method pretend it’s a heap and fix the heap-order property! buildHeap(){ for (i=size/2; i>0; i--) percolateDown(i); }

23 Build(this)Heap

24 Finally…

25 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)

26 Heap Sort Input: unordered array A[1..N] 1.Build a max heap (largest element is A[1]) 2.For i = 1 to N-1: A[N-i+1] = Delete_Max()

27 Properties of Heap Sort Worst case time complexity O(n log n) –Build_heap O(n) –n Delete_Max’s for O(n log n) In-place sort – only constant storage beyond the array is needed

28 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 in a cache line –fit one set of children on a memory page/disk block

30 Coming Up Mergeable heaps –Leftist heaps –Skew heaps –Binomial queues Read Weiss Ch. 6 Midterm results

31 New Operation: Merge Merge(H1,H2): Merge two heaps H1 and H2 of size O(N). –E.g. Combine queues from two different sources to run on one CPU. 1.Can do O(N) Insert operations: O(N log N) time 2.Better: Copy H2 at the end of H1 (assuming array implementation) and use Floyd’s Method for BuildHeap. Running Time: O(N) Can we do even better? (i.e. Merge in O(log N) time?)

32 Binomial Queues Binomial queues support all three priority queue operations Merge, Insert and DeleteMin in O(log N) time Idea: Maintain a collection of heap-ordered trees –Forest of binomial trees Recursive Definition of Binomial Tree (based on height k): –Only one binomial tree for a given height –Binomial tree of height 0 = single root node –Binomial tree of height k = B k = Attach B k-1 to root of another B k-1

33 Building a Binomial Tree To construct a binomial tree B k of height k: 1.Take the binomial tree B k-1 of height k-1 2.Place another copy of B k-1 one level below the first 3.Attach the root nodes Binomial tree of height k has exactly 2 k nodes (by induction) B 0 B 1 B 2 B 3

34 Building a Binomial Tree To construct a binomial tree B k of height k: 1.Take the binomial tree B k-1 of height k-1 2.Place another copy of B k-1 one level below the first 3.Attach the root nodes Binomial tree of height k has exactly 2 k nodes (by induction) B 0 B 1 B 2 B 3

35 Building a Binomial Tree To construct a binomial tree B k of height k: 1.Take the binomial tree B k-1 of height k-1 2.Place another copy of B k-1 one level below the first 3.Attach the root nodes Binomial tree of height k has exactly 2 k nodes (by induction) B 0 B 1 B 2 B 3

36 Building a Binomial Tree To construct a binomial tree B k of height k: 1.Take the binomial tree B k-1 of height k-1 2.Place another copy of B k-1 one level below the first 3.Attach the root nodes Binomial tree of height k has exactly 2 k nodes (by induction) B 0 B 1 B 2 B 3

37 Building a Binomial Tree To construct a binomial tree B k of height k: 1.Take the binomial tree B k-1 of height k-1 2.Place another copy of B k-1 one level below the first 3.Attach the root nodes Binomial tree of height k has exactly 2 k nodes (by induction) B 0 B 1 B 2 B 3

38 Building a Binomial Tree To construct a binomial tree B k of height k: 1.Take the binomial tree B k-1 of height k-1 2.Place another copy of B k-1 one level below the first 3.Attach the root nodes Binomial tree of height k has exactly 2 k nodes (by induction) B 0 B 1 B 2 B 3

39 Building a Binomial Tree To construct a binomial tree B k of height k: 1.Take the binomial tree B k-1 of height k-1 2.Place another copy of B k-1 one level below the first 3.Attach the root nodes Binomial tree of height k has exactly 2 k nodes (by induction) B 0 B 1 B 2 B 3

40 Why Binomial? Why are these trees called binomial? –Hint: how many nodes at depth d? B 0 B 1 B 2 B 3

41 Why Binomial? Why are these trees called binomial? –Hint: how many nodes at depth d? Number of nodes at different depths d for B k = [1], [1 1], [1 2 1], [ ], … Binomial coefficients of (a + b) k = k!/((k-d)!d!) B 0 B 1 B 2 B 3

42 Definition of Binomial Queues 3 Binomial Queue = “forest” of heap-ordered binomial trees B 0 B 2 B 0 B 1 B 3 Binomial queue H1 5 elements = 101 base 2  B 2 B 0 Binomial queue H2 11 elements = 1011 base 2  B 3 B 1 B 0

43 Binomial Queue Properties Suppose you are given a binomial queue of N nodes 1.There is a unique set of binomial trees for N nodes 2.What is the maximum number of trees that can be in an N-node queue? –1 node  1 tree B 0 ; 2 nodes  1 tree B 1 ; 3 nodes  2 trees B 0 and B 1 ; 7 nodes  3 trees B 0, B 1 and B 2 … –Trees B 0, B 1, …, B k can store up to … + 2 k = 2 k+1 – 1 nodes = N. –Maximum is when all trees are used. So, solve for (k+1). –Number of trees is  log(N+1) = O(log N)

44 Binomial Queues: Merge Main Idea: Merge two binomial queues by merging individual binomial trees –Since B k+1 is just two B k ’s attached together, merging trees is easy Steps for creating new queue by merging: 1.Start with B k for smallest k in either queue. 2.If only one B k, add B k to new queue and go to next k. 3.Merge two B k ’s to get new B k+1 by making larger root the child of smaller root. Go to step 2 with k = k + 1.

45 Example: Binomial Queue Merge H1: H2:

46 Example: Binomial Queue Merge H1: H2:

47 Example: Binomial Queue Merge H1: H2:

48 Example: Binomial Queue Merge H1: H2:

49 Example: Binomial Queue Merge H1: H2:

50 Example: Binomial Queue Merge H1: H2:

51 Binomial Queues: Merge and Insert What is the run time for Merge of two O(N) queues? How would you insert a new item into the queue?

52 Binomial Queues: Merge and Insert What is the run time for Merge of two O(N) queues? –O(number of trees) = O(log N) How would you insert a new item into the queue? –Create a single node queue B 0 with new item and merge with existing queue –Again, O(log N) time Example: Insert 1, 2, 3, …,7 into an empty binomial queue

53 Insert 1,2,…,7 1

54 Insert 1,2,…,7 1 2

55 Insert 1,2,…,

56 Insert 1,2,…,

57 Insert 1,2,…,

58 Insert 1,2,…,

59 Insert 1,2,…,

60 Insert 1,2,…,

61 Binomial Queues: DeleteMin Steps: 1.Find tree B k with the smallest root 2.Remove B k from the queue 3.Delete root of B k (return this value); You now have a new queue made up of the forest B 0, B 1, …, B k-1 4.Merge this queue with remainder of the original (from step 2) Run time analysis: Step 1 is O(log N), step 2 and 3 are O(1), and step 4 is O(log N). Total time = O(log N) Example: Insert 1, 2, …, 7 into empty queue and DeleteMin

62 Insert 1,2,…,

63 DeleteMin

64 Merge

65 Merge

66 Merge

67 Merge DONE!

68 Implementation of Binomial Queues Need to be able to scan through all trees, and given two binomial queues find trees that are same size –Use array of pointers to root nodes, sorted by size –Since is only of length log(N), don’t have to worry about cost of copying this array –At each node, keep track of the size of the (sub) tree rooted at that node Want to merge by just setting pointers –Need pointer-based implementation of heaps DeleteMin requires fast access to all subtrees of root –Use First-Child/Next-Sibling representation of trees

69 Efficient BuildHeap for Binomial Queues Insert one at a time - O(n log n) Better algorithm: –Start with each element as a singleton tree –Merge trees of size 1 –Merge trees of size 2 –Merge trees of size 4 Complexity:

70 Other Mergeable Priority Queues: Leftist and Skew Heaps Leftist Heaps: Binary heap-ordered trees with left subtrees always “longer” than right subtrees –Main idea: Recursively work on right path for Merge/Insert/DeleteMin –Right path is always short  has O(log N) nodes –Merge, Insert, DeleteMin all have O(log N) running time (see text) Skew Heaps: Self-adjusting version of leftist heaps (a la splay trees) –Do not actually keep track of path lengths –Adjust tree by swapping children during each merge –O(log N) amortized time per operation for a sequence of M operations See Weiss for details…

71 Coming Up For Wednesday: Read Weiss on Leftist Heaps and Skew Heaps How do they work? How does their complexity compare to Binomial Queues? Which seems easiest to implement? Which is likely to have lowest overhead?

72 Leftist Heaps An alternative heap structure that also enables fast merges Based on binary trees rather than k-ary trees

73 Idea: Hang a New Tree = ? 10 Now, just percolate down!

74 Idea: Hang a New Tree = ? Now, just percolate down!

75 Idea: Hang a New Tree = ? Now, just percolate down!

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

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

78 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

79 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) = another way of looking at it: npl is the height of complete subtree rooted at this node 0

80 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?

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

82 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

83 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

84 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)

85 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

86 Example merge ? merge 10 5 ? 0 merge

87 Sewing Up the Example ? ? ? Done?

88 Finally…

89 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?

90 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!

91 Example merge merge merge

92 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); }

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

94 Summary of Heap ADT Analysis Consider a heap of N nodes Space needed: O(N) –Actually, O(MaxSize) where MaxSize is the size of the array –Pointer-based implementation: pointers for children and parent Total space = 3N + 1 (3 pointers per node + 1 for size) FindMin: O(1) time; DeleteMin and Insert: O(log N) time BuildHeap from N inputs: What is the run time? –N Insert operations = O(N log N) –O(N): Treat input array as a heap and fix it using percolate down Thanks, Floyd! Mergable Heaps: Binomial Queues, Leftist Heaps, Skew Heaps