today
play

TODAY Heapsort API Elementary implementations Binary heaps - PowerPoint PPT Presentation

BBM 202 - ALGORITHMS D EPT . OF C OMPUTER E NGINEERING P RIORITY Q UEUES AND H EAPSORT Acknowledgement: The course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton University. TODAY Heapsort


  1. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. insert S T P R N H O A E I G S add to heap T P R N H O A E I G 22

  2. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. insert S T P R N H O A violates heap order E I G S 11 (swim up) T P R N H O A E I G S 11 23

  3. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. insert S T P R 5 N S O A violates heap order E I G H 11 (swim up) T P R N S O A E I G H 5 11 11 24

  4. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. insert S T S R 2 5 N P O A violates heap order E I G H 11 (swim up) T S R N P O A E I G H 2 5 11 11 25

  5. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. heap ordered T S R N P O A E I G H T S R N P O A E I G H 26

  6. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. remove the maximum T 1 S R N P O A E I G H T S R N P O A E I G H 1 27

  7. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. remove the maximum T 1 S R N P O A E I G H 11 exchange with root T S R N P O A E I G H 1 11 28

  8. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. remove the maximum H 1 S R N P O A E I G T 11 exchange with root H S R N P O A E I G T 1 11 29

  9. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. remove the maximum violates heap order H 1 (sink down) S R N P O A E I G T H S R N P O A E I G T 1 30

  10. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. remove the maximum violates heap order S 1 (sink down) H R 2 N P O A E I G T S H R N P O A E I G T 1 2 31

  11. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. remove the maximum violates heap order S 1 (sink down) P R 2 5 N H O A E I G T S P R N H O A E I G T 1 2 5 32

  12. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. heap ordered S P R N H O A E I G S P R N H O A E I G 33

  13. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. remove the maximum S 1 P R N H O A E I G S P R N H O A E I G 1 34

  14. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. remove the maximum S 1 P R N H O A E I G exchange with root S P R N H O A E I G 1 35

  15. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. remove the maximum G 1 P R N H O A E I S 10 exchange with root G P R N H O A E I S 1 10 36

  16. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. remove the maximum violates heap order G 1 (sink down) P R N H O A E I S G P R N H O A E I S 1 37

  17. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. remove the maximum violates heap order R 1 (sink down) P G 3 N H O A E I S R P G N H O A E I S 1 3 38

  18. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. remove the maximum violates heap order R 1 (sink down) P O 3 6 N H G A E I S R P O N H G A E I S 1 3 6 39

  19. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. heap ordered R P O N H G A E I R P O N H G A E I 40

  20. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. insert S R P O N H G A E I S add to heap R P O N H G A E I S 10 41

  21. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. insert S R P O N H G A violates heap order E I S 10 (swim up) R P O N H G A E I S 10 42

  22. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. insert S R P O 5 N S G A violates heap order E I H 10 (swim up) R P O N S G A E I H 5 10 43

  23. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. insert S R S O 2 5 N P G A violates heap order E I H 10 (swim up) R S O N P G A E I H 2 5 10 44

  24. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. insert S S 1 R O 2 5 N P G A violates heap order E I H 10 (swim up) S R O N P G A E I H 1 2 5 10 45

  25. Binary heap operations Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. heap ordered S R O N P G A E I H S R O N P G A E I H 46

  26. Binary heap: Java implementation public class MaxPQ<Key extends Comparable<Key>> { private Key[] pq; private int N; public MaxPQ(int capacity) { pq = (Key[]) new Comparable[capacity+1]; } public boolean isEmpty() { return N == 0; } PQ ops public void insert(Key key) { /* see previous code */ } public Key delMax() { /* see previous code */ } private void swim(int k) { /* see previous code */ } heap helper functions private void sink(int k) { /* see previous code */ } private boolean less(int i, int j) { return pq[i].compareTo(pq[j]) < 0; } array helper functions private void exch(int i, int j) { Key t = pq[i]; pq[i] = pq[j]; pq[j] = t; } } 47

  27. Priority queues implementation cost summary order-of-growth of running time for priority queue with N items implementation insert del max max unordered array 1 N N ordered array N 1 1 binary heap log N log N 1 d-ary heap log d N d log d N 1 Fibonacci 1 log N † 1 impossible 1 1 1 why impossible? † amortized 48

  28. Binary heap considerations Immutability of keys. • Assumption: client does not change keys while they're on the PQ. • Best practice: use immutable keys. 
 Underflow and overflow. • Underflow: throw exception if deleting from empty PQ. • Overflow: add no-arg constructor and use resizing array. 
 leads to log N Minimum-oriented priority queue. amortized time per op (how to make worst case?) • Replace less() with greater() . • Implement greater() . 
 Other operations. • Remove an arbitrary item. can implement with sink() and swim() [stay tuned] • Change the priority of an item. 49

  29. 
 
 
 
 
 
 
 
 
 
 Immutability: implementing in Java Data type. Set of values and operations on those values. Immutable data type. Can't change the data type value once created. 
 public final class Vector { can't override instance methods private final int N; all instance variables private and final private final double[] data; public Vector(double[] data) { this.N = data.length; this.data = new double[N]; defensive copy of mutable for (int i = 0; i < N; i++) instance variables this.data[i] = data[i]; } instance methods don't change … instance variables } Immutable. String , Integer , Double , Color , Vector , Transaction , Point2D . Mutable. StringBuilder , Stack , Counter , Java array. 50

  30. Immutability: properties Data type. Set of values and operations on those values. Immutable data type. Can't change the data type value once created. Advantages. • Simplifies debugging. • Safer in presence of hostile code. • Simplifies concurrent programming. • Safe to use as key in priority queue or symbol table. 
 Disadvantage. Must create new object for each data type value. “ Classes should be immutable unless there's a very good reason to make them mutable.… If a class cannot be made immutable, you should still limit its mutability as much as possible. ” — Joshua Bloch (Java architect) 51

  31. P RIORITY Q UEUES AND H EAPSORT ‣ Heapsort ‣ API ‣ Elementary implementations ‣ Binary heaps ‣ Heapsort

  32. Heapsort Basic plan for in-place sort. • Create max-heap with all N keys. • Repeatedly remove the maximum key. 1 S 2 3 O R start with array of keys 4 5 6 7 E X A T in arbitrary order 8 9 10 11 P L E M X T S build a max-heap (in place) L R A P E M O E 1 A 2 3 sorted result E E (in place) 4 5 6 7 M O P L 8 9 10 11 S T X R 53

  33. Heapsort Starting point. Array in arbitrary order. we assume array entries are indexed 1 to N S 1 O R 2 3 4 5 6 7 T E X A M P L E 8 9 10 11 S O R T E X A M P L E 1 2 3 4 5 6 7 8 9 10 11 54

  34. Heapsort Heap construction. Build max heap using bottom-up method. S O R 6 7 T E X A M P L E 8 9 10 11 1-node heaps S O R T E X A M P L E 6 7 8 9 10 11 55

  35. Heapsort Heap construction. Build max heap using bottom-up method. sink 5 S O R 5 T E X A M P L E S O R T E X A M P L E 5 56

  36. Heapsort Heap construction. Build max heap using bottom-up method. sink 5 S O R 5 T L X A M P E E 10 S O R T L X A M P E E 5 10 57

  37. Heapsort Heap construction. Build max heap using bottom-up method. sink 5 S 3-node heap O R T L X A M P E E S O R T L X A M P E E 58

  38. Heapsort Heap construction. Build max heap using bottom-up method. sink 4 S O R 4 T L X A M P E E S O R T L X A M P E E 4 59

  39. Heapsort Heap construction. Build max heap using bottom-up method. sink 4 S 3-node heap O R T L X A M P E E S O R T L X A M P E E 60

  40. Heapsort Heap construction. Build max heap using bottom-up method. sink 3 S O R 3 T L X A M P E E S O R T L X A M P E E 3 61

  41. Heapsort Heap construction. Build max heap using bottom-up method. sink 3 S O X 3 6 T L R A M P E E S O X T L R A M P E E 3 6 62

  42. Heapsort Heap construction. Build max heap using bottom-up method. sink 3 S 3-node heap O X T L R A M P E E S O X T L A A M P E E 63

  43. Heapsort Heap construction. Build max heap using bottom-up method. sink 2 S O X 2 T L R A M P E E S O X T L R A M P E E 2 64

  44. Heapsort Heap construction. Build max heap using bottom-up method. sink 2 S T X 2 4 O L R A M P E E S T X O L R A M P E E 2 4 65

  45. Heapsort Heap construction. Build max heap using bottom-up method. sink 2 S T X 2 4 P L R A M O E E 9 S T X P L R A M O E E 2 4 9 66

  46. Heapsort Heap construction. Build max heap using bottom-up method. sink 2 S 7-node heap T X P L R A M O E E S T X P L R A M O E E 67

  47. Heapsort Heap construction. Build max heap using bottom-up method. sink 1 S 1 T X P L R A M O E E S T X P L R A M O E E 1 68

  48. Heapsort Heap construction. Build max heap using bottom-up method. sink 1 X 1 T S 3 P L R A M O E E X T S P L R A M O E E 1 3 69

  49. Heapsort Heap construction. Build max heap using bottom-up method. 11-node heap end of construction phase X T S P L R A M O E E X T S P L R A M O E E 70

  50. Heapsort Sortdown. Repeatedly delete the largest remaining item. exchange 1 and 11 X 1 T S P L R A M O E E 11 X T S P L R A M O E E 1 11 71

  51. Heapsort Sortdown. Repeatedly delete the largest remaining item. exchange 1 and 11 E 1 T S P L R A M O E X 11 E T S P L R A M O E X 1 11 72

  52. Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 E 1 T S P L R A M O E X E T S P L R A M O E X 1 73

  53. Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 T 1 E S 2 P L R A M O E X T E S P L R A M O E X 1 2 74

  54. Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 T 1 P S 2 4 E L R A M O E X T P S E L R A M O E X 1 2 4 75

  55. Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 T 1 P S 2 4 O L R A M E E X 9 T P S O L R A M E E X 1 2 4 9 76

  56. Heapsort Sortdown. Repeatedly delete the largest remaining item. T P S O L R A M E E X T P S O L R A M E E X 77

  57. Heapsort Sortdown. Repeatedly delete the largest remaining item. exchange 1 and 10 T 1 P S O L R A M E E X 10 T P S O L R A M E E X 1 10 78

  58. Heapsort Sortdown. Repeatedly delete the largest remaining item. exchange 1 and 10 E 1 P S O L R A M E T X 10 E P S O L R A M E T X 1 10 79

  59. Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 E 1 P S O L R A M E T X E P S O L R A M E T X 1 80

  60. Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 S 1 P E 3 O L R A M E T X S P E O L R A M E T X 1 3 81

  61. Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 S 1 P R 3 6 O L E A M E T X S P R O L E A M E T X 1 3 6 82

  62. Heapsort Sortdown. Repeatedly delete the largest remaining item. S P R O L E A M E T X S P R O L E A M E T X 83

  63. Heapsort Sortdown. Repeatedly delete the largest remaining item. exchange 1 and 9 S 1 P R O L E A M E T X 9 S P R O L E A M E T X 1 9 84

  64. Heapsort Sortdown. Repeatedly delete the largest remaining item. exchange 1 and 9 E 1 P R O L E A M S T X 9 E P R O L E A M S T X 1 9 85

  65. Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 E 1 P R O L E A M S T X E P R O L E A M S T X 1 86

  66. Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 R 1 P E 3 O L E A M S T X R P E O L E A M S T X 1 3 87

  67. Heapsort Sortdown. Repeatedly delete the largest remaining item. R P E O L E A M S T X R P E O L E A M S T X 88

  68. Heapsort Sortdown. Repeatedly delete the largest remaining item. exchange 1 and 8 R 1 P E O L E A M S T X 8 R P E O L E A M S T X 1 8 89

  69. Heapsort Sortdown. Repeatedly delete the largest remaining item. exchange 1 and 8 M 1 P E O L E A R S T X 8 M P E O L E A R S T X 1 8 90

  70. Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 M 1 P E O L E A R S T X M P E O L E A R S T X 1 91

  71. Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 P 1 M E 2 O L E A R S T X P M E O L E A R S T X 1 2 92

  72. Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 P 1 O E 2 4 M L E A R S T X P O E M L E A R S T X 1 2 4 93

  73. Heapsort Sortdown. Repeatedly delete the largest remaining item. P O E M L E A R S T X P O E M L E A R S T X 94

  74. Heapsort Sortdown. Repeatedly delete the largest remaining item. exchange 1 and 7 P 1 O E 7 M L E A R S T X P O E M L E A R S T X 1 7 95

  75. Heapsort Sortdown. Repeatedly delete the largest remaining item. exchange 1 and 7 A 1 O E 7 M L E P R S T X A O E M L E P R S T X 1 7 96

  76. Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 A 1 O E M L E P R S T X A O E M L E P R S T X 1 97

  77. Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 O 1 A E 2 M L E P R S T X O A E M L E P R S T X 1 2 98

  78. Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 O 1 M E 2 4 A L E P R S T X O M E A L E P R S T X 1 2 4 99

  79. Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 O M E A L E P R S T X O M E A L E P R S T X 100

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