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 G E I S add to heap T P R N H O A E I G 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 T P R N H O A violates heap order G E I S 11 (swim up) T P R N H O A E I G S 11 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 T P R N 5 S O A violates heap order G E I H 11 (swim up) T P R N S O A E I G H 5 11 11 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 T R S 2 P N 5 O A violates heap order G E I H 11 (swim up) T S R N P O A E I G H 2 5 11 11 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 T R S P N O A G E I H T S R N P O A E I G H 26
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 R S P N O A G E I H T S R N P O A E I G H 1 27
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 R S P N O A G E I H 11 exchange with root T S R N P O A E I G H 1 11 28
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 R S P N O A G E I T 11 exchange with root H S R N P O A E I G T 1 11 29
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) R S P N O A G E I T H S R N P O A E I G T 1 30
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) R H 2 P N O A G E I T S H R N P O A E I G T 1 2 31
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) R P 2 N 5 H O A G E I T S P R N H O A E I G T 1 2 5 32
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 P H N O A G E I S P R N H O A E I G 33
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 R P H N O A G E I S P R N H O A E I G 1 34
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 R P H N O A G E I exchange with root S P R N H O A E I G 1 35
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 R P H N O A E I S 10 exchange with root G P R N H O A E I S 1 10 36
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) R P H N O A E I S G P R N H O A E I S 1 37
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) G P 3 H N O A E I S R P G N H O A E I S 1 3 38
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 H G N 6 A E I S R P O N H G A E I S 1 3 6 39
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 H G N A E I R P O N H G A E I 40
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 H G N A E I S add to heap R P O N H G A E I S 10 41
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 H G N A violates heap order E I S 10 (swim up) R P O N H G A E I S 10 42
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 G N 5 S A violates heap order H E I 10 (swim up) R P O N S G A E I H 5 10 43
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 G N 5 P A violates heap order H E I 10 (swim up) R S O N P G A E I H 2 5 10 44
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 G N 5 P A violates heap order H E I 10 (swim up) S R O N P G A E I H 1 2 5 10 45
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 G N P A H E I S R O N P G A E I H 46
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) array helper functions { return pq[i].compareTo(pq[j]) < 0; } private void exch(int i, int j) { Key t = pq[i]; pq[i] = pq[j]; pq[j] = t; } } 47
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 log d N d log d N d-ary heap 1 log N † Fibonacci 1 1 impossible 1 1 1 why impossible? † amortized 48
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
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
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
P RIORITY Q UEUES AND H EAPSORT ‣ Heapsort ‣ API ‣ Elementary implementations ‣ Binary heaps ‣ Heapsort ‣ Event-driven simulation
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 M P L E X T S build a max-heap (in place) L R A P E E M O 1 A sorted result 2 3 E E (in place) 4 5 6 7 M O P L 8 9 10 11 X R S T 53
Heapsort Starting point. Array in arbitrary order. we assume array entries are indexed 1 to N S 1 O R 2 3 4 T 5 E 6 X 7 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
Heapsort Heap construction. Build max heap using bottom-up method. S O R T E 6 X 7 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
Heapsort Heap construction. Build max heap using bottom-up method. sink 5 S O R T 5 E X A M P L E S O R T E X A M P L E 5 56
Heapsort Heap construction. Build max heap using bottom-up method. sink 5 S O R T 5 L X A M P E E 10 S O R T L X A M P E E 5 10 57
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
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
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
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
Heapsort Heap construction. Build max heap using bottom-up method. sink 3 S O X 3 R T L 6 A M P E E S O X T L R A M P E E 3 6 62
Heapsort Heap construction. Build max heap using bottom-up method. sink 3 S 3-node heap O X R T L A M P E E S O X T L A A M P E E 63
Heapsort Heap construction. Build max heap using bottom-up method. sink 2 S O X 2 R T L A M P E E S O X T L R A M P E E 2 64
Heapsort Heap construction. Build max heap using bottom-up method. sink 2 S T X 2 O R 4 L A M P E E S T X O L R A M P E E 2 4 65
Heapsort Heap construction. Build max heap using bottom-up method. sink 2 S T X 2 R 4 P L A M O E E 9 S T X P L R A M O E E 2 4 9 66
Heapsort Heap construction. Build max heap using bottom-up method. sink 2 S 7-node heap T X R P L A M O E E S T X P L R A M O E E 67
Heapsort Heap construction. Build max heap using bottom-up method. sink 1 S 1 T X R P L A M O E E S T X P L R A M O E E 1 68
Heapsort Heap construction. Build max heap using bottom-up method. sink 1 X 1 T S 3 R P L A M O E E X T S P L R A M O E E 1 3 69
Heapsort Heap construction. Build max heap using bottom-up method. 11-node heap end of construction phase X T S R P L A M O E E X T S P L R A M O E E 70
Heapsort Sortdown. Repeatedly delete the largest remaining item. exchange 1 and 11 X 1 T S R P L A M O E E 11 X T S P L R A M O E E 1 11 71
Heapsort Sortdown. Repeatedly delete the largest remaining item. exchange 1 and 11 E 1 T S R P L A M O E X 11 E T S P L R A M O E X 1 11 72
Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 E 1 T S R P L A M O E X E T S P L R A M O E X 1 73
Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 T 1 E S 2 R P L A M O E X T E S P L R A M O E X 1 2 74
Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 T 1 P S 2 R 4 E L A M O E X T P S E L R A M O E X 1 2 4 75
Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 T 1 P S 2 O R 4 L A M E E X 9 T P S O L R A M E E X 1 2 4 9 76
Heapsort Sortdown. Repeatedly delete the largest remaining item. T P S O R L A M E E X T P S O L R A M E E X 77
Heapsort Sortdown. Repeatedly delete the largest remaining item. exchange 1 and 10 T 1 P S O R L A M E E X 10 T P S O L R A M E E X 1 10 78
Heapsort Sortdown. Repeatedly delete the largest remaining item. exchange 1 and 10 E 1 P S O R L A M E T X 10 E P S O L R A M E T X 1 10 79
Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 E 1 P S O R L A M E T X E P S O L R A M E T X 1 80
Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 S 1 P E 3 O R L A M E T X S P E O L R A M E T X 1 3 81
Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 S 1 R P 3 O L 6 E A M E T X S P R O L E A M E T X 1 3 6 82
Heapsort Sortdown. Repeatedly delete the largest remaining item. S R P O L E A M E T X S P R O L E A M E T X 83
Heapsort Sortdown. Repeatedly delete the largest remaining item. exchange 1 and 9 S 1 R P O L E A M E T X 9 S P R O L E A M E T X 1 9 84
Heapsort Sortdown. Repeatedly delete the largest remaining item. exchange 1 and 9 E 1 R P O L E A M S T X 9 E P R O L E A M S T X 1 9 85
Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 E 1 R P O L E A M S T X E P R O L E A M S T X 1 86
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
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
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
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
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
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
Heapsort Sortdown. Repeatedly delete the largest remaining item. sink 1 P 1 O E 2 M 4 L E A R S T X P O E M L E A R S T X 1 2 4 93
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
Heapsort Sortdown. Repeatedly delete the largest remaining item. exchange 1 and 7 P 1 O E M L E 7 A R S T X P O E M L E A R S T X 1 7 95
Heapsort Sortdown. Repeatedly delete the largest remaining item. exchange 1 and 7 A 1 O E M L E 7 P R S T X A O E M L E P R S T X 1 7 96
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
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
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
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
Recommend
More recommend