CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.

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.
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.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting an.
Lec 6 Feb 17, 2011  Section 2.5 of text (review of heap)  Chapter 3.
Dijkstra/Prim 1 make-heap |V| insert |V| delete-min |E| decrease-key Priority Queues make-heap Operation insert find-min delete-min union decrease-key.
CS 315 Lecture 15 March 31 Goals Complete heap operations review insert, deletemin decreaseKey, increaseKey heap building Heap sorting Some applications.
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting an.
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on MinHeap: Insert Delete Converting an array.
CS 315 Lecture 18 Nov 15 Goals Complete heap operations review insert, deletemin decreaseKey, increaseKey heap building Heap sorting Some applications.
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.
Lecture 11 Binary Heap King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
1 Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters.
Fundamental Structures of Computer Science March 02, 2006 Ananda Guna Binomial 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.
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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.
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.
Priority Queues (Heaps)
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
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.
8 January Heap Sort CSE 2011 Winter Heap Sort Consider a priority queue with n items implemented by means of a heap  the space used is.
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.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
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.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003.
Priority Queues CS /02/05 L7: PQs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
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)
Source: Muangsin / Weiss
Binary Heaps What is a Binary Heap?
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
Binary Heaps What is a Binary Heap?
Binary Heaps What is a Binary Heap?
Heaps, Heap Sort, and Priority Queues
CMSC 341: Data Structures Priority Queues – Binary Heaps
Binary Heaps Priority Queues
CMSC 341 Lecture 14 Priority Queues & 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.
Binary Heaps Priority Queues
CE 221 Data Structures and Algorithms
Heap code in C++ template <class eType>
Priority Queues (Heaps)
Heaps & Multi-way Search Trees
Priority Queues (Heaps)
Presentation transcript:

CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap

Priority queue primary operations: Insert deleteMin secondary operations: Merge (union) decreaseKey increaseKey Performance goal: O(log n) for all operations (worst-case)

Array implementation of priority queue option 1: keep the array sorted insert O(n) time deleteMin O(1) time option 2: array is not sorted insert O(1) time deleteMin O(n) time Similar performance can be observed with sorted array and unsorted array. All these options require O(n) operations for one of the two operations.

More efficient implementations of priority queue binary heap skew heap binomial heap binary search tree No pointers are used (like in hashing) Uses pointer (like a linked list)

Binary Heap as an array and as a tree Array of 10 keys stored in A[1] through A[10] Can be viewed as a tree. Index j has index 2j and 2j+1 as children.

Connection between size and height of a heap h = 1 h = 2 h = 2 h = 3 h = 3 In general, if the number of nodes is N, height is at most = O(log N)

Max-Heap property Key at each node must be bigger than or equal to its two children. Min-heap: parent key < = child key. Left-child can be than right-child. See above example.

MinHeap and non-Min Heap examples A MinHeap Violates MinHeap property 21> Not a Heap Violates heap structural property Not a Heap Violates heap structural property Not a Heap

Heap class definition A class BinaryHeap {private: vector array; int currentSize; public: // member functions } Note: currentSize is different from the size of the vector (which is the number of allocated memory cells).

Insert 2 into the heap 1 4 Heap Insertion Example Size = 6, so initially, j= 6; Since H[j/2]= 4 > 2, H[3] is copied to H[6]. Thus the heap now looks as follows: The new value of j=3. Since H[j/2] =H[1] = 3 is also greater than 2, H[1] copied to H[3]. Now the heap looks as in the next slide. 6 Create a hole and percolate it up.

The new value of j = [3/2] = 1. Now, j/2 = 0 so the iteration stops. The last line sets H[j] = H[1] = The final heap looks as follows: Heap Insertion Example continued

Code for insert void insert(const Comparble & x) { if (currentSize == array.size() – 1) array.reSize(array.size() * 2); //percolate up int hole = ++currentSize; for (; hole > 1 && x < array[ hole / 2]; hole/= 2) array[ hole ] = array[ hole / 2 ]; array[ hole ] = x; } Try some more examples and make sure that the code works correctly.

Complexity of insert operation The height of a heap with n nodes is O(log n). The insert procedure percolates up along a path from a leaf to the root. Along the path at each level, one comparison and one data movement is performed. Conclusion: Insert performs O (log n) elementary operations (comparisons and data movements) to insert a key into a heap of size n in the worst- case.

DeleteMin Operation Observation: The minimum key is at the root. FindMin() can be performed in O(1) time: Comparable findMin() { if (currentSize == 0) cout << “heap is empty.” << endl; else return array[1]; } Deleting this key creates a hole at the root and we perform percolate down to fill the hole.

Example of DeleteMin After deleting the min key, the hole is at the root. How should we fill the hole? We also need to vacate the index 6 since the size of the heap now reduces to 5. We need to find the right place for key

Example of DeleteMin Hole should be filled with smallest key, and it is in array[2] or array[3].

Example of DeleteMin Try some more examples

DeleteMin – Outline of general case LR i Hole is currently in index i. its children are 2i and 2i+1. Suppose array[2i] = x, array[2i+1]= y. Case 1: (k <= x) && (k <= y) In this case, put k in array[I] and we are done. Case 2: (k > x) && (y > x). Thus, x is the minimum of the three keys k, x and y. We move the key x up so the hole percolates to 2i. Now the hole percolates down to 2i. Case 3: (k >x) && (x >y). Move y up and the hole percolates down to 2i+1. Case 4: the hole is at a leaf. put k there and this terminates ? x y Vacated key = k

Code for delete-min /** * Remove the minimum item and place it in minItem. * Throws Underflow if empty. */ void deleteMin( Comparable & minItem ) { if( isEmpty( ) ) throw UnderflowException( ); minItem = array[ 1 ]; array[ 1 ] = array[ currentSize-- ]; percolateDown( 1 ); }

Code for percolateDown void 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; }