1 / 12

Chapter 6: Priority Queues (Heaps)

Chapter 6: Priority Queues (Heaps). Priority Queue ADT. Heap Implementation. Heap Applications. Leftist Heaps. CS 340. Page 100. Priority Queues.

dominy
Download Presentation

Chapter 6: Priority Queues (Heaps)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6: Priority Queues (Heaps) • Priority Queue ADT • Heap Implementation • Heap Applications • Leftist Heaps CS 340 Page 100

  2. Priority Queues Often, a FIFO queue structure has a need for a prioritization capability, so elements with greater priority are removed before lower-priority elements that were actually inserted first. Examples: • Short print jobs may be prioritized over longer print jobs on a printer queue. • Real-time network applications (e.g., video, audio) may be prioritized over e-mail and simple file transfers on a network switch’s forwarding queue. • System maintenance tasks (e.g., memory defragmentation, mouse interrupts) may be prioritized over application software tasks on an operating system’s job queue. CS 340 Page 101

  3. 10 17 13 25 31 15 30 45 61 58 47 23 10 17 13 25 31 15 30 45 61 58 47 23 One Priority Queue Implementation: The Heap A heap is a complete binary tree in which every node’s value is less than or equal to all of its offsprings’ values. Note: One convenient aspect of the heap is that it can be stored in an array. Offspring of node i: nodes 2i+1 and 2i+2 Parent of node i: node (i - 1) / 2 CS 340 Page 102

  4. 10 10 Percolate Up Percolate Up Insert 12 17 13 17 13 15 30 25 31 15 30 25 31 47 23 12 45 61 58 47 23 45 61 58 10 10 17 13 17 12 12 30 13 30 25 31 25 31 47 23 15 47 23 15 45 61 58 45 61 58 Inserting Into A Heap When inserting a new element into a heap, start at the new leaf node that would maintain the binary tree’s completeness, and “percolate” the new element up to its appropriate position to maintain the heap order property. CS 340 Page 103

  5. 29 Delete Min 43 51 63 77 43 51 65 58 91 73 63 77 75 87 80 65 58 91 73 75 87 80 43 Percolate Down Percolate Down 43 58 51 63 77 65 73 51 91 75 87 80 63 77 65 58 91 73 75 87 80 Deleting From A Heap When deleting the minimum element from a heap, create a “hole” node at the root (where the minimum element was), and slide the smaller of the hole’s offspring up until an appropriate slot is found for the last element. CS 340 Page 104

  6. PC2A PC2B ATM SWITCH #2 PC1A PC1B PC2C TOKEN RING #2 PC1C PC2D PC1G PC2G TOKEN RING #1 FIBER BACKBONE PC1F PC2E ATM SWITCH #1 PC2F PC1D PC1E Example Application: Discrete Event Simulation Rather than going to the expense of implementing a complicated system, sometimes it is possible to simulate the system using a statistical model, and to work out the obvious bugs prior to actual implementation. A heap makes a convenient structure in such simulations, where the heap nodes represent the discrete “events” of the system, ordered according to the time at which they “occur”. Network Simulation Example CS 340 Page 105

  7. Network Simulation Example (Continued) Delete Minimum... Process Event Delete Minimum Delete Minimum Process Event 045: PC1B xmits on TR1 053: ATMS1 xmits on FB 049: ATMS2 recvs on TR2 072: PC1B xmits on TR1 068: PC2F xmits on TR2 080: PC1D xmits on TR1 059: PC2B recvs on TR2 049: ATMS2 recvs on TR2 049: ATMS2 recvs on TR2 053: ATMS1 xmits on FB 053: ATMS1 xmits on FB 059: PC2B recvs on TR2 059: PC2B recvs on TR2 072: PC1B xmits on TR1 072: PC1B xmits on TR1 068: PC2F xmits on TR2 068: PC2F xmits on TR2 080: PC1D xmits on TR1 080: PC1D xmits on TR1 047: ATMS1 recvs on TR1 049: ATMS2 recvs on TR2 053: ATMS1 xmits on FB 053: ATMS1 xmits on FB 050: ATMS1 xmits on FB 049: ATMS2 recvs on TR2 072: PC1B xmits on TR1 072: PC1B xmits on TR1 068: PC2F xmits on TR2 068: PC2F xmits on TR2 080: PC1D xmits on TR1 080: PC1D xmits on TR1 059: PC2B recvs on TR2 059: PC2B recvs on TR2 CS 340 Page 106

  8. 37 39 42 55 48 63 41 46 64 75 49 52 71 90 54 87 78 93 10 17 12 13 30 25 31 47 23 15 45 61 58 Merging Heaps One weakness of the heap structure is the difficulty with which two heaps are merged into one. How would you merge the two heaps above into a single heap, maintaining the completeness of the binary tree and the heap order property? When might it be necessary to merge two heaps? • One printer goes down, and its print jobs are redirected to a second printer with its own priority queue. • One network route becomes too congested so a switch must merge the forwarding queues for two of its outgoing lines. • The operating system for a multiprocessor system decides to devote one processor to a certain task, merging its job queue with that of another processor. CS 340 Page 107

  9. 10 7 17 12 11 59 13 77 25 31 24 46 23 45 61 58 37 63 50 71 84 95 Leftist Heaps One solution to the priority queue merging problem is the leftist heap. For any node X in a binary tree, define nullPathLength(X) to be the length of the shortest path from X to a descendant node without two children. A leftist heap is a binary tree with the heap order property (i.e., every node’s value is less than or equal to its offsprings’ values), as well as the leftist heap property: the null path length of each node’s left child is greater than or equal to the null path length of its right child. Like the heap, the leftist heap performs insertions and removals in O(logn) time, but the leftist heap also performs merges in O(logn) time, a big improvement over the heap’s O(n) merge time. Examples: CS 340 Page 108

  10. Merging Leftist Heaps template <classEtype> leftNode<Etype> * leftistHeap<Etype>:: Merge(leftNode<Etype> *h1, leftNode<Etype> *h2) { if (h1 == NULL) return h2; if (h2 == NULL) return h1; if (h2->element > h1->element) return Merge1(h1, h2); return Merge1(h2, h1); } template <classEtype> leftNode<Etype> * leftistHeap<Etype>:: Merge1(leftNode<Etype> *h1, leftNode<Etype> *h2) { if (h1->left == NULL) h1->left = h2; else { h1->right = Merge(h1->right, h2); if (h1->left->nullPathLength < h1->right->nullPathLength) Swap(h1->left, h1->right); h1->nullPathLength = h1->right->nullPathLength + 1; } return h1; } CS 340 Page 109

  11. 10 7 7 17 12 11 10 11 59 25 31 13 77 24 46 17 12 7 24 46 45 61 58 23 37 63 50 25 31 13 59 11 37 63 50 71 84 45 61 58 23 77 Final Swap 24 46 71 84 Original Merge Call 95 10 10 10 37 63 50 95 17 12 17 17 12 12 59 71 84 25 31 13 59 25 25 31 31 13 13 77 59 95 45 61 58 23 77 45 45 61 61 58 58 23 23 1st Recursive Call 77 59 12 12 77 2nd Recursive Call 13 13 77 59 77 59 3rd Recursive Call 23 23 59 77 Leftist Heap Merging Example CS 340 Page 110

  12. STL Priority Queues #include <queue> #include <vector> #include <iostream> using namespace std; void main( ) { // The first priority_queue uses the // default vector base container priority_queue <int> q1; q1.push( 87 ); q1.push( 65 ); q1.push( 43 ); q1.push( 21 ); cout << "q1 = ( "; while ( !q1.empty( ) ) { cout << q1.top( ) << " "; q1.pop( ); } cout << ")" << endl; // The second priority_queue uses the vector // base container, but specifies that the comparison // function greater be used for ordering elements, // i.e., that the priority queue be a min-heap. priority_queue <int, vector<int>, greater<int>> q2; q2.push( 87 ); q2.push( 65 ); q2.push( 43 ); q2.push( 21 ); cout << "q2 = ( "; while ( !q2.empty( ) ) { cout << q2.top( ) << " "; q2.pop( ); } cout << ")" << endl; } The Standard Template Library includes a template class for priority queues, priority_queue<Object>, implemented as a maximum heap. CS 340 Page 111

More Related