Binary Heaps Priority Queues

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.
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
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.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
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
CS 315 Lecture 15 March 31 Goals Complete heap operations review insert, deletemin decreaseKey, increaseKey heap building Heap sorting Some applications.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
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.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Chapter 21 Binary Heap.
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.
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.
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.
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)
Topics covered (since exam 1):
Binary Heaps Priority Queues
Source: Muangsin / Weiss
Binary Heaps What is a Binary Heap?
Bohyung Han CSE, POSTECH
CMSC 341 Lecture 13 Leftist Heaps
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
The Heap Data Structure
Binary Heaps What is a Binary Heap?
Binary Heaps What is a Binary Heap?
ADT Heap data structure
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
Part-D1 Priority Queues
Binary Heaps Priority Queues
Data Structures & Algorithms Priority Queues & HeapSort
Heapsort Heap & Priority Queue.
Topics covered (since exam 1):
CMSC 341 Lecture 14 Priority Queues & Heaps
Binary Tree Application Operations in Heaps
BuildHeap The general algorithm is to place the N keys in an array and consider it to be an unordered binary tree. The following algorithm will build a.
Heaps and the Heapsort Heaps and priority queues
CMSC 341 Binary Search Trees.
Heap Sort The Heap Data Structure
CE 221 Data Structures and Algorithms
Heaps Priority Queues.
CMSC 341 Binary Search Trees.
Heap code in C++ template <class eType>
CMSC 341 Lecture 19.
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
Topics covered (since exam 1):
Heaps & Multi-way Search Trees
Priority Queues (Heaps)
Presentation transcript:

Binary Heaps Priority Queues CMSC 341 Binary Heaps Priority Queues

Priority Queues Priority: some property of an object that allows it to be prioritized with respect to other objects of the same type Min Priority Queue: homogeneous collection of Comparables with the following operations (duplicates are allowed). Smaller value means higher priority. void insert (const Comparable &x) void deleteMin( ) void deleteMin ( Comparable & min) const Comparable &findMin( ) const Construct from a set of initial values bool isEmpty( ) const bool isFull( ) const void makeEmpty( ) void insert (const Comparable &x) -- inserts x into the queue. Error if PQ is full. void deleteMin() -- removes the element with minimum priority. It is an error to attempt this operation on an empty PQ. void deleteMin(Comparable &x) -- remove min and return it in x. Error if PQ is empty. const Comparable &findMin() const -- return the min element. Error if PQ is empty. Construct from set of initial values bool isEmpty() const; bool isFull() const; void makeEmpty(); This is a min queue. A max queue would be similar with operations deleteMax instead of deleteMin. 4/7/2006

Priority Queue Applications Printer management: the shorter document on the printer queue, the higher its priority. Jobs queue within an operating system: users’ tasks are given priorities. System priority high. Simulations the time an event “happens” is its priority Sorting (heap sort) an elements “value” is its priority 4/7/2006

Possible Implementations Use a sorted list. Sorted by priority upon insertion. findMin( ) --> list.front( ) insert( ) --> list.insert( ) deleteMin( ) --> list.erase( list.begin( ) ) Use ordinary BST findMin( ) --> tree.findMin( ) insert( ) --> tree.insert( ) deleteMin( ) --> tree.delete( tree.findMin( ) ) Use balanced BST guaranteed O(lg n) for Red-Black Sorted list findMin O(1) insert O(n) deleteMin O(1) BST findMin O(n) worst, O(lg n) avg insert same deleteMin same Sorted list: too slow BST: overkill (many ops not needed for PQ), storage costs, O(n lg n) construction 4/7/2006

Min Binary Heap A min binary heap is a complete binary tree with the further property that at every node neither child is smaller than the value in that node (or equivalently, both children are at least as large as that node). This property is called a partial ordering. As a result of this partial ordering, every path from the root to a leaf visits nodes in a non-decreasing order. What other properties of the Min Binary Heap result from this property? Other Properties min is at the root max is in a leaf Also mention max binary heap 4/7/2006

Min Binary Heap Performance Performance (n is the number of elements in the heap) construction O( n ) findMin O( 1 ) insert O( lg n ) deleteMin O( lg n ) Heap efficiency results, in part, from the implementation conceptually a complete binary tree implementation in an array/vector (in level order) with the root at index 1 Draw tree and corresponding array 4/7/2006

Min Binary Heap Properties For a node at index i its left child is at index 2i its right child is at index 2i+1 its parent is at index i/2 No pointer storage Fast computation of 2i and i/2 by bit shifting i << 1 = 2i i >> 1 = i/2 4/7/2006

Min BinaryHeap Definition template <typename Comparable> class MinBinaryHeap { public: explicit MinBinaryHeap(int capacity = BIG); explicit MinBinaryHeap(vector<Comparable>& items); bool isEmpty() const; const Comparable& findMin() const; void insert (const Comparable& x); void deleteMin(); void deleteMin(Comparable& min_item); void makeEmpty(); private: int currentSize; vector< Comparable > array; void buildHeap(); void percolateDown(int hole); }; 4/7/2006

MinBinaryHeap Implementation template <typename Comparable> const Comparable& MinBinaryHeap::findMin( ) const { if ( isEmpty( ) ) throw Underflow( ); return array[1]; } findMin: just return element at the root worst case asymptotic performance = O(1) 4/7/2006

Insert Operation Must maintain CBT property (heap shape): easy, just insert new element at “the end” of the array Min heap order could be wrong after insertion if new element is smaller than its ancestors continuously swap the new element with its parent until parent is not greater than it called sift up or percolate up Performance of insert is O( lg n ) in the worst case because the height of a CBT is O( lg n ) Example 2 8 3 10 16 7 18 13 15 Insert 4 (at index 10) 13 15 4 percolate up 4 3 10 8 7 18 13 15 16 4/7/2006

MinBinaryHeap Insert (cont’d) template <typename Comparable> void MinBinaryHeap<Comparable>:: insert(const Comparable & x) { if (currentSize == array.size( ) -1) array.resize( array.size( ) * 2) int hole = ++currentSize; // percolate up for (; hole > 1 && x < array[hole/2]; hole /= 2) array[hole] = array[hole/2]; // put x in hole array[hole] = x; } Loop conditions: not at root yet parent larger than x Increment: move hole to parent slot 4/7/2006

Deletion Operation Steps remove min element (the root) maintain heap shape maintain min heap order To maintain heap shape, actual node removed is “last one” in the array replace root value with value from last node and delete last node sift-down the new root value continually exchange value with the smaller child until no child is smaller 4/7/2006

MinBinaryHeap Deletion(cont) template <typename Comparable> void MinBinaryHeap<Comparable>:: deleteMin(Comparable& minItem) { if ( isEmpty( ) ) throw Underflow( ); minItem = array[1]; array[1] = array[currentSize--]; percolateDown(1); } percolateDown just moves element down to its right spot, swapping others up as it goes. Always swapped with the smaller child. Worst case: element goes from root to leaf = O(lg n) Do examples. 4/7/2006

MinBinaryHeap PercolateDown (cont’d) template <typename Comparable> void MinBinaryHeap<Comparable>::percolateDown(int hole) { int child; Comparable tmp = array[hole]; for (; hole * 2 <= currentSize; hole = child) child= hole * 2; if (child != currentSize && array[child + 1] < array[ child ] ) child++; if (array [child] < tmp) array[ hole ] = array[ child ]; else break; } array[hole] = tmp;

Constructing a Min Binary Heap A BH can be constructed in O(n) time. Suppose we are given an array of objects in an arbitrary order. Since it’s an array with no holes, it’s already a CBT. It can be put into heap order in O(n) time. Create the array and store n elements in it in arbitrary order. O(n) to copy all the objects. Heapify the array starting in the “middle” and working your way up towards the root for (int index = n/2 ; index > 0; index--) percolateDown( index ); Example: Insert in level order: 18, 2, 13, 10, 15, 3, 7, 16, 8 n = 9, n/2 = 4 siftDown(4) -- 10 moves down siftDown(3) -- 13 moves down siftDown(2) -- no action required siftDown(1) -- 18 moves down 4/7/2006

Constructing a Min BinaryHeap (cont’d) template <typename Comparable> MinBinaryHeap( const vector<Comparable>& items ) :array(items.size() + 1), currentSize((items.size( )) { for (int i = 0; i < items.size( ); i++) array[ i + 1 ] = items[ i ] ; buildHeap( ); } void MinBinaryHeap<Comparable>:: buildHeap( ) for(int i = currentSize/2; i > 0; i--) percolateDown( i ); 4/10/2006

Performance of Construction A CBT has 2h-1 nodes on level h-1. On level h-l, at most 1 swap is needed per node. On level h-2, at most 2 swaps are needed. … On level 0, at most h swaps are needed. Number of swaps = S = 2h*0 + 2h-1*1 + 2h-2*2 + … + 20*h = = h(2h+1-1) - ((h-1)2h+1+2) = 2h+1(h-(h-1))-h-2 = 2h+1-h-2 4/7/2006

Performance of Construction (cont) But 2h+1-h-2 = O(2h) But n = 1 + 2 + 4 + … + 2h = Therefore, n = O(2h) So S = O(n) A heap of n nodes can be built in O(n) time. 4/7/2006

Heap Sort Given n values we can sort them in place in O(n log n) time Insert values into array -- O(n) heapify -- O(n) repeatedly delete min -- O(lg n), n times Using a min heap, this code sorts in reverse (high down to low) order. With a max heap, it sorts in normal (low up to high) order. Given an unsorted array A[ ] of size n for (i = n-1; i >= 1; i--) { x = findMin( ); deleteMin( ); A[i+1] = x; } Sort: 2, 10, 3, 13, 17, 5, 9 4/7/2006

Limitations MinBinary heaps support insert, findMin, deleteMin, and construct efficiently. They do not efficiently support the meld or merge operation in which 2 BHs are merged into one. If H1 and H2 are of size n1 and n2, then the merge is in O(n1 + n2) . 4/7/2006

Leftist Min Heap Supports findMin -- O( 1 ) deleteMin -- O( lg n ) insert -- O( lg n ) construct -- O( n ) merge -- O( lg n ) 4/7/2006

Leftist Tree The null path length, npl(X), of a node, X, is defined as the length of the shortest path from X to a node without two children (a non-full node). Note that npl(NULL) = -1. A Leftist Tree is a binary tree in which at each node X, the null path length of X’s right child is not larger than the null path length of the X’s left child . I.E. the length of the path from X’s right child to its nearest non-full node is not larger than the length of the path from X’s left child to its nearest non-full node. An important property of leftist trees: At every node, the shortest path to a non-full node is along the rightmost path. “Proof”: Suppose this was not true. Then, at some node the path on the left would be shorter than the path on the right, violating the leftist tree definition. 4/10/2006

Leftist Min Heap A leftist min heap is a leftist tree in which the values in the nodes obey heap order (the tree is partially ordered). Since a LMH is not necessarily a CBT we do not implement it in an array. An explicit tree implementation is used. Operations findMin -- return root value, same as MBH deleteMin -- implemented using meld operation insert -- implemented using meld operation construct -- implemented using meld operation 4/7/2006

Meld Meld (H1, H2) // pseudo-code { // rename the LHs so that H1 has the smaller root (or is the only LH) if (!root( H1 ) || (root_value( H1 ) > root_value( H2 ) ) swap (H1, H2) // if H1 exists, meld H1’s right subtree with H2 // and replace H1’s right subtree with the result of the meld if ( root( H1 ) != NULL ) right( H1 ) = Meld( right( H1 ), H2 ) // if the null-path-length of H1’s left subtree is shorter // than the null-path length of H1’s right subtree, swap subtrees if (left_length( H1 ) < right_length( H1 ) swap( left( H1 ), right( H1 ); } 4/7/2006

Meld (cont) Performance: O( lg n ) the rightmost path of each tree has at most lg(n+1) nodes. So O( lg n ) nodes will be involved. 4/7/2006

4/7/2006

4/7/2006

4/7/2006

Student Exercise Show the steps needed to merge the Leftist Heaps below. The final result is shown on the next slide. 6 8 17 12 10 15 19 20 30 25 4/3/2007

Student Exercise Final Result 6 8 17 12 19 10 20 15 30 25 4/3/2007

Min Leftist Heap Operations Other operations implemented using Meld( ) insert (item) make item into a 1-node LH, X Meld(*this, X) deleteMin Meld(left subtree, right subtree) construct from N items make N LHs from the N values, one element in each meld each in one at a time (simple, but slow) use queue and build pairwise (complex but faster) 4/7/2006

LH Construct Algorithm: make N leftist heaps, H1….HN each with one data value Instantiate Queue<LeftistHeap> Q; for (i = 1; i <= N; i++) Q.Enqueue(Hi); Leftist Heap H = Q.Dequeue( ); while ( !Q.IsEmpty( ) ) Q.Enqueue( meld( H, Q.Dequeue( ) ) ); H = Q.Dequeue( ); Performance first time: N/2 melds, height = 1 second time: N/4 melds, height = 2 = N/2 + 2N/4 + 3N/8 + … 1 logN = O(N) 4/7/2006