Understanding Priority Queues and Heaps in Java

Slide Note
Embed
Share

Explore the concepts of priority queues, heaps, and their implementations in Java. Learn about efficiency tradeoffs, interface vs. implementation, and the primary operations of priority queues. Discover the importance of comparable elements and the various data structures used for efficient operations in CS2110.


Uploaded on Sep 10, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. PRIORITY QUEUES & HEAPS Lecture 14 CS2110 Spring 2019

  2. Announcements 2 Prelim tonight Room assignments on website

  3. JavaHyperText Topics 3 Interface, implements Stack, queue Priority queue Heaps, heapsort

  4. Interface vs. Implementation 4 Interface: the operations of an ADT What you see on documentation web pages Method names and specifications Abstract from details: what to do, not how to do it Java syntax: interface Implementation: the code for a data structure What you see in source files Fields and method bodies Provide the details: how to do operation Java syntax: class Could be many implementations of an interface e.g. List: ArrayList, LinkedList

  5. ADTs (interfaces) 5 ADT List Set Map Stack Queue Priority Queue Description Ordered collection (aka sequence) Unordered collection with no duplicates Collection of keys and values, like a dictionary Last-in-first-out (LIFO) collection First-in-first-out (FIFO) collection Later this lecture!

  6. Implementations of ADTs 6 Interface List Set Map Stack Queue Priority Queue Implementation (data structure) ArrayList, LinkedList HashSet, TreeSet HashMap, TreeMap Can be done with a LinkedList Can be done with a LinkedList Can be done with a heap later this lecture!

  7. Efficiency Tradeoffs 7 Class: ArrayList LinkedList chained nodes O(1) O(n) Backing storage: array prepend(val) O(n) get(i) O(1) Which implementation to choose depends on expected workload for application

  8. Priority Queues 8

  9. Priority Queue 9 Primary operation: Stack: remove newest element Queue: remove oldest element Priority queue: remove highest priority element Priority: Additional information for each element Needs to be Comparable

  10. Priority Queue 10 Priority 0 1 2 2 Task Practice for swim test Learn the Cornell Alma Mater Study for 2110 prelim Find Eric Andre ticket for sale

  11. java.util.PriorityQueue<E> 11 class PriorityQueue<E> { boolean add(E e); //insert e. E poll(); //remove&return min elem. E peek(); //return min elem. boolean contains(E e); boolean remove(E e); int size(); ... }

  12. Implementations 12 LinkedList add() put new element at front O(1) poll()must search the list O(n) peek() must search the list O(n) LinkedList that is always sorted add() must search the list O(n) poll() highest priority element at front O(1) peek() same O(1) Balanced BST add() must search the tree & rebalance O(log n) poll() same O(log n) peek() same O(log n) Can we do better?

  13. Heaps 13

  14. A Heap.. 14 Is a binary tree satisfying 2 properties: 1) Completeness. Every level of the tree (except last) is completely filled, and on last level nodes are as far left as possible. Do not confuse with heap memory different use of the word heap.

  15. Completeness 15 Every level (except last) completely filled. 55 Nodes on bottom level are as far left as possible. 38 22 35 12 19 21 20 6 4 10 8

  16. Completeness 16 Not a heap because: missing a node on level 2 55 bottom level nodes are not as far left as possible 38 22 35 12 19 20 4 10 8 missing nodes

  17. A Heap.. 17 Is a binary tree satisfying 2 properties: 1) Completeness. Every level of the tree (except last) is completely filled, and on last level nodes are as far left as possible. 2) Heap-order. Max-Heap: every element in tree is <= its parent Min-Heap: every element in tree is >= its parent

  18. Heap-order (max-heap) 18 Every element is <= its parent 55 38 22 35 12 19 2 20 6 4 10 18 Note: Bigger elements can be deeper in the tree!

  19. Piazza Poll #1 19

  20. A Heap.. 20 Is a binary tree satisfying 2 properties 1) Completeness. Every level of the tree (except last) is completely filled. All holes in last level are all the way to the right. 2) Heap-order. Max-Heap: every element in tree is <= its parent Primary operations: 1) add(e): add a new element to the heap 2) poll(): delete the max element and return it 3) peek(): return the max element

  21. Priority queues 21 Heaps can implement priority queues Each heap node contains priority of a queue item (For values+priorities, see JavaHyperText)

  22. Priority queues 22 Heaps can implement priority queues Efficiency we will achieve: add(): O(log n) poll(): O(log n) peek(): O(1) No linear time operations: better than lists peek() is constant time: better than balanced trees

  23. Heap Algorithms 23

  24. Heap: add(e) 24 55 38 22 35 12 19 2 20 6 4 10 18 50 1. Put in the new element in a new node (leftmost empty leaf)

  25. Heap: add(e) 25 Time is O(log n) 55 22 50 38 19 50 22 35 12 2 20 6 4 10 18 50 19 1. Put in the new element in a new node (leftmost empty leaf) 2. Bubble new element up while greater than parent

  26. Heap: poll() 26 55 55 38 50 35 12 22 2 19 20 6 4 10 18 1. Save root element in a local variable

  27. Heap: poll() 27 55 19 55 38 50 35 12 22 2 19 19 20 6 4 10 18 1. Save root element in a local variable 2. Assign last value to root, delete last node.

  28. Heap: poll() 28 Time is O(log n) 55 19 50 55 38 50 19 22 35 12 22 19 2 20 6 4 10 18 1. Save root element in a local variable 2. Assign last value to root, delete last node. 3. While less than a child, switch with bigger child (bubble down)

  29. Heap: peek() 29 50 Time is O(1) 50 38 22 35 12 19 2 20 6 4 10 18 1. Return root value

  30. Heap Implementation 30 (max heap)

  31. Tree implementation 31 public class HeapNode<E> { private E value; private HeapNode left; private HeapNode right; ... } But since tree is complete, even more space- efficient implementation is possible

  32. Array implementation 32 public class Heap<E> { (* represent tree as array *) private E[] heap; ... }

  33. Numbering tree nodes Number node starting at root row by row, left to right 55 0 Same order as level-order traversal 38 22 1 2 35 12 19 21 3 k=3 4 5 6 20 6 4 7 8 9 2(3)+1 = 7 2(3)+2 = 8 Children of node k are nodes 2k+1 and 2k+2 Parent of node k is node (k-1)/2

  34. Represent tree with array Store node number i in index i of array b Children of b[k] are b[2k +1] and b[2k +2] Parent of b[k] is b[(k-1)/2] 55 0 38 22 1 2 35 12 19 21 3 4 5 6 20 6 4 7 8 9 parent 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 55 38 22 35 12 19 21 20 6 4 children

  35. Constructor 35 class Heap<E> { E[] b; // heap is b[0..n-1] int n; /** Create heap with max size */ public Heap(int max) { b= new E[max]; // n == 0, so heap invariant holds // (completeness & heap-order) } }

  36. add() (assuming enough room in array) 36 class Heap<E> { /** Add e to the heap */ public void add(E e) { b[n]= e; n= n + 1; bubbleUp(n - 1); // on next slide } }

  37. add(). heap is in b[0..n-1] 37 class Heap<E> { /** Bubble element #k up to its position. * Pre: heap inv holds except maybe for k */ private void bubbleUp(int k) { int p= (k-1)/2; // inv: p is parent of k and every element // except perhaps k is <= its parent while ( ) { k > 0 && b[k].compareTo(b[p]) > 0 swap(b[k], b[p]); k= p; p= (k-1)/2; } }

  38. peek() 38 /** Return largest element * (return null if list is empty) */ public E poll() { if (n == 0) return null; return b[0]; // largest value at root. }

  39. poll(). heap is in b[0..n-1] 39 /** Remove and return the largest element * (return null if list is empty) */ public E poll() { if (n == 0) return null; E v= b[0]; // largest value at root n= n 1; // move last b[0]= b[n]; // element to root bubbleDown(); // on next slide return v; }

  40. poll() 40 /** Bubble root down to its heap position. Pre: b[0..n-1] is a heap except maybe b[0] */ private void bubbleDown() { int k= 0; int c= biggerChild(k); // on next slide // inv: b[0..n-1] is a heap except maybe b[k] AND // b[c] is b[k] s biggest child while ( ) { c < n && b[k] < b[c] swap(b[k], b[c]); k= c; c= biggerChild(k); } }

  41. poll() 41 /** Return index of bigger child of node k */ public int biggerChild(int k) { int c= 2*k + 2; // k s right child if (c >= n || b[c-1] > b[c]) c= c-1; return c; }

  42. Piazza Poll #2 42

  43. Efficiency 43 class PriorityQueue<E> { TIME* boolean add(E e); //insert e. log E poll(); //remove&return min elem. log E peek(); //return min elem. constant boolean contains(E e); linear boolean remove(E e); linear int size(); constant } *IF implemented with a heap!

  44. Heapsort 44 (if time, in JavaHyperText if not)

  45. Heapsort 45 0 1 2 3 4 55 4 12 6 14 Goal: sort this array in place Approach: turn the array into a heap and then poll repeatedly

  46. Heapsort 46 // Make b[0..n-1] into a max-heap (in place) 55 0 0 1 2 3 4 6 4 14 6 55 4 12 6 14 55 4 12 6 14 4 6 14 12 1 2 6 4 14 6 4 3

  47. Heapsort 47 // Make b[0..n-1] into a max-heap (in place) // inv: b[0..k] is a heap, b[0..k] <= b[k+1..], b[k+1..] is sorted for (k= n-1; k > 0; k= k-1) { b[k]= poll i.e., take max element out of heap. } 55 55 6 14 0 0 1 2 3 4 4 12 6 14 6 55 6 14 4 55 6 4 6 14 6 12 1 2 6 4 14 6 4 3

  48. Heapsort 48 // Make b[0..n-1] into a max-heap (in place) // inv: b[0..k] is a heap, b[0..k] <= b[k+1..], b[k+1..] is sorted for (k= n-1; k > 0; k= k-1) { b[k]= poll i.e., take max element out of heap. } 14 12 6 4 6 14 4 12 6 0 0 1 2 3 4 55 4 12 6 14 14 6 12 4 12 4 6 6 4 4 4 6 14 55 4 6 14 6 4 12 4 1 2 6 4 14 6 4 3

  49. Announcements 49 Prelim tonight Room assignments on website

More Related Content