Priority Queues (Heaps)

Slides:



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

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.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
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.
Heaps, Heap Sort, and Priority Queues
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.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
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.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
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.
Heap: A Special Kind of Tree
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
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.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Heaps Priority Queues. 1/6/2016Data Structure: Heaps2 Outline Binary heaps Binomial queues Leftist heaps.
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
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.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
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.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
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)
CS 201 Data Structures and Algorithms
Priority Queues (Heaps)
Binary Heaps Priority Queues
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
CMSC 341 Lecture 13 Leftist Heaps
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
Heaps, Heap Sort, and Priority Queues
Binary Heaps Priority Queues
CMSC 341 Lecture 14 Priority Queues & Heaps
Priority Queue and Binary Heap Neil Tang 02/12/2008
Heaps and the Heapsort Heaps and priority queues
Binary Heaps Priority Queues
CE 221 Data Structures and Algorithms
Heap code in C++ template <class eType>
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Priority Queues (Heaps)
Presentation transcript:

Priority Queues (Heaps) Efficient implementation of the priority queue ADT Uses of priority queues Advanced implementation of priority queues

Motivation Implementation of a job list for a printer Jobs should be first-in-first-service. Generally, jobs sent to a printer are placed on a queue. However, we need to deal with the following special cases: one job might be particularly important, so it might be desirable to allow that job to be run as soon as the printer is available. If, when the printer becomes available, there are several 1-page jobs and one 100-page job, it might be reasonable to make the long job go last, even if it is not the last job submitted. Need a new data structure!

Motivation Operating system scheduler in a multi-user environment Generally a process is allowed to run only for a fixed period of time. One algorithm uses a queue. Jobs are initially placed at the end of the queue. The scheduler will repeatedly take the first job on the queue, run it until either it finishes or its time limit is up, and place it at the end of the queue. This strategy is generally not appropriate. Very short jobs will seem to take a long time because of the wait involved to run. In general, it is important that short jobs finish as fast as possible. Short jobs should have precedence over long jobs. Furthermore, some jobs that are not short are still very important and should also have precedence. Need a new data structure!

What is a Priority Queue? A priority queue is a data structure that allows at least the following two operations: insert: inserts a data item into the priority queue. deleteMin: finds, returns, and removes the minimum element in the priority queue The minimum element is viewed as the data item with the highest priority in the priority queue. deleteMin insert Priority Queue Basic model of a priority queue

Simple Implementations Use a simple linked list Performing insertion at the front in O(1) Performing deletion of the minimum by traversing the list, which requires O(N) time. Use a linked list, but insist that the list be kept always sorted This makes insertions expensive (O(N)) and deleteMins cheap (O(1)). Use a binary search tree Performing both insert and deleteMin in O(logN) average-case time. Using a search tree could be overkill because it supports a host of operations (such as, find, findMax, remove, printTree) that are not required.

Binary Heap What is a binary heap (briefly called heap) ? A binary tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right. (Such a tree is known as a complete binary tree.) For every node x, the key in the parent of x is smaller than (or equal to) the key in x, with the exception of the root. (heap-order property)

Complete Binary Trees The height of a complete binary tree is logN, which is clearly O(logN). A complete binary tree of height h has between 2h and 2h+1-1 nodes. (using proof by induction) An array implementation A complete binary tree can be represented in an array and no links are necessary. For any element in array position j, The left child is in position 2j The right child is in the cell after the left child (2j+1) The parent is in position j/2 The only problem with this implementation is that an estimate of the maximum heap size is required in advance, but typically this is not a problem (and we can resize if needed). 1 2 3 4 5 6 7 8 9 10 A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12

Class Interface for Binary Heap A heap data structure will, then, consist of an array (of Comparable objects) and an integer representing the current heap size. template <class comparable> class BinaryHeap { public: explicit BinaryHeap (int capacity =100); bool isEmpty ( ) const; bool isFull ( ) const; const Comparable & findMin ( ) const;   void insert ( const Comparable & x); void deleteMin ( ); // removes the minimum. void deleteMin ( Comparable & minItem); // removes the minimum and stores the removed value // in an object passed by reference. void makeEmpty ( ); private int currentSize; // Number of elements in heap vector <Comparable> array; // The heap array void buildHeap ( ); // Establish a heap from an arbitrary arrangement of items. void percolateDown ( int hole); // Internal method to percolate down in the heap. }; // hole is the index at which the percolate begins.

Basic Heap Operations - Insert Create a hole in the next available location (to make sure that the tree is a complete binary tree). If X can be placed in the hole without violating heap order, then we do so and are done. Otherwise we slide the element that is in the hole’s parent node into the hole, thus bubbling the hole up toward the root. Continue this process until X can be placed in the hole. for example: insert 14 31 31 21 31 21 14

Basic Heap Operations - Insert C++ code for the insert method /** * Insert item x into the binary heap, maintaining heap order. * Duplicates are allowed. * Throw Overflow if container if full. */ template <class Comparable> void BinaryHeap <Comparable> : : insert (const Comparable & x) { if isFull ( )) throw Overflow(); //percolate up int hole = ++currentSize; // create a hole in the next available location. for ( ; hole>1 && x <array[hole/2]; hole /=2) // do the “bubbling” process up to the root to make sure array[hole]=array[hole/2]; // that the smaller element is moved up. array[hole]=x; } The element of the parent node of the hole 1 2 3 4 5 6 7 8 9 10 11 12 A B C D E F G H I J hole = 1 means we have reached the root. Move up to its parent Do we really have to check hole>1 in each iteration of the loop? We can put a very small value in position 0 in order to make the loop terminate. This value must be guaranteed to be smaller than (or equal to) any element in the heap.

Running Time of insert The running of insert in the worst case is O(logN) In the case that the element to be inserted is the new minimum and is percolated all the way to the root. The height of the heap is logN = O(logN) On average, the percolation terminates early; it has been proven that 2.607 comparisons are required on average to perform an insert.

Basic Heap Operations - deleteMin Find the minimum is easy; the minimum element is always at the root. How to remove the minimum element? When the minimum is removed, a hole is create at the root. Since the heap now becomes one smaller, it follows that the last element X in the heap must move somewhere in the heap. Check if X can be placed in the hole If not, we slide the smaller of the hole’s children into the hole, thus pushing the hole down one level. Repeat this step until X can be place in the hole. 31 21 14 19

Basic Heap Operations - deleteMin For example: 31 21 14 19 31 21 14 19 14 19 21 31 31 21 14 19 21 14 26 19 31 31 21 14 26 19

Basic Heap Operations - deleteMin C++ code for deleteMin /** * Remove the smallest item from the binary heap * and place it in minItem. Throw Underflow if empty. */ template <class Comparable> void BinaryHeap <Comparable> : :deletMin (Comparable & minItem) { if (isEmpty ( ) ) throw Underflow(); minItem=array[1]; // the minimum element is always in the root. array[1]=array[currentSize--}; // place the last element in the root. percolateDown (1); // push this element down to the appropriate place. }

Basic Heap Operations - deleteMin C++ code for percolateDown /** * Internal method to percolate down in the heap. * hole is the index at which the percolate begins */ template <class Comparable> void BinaryHeap<Comparable> : : percolateDown (int hole) { int child; Comparable tmp = array [hole]; // the element we need to push down to the heap. for ( ; hole * 2 <=currentSize; hole=child) { child = hole * 2; // go to the left child. if (child!=currentSize && array[child+1] < array [child]) child++; // decide which child is smaller if (array[child]<tmp) array[hole]=array[child]; // percolate down else break; // find the appropriate place for X = tmp. } array[hole]=tmp; 31 21 14 19 31 21 14 26 19

Running Time - deleteMin The worst-case running time for deleteMin is O(logN). On average, the element that is placed at the root is percolated almost to the bottom of the heap (which is the level it came from), so the average running time is O(logN).

Priority Queues (Review) A priority queue is a data structure that allows at least the following two operations: insert: inserts a data item into the priority queue. deleteMin: finds, returns, and removes the minimum element in the priority queue The minimum element is viewed as the data item with the highest priority in the priority queue. Binary heap Complete binary tree: a binary tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right. Heap-order property: for every node x, the key in the parent of x is smaller than (or equal to) the key in x, with the exception of the root. A A A B C B B C D C D D

Priority Queues (Review) 1 2 3 4 5 6 7 8 9 10 A heap can be implemented using an array. For any element in array position j, The left child is in position 2j The right child is in the cell after the left child (2j+1) The parent is in position j/2 Insert data item X: Create a hole in the next available location (to make sure that the tree is a complete binary tree). Percolate the hole up toward the root so that X can be placed in the hole (to keep the heap-order property). deleteMin: Remove the element in the root and create a hole in the root. Percolate the hole down so that the last element in the heap can be placed in the hole. A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12 21 14 26 19 31 31 21 14 19 deleteMin 31 21 14 26 19 31 21 31 21 14 31 21 14 19 31 21 14 19 31 21 14 19 31 Insert 14

Build a Heap How to build a heap for N elements? build a complete binary tree first ignore the heap-order property at the moment apply percolate down for each node starting at the last non-leaf node. Example(s): build a heap for 150, 80, 40,30,10,70, 110, 100, 20, 90, 60, 50, 120, 140, 130 Build a complete binary tree 150 For any element in array position j, The left child is in position 2j The right child is in the cell after the left child (2j+1) The parent is in position j/2 80 40 30 10 70 110 100 20 90 60 50 120 140 130 1 2 3 4 5 6 7 8 9 10 11 12 150 80 40 30 70 110 100 20 90 13 14 15 60 50 120 140 130

Build a Heap Example(s): build a heap for 150, 80, 40,30,10,70, 110, 100, 20, 90, 60, 50, 120, 140, 130 apply percolate down for each node starting at the last non-leaf node Percolate 110 150 Percolate 70 150 80 40 30 10 70 110 100 20 90 60 50 120 140 130 80 40 30 10 70 110 70 100 20 90 60 50 120 140 130 150 80 40 30 10 50 110 100 20 90 60 70 120 140 130 Percolate 10 Percolate 30 150 80 40 30 10 50 110 100 20 90 60 70 120 140 130 10 30

Build a Heap Example(s): build a heap for 150, 80, 40,30,10,70, 110, 100, 20, 90, 60, 50, 120, 140, 130 apply percolate down for each node starting at the last non-leaf node 150 80 40 20 10 50 110 100 30 90 60 70 120 140 130 Percolate 40 40 Percolate 80 150 80 40 20 10 50 110 100 30 90 60 70 120 140 130 150 10 40 20 80 50 110 100 30 90 60 70 120 140 130 80

Build a Heap Example(s): build a heap for 150, 80, 40,30,10,70, 110, 100, 20, 90, 60, 50, 120, 140, 130 apply percolate down for each node starting at the last non-leaf node 150 10 40 20 60 50 110 100 30 90 80 70 120 140 130 150 10 150 40 20 60 50 110 100 30 90 80 70 120 140 130 Percolate 150 10 20 40 150 60 50 110 100 30 90 80 70 120 140 130 10 20 40 30 60 50 110 100 150 90 80 70 120 140 130

C++ code for buildHeap /** * Establish heap order property from an arbitrary arrangement of items. * Runs in linear time. */ template <class Comparable> void BinaryHeap <comparable> : : buildHeap ( ) { for (int i=currentSize/2; i>0; i--) percolateDown (i); } 150 80 40 30 10 70 110 100 20 90 60 50 120 140 130 The last non-leaf node in the complete binary tree. Percolate down non-leaf nodes one by one until the root is percolated down. 1 2 3 4 5 6 7 8 9 10 11 12 150 80 40 30 70 110 100 20 90 13 14 15 60 50 120 140 130

Performance of buildHeap 150 150 The running time of buildHeap is O(N). Why? Each non-leaf node may percolate down to a leaf. The number of edges that one non-leaf node traverses down to a leaf is at most its height. When a non-leaf node percolates down along one edge, we need to make two comparisons. One to find the smaller child One to compare the smaller child with the node. The total number of comparisons we need to build the heap is bounded by the the sum of the heights of all the nodes in the heap. 10 40 20 60 50 110 100 30 90 80 70 120 140 130 10 20 40 30 60 50 110 100 150 90 80 70 120 140 130 The height of a node is the length of the Longest path from that node to a leaf.

Performance of buildHeap 150 150 Why? (cont.) Estimate the sum of the heights of all the nodes in the heap. Assume the heap consists of N nodes. The height of the heap is h =  logN . The heap consists of 1 node at height h (root) 2 nodes at height h-1 22 nodes at height h-2 23 nodes at height h-3 …….. 2i nodes at height h-i The sum of the heights of all the nodes in the heap, We can prove that S = O(N). The total number of comparisons is O(N). The running time is O(N). 10 40 20 60 50 110 100 30 90 80 70 120 140 130 10 20 40 30 60 50 110 100 150 90 80 70 120 140 130

The Selection Problem (1) The problem: Given a list of N elements and an integer k, the selection problem asks to find the k th smallest element. A naïve algorithm is to sort the N elements using a simple sorting algorithm, and then pick up the k th smallest element, which takes O(N2) time. A better algorithm using heap Apply the buildHeap algorithm to the list of N elements. Perform k deleteMin operations. The last element extracted from the heap is our answer. 10 20 40 30 60 50 110 100 150 90 80 70 120 140 130 1 2 3 4 5 6 7 8 9 10 11 12 150 80 40 30 70 110 100 20 90 13 14 15 60 50 120 140 130

Examples for the Selection Problem (1) Find the 4th smallest element in 150, 80, 40,30,10,70, 110, 100, 20, 90, 60, 50, 120, 140, 130 Apply the buildHeap algorithm 10 20 40 30 60 50 110 100 150 90 80 70 120 140 130 1 2 3 4 5 6 7 8 9 10 11 12 150 80 40 30 70 110 100 20 90 13 14 15 60 50 120 140 130

Examples for the Selection Problem (1) Find the 4th smallest element in 150, 80, 40,30,10,70, 110, 100, 20, 90, 60, 50, 120, 140, 130 Perform 4 deleteMin operations 130 20 40 30 60 50 110 100 150 90 80 70 120 140 20 130 40 30 60 50 110 100 150 90 80 70 120 140 20 30 40 130 60 50 110 100 150 90 80 70 120 140 20 30 40 100 60 50 110 130 150 90 80 70 120 140 140 30 40 100 60 50 110 130 150 90 80 70 120 30 140 40 100 60 50 110 130 150 90 80 70 120 30 60 40 100 140 50 110 130 150 90 80 70 120 Remove 20 30 60 40 100 80 50 110 130 150 90 140 70 120 10 20 40 30 60 50 110 100 150 90 80 70 120 140 130 Remove 10 Remove 30 40 60 50 100 80 70 110 130 150 90 140 120 40 60 50 100 80 120 110 130 150 90 140 70 120 60 40 100 80 50 110 130 150 90 140 70 40 60 120 100 80 50 110 130 150 90 140 70 Remove 40 120 60 50 100 80 70 110 130 150 90 140 50 60 120 100 80 70 110 130 150 90 140 50 60 70 100 80 120 110 130 150 90 140

Running Time To construct the heap using buildHeap takes O(N) time. Each deleteMin takes O(logN) time; k deleteMins take O(klogN) time. The total running time is O(N+klogN).

The Selection Problem (2) The problem: Given a list of N elements and an integer k, the selection problem asks to find the k th largest element. Notice that although finding the minimum can be performed in constant time, a heap designed to find the minimum element is of no help in finding the maximum element. In fact, a heap has very little ordering information, so there is no way to find any particular element without a linear scan through the entire heap.

The Selection Problem (2) The problem: Given a list of N elements and an integer k, the selection problem asks to find the k th largest element. The algorithm: Read in the first k elements in the list and build a heap for these k elements. Obviously, the kth largest element of these k elements, denoted by Sk, is the smallest element of these k elements. Sk is in the root node. When a new element is read it is compared with Sk. If the new element is larger, then it replace Sk in the heap. The heap will then have a new smallest element Sk, which may or may not be the newly added element. At the end of the input, we find the smallest element in the heap and return it as the answer.

Examples for the Selection Problem (2) Find the 12th largest element in 150, 80, 40,30,10,70, 110, 100, 20, 90, 60, 50, 120, 140, 130 Construct a heap for the first 12th elements The 12th largest element is the smallest element in the heap. 10 20 40 30 60 50 110 100 150 90 80 70 150 80 40 30 10 70 110 100 20 90 60 50 120 140 130 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Examples for the Selection Problem (2) Find the 12th largest element in 150, 80, 40,30,10,70, 110, 100, 20, 90, 60, 50, 120, 140, 130 Read in 120, 140, 130 one by one and compare it with the smallest element in the heap. 10 20 40 30 60 50 110 100 150 90 80 70 120 20 40 30 60 50 110 100 150 90 80 70 20 30 40 120 60 50 110 100 150 90 80 70 20 120 40 30 60 50 110 100 150 90 80 70 20 30 40 100 60 50 110 120 150 90 80 70 Read in 120 Replace 10 with 120 Percolate 120 down to find the smallest element in the heap, that is the 12th largest element in the first 13 elements in the list. 1 2 3 4 5 6 7 8 9 10 11 12 150 80 40 30 70 110 100 20 90 13 14 15 60 50 120 140 130

Examples for the Selection Problem (2) Find the 12th largest element in 150, 80, 40,30,10,70, 110, 100, 20, 90, 60, 50, 120, 140, 130 Read in 120, 140, 130 one by one and compare it with the smallest element in the heap. 30 60 40 100 140 50 110 120 150 90 80 70 30 140 40 100 60 50 110 120 150 90 80 70 30 60 40 100 80 50 110 120 150 90 140 70 20 30 40 100 60 50 110 120 150 90 80 70 140 30 40 100 60 50 110 120 150 90 80 70 Read in 140 Replace 20 with 140 Percolate 140 down to find the smallest element in the heap, that is the 12th largest element in the first 14 elements in the list. 1 2 3 4 5 6 7 8 9 10 11 12 150 80 40 30 70 110 100 20 90 13 14 15 60 50 120 140 130

Examples for the Selection Problem (2) Find the 12th largest element in 150, 80, 40,30,10,70, 110, 100, 20, 90, 60, 50, 120, 140, 130 Read in 120, 140, 130 one by one and compare it with the smallest element in the heap. 30 60 40 100 80 50 110 120 150 90 140 70 130 60 40 100 80 50 110 120 150 90 140 70 40 60 50 100 80 130 110 120 150 90 140 70 40 60 130 100 80 50 110 120 150 90 140 70 40 60 50 100 80 70 110 120 150 90 140 130 Read in 130 Replace 30 with 130 Percolate 130 down to find the smallest element in the heap, that is the 12th largest element in the first 15 elements in the list. 1 2 3 4 5 6 7 8 9 10 11 12 150 80 40 30 70 110 100 20 90 13 14 15 60 50 120 140 130

Running Time It take O(k) time to build a heap for the first k elements in the list. We need to compare the last N-k elements one by one with the smallest element in the heap. To delete the smallest element in the heap and insert a new element if this is necessary takes O(logk) time. Totally, it takes O((N-k)logk) time. The running time is O(k + (N-k)logk) = O(Nlogk)

Leftist Heaps Binary heaps support: Insert: worst-case running time O(logN); average-case running time O(1). deleteMin: worst-case running time O(logN); average-case running time O(logN); Binary heaps cannot support merging efficiently (O(N)). A link-based data structure, called leftist heap, efficiently supports merging (O(logN)). We define the null path length, npl(X), of any node X in a binary tree to be the length of the shortest path from X to a node without two children. npl(NULL) = -1. The null path length of any node is 1 more than the minimum of the null path lengths of its children. 1 10 20 1 40 A path from node n1 to nk is defined as a sequence of nodes n1, n2, …, nk such that ni is the parent of ni+1 for 1 <= i < k, 30 60 90

What is a Leftist Heap? A binary tree The leftist heap property: for every node X in the heap, the null path length of the left child is at least as large as that of the right child. The heap-order property: for every node x, the key in the parent of x is smaller than (or equal to) the key in x, with the exception of the root. 1 10 1 10 20 40 20 40 30 60 30 60 90 90 80

Leftist Heap Property The leftist heap property: for every node X in the heap, the null path length of the left child is at least as large as that of the right child. This property does not ensure that the tree is balanced, because it clearly biases the tree to get deep toward the left. Leftist heaps may consist of a long path of left nodes; the right path ought to be short. 3 6 10 12 7 21 14 18 24 8 37 23 33 17 18 26

Length of Right Path Theorem: A leftist tree with r nodes on the right path must have at least 2r-1 nodes. Proof by induction (p. 230) The right path of a binary tree is the path from the root to the right most node. A leftist tree of N nodes has a right path containing at most log(N+1) nodes. The general idea for the leftist heap operations is to perform all the work on the right path, which is guaranteed to be short. The problem is that performing inserts and merges on the right path could destroy the leftist heap property. So, we need to restore the property. 3 6 10 12 7 21 14 18 24 8 37 23 33 17 18 26

Leftist Heap Operations  merge The fundamental operation on leftist heaps is merging. A recursive solution: merge H1 and H2 If either of the two heaps is empty, then we can return the other heap. Otherwise, First Step: recursively merge the heap with the larger root with the right subheap of the heap with smaller root. 3 10 8 21 14 17 23 26 6 12 7 18 24 37 18 33 H2 H1

Recursive Solution for merge Assume the recursive call returns the leftist heap below (it is reasonable to assume that the resulting tree is a leftist heap, because it was obtained via a recursive step). 3 6 10 8 12 7 21 14 17 H1 3 18 24 8 37 23 26 1 2 10 6 33 18 attach to H1 as its right child 17 Obtained by merging H2 with the right child of H1 21 14 12 7 26 23 18 24 8 37 The resulting heap satisfies the heap-order property. The root may not satisfy the the leftist property, but the remainder of the tree must be leftist. The right subtree of the root is leftist, because of the recursive step. The left subtree of the root has not changed, so it too must still be leftist. We only need to fix the root. 33 17 18 26

Recursive Solution for merge Second Step: Attach the leftist subheap returned by the recursive call as the right child of the root of the heap with smaller root. Third Step: If the resulting heap is not leftist, swap the root’s left and right children. 3 3 1 2 2 10 6 6 1 10 21 14 12 7 12 7 21 14 23 18 24 8 37 18 24 8 37 23 33 33 18 17 18 17 26 26

C++ Code for merge // left child // right child // null path length

C++ Code for merge If either of the two heaps is empty, then we return the other heap. Call merge1() to merge two leftist heaps. Be sure the root of the first heap is smaller than that of the second heap. Dealing with the special case that the smaller root of two input leftist heaps is a single node. Recursively merge the right subheap of h1 with h2, and attach the returned leftist heap as the right subheap of h1. If the null path length of the left child is samller than that of the right child, swap the root’s left and right children. Update the null path length information for the root.

Execution of the Recursive merge merge(H1, H2) 3 6 10 8 12 7 merge(3, 6) 21 14 17 18 24 37 18 H1 H2 23 33 26 8 6 12 7 17 merge(8, 6) merge(3, 6) 18 24 37 18 26 33

Execution of the Recursive merge 8 7 merge(8, 7) 17 37 18 merge(8, 6) merge(3, 6) 26 8 18 merge(8, 18) merge(8, 7) 17 merge(8, 6) merge(3, 6) 26 merge(8, 18) 18 merge(8, 7) NULL merge(8, 6) merge(3, 6)

Execution of the Recursive merge Return leftist heap: 18 Return to the call merge(8, 18) : 8 attach 8 18 merge(8, 7) 17 18 17 merge(8, 6) merge(3, 6) 26 26 Return to the call merge(8, 7) : attach swap 7 7 7 8 merge(8, 6) 37 37 8 17 18 8 37 merge(3, 6) 18 17 18 17 26 26 26

Execution of the Recursive merge Return to the call merge(8, 6) : attach 8 6 6 12 7 12 7 17 merge(3, 6) 18 24 8 37 18 18 24 37 26 33 33 17 18 26

Execution of the Recursive merge Return to the call merge(3, 6) : 3 6 10 8 12 7 21 14 17 18 24 37 18 attach 23 33 26 swap 3 3 6 10 6 10 7 12 7 12 21 14 21 14 8 18 24 8 37 18 24 37 23 23 18 33 17 18 33 17 26 26

Running Time of the Recursive merge The recursive calls are performed along the right paths of both input leftist heaps. The sum of the length of the right paths of both input heaps is O(logN). The length of the right path of the first input leftist heap is at most log(N+1) - 1. At each node visited during the recursive calls, only constant work (attaching and swapping if necessary) is performed. The running time of merge is O(logN).

Merge Leftist Heaps (review) A recursive solution for merging two leftist heaps, H1 and H2 If either of the two heaps is empty, then we can return the other heap. Otherwise, First Step: recursively merge the heap with the larger root with the right subheap of the heap with smaller root. Second Step: Attach the leftist subheap returned by the recursive call as the right child of the root of the heap with smaller root. Third Step: If the resulting heap is not leftist, swap the root’s left and right children.

Non-Recursive Solution for merge To fulfill the merge operation non-recursively, we perform two passes. The first pass: Create a new tree by merging the right paths of both heaps. To do this, we arrange the nodes on the right paths of H1 and H2 in sorted order, keeping their respective left children. 3 10 8 21 14 17 23 26 6 12 7 18 24 37 18 H1 H2 33

Non-Recursive Solution for merge How to merge the right paths of both heaps? The nodes in each path from the root in a leftist heap is in an increasing order. A path from node n1 to nk is defined as a sequence of nodes n1, n2, …, nk such that ni is the parent of ni+1for 1 <= i < k Based on the heap-order property, ni is smaller than ni+1for 1 <= i < k The nodes on the right paths of H1 and H2 are in an increasing order. 3 6 10 8 12 7 21 14 17 18 24 37 18 23 26 H1 H2 33

Non-Recursive Solution for merge How to merge the right paths of both heaps? (cont.) To arrange the nodes on the right paths of H1 and H2 in sorted order (keeping their respective left children), we only need to perform one pass merge. 3 8 merge 10 17 3 10 21 14 23 6 12 18 24 33 7 37 8 17 26 18 21 14 26 H1 23 6 7 18 12 37 The resulting heap satisfies the heap-order property. 18 24 H2 Running time: O(logN) 33

Non-Recursive Solution for merge To fulfill the merge operation non-recursively, we perform two passes. The second pass: A second pass is made up the heap, and child swaps are performed at nodes along the right path that violate the leftist heap property. 2 swap 3 3 1 6 2 10 6 10 1 12 1 7 7 12 21 14 21 14 1 37 8 8 37 18 24 18 24 23 23 33 17 18 17 18 33 26 26

Running Time of Non-Recursive merge To fulfill the merge operation non-recursively, we perform two passes. The first pass: Create a new tree by merging the right paths of both heaps. To do this, we arrange the nodes on the right paths of H1 and H2 in sorted order, keeping their respective left children. The second pass: A second pass is made up the heap, and child swaps are performed at nodes along the right path that violate the leftist heap property. Total running time is O(logN) The first pass takes O(logN) time The second pass takes O(logN) time

Other Leftist Heap Operations insert: we carry out insertions by making the item to be inserted a one-node heap and performing a merge. deleteMin: remove the root, creating two heaps, which can then be merged. The running time of both insert and deleteMin is O(logN).