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

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
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
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 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a heap abstract data structure Demonstrate.
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
Leftist Heaps Text Read Weiss, §23.1 (Skew Heaps) Leftist Heap Definition of null path length Definition of leftist heap Building a Leftist Heap Sequence.
Priority Queues And the amazing binary heap Chapter 20 in DS&PS Chapter 6 in DS&AA.
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.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
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.
CS 261 – Data Structures Priority Queues & Heaps.
1 Priority Queues A priority queue is an ADT where: –Each element has an associated priority –Efficient extraction of the highest-priority element is supported.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Dr. Andrew Wallace PhD BEng(hons) EurIng
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
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.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
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.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Cpt S 223 – Advanced Data Structures Priority Queues
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
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.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2014W1 1.
Priority Queues (Heaps)
Foundations of Data Structures Practical Session #8 Heaps.
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.
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
Heaps Priority Queues. 1/6/2016Data Structure: Heaps2 Outline Binary heaps Binomial queues Leftist heaps.
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.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
CS 367 Introduction to Data Structures Lecture 8.
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 Priority Queues (Heaps)  Sections 6.1 to Priority Queues  Regular queues which supports –First In, First Out –Enqueue(): add a new element.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
"Teachers open the door, but you must enter by yourself. "
CS 201 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
Priority Queues (Heaps)
Source: Muangsin / Weiss
CMSC 341 Lecture 13 Leftist Heaps
Chapter 8 – Binary Search Tree
Heapsort Heap & Priority Queue.
Ch. 8 Priority Queues And Heaps
"Teachers open the door, but you must enter by yourself. "
CE 221 Data Structures and Algorithms
Heaps Priority Queues.
Priority Queues & Heaps
Priority Queues CSE 373 Data Structures.
Priority Queues & Heaps
Leftist Heaps Text Leftist Heap Building a Leftist Heap
Heaps & Multi-way Search Trees
Heaps Section 6.4, Pg. 309 (Section 9.1).
Priority Queues (Heaps)
Presentation transcript:

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

CS 340 Page 101 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. Priority Queues

CS 340 Page 102 A heap is a complete binary tree in which every node’s value is less than or equal to all of its offsprings’ values. One Priority Queue Implementation: The Heap 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 103 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. Inserting Into A Heap Insert Percolate Up

CS 340 Page 104 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. Deleting From A Heap Delete Min Percolate Down

CS 340 Page 105 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”. Example Application: Discrete Event Simulation Network Simulation Example

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

CS 340 Page 107 One weakness of the heap structure is the difficulty with which two heaps are merged into one. Merging Heaps 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 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. 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. 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 108 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: Leftist Heaps

CS 340 Page 109 template leftNode * leftistHeap :: Merge(leftNode *h1, leftNode *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 leftNode * leftistHeap :: Merge1(leftNode *h1, leftNode *h2) { if (h1->left == NULL) h1->left = h2; else { h1->right = Merge(h1->right, h2); if (h1->left->nullPathLength right->nullPathLength) Swap(h1->left, h1->right); h1->nullPathLength = h1->right->nullPathLength + 1; } return h1; } template leftNode * leftistHeap :: Merge(leftNode *h1, leftNode *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 leftNode * leftistHeap :: Merge1(leftNode *h1, leftNode *h2) { if (h1->left == NULL) h1->left = h2; else { h1->right = Merge(h1->right, h2); if (h1->left->nullPathLength right->nullPathLength) Swap(h1->left, h1->right); h1->nullPathLength = h1->right->nullPathLength + 1; } return h1; } Merging Leftist Heaps

CS 340 Page 110 Leftist Heap Merging Example Original Merge Call st Recursive Call nd Recursive Call rd Recursive Call Final Swap

CS 340 Page 111 STL Priority Queues The Standard Template Library includes a template class for priority queues, priority_queue, implemented as a maximum heap. #include using namespace std; void main( ) { // The first priority_queue uses the // default vector base container priority_queue 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, greater > 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; } #include using namespace std; void main( ) { // The first priority_queue uses the // default vector base container priority_queue 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, greater > 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; }