Tutorial 10 Heap & Priority Queue. Heap – What is it? Special complete binary tree data structure –Complete BT: no empty node when checked top-down (level.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

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.
Sorting (2nd part) & Binary Search Tree
Heaps CSE 331 Section 2 James Daly. Reminder Project 2 is out Covers tree sets Due Friday at midnight Exam 1 will be Thursday, February 26 Review next.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
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
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
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.
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
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.
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
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:
Computer Science and Software Engineering University of Wisconsin - Platteville 12. Heap Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus.
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.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Brought to you by Max (ICQ: TEL: ) February 5, 2005 Advanced Data Structures Introduction.
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.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
data ordered along paths from root to leaf
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
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.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
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.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
What is a heap? Always keep the thing we are most interested in close to the top (and fast to access). Like a binary search tree, but less structured.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
David Luebke 1 12/23/2015 Heaps & Priority Queues.
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Priority Queues and Binary Heaps Algorithms.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Chapter 6: Heapsort Combines the good qualities of insertion sort (sort in place) and merge sort (speed) Based on a data structure called a “binary heap”
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
Chapter 13 Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-in-first-out The “smallest”
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.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
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.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
CS 367 Introduction to Data Structures Lecture 8.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
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.
Sorting Cont. Quick Sort As the name implies quicksort is the fastest known sorting algorithm in practice. Quick-sort is a randomized sorting algorithm.
Priority Queues, Heaps, and Heapsort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1 Last modified: 2/22/2016.
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.
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)
"Teachers open the door, but you must enter by yourself. "
Hashing Exercises.
Heap Sort Example Qamar Abbas.
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.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Priority Queues.
i206: Lecture 14: Heaps, Graphs intro.
Priority Queues.
Tree Representation Heap.
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps
Presentation transcript:

Tutorial 10 Heap & Priority Queue

Heap – What is it? Special complete binary tree data structure –Complete BT: no empty node when checked top-down (level by level), left to right! –We actually implement this using compact array (or vector for size flexibility) One to one mapping between complete binary tree and compact array! We have a very efficient Heap manipulation using array/vector –Index start from 0 –Parent(i): floor((i-1)/2) –Left(i): 2i+1 –Right(i): 2i+2 –Special because it must satisfy recursive heap property for each node x: The node x is larger than any of its left/right children (MaxHeap), or The node x is smaller than any of its left/right children (MinHeap) PS: Compare this with BST property! –Discussed in q2.

Heap – Operations Note: some other lecture notes/text books use different terms! Two key operations: bubble up or bubble down –Fix heap property by bubbling a node upwards (exchange with parent) or downwards (exchange with one of the largest/smallest child). Standard operations –Insertion: HeapInsert() a.k.a: fixHeap(), BubbleUp() Insert to last, then bubble up  O(log n), see q1 –Deletion: HeapDelete() a.k.a: ExtractMax(), ExtractMin() Take the root, replace with last, and then bubble down  O(log n) For deleting non-root nodes, see q1 –No default searching operation: We usually only access root (max/min) in O(1) Accessing any other node will normally incur O(n) cost, but see q3 –Updating a node value requires bubble up or bubble down, see q3 Special operation –Array  Heap: Heapify() a.k.a: HeapConstruction()  O(n), not O(n log n), see q1

Heap - Usage Heap can be used as an efficient Priority Queue –Items are inserted as per normal O(log n) –But these items will come out (de-queued) based on their priority value! As we perform ExtractMax() or ExtractMin() from a max (or min) heap! O(log n) –Faster than using other data structure, e.g. sorted array Heap can also be used for sorting (discussed in q3): –HeapSort() – O(n log n) –Or even for “partial sort” – O(k log n), e.g. Google default top 10 search results The term “Barack Obama” on 6 Nov 08 produces 83 million hits Sorting (80m log 80m) and display top 10 is “slow” Create heap O(80m) + take top-10 in O(10 log 80m) is much faster Heap can be built beforehand and stored in Google server, so it is just O(10 log 80m) However, we can do better by mapping search query into an “answer page” in O(1)… –Only possible if we have large storage space. –Other sorting algorithms can do partial sort, but not natural Quick sort can stop processing right part if pivot > k Partitioning algorithm will ensure everything on the left side of pivot is the top k…

Student Presentation Gr3 (average: 2 times) 1.Rebecca Chen or Cao Hoangdang or Ding Ying Shiaun or Du Xin 2.Nur Liyana Bte Roslie or Jashan Deep Kaur 3.Huang Chuanxian or Leow Wei Jie or Tan Kar Ann Gr4 (average: 3 times) 1.Cynthia Tan or Tan Peck Luan 2.Liew Hui Sun or Hanyenkno Afi 3.Goh Khoon Hiang or Jasmine Choy Overview of the questions: 1.Trace Heap Operations (1 or 2 students)Trace Heap Operations (1 or 2 students) 2.IsHeap(Array) (1 student)IsHeap(Array) (1 student) 3.FindKthBest (1, 2, or 3 students)FindKthBest (1, 2, or 3 students) Gr5 (average: 4 times) 1.Zheng Yang 2.Stephanie Teo 3.Tan Yan Hao Gr6 (average: 3 times) 1.Koh Jye Yiing or Wang Shuling 2.Siddhartha or Laura Chua 3.Rasheilla or Brenda Koh or Gary Kwong or Low Wei Chen 5

Q1 – Trace Heap Operations Insert 3, 1, 4, 1, 5, 9, 2, 6, 4 to empty max heap, delete 5, O(n log n) Heapify array 3, 1, 4, 1, 5, 9, 2, 6, 4 to max heap, delete 6, O(n) The resulting max heap (before deleting 5 and 6, respectively) are different but both are valid. However, Heapify() is faster as it is O(n). Link

Q2 –IsHeap(Array)? class BinaryHeap { int currentSize; //maintain the number of elements in the heap Comparable[] array; //stores the heap elements BinaryHeap() { //constructor currentSize = 0; array = new Comparable[ DEFAULT_CAPACITY ]; } // with other standard heap methods... } Give code for checking if the heap is a valid max-heap: (is a complete tree and maintains heap property)

Q2 –IsHeap(Array)? (Answer) bool isHeap() { for (int i = 0; i < currentSize; i++) { if (array[i] == NULL) // check if compact array == complete binary tree return false; // not a complete tree int child = 2 * i + 1; // check heap property if (child < currentSize) { if ((child+1) < currentSize) child = (array[child]>array[child+1])?child:(child+1); if (array[child] > array[i]) return false; } return true; } Link

Q3 – FindKthBest (1) findKthBest(int k), which returns the athlete with the k th least penalty points. What is the complexity? –Take/delete top k-1 items from min heap, so, after k-1 steps, you get the k th one as the root of the remaining min heap (the answer), this is (k-1) log n –Then, restore the deleted k-1 items into the heap again so that the next queries are correct, this is another (k-1) log n –Overall: (2k-2) log n which is O(k log n) –The complexity depends on k and n, ‘k’ cannot be dropped… update(Athlete, points), which adds the new penalty points to Athletes record. What is the complexity? –Use hash table to map Athlete to index in Heap data structure! Otherwise we need O(n) to scan for the correct index for this Athlete, now just O(1) –When we modify the value (points) in Heap, we need to bubble up/down to fix the heap property, which is maximum O(log n)… Update the corresponding values in Hash Table too, which is O(1) per update. –Overall complexity is O(1) + O(log n) * O(1) = O(log n)

Q3 – FindKthBest (2) Is there some other data structure which can be used to give more optimized performance? (findKthBest + update) –If the data is static, we can put the scores in array, sort the array one time O(n log n) and return array[k-1] O(1) as the answer, faster than O(k log n) per query in Q3a. –However, the data will be frequently updated, thus, this method will be slow, as re-sorting the array takes O(n log n)! –Since data is going to be frequently updated and we want top k results, we can use an augmented BST instead of a heap to store the Athlete objects! – –Store the number of nodes in the left and right sub-tree in each node! Thus, we can know where to go (left or right or stay) to find index-k in O(log n)! –BST would take O(n lg n) time to create (one time task), but the query time for findKthBest will be O(log n), faster than O(k log n) –The time for update would remain unchanged, still O(log n), delete old value O(log n) and re-insert new value to BST O(log n).

Homework: Trace Heap Sort Perform partial sort to get top 3 results on this max heap (from q1)! Continue all the way to complete the Heap Sort on this max heap! Link

Class Photo I usually take class photo every semester Please move to the center We will take two photos: –One without me. –One with me (one student will do the photo taking) After Thursday, you can see our photos here: –

Advertisement CS1102/C IEEE Revision Series Conducted by: me… Very relevant for this module! –Venue: LT 3 –Time: Friday,14 November 2008, 6-9pm I will discuss past papers and share how to tackle CS1102/C exam! Be there… you do not want to miss this!

Questions from last semesters Use them as additional learning materials…

Heap Insert/Delete Given the minheap h below, Show what the minheap h would look like after each of the following pseudocode operations one after another: a)h.heapInsert(8) b)h.heapInsert(5) c)h.heapDelete()

Solution a)h.heapInsert(8) b)h.heapInsert(5) c)h.heapDelete()

Extension After these three operations a)h.heapInsert(8) b)h.heapInsert(5) c)h.heapDelete() Continue with this: d)h.heapInsert(14) e)h.heapInsert(1) // assume this is 1a f)h.heapDelete() g)h.heapInsert(1) // duplicate, this is 1b h)h.heapInsert(15)

Extension – Solution Final heap:

Heap Construction Show the result of inserting: 12, 10, 1, 14, 6, 5, 8, 15, 3, 9, 7, 4, 11, 13, and 2 One at a time! Into an initially empty maxheap... Then show/compare the result by using the heap construction algorithm instead!

Solution Using Individual Insertions Using Heap Construction Algorithm