1 Programming II (CS300) Chapter 12: Heaps and Priority Queues MOUNA KACEM mouna@cs.wisc.edu Fall 2018
Heaps and Priority Queues 2 Priority Queues Heaps
Priority Queue 3 QueueADT Objects are added and removed according to the First-In, First-Out (FIFO) principle PriorityQueueADT A collection of prioritized elements that allows Arbitrary element insertion of a new element, and Removal of the element having highest priority The priority of an element added to a priority-queue is designated by a key The element with minimal value of key will have the highest priority
Priority-Queue Examples 4
Priority Queue 5 public interface PriorityQueueADT<T extends Comparable<T>>{ public void insert(t newobject); public T removebest(); // removes and returns the element with the highest priority } public T peekbest(); // returns without removing the element with the highest priority public boolean isempty();
Java.lang.Comparable Interface 6 Interface providing a mean for defining comparison between two objects Comparable interface Includes a single method: compareto a.compareto(b) must return an integer i such that i < 0 designated that a < b i = 0 designated that a = b i > 0 designated that a > b for more details about the interface Comparable, please visit https://docs.oracle.com/javase/8/docs/api/java/lang/comparable.html
Implementation Strategies for a Priority-Queue 7 Implementing a priorityqueue with an Unsorted LinkedList head tail (18, 3) (15, 1) (45, 0) (10, 2) Method insert removebest peekbest isempty Running Time O(1) O(n) O(n) O(1)
Implementation Strategies for a Priority-Queue 8 Implementing a priorityqueue with a Sorted LinkedList head tail (45, 0) (15, 1) (10, 2) (18, 3) Method insert removebest peekbest isempty Running Time O(n) O(1) O(1) O(1)
Implementation Strategies for a Priority-Queue 9 Using Unsorted LinkedList Using Sorted LinkedList Method Running Time Method Running Time insert O(1) insert O(n) removebest O(n) removebest O(1) peekbest O(n) peekbest O(1) isempty O(1) isempty O(1)
Heaps and Priority Queues 10 Priority Queues Heaps
Heaps 11 Binary Heap is a binary tree that should satisfy these two constraints Structural constraint A Heap is a complete binary tree All levels of a complete binary tree are completely filled except possibly the last level and the last level has all keys as left as possible
Heaps 12 Structural Constraint (complete binary tree) Non-Heap Heap Non-Heap Heap Heap Non-Heap
Heaps 13 Binary Heap is a binary tree that should satisfy these two constraints Structural constraint A Heap is a complete binary tree Value-relationship constraint A Heap must satisfy the heap ordering property. There are two ordering properties: min-heap property: the value of each node is greater than or equal to the value of its parent, with the minimum-value element at the root. max-heap property: the value of each node is less than or equal to the value of its parent, with the maximum-value element at the root.
Heaps 14 Value-Relationship Constraint Max-Heap Min-Heap Max-Heap Max-Heap Min-Heap
Non-Heaps 15
Binary Tree Traversal Algorithms 16 Binary tree traversals: breadth-first traversal or level-order traversal Visit nodes by levels from top to bottom and from left to right depth-first traversal PreOrder traversal visit the parent first and then left and right children InOrder traversal visit the left child, then the parent and the right child PostOrder traversal visit left child, then the right child and then the parent
Binary-Tree Level-order Traversal 17 Level 0 Level 1 Level 2 Level 3 Level-order traversal: F, B, G, A, D, I, C, E, H
Array-Representation of a Heap 18
Implementing a Priority-Queue using a min-heap 19 public interface PriorityQueueADT<T extends Comparable<T>>{ public void insert(t newobject); // insert an element to the priority Queue public T peekbest(); // returns without removing the element with the highest priority public boolean isempty(); // checks whether the priority Queue is empty or not } public T removebest(); // removes and returns the element with the highest priority
Implementing a Priority-Queue using a min-heap 20 Adding an element to the Heap (insert operation) Michael T. Goofrich, et al., "Data Structures & Algorithms", Wiley, six edition, 2014, pp. 360-384.
Implementing a Priority-Queue using a min-heap 21 Adding an element to the Heap (insert operation) 1. First Step: Maintain the complete binary tree property If the heap is empty or the bottom level is already full Place the new node at the leftmost position of a new level
Implementing a Priority-Queue using a min-heap 22 Adding an element to the Heap (insert operation) 1. First Step: Maintain the complete binary tree property If the heap is not empty and the bottom level is not already full Place the new node just beyond the rightmost node at the bottom level of the heap
Implementing a Priority-Queue using a min-heap 23 Adding an element to the Heap (insert operation) 1. First Step: Maintain the complete binary tree property 2. Second Step: Restore the heap-order property (Up-Heap Bubbling) After the first step, the tree representing the heap is complete. But, it may violate the heap-order property
Implementing a Priority-Queue using a min-heap 24 Adding an element to the Heap (insert operation) 1. Second Step: Restore the heap-order property (Up-Heap Bubbling) After the first step, the tree representing the heap is complete. But, it may violate the heap-order property if the priority queue was empty before the insertion Nothing to do
Implementing a Priority-Queue using a min-heap 25 Adding an element to the Heap (insert operation) 1. Second Step: Restore the heap-order property (Up-Heap Bubbling) if the priority queue was not empty before the insertion Suppose that The new node is inserted at the position p of the priority queue. (p cannot be the position of the root as the priority queue was not empty before the insertion operation) The new inserted node s parent is located at the position q of the priority queue We compare the key (aka priority) at position p to that of p s parent at position q
Implementing a Priority-Queue using a min-heap 26 Adding an element to the Heap (insert operation) 1. Second Step: Restore the heap-order property (Up-Heap Bubbling) if the priority queue was not empty before the insertion We compare the key (aka priority) at position p to that of p s parent at position q if key k p k q, the heap-order property is satisfied and the algorithm terminates if key k p < k q, then restore the heap-order property swap the entries stored at positions p and q Again, the heap-order property may be violated, so repeat the process: Going up in the binary tree until no violation of the heap-order property occurs
Implementing a Priority-Queue using a min-heap 27 Adding an element to the Heap (insert operation) (a) (b)
Implementing a Priority-Queue using a min-heap 28 Adding an element to the Heap (insert operation) (c) (d)
Implementing a Priority-Queue using a min-heap 29 Adding an element to the Heap (insert operation) (e) (f)
Implementing a Priority-Queue using a min-heap 30 Adding an element to the Heap (insert operation) (g)
Implementing a Priority-Queue using a min-heap 31 Adding an element to the Heap (insert operation) The upward movement of the newly inserted entry by means of swaps is conventionally called up-heap bubbling or percolate up A swap either resolves the violation of the heap-order property or propagates it one level up in the heap. In the worst case, upheap bubbling causes the new entry to move all the way up to the root of heap. In the worst case, the number of swaps performed in the execution of method insert is equal to the height of the heap (O(log(n))) The insert operation using a heap is of time complexity: O(log(n)) where n represents the number of elements in the heap (i.e. size of the heap)
Implementation Strategies for a Priority-Queue 32 Using Unsorted LinkedList Method Running Time insert O(1) removebest O(n) peekbest O(n) isempty O(1) Using Sorted LinkedList Method Running Time insert O(n) removebest O(1) peekbest O(1) isempty O(1) Using a Heap Method Running Time insert O(log(n)) removebest peekbest isempty Recall that the height h of a complete binary tree with n nodes is at most O(log(n)).
Implementing a Priority-Queue using a min-heap 33 Removing an element from the Heap (removebest operation) Using a min-heap, the entry with the highest priority (with the smallest key) is stored at the root r of the binary tree representing the heap Deleting the root node would leave two disconnected subtrees
Implementing a Priority-Queue using a min-heap 34 Removing an element to the Heap (removebest operation) Using a min-heap, the entry with the highest priority (with the smallest key) is stored at the root r of the binary tree representing the heap Deleting the root node would leave two disconnected subtrees
Implementing a Priority-Queue using a min-heap 35 Removing an element to the Heap (removebest operation) Using a min-heap, the entry with the highest priority (with the smallest key) is stored at the root r of the binary tree representing the heap Deleting the root node would leave two disconnected subtrees Solution: Delete the leaf at the last position p of the tree and copy its value to the root r Then, perform a Heap-Down Bubbling or percolate down process after the removal
Implementing a Priority-Queue using a min-heap 36 Removing an element to the Heap (removebest operation)
Implementing a Priority-Queue using a min-heap 37 Removing an element to the Heap (removebest operation)
Implementing a Priority-Queue using a min-heap 38 Removing an element to the Heap (removebest operation)
Implementing a Priority-Queue using a min-heap 39 Removing an element to the Heap (removebest operation)
Implementation Strategies for a Priority-Queue 40 Using Unsorted LinkedList Method Running Time insert O(1) removebest O(n) peekbest O(n) isempty O(1) Using Sorted LinkedList Method Running Time insert O(n) removebest O(1) peekbest O(1) isempty O(1) Using a Heap Method Running Time insert O(log(n)) removebest O(log(n)) peekbest isempty
Implementation Strategies for a Priority-Queue 41 Using Unsorted LinkedList Method Running Time insert O(1) removebest O(n) peekbest O(n) isempty O(1) Using Sorted LinkedList Method Running Time insert O(n) removebest O(1) peekbest O(1) isempty O(1) Using a Heap Method Running Time insert O(log(n)) removebest O(log(n)) peekbest O(1) isempty O(1)
Array-Based implementation of a Heap 42
Array-Based implementation of a Heap 43 The array-based representation of a binary tree is suitable for a complete binary tree. The elements of the tree are stored in an array T such that the element at position p is stored in T with index equal to the level number f (p) of p, defined as follows: If p is the root, then f (p) = 0. If p is the left child of position q, then f (p) = 2 f (q)+1. If p is the right child of position q, then f (p) = 2 f (q)+2. For a tree with of size n, the elements have contiguous indices in the range [0,n 1] The last position of is always at index n 1.