CS 315 Lecture 18 Nov 15 Goals Complete heap operations review insert, deletemin decreaseKey, increaseKey heap building Heap sorting Some applications.

Slides:



Advertisements
Similar presentations
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue.
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.
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
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.
Lec 6 Feb 17, 2011  Section 2.5 of text (review of heap)  Chapter 3.
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
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.
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.
Lecture 11 Binary Heap King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
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 Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting.
Priority Queues, Heaps & Leftist Trees
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
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.
CSE 326: Data Structures Lecture #13 Priority Queues and Binary Heaps Nick Deibel Winter Quarter 2002.
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.
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.
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.
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.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Priority Queues (Heaps)
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue. Nov 4,
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
Heaps and Heapsort Prof. Sin-Min Lee Department of Computer Science San Jose State University.
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.
Chapter 12 Heaps & HeapSort © John Urrutia 2014, All Rights Reserved1.
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.
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.
CS 367 Introduction to Data Structures Lecture 8.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
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.
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). 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 An abstract data type (ADT) Similar to a queue
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
Binary Heaps What is a Binary Heap?
Heaps, Heap Sort, and Priority Queues
Binary Heaps Priority Queues
CMSC 341 Lecture 14 Priority Queues & Heaps
Heaps and the Heapsort Heaps and priority queues
Tree Representation Heap.
Binary Heaps Priority Queues
CE 221 Data Structures and Algorithms
Priority Queues An abstract data type (ADT) Similar to a queue
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
Heaps & Multi-way Search Trees
Presentation transcript:

CS 315 Lecture 18 Nov 15 Goals Complete heap operations review insert, deletemin decreaseKey, increaseKey heap building Heap sorting Some applications of heap

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; }

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; } Summary: percolateDown(j) can be called when the heap property is violated at node j in that array[j] could be > array[2j] or array[2j+1]. (All the nodes in the subtree rooted at j satisfy the heap property.) After the call, the heap property is satisfied at all the nodes in the subtree rooted at j.

increaseKey operation Suppose the priority of a key stored in position p changes. Specifically, we want to increase the key in A[p] by quantity d. Example: p = 3, d = 6 New key is 22 After changing the key to 22, call percolateDown(3) Change this to 22

increaseKey operation Example: percolateDown(3) ‏

increaseKey operation void increaseKey(int j, int d) { // change array[j] to array[j]+d and restore heap // d >= 0 is a pre-condition array[j] += d; percolateDown(j); }

decreaseKey operation In this case, we need to percolate the changed key up the heap. Example: j = 4 and d = 12. Now, it is possible that array[j] is < array[j / 2] so we need to move the key up until the correct place is found Change to

decreaseKey operation void decreaseKey(int j, int d) { // change array[j] to array[j] – d and restore heap // d >= 0 is a pre-condition int temp = array[j]- d; while (j > 1 && array[j/2] > temp){ array[j] = array[j/2]; j/= 2; }; array[j] = temp; } Note that this procedure is very similar to insert (shown below): 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;}

Removing a key at index p in a heap Example: Remove key at index Remove key One way to do this: apply decreaseKey to make the key the smallest. (Ex: d = 32) ‏ perform deleteMin What is the resulting heap?

Converting an array into a heap We pass a vector items containing n (arbitrary) keys to the heap class constructor whose task it is to store these keys as a heap. Read these n keys into the heap array. call a procedure buildHeap to convert array into a heap. binaryHeap(const vector & items) { int n = items.size(); for (int i = 0; i < n; ++i) array[i+1] = items[i]; buildHeap(n); }

Converting an array into a heap Starting with an arbitrary array, how to convert it to a heap? Solution 1: We can view the array as a heap of size 1. Successively insert keys array[2], array[3], etc. into the heap until all the keys are inserted. void buildHeap(int n) { currentSize = 1; for (int j= 2; j <= n; ++j) { insert(array[j]); } }; This takes O(n log n) time. (n operations each taking O(log n) time.) ‏

Alternate (faster) way to buildHeap Idea: perform percolateDown(j) starting from j = currentSize/2 to j = 1. Why does it work? Before percolateDown(j) is called, we have already performed percolateDown at 2j and 2j+1, and so in the subtree rooted at j, the only node where the heap property may not hold is j. Thus, after percolateDown(j) is called, the subtree rooted at j is a heap. The last call is percolateDown(1) so the tree rooted at 1 is a heap at the end. Code for solution 2: void buildHeap2() { n = array.size(); currentSize = n; for (int j = n/2; j > 0; --j)‏ percolateDown(j); }

BuildHeap using percolateDown is faster Solution 2 builds a heap in O(n) time. Analysis of this faster algorithm is presented in the text. We will skip it since it is complicated. (Involves summation of a series called the arithmetic- geometric series.) ‏

Heap-sorting To sort n numbers: read the numbers into an array A. call heap constructor to build a heap from A. perform n deleteMin operations. Store the successive outputs of deleteMin in the array A. A is now sorted. Total number of operations = O(n) (for builing heap) ‏ + O(n log n) (for n deleteMin operations) ‏ = O(n log n) ‏

Heap-sorting void sort(vector & out) { // the sorted array is output in array out out.resize(currentSize); int j=0; while (!isEmpty()) out[j++] = deleteMin(); } It is possible to collect the output in the heap array itself. As the heap size is shrinking, we can use the unused slots of the array to collect the delete min keys. The resulting heap will be sorted in descending order.

Heap-sorting animation

17 Application of Heaps: Interval Partitioning Interval partitioning. n Lecture j starts at s j and finishes at f j. n Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room. Ex: This schedule uses 4 classrooms to schedule 10 lectures. Time 99:301010:301111:301212:3011:3022:30 h c b a e d g f i j 33:3044:30

18 Interval Partitioning Interval partitioning. n Lecture j starts at s j and finishes at f j. n Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room. Ex: This schedule uses only 3. Time 99:301010:301111:301212:3011:3022:30 h c a e f g i j 33:3044:30 d b

19 Interval Partitioning: Lower Bound on Optimal Solution Def. The depth of a set of open intervals is the maximum number that contain any given time. Key observation. Number of classrooms needed  depth. Ex: Depth of schedule below = 3  schedule below is optimal. Q. Does there always exist a schedule equal to depth of intervals? Time 99:301010:301111:301212:3011:3022:30 h c a e f g i j 33:3044:30 d b a, b, c all contain 9:30

20 Interval Partitioning: Greedy Algorithm Greedy algorithm. Consider lectures in increasing order of start time: assign lecture to any compatible classroom. Implementation. O(n log n). n For each classroom k, maintain the finish time of the last job added. n Keep the classrooms in a priority queue. Sort intervals by starting time so that s 1  s 2 ...  s n. d  0 for j = 1 to n { if (lecture j is compatible with some classroom k) schedule lecture j in classroom k else allocate a new classroom d + 1 schedule lecture j in classroom d + 1 d  d + 1 } number of allocated classrooms

Machine Scheduling  m identical machines (drill press, cutter, sander, etc.) ‏  n jobs/tasks to be performed  assign jobs to machines so that the time at which the last job completes is minimum

Another formulation of the same problem A set of n items should be distributed to m people (or parties) so that each one gets nearly the same share as the others. Each item has a weight (value).

Machine Scheduling Example 3 machines and 7 jobs job times are [6, 2, 3, 5, 10, 7, 14] possible schedule A B C time >

Machine Scheduling Example Finish time = 21 Objective: Find schedules with minimum finish time. A B C time >

LPT Schedules  Longest Processing Time first.  Jobs are scheduled in the order 14, 10, 7, 6, 5, 3, 2  Each job is scheduled on the machine on which it finishes earliest.  For each machine, maintain information about when it is ready.  Choice of data structure: priority queue

LPT Schedule [14, 10, 7, 6, 5, 3, 2] A B C Finish time is 16!

LPT Schedule  LPT rule does not guarantee minimum finish time schedules.  Usually LPT finish time is very close to minimum finish time.

LPT implementation using a minheap  Minheap has the finish times of the m machines.  Initial finish times are all 0.  To schedule a job remove the machine with minimum finish time from the priority queue.  Update the finish time of the selected machine and put the machine back into the priority queue.

LPT implementation using a minheap ListPtrs LPTschedule(vector jobs, int m) { // jobs is a sorted array of job durations, m is the // number of machines ListPtrs L(m); // array of m null pointers BinaryHeap H; for (int j=0; j < m ; ++j) { pair p = new pair(j,0); H.insert(p); } for (int j=0; j < jobs.size(); ++j) { pair p = H.deleteMin(); int id = p.getId(); int ft = p.getFinishTime(); L[id].insert(j); ft+=jobs[j]; p = new pair(id, ft); H.insert(p); } }

LPT implementation using a min-heap  Sorting n jobs takes O(n log n) steps.  Initialization of priority queue with m 0’s O(m) time  One delete-min and one insert to schedule each job each put and remove min operation takes O(log m) time  time to schedule is O(m + n log m) ‏  overall time is O(n log n + n log m + m)

Summary of heap binary heap is implemented as an array. (Tree representation is conceptual – for understanding, not explicit.) heap data members are: array (a vector) and currentSize. efficient heap operations: insert, findmin, deletemin, decreaseKey, increaseKey – all of which can be performed in O(log n), findmin can be done in O(1) in the worst-case. an important helper function is percolateDown. It also takes O(log n) time.

Summary of heap build-heap is the process of converting a set of keys into a heap-ordered array. build-heap can be performed in O(n log n) time by a series of n – 1 insertions. faster build-heap can be performed in O(n) time using n/2 percolate-down operations. heap-sorting takes O(n log n) in the worst-case Step 1: build a heap Step 2: perform n – 1 deleteMin operations and put the key at the end of the heap-array.

Summary of heap Some heap operations are expensive (i.e., can take n steps in the worst-case): search(x) – search for a key x. delete(x) – delete the key x (if present) in a heap Priority queue has many applications Example: scheduling problem, shortest-path problem (Dijkstra’s algorithm) etc.