CMSC 341: Data Structures Priority Queues – Binary Heaps

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.
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.
1 Binary heaps binary tree that satisfy two properties –structural property (is a complete tree) –heap-ordering property (minimum item on top) Can have.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
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.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Dan Grossman Fall 2013.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
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.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
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)
Priority Queues A priority queue is an ADT where:
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structures & Algorithms
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues and Heaps
October 30th – Priority QUeues
Hashing Exercises.
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
ADT Heap data structure
Heaps, Heap Sort, and Priority Queues
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Binary Heaps Text Binary Heap Building a Binary Heap
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued
Binary Heaps Priority Queues
Instructor: Lilian de Greef Quarter: Summer 2017
CSE 373: Data Structures and Algorithms
CMSC 341 Lecture 14 Priority Queues & Heaps
CSE 332: Data Structures Priority Queues – Binary Heaps
CSE332: Data Abstractions Lecture 4: Priority Queues
Heaps and the Heapsort Heaps and priority queues
CE 221 Data Structures and Algorithms
CS Data Structure: Heaps.
Priority Queues (Chapter 6.6):
CSE 326: Data Structures Priority Queues & Binary Heaps
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
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) A heap.
Data Structures and Analysis (COMP 410)
Priority Queues & Heaps
CSE373: Data Structures & Algorithms Lecture 7: Binary Heaps, Continued Dan Grossman Fall 2013.
CSE 373 Data Structures and Algorithms
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
Priority Queues & Heaps
Priority Queues (Chapter 6):
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
CSE 373: Data Structures and Algorithms
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
Heaps.
CSE 373: Data Structures and Algorithms
Priority Queues (Heaps)
Presentation transcript:

CMSC 341: Data Structures Priority Queues – Binary Heaps

Recall Queues FIFO: First-In, First-Out Some contexts where this seems right? Some contexts where some things should be allowed to skip ahead in the line? Cars: Rotation of the lights controlling who gets to go HOV lanes in on-ramps, Emergency Vehicles Processor Scheduling: Not all “fair shares” are equal

Queues that Allow Line Jumping Need a new ADT Operations: Insert an Item, Remove the “Best” Item 6 2 15 23 12 18 45 3 7 insert deleteMin

Priority Queue ADT PQueue data : collection of data with priority PQueue operations insert deleteMin PQueue property: for two elements in the queue, x and y, if x has a lower priority value than y, x will be deleted before y We need a new ADT for this. One that returns the best (which we’ll generally define as lowest priority value) node next. I’m often going to represent priority queues as collections of priorities; remember that they are collections of data with priorities. MOREOVER, some priority queues don’t even need priorities, just the ability to compare priorities. (all of the ones we’ll talk about fall in this category).

Applications of the Priority Queue Select print jobs in order of decreasing length Forward packets on routers in order of urgency Select most frequent symbols for compression Sort numbers, picking minimum first Anything greedy There are, of course, gobs of applications for priority queues. Here are just a few.

Potential Implementations O(1)/O(N)worst-array full, should say WHY, might reject on full instead. O(N) – to find value insert deleteMin Unsorted list (Array) O(1) O(n) Unsorted list (Linked-List) Sorted list (Array) O(1)* Sorted list (Linked-List) O(1) O(N) – to find value O(log N) to find loc w. Bin search, but O(N) to move vals O(1) to find val, but O(N) to move vals, (or O(1) if in reverse order) O(N) to find loc, O(1) to do the insert O(1) O(N) O(N) We are going to do an insert = O(log N) worst case, O(1) on average (on average 1.67 levels) Deletemin O(log N) – worst and average case. Plus – good memory usage O(log N) close to O(1) 1.67 levels on average O(log N) Binary Heap

Recall From Lists, Queues, Stacks Use an ADT that corresponds to your needs The right ADT is efficient, while an overly general ADT provides functionality you aren’t using, but are paying for anyways Heaps provide O(log n) worst case for both insert and deleteMin, O(1) average insert

Binary Heap Properties Structure Property Ordering Property Describe these for a binary search tree. For an AVL tree.

Brief interlude: Some Definitions: skip A Perfect binary tree – A binary tree with all leaf nodes at the same depth. All internal nodes have 2 children. height h 2h+1 – 1 nodes 2h – 1 non-leaves 2h leaves 11 5 21 2 9 16 25 This is NOT a heap! (but it has the heap structure property) 1 3 7 10 13 19 22 30

Heap Structure Property A binary heap is a complete binary tree. Complete binary tree – binary tree that is completely filled, with the possible exception of the bottom level, which is filled left to right. Examples: Since they have this regular structure property, we can take advantage of that to store them in a compact manner. Since they have this regular structure property, we can take advantage of that to store them in a compact manner. If I asked to you store a plain old binary tree in an array, how would you do it?

Representing Complete Binary Trees in an Array 1 2 3 4 5 6 9 8 10 11 12 A From node i: left child: right child: parent: B C 2 * i 7 D E F G (2 * i)+1 H I J K L └ i / 2┘ Left child of node I = 2 * I Right child I = (2*i) +1 Parent of node I is at i/2 (floor) implicit (array) implementation: A B C D E F G H I J K L 1 2 3 4 5 6 7 8 9 10 11 12 13

Why this approach to storage? no pointers (space). *2, /2, + are faster operations than dereferencing a pointer. (faster operations) but also, better locality. can get to parent easily Can we use the implicit representation for binary search trees? Uses less space (only values, no pointers) *2 and /2,+ are usually faster operations than dereferencing a pointer An array MAY get better locality in general than a linked structure. Can get to the parent easily (and without an extra pointer)

Heap Order Property Heap order property: For every non-root node X, the value in the parent of X is less than the value in X. This is the order for a MIN heap – could do the same for a max heap. 10 10 20 80 20 80 40 60 85 99 Tree is PARTIALLY ORDERED. For each node: its value is less than value of all of its descendants. This is the definition for a MIN heap – could do the same for a max heap. 30 15 50 700 not a heap This is a PARTIAL order (diff than BST) For each node, its value is less than all of its descendants (no distinction between left and right)

Heap Operations findMin: insert(val): percolate up. How? findMin: insert(val): percolate up. deleteMin: percolate down. Is the tree unique? Swap 85 and 99. Swap 700 and 85? 10 20 80 40 60 85 99 Is the tree unique? Swap 85 and 99. Swap 700 and 85? 50 700 65

Heap – Insert(val) Basic Idea: Put val at “next” leaf position Percolate up by repeatedly exchanging node until no longer needed How long does this take? How long does this take? – max # of exchanges = O(log N) On “average” only need to move up 1.67 levels so get O(1) How long does step 1 take? QUESTION: Max number of exchanges is ? O(log N) – must percolate up to root. On average, analysis shows that you tend to only need to move up 1.67 levels, so get O(1) on average. Optimization in the book – bubble up an EMPTY space, and then do a swap (reduces the # of swaps).

Inserting a Value 4 12 5 26 25 14 15 29 45 35 31 21 3 Insert 3 insert here to keep tree complete i 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 array __ 4 5 12 26 25 14 15 29 45 35 31 21 3 __ __ currentsize = 13 Insert 3

Inserting a Value 4 12 5 26 25 14 15 29 45 35 31 21 3 Insert 3 save new value in a temporary location: tmp  3 Insert 3

Inserting a Value 4 12 5 26 25 14 15 29 45 35 31 21 14 3 Insert 3  copy 14 down because 14 > 3 3 tmp  Insert 3

Inserting a Value 4 12 5 26 25 12 15 29 45 35 31 21 14 3 Insert 3 copy 12 down because 12 > 3 3 tmp  Insert 3

Inserting a Value 4 4 5 26 25 12 15 29 45 35 31 21 14 3 Insert 3 copy 4 down because 4 > 3 3 tmp  Insert 3

Inserting a Value 3 4 5 26 25 12 15 29 45 35 31 21 14 Insert 3

Heap After Insert 3 4 5 26 25 12 15 29 45 35 31 21 14

Deleting a Value (note new tree!) 3 7 5 26 25 12 15 29 45 35 31 21 14 Delete 3

Deleting a Value 7 5 26 25 12 15 29 45 35 31 21 14 3 Delete 3 save root value … tmp  3 Delete 3

X Deleting a Value 14 7 5 26 25 12 15 29 45 35 31 21 14 3 Delete 3 copy value of last node in complete tree into temporary location; decrement currentsize 14 7 5 26 25 12 15 X 29 45 35 31 21 14 tmp  3 Delete 3

Deleting a Value 14 7 5 26 25 12 15 29 45 35 31 21 3 Delete 3 push down root … compare children select smaller 7 5 26 25 12 15 29 45 35 31 21 tmp  3 Delete 3

Deleting a Value 14 5 7 26 25 12 15 29 45 35 31 21 3 Delete 3 push down root … copy smaller value into parent 7 26 25 12 15 29 45 35 31 21 tmp  3 Delete 3

Deleting a Value 14 5 7 26 25 12 15 29 45 35 31 21 3 Delete 3 push down root … 7 26 25 12 15 29 45 35 31 21 compare children select smaller (25) tmp  3 Delete 3

Deleting a Value 5 7 14 26 25 12 15 29 45 35 31 21 3 Delete 3 push down root … 7 14 26 25 12 15 29 45 35 31 21 copy 14 into parent because 14 < smaller child tmp  3 Delete 3

Deleting a Value 5 7 14 26 25 12 15 29 45 35 31 21 return 3 Delete 3

Heap – Deletemin Basic Idea: Remove root (that is always the min!) Put “last” leaf node at root Find smallest child of node Swap node with its smallest child if needed. Repeat steps 3 & 4 until no swaps needed. If I < = both children, then you are done. How long does step 1 take? QUESTION: Max number of exchanges is ? O(log N) – must percolate all the way to the bottom level. In fact this is often the case, you just took the value from the bottom, there is a good chance it has to go down to the lowest level. O(log N) Optimization in the book – bubble up an EMPTY space down, and then do a swap (reduces the # of swaps). Max # of exchanges? = O(log N), there is a good chance goes to bottom

Insert: 16, 32, 4, 69, 105, 43, 2 1 2 3 4 5 6 7 8 Deletemin - > returns 2 Deletemin -> returns 4

Building a Heap 10 11 12 0 1 2 3 5 11 3 10 6 9 4 8 1 7 2 12 Red nodes need to percolate down Let’s try pretending it’s a heap already and just fixing the heap-order property. The red nodes are the ones that are out of order. Question: which nodes MIGHT be out of order in any heap?

Building a Heap Adding the items one at a time is O(n log n) in the worst case Can we do this in O(n)

Working on Heaps What are the two properties of a heap? Structure Property Order Property How do we work on heaps? Fix the structure Fix the order

BuildHeap: Floyd’s Method 10 11 12 0 1 2 3 5 11 3 10 6 9 4 8 1 7 2 12 Add elements arbitrarily to form a complete tree. Pretend it’s a heap and fix the heap-order property! 12 Red nodes need to percolate down 5 11 Let’s try pretending it’s a heap already and just fixing the heap-order property. The red nodes are the ones that are out of order. Question: which nodes MIGHT be out of order in any heap? 3 10 6 9 4 8 1 7 2

Buildheap pseudocode private void buildHeap() { for ( int i = currentSize/2; i > 0; i-- ) percolateDown( i ); } runtime:

BuildHeap: Floyd’s Method 10 11 12 0 1 2 3 12 5 11 Red nodes need to percolate down 3 10 6 9 Let’s try pretending it’s a heap already and just fixing the heap-order property. The red nodes are the ones that are out of order. Question: which nodes MIGHT be out of order in any heap? 4 8 1 7 2

BuildHeap: Floyd’s Method 12 5 11 3 10 2 9 4 8 1 7 6

BuildHeap: Floyd’s Method 12 12 5 11 5 11 3 10 2 9 3 1 2 9 4 8 1 7 6 4 8 10 7 6

BuildHeap: Floyd’s Method 12 12 5 11 5 11 3 10 2 9 3 1 2 9 4 8 1 7 6 4 8 10 7 6 12 5 2 3 1 6 9 4 8 10 7 11

BuildHeap: Floyd’s Method 12 12 5 11 5 11 3 10 2 9 3 1 2 9 4 8 1 7 6 4 8 10 7 6 12 12 5 2 1 2 3 1 6 9 3 5 6 9 4 8 10 7 11 4 8 10 7 11

Finally… 1 3 2 4 5 6 9 12 8 10 7 11 runtime: - Runtime bounded by sum of heights of nodes, which is linear. O(n) - How many nodes at height 1, and height 2, up to root, - See text, Thm. 6.1 p. 194 for detailed proof. Finally… 1 3 2 4 5 6 9 12 8 10 7 11 How long does this take? Well, everything above the fringe might move 1 step. Everything height 2 or greater might move 2 steps. Most nodes move only a small number of steps  the runtime is O(n). (see text for proof) Full sum = (I=0 to height) SUM (h-I) * 2^i runtime:

Running time for this? 1 3 2 4 5 6 9 12 8 10 7 11 runtime: - Runtime bounded by sum of heights of nodes, which is linear. O(n) - How many nodes at height 1, and height 2, up to root, - See text, Thm. 6.1 p. 194 for detailed proof. Running time for this? 1 3 2 4 5 6 9 12 8 10 7 11 How long does this take? Well, everything above the fringe might move 1 step. Everything height 2 or greater might move 2 steps. Most nodes move only a small number of steps  the runtime is O(n). (see text for proof) Full sum = (I=0 to height) SUM (h-I) * 2^i runtime:

Can you implement this percolate method - Runtime bounded by sum of heights of nodes, which is linear. O(n) - How many nodes at height 1, and height 2, up to root, - See text, Thm. 6.1 p. 194 for detailed proof. Can you implement this percolate method How long does this take? Well, everything above the fringe might move 1 step. Everything height 2 or greater might move 2 steps. Most nodes move only a small number of steps  the runtime is O(n). (see text for proof) Full sum = (I=0 to height) SUM (h-I) * 2^i

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