binary heaps
play

Binary Heaps Autumn 2018 Shrirang (Shri) Mare - PowerPoint PPT Presentation

CSE 373: Data Structures and Algorithms Binary Heaps Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber, Whitaker Brand, Zora Fung, Stuart Reges,


  1. CSE 373: Data Structures and Algorithms Binary Heaps Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber, Whitaker Brand, Zora Fung, Stuart Reges, Justin Hsia, Ruth Anderson, and many others for sample slides and materials ...

  2. Problems 1. Merging multiple sorted arrays - OutArray[k] = min(Array1[i1], Array2[i2]) // for 2 sorted arrays - OutArray[k] = min(Array1[i1], Array2[i2], …, Arrayk[ik]) // for k sorted arrays 2. Given n 2D points, find k points which are closest to point P(x, y) S = Set of k distances For each point Q in the remaining points: if dist(P, Q) is less than max(S) removeMax from S Insert dist(P, Q) in S 3. Priority queues: Job schedulers - Among all the job in a queue, get the job with the highest priority: removeMax(Priority Queue) � 2

  3. Problems 1. Merging multiple sorted arrays - OutArray[k] = min(Array1[i1], Array2[i2]) // for 2 sorted arrays - OutArray[k] = min(Array1[i1], Array2[i2], …, Arrayk[ik]) // for k sorted arrays 2. Given n 2D points, find k points which are closest to point P(x, y) S = Set of k distances For each point Q in the remaining points: if dist(P, Q) is less than max(S) removeMax from S Insert dist(P, Q) in S 3. Priority queues: Job schedulers - Among all the job in a queue, get the job with the highest priority: removeMax(Priority Queue) � 3

  4. Desired behavior: Get extreme values (min or max) 1. Merging multiple sorted arrays - OutArray[k] = min(Array1[i1], Array2[i2]) // for 2 sorted arrays - OutArray[k] = min(Array1[i1], Array2[i2], …, Arrayk[ik]) // for k sorted arrays 2. Given n 2D points, find k points which are closest to point P(x, y) S = Set of k distances For each point Q in the remaining points: if dist(P, Q) is less than max(S) removeMax from S Insert dist(P, Q) in S 3. Priority queues: Job schedulers - Among all the job in a queue, get the job with the highest priority: removeMax(Priority Queue) � 4

  5. Min Priority Queue ADT - Collection where elements ordered based on priority. - Behavior: - removeMin(): return element with smallest priority, removes element from collection - peekMin(): find, but do not remove, the element with smallest priority - insert(element): add element to the collection - Max Priority Queue ADT: - Same as Min Priority Queue ADT, just returns the largest instead of the smallest � 5

  6. Binary heap data structure - Invented in 1964 for sorting - Priority Queues is one of the main applications for binary heaps - Lots of other applications: greedy algorithms, shortest path - Basically, min-heap (or max-heap) is ideal when you want to maintain a collection of elements where you need to add arbitrary values but need an efficient removeMin (or removeMax). � 6

  7. Binary heap Binary heap is a 1. binary tree 2. that satisfies the heap property, and 3. where every heap is a “complete” tree � 7

  8. Binary heap: Heap property � 8

  9. Binary heap: Heap property max-heap: Every node is larger than (or equal to) its children � 8

  10. Binary heap: Heap property max-heap: Every node is larger than (or equal to) its children min-heap: Every node is smaller than (or equal to) its children � 8

  11. Binary heap: Complete binary tree A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. There are no “gaps” in a complete tree. � 9

  12. Complete binary tree Complete binary tree 1 depth 0 2 3 depth 1 4 7 6 5 depth 2 9 depth 3 There as not complete binary trees � 10

  13. Question: Valid min-heap? Complete binary tree? 1 Heap property satisfied? 2 3 4 7 6 5 9 � 11

  14. Question: Valid min-heap? Complete binary tree? Yes! 1 Heap property satisfied? Yes! 2 3 4 7 6 5 9 � 12

  15. Question: Valid min-heap? Complete binary tree? 1 Heap property satisfied? 2 8 4 7 6 5 9 � 13

  16. Question: Valid min-heap? Complete binary tree? No! 1 Heap property satisfied? No! 2 8 4 7 6 5 9 � 14

  17. Binary heap insert 2 insert(12) 5 3 4 17 6 13 9 � 15

  18. Binary heap insert 2 insert(12) 5 3 4 17 6 13 9 12 � 16

  19. Binary heap insert 2 insert(12) 5 insert(7) 3 4 17 6 13 9 12 � 17

  20. Binary heap insert 2 insert(12) 5 insert(7) 3 4 17 6 13 9 7 12 � 18

  21. Binary heap insert 2 insert(12) 5 insert(7) 3 4 17 6 13 9 7 12 Heap broken � 19

  22. Binary heap insert 2 insert(12) 5 insert(7) 3 4 7 6 13 9 12 17 Heap broken � 20

  23. Binary heap insert 2 insert(12) 5 insert(7) 3 4 7 6 13 9 12 17 � 21

  24. Binary heap insert 2 insert(12) 5 insert(7) 3 insert(1) 4 7 6 13 9 12 17 � 22

  25. Binary heap insert 2 insert(12) 5 insert(7) 3 insert(1) 4 7 6 13 9 1 12 17 � 23

  26. Binary heap insert 2 insert(12) 5 insert(7) 3 insert(1) 4 7 6 13 9 1 12 17 Heap broken � 24

  27. Binary heap insert 2 insert(12) 5 insert(7) 3 insert(1) 4 1 6 13 9 7 12 17 Heap broken � 25

  28. Binary heap insert 2 insert(12) 5 insert(7) 1 insert(1) 4 3 6 13 9 7 12 17 Heap broken � 26

  29. Binary heap insert 1 insert(12) 5 insert(7) 2 insert(1) 4 3 6 13 9 7 12 17 Heap broken � 27

  30. Binary heap insert 1 insert(12) 5 insert(7) 2 insert(1) 4 3 6 13 9 7 12 17 � 28

  31. Binary heap insert percolateUp 1 insert(12) 5 insert(7) 2 insert(1) 4 3 6 13 9 7 12 17 � 29

  32. Binary heap removeMin 1 removeMin() 5 2 4 3 6 13 9 7 12 17 � 30

  33. Binary heap removeMin removeMin() 5 2 4 3 6 13 9 7 12 17 � 31

  34. Binary heap removeMin 7 removeMin() 5 2 4 3 6 13 9 12 17 � 32

  35. Binary heap removeMin 7 removeMin() 5 2 4 3 6 13 9 12 17 Heap broken � 33

  36. Binary heap removeMin percolateDown 7 removeMin() 5 2 4 3 6 13 9 12 17 Heap broken � 34

  37. Binary heap removeMin percolateDown 2 removeMin() 5 7 4 3 6 13 9 12 17 Heap broken � 35

  38. Binary heap removeMin percolateDown 2 removeMin() 5 3 4 7 6 13 9 12 17 Heap broken � 36

  39. Binary heap removeMin percolateDown 2 removeMin() 5 3 4 7 6 13 9 12 17 � 37

  40. Binary heap: removeMin() runTime � 38

  41. Binary heap: removeMin() runTime findLastNodeTime + removeRootTime + numOfSwaps * swapTime � 38

  42. Binary heap: removeMin() runTime findLastNodeTime + removeRootTime + numOfSwaps * swapTime n + 1 + log(n) * 1 = O (n) � 38

  43. Binary heap: removeMin() runTime findLastNodeTime + removeRootTime + numOfSwaps * swapTime n + 1 + log(n) * 1 = O (n) How can we do better? � 38

  44. Binary heap: removeMin() runTime findLastNodeTime + removeRootTime + numOfSwaps * swapTime n + 1 + log(n) * 1 = O (n) How can we do better? What do we make efficient in the above expression? � 38

  45. Array implementation of a complete binary tree 1 2 3 � 39

  46. Array implementation of a complete binary tree 1 2 3 � 40

  47. Binary heap: Array implementation 2 11 3 4 7 6 13 9 12 17 Indices 0 1 2 3 4 5 6 7 8 9 10 11 12 � 41

  48. Binary heap: Array implementation 5 3 4 7 6 13 9 12 17 2 Indices 0 1 2 3 4 5 6 7 8 9 10 11 12 � 42

  49. Binary heap: Array implementation 4 7 6 13 9 12 17 2 3 5 Indices 0 1 2 3 4 5 6 7 8 9 10 11 12 � 43

  50. Binary heap: Array implementation 9 12 17 2 3 5 4 7 6 13 Indices 0 1 2 3 4 5 6 7 8 9 10 11 12 � 44

  51. Binary heap: Array implementation 2 3 5 4 7 6 9 13 12 17 Indices 0 1 2 3 4 5 6 7 8 9 10 11 12 � 45

  52. Binary heap: Array implementation leftChild(i) = 2i rightChild(i) = 2i + 1 parent(i) = i / 2 2 3 5 4 7 6 9 13 12 17 Indices 0 1 2 3 4 5 6 7 8 9 10 11 12 � 46

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend