Binary and Binomial Heaps These lecture slides are
Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters 6, 19. Princeton University • COS 423 • Theory of Algorithms • Spring 2002 • Kevin Wayne
Priority Queues Supports the following operations. n Insert element x. n Return min element. n Return and delete minimum element. n Decrease key of element x to k. Applications. n Dijkstra's shortest path algorithm. n Prim's MST algorithm. n Event-driven simulation. n Huffman encoding. n Heapsort. n . . . 2
Priority Queues in Action Dijkstra's Shortest Path Algorithm PQinit() for each v V key(v) PQinsert(v) key(s) 0 while (!PQisempty()) v = PQdelmin() for each w Q s. t (v, w) E if (w) > (v) + c(v, w) PQdecrease(w, (v) + c(v, w)) 3
Priority Queues Heaps Operation Linked List Binary Binomial Fibonacci * Relaxed make-heap 1 1 1 insert 1 log N 1 1 find-min N 1 log N 1 1 delete-min N log N union 1 N log N 1 1 decrease-key 1 log N 1 1 delete N log N is-empty 1 1 1 Dijkstra/Prim 1 make-heap |V| insert |V| delete-min |E| decrease-key O(|V|2) O(|E| log |V|) O(|E| + |V| log |V|) 4
Binary Heap: Definition Binary heap. n n Almost complete binary tree. – filled on all levels, except last, where filled from left to right Min-heap ordered. – every child greater than (or equal to) parent 06 14 78 83 45 18 91 81 47 77 84 53 99 64 5
Binary Heap: Properties. n n Min element is in root. Heap with N elements has height = log 2 N. 06 N = 14 Height = 3 14 78 83 45 18 91 81 47 77 84 53 99 64 6
Binary Heaps: Array Implementation Implementing binary heaps. n Use an array: no need for explicit parent or child pointers. – Parent(i) = i/2 – Left(i) = 2 i – Right(i) = 2 i + 1 06 1 14 45 2 3 78 18 47 53 4 5 6 7 83 91 81 77 84 99 64 8 9 10 11 12 13 14 7
Binary Heap: Insertion Insert element x into heap. n n Insert into next available slot. Bubble up until it's heap ordered. – Peter principle: nodes rise to level of incompetence 06 14 78 83 45 18 91 81 47 77 84 53 99 64 42 next free slot 8
Binary Heap: Insertion Insert element x into heap. n n Insert into next available slot. Bubble up until it's heap ordered. – Peter principle: nodes rise to level of incompetence swap with parent 06 14 78 83 45 18 91 81 47 77 84 53 99 64 42 9
Binary Heap: Insertion Insert element x into heap. n n Insert into next available slot. Bubble up until it's heap ordered. – Peter principle: nodes rise to level of incompetence swap with parent 06 14 78 83 45 18 91 81 47 77 84 42 99 64 42 53 10
Binary Heap: Insertion Insert element x into heap. n n n Insert into next available slot. Bubble up until it's heap ordered. – Peter principle: nodes rise to level of incompetence O(log N) operations. stop: heap ordered 06 14 78 83 42 18 91 81 47 77 84 45 99 64 53 11
Binary Heap: Decrease Key Decrease key of element x to k. n Bubble up until it's heap ordered. n O(log N) operations. 06 14 78 83 42 18 91 81 47 77 84 45 99 64 53 12
Binary Heap: Delete Min Delete minimum element from heap. n n Exchange root with rightmost leaf. Bubble root down until it's heap ordered. – power struggle principle: better subordinate is promoted 06 14 78 83 42 18 91 81 47 77 84 45 99 64 53 13
Binary Heap: Delete Min Delete minimum element from heap. n n Exchange root with rightmost leaf. Bubble root down until it's heap ordered. – power struggle principle: better subordinate is promoted 53 14 78 83 42 18 91 81 47 77 84 45 99 64 06 14
Binary Heap: Delete Min Delete minimum element from heap. n n Exchange root with rightmost leaf. Bubble root down until it's heap ordered. – power struggle principle: better subordinate is promoted exchange with left child 53 14 78 83 42 18 91 81 47 77 84 45 99 64 15
Binary Heap: Delete Min Delete minimum element from heap. n n Exchange root with rightmost leaf. Bubble root down until it's heap ordered. – power struggle principle: better subordinate is promoted exchange with right child 14 53 78 83 42 18 91 81 47 77 84 45 99 64 16
Binary Heap: Delete Min Delete minimum element from heap. n n n Exchange root with rightmost leaf. Bubble root down until it's heap ordered. – power struggle principle: better subordinate is promoted O(log N) operations. stop: heap ordered 14 18 78 83 42 53 91 81 47 77 84 45 99 64 17
Binary Heap: Heapsort. n Insert N items into binary heap. n Perform N delete-min operations. n O(N log N) sort. n No extra storage. 18
Binary Heap: Union. n n n Combine two binary heaps H 1 and H 2 into a single heap. No easy solution. – (N) operations apparently required Can support fast union with fancier heaps. H 1 H 2 14 11 78 41 18 91 81 53 77 84 62 99 64 19
Priority Queues Heaps Operation Linked List Binary Binomial Fibonacci * Relaxed make-heap 1 1 1 insert 1 log N 1 1 find-min N 1 log N 1 1 delete-min N log N union 1 N log N 1 1 decrease-key 1 log N 1 1 delete N log N is-empty 1 1 1 20
Binomial Tree Binomial tree. n Recursive definition: Bk B 0 Bk-1 B 0 B 1 B 2 B 3 B 4 21
Binomial Tree Useful properties of order k binomial tree Bk. n Number of nodes = 2 k. n Height = k. n Degree of root = k. n Bk+1 Deleting root yields binomial trees Bk-1, … , B 0. Proof. n B 0 Bk B 2 B 1 B 0 By induction on k. B 1 B 2 B 3 B 4 22
Binomial Tree A property useful for naming the data structure. n Bk has nodes at depth i. depth 0 depth 1 depth 2 depth 3 depth 4 B 4 23
Binomial Heap Binomial heap. Vuillemin, 1978. n Sequence of binomial trees that satisfy binomial heap property. – each tree is min-heap ordered – 0 or 1 binomial tree of order k 8 45 55 30 23 32 24 22 48 29 10 31 17 6 3 44 37 18 50 B 4 B 1 B 0 24
Binomial Heap: Implementation. n n Represent trees using left-child, right sibling pointers. – three links per node (parent, left, right) Roots of trees connected with singly linked list. – degrees of trees strictly decreasing from left to right heap 6 3 18 6 nt e r Pa 29 10 44 37 50 48 50 Binomial Heap t 17 gh 31 Ri 48 18 37 29 ft e L 3 10 31 17 44 Leftist Power-of-2 Heap 25
Binomial Heap: Properties of N-node binomial heap. n n Min key contained in root of B 0, B 1, . . . , Bk. Contains binomial tree Bi iff bi = 1 where bn b 2 b 1 b 0 is binary representation of N. n At most log 2 N + 1 binomial trees. n Height log 2 N. 8 45 55 30 23 32 24 22 48 29 10 31 17 50 B 4 6 3 44 37 18 N = 19 # trees = 3 height = 4 binary = 10011 B 0 26
Binomial Heap: Union Create heap H that is union of heaps H' and H''. n n "Mergeable heaps. " Easy if H' and H'' are each order k binomial trees. – connect roots of H' and H'' – choose smaller key to be root of H 6 8 45 55 30 23 32 24 22 48 29 10 31 17 44 50 H' H'' 27
Binomial Heap: Union 8 30 45 + 32 23 24 22 48 29 10 31 17 6 3 44 37 15 7 33 25 18 12 50 28 55 41 19 + 7 = 26 + 1 1 0 0 1 1 1 1 1 0 28
Binomial Heap: Union 8 30 45 + 55 32 23 24 22 48 29 10 31 17 6 3 44 37 15 7 33 25 18 12 50 28 41 29
Binomial Heap: Union 12 18 8 30 45 + 55 32 23 24 22 48 29 10 31 17 6 3 44 37 15 7 33 25 18 12 50 28 41 30
7 3 12 37 18 25 8 30 45 + 55 32 23 24 22 48 29 10 31 17 6 3 44 37 15 7 33 25 18 12 50 28 41 12 18 31
3 28 15 7 33 25 37 7 3 12 37 18 25 41 8 30 45 + 55 32 23 24 22 48 29 10 31 17 6 3 44 37 15 7 33 25 18 12 50 28 41 12 18 32
3 28 15 7 33 25 37 7 3 12 37 18 25 41 8 30 45 + 32 23 24 22 48 29 10 31 17 6 3 44 37 15 7 33 25 18 12 50 28 55 41 28 15 7 33 25 3 12 37 18 41 33
3 28 15 7 33 25 37 7 3 12 37 18 25 41 8 30 45 + 32 23 22 24 48 29 10 31 17 6 3 44 37 15 7 33 25 18 12 50 28 55 41 6 8 45 55 30 23 32 24 22 48 50 29 10 31 17 44 28 15 7 33 25 3 12 37 18 41 34
Binomial Heap: Union Create heap H that is union of heaps H' and H''. n Analogous to binary addition. Running time. O(log N) n Proportional to number of trees in root lists 2( log 2 N + 1). 19 + 7 = 26 + 1 1 0 0 1 1 1 1 1 0 35
Binomial Heap: Delete Min Delete node with minimum key in binomial heap H. n Find root x with min key in root list of H, and delete n H' broken binomial trees n H Union(H', H) Running time. O(log N) 8 45 30 23 32 24 22 48 29 10 31 17 3 6 44 37 18 H 50 55 36
Binomial Heap: Delete Min Delete node with minimum key in binomial heap H. n Find root x with min key in root list of H, and delete n H' broken binomial trees n H Union(H', H) Running time. O(log N) 6 8 45 55 30 23 32 24 22 48 29 10 31 17 44 18 37 H 50 H' 37
Binomial Heap: Decrease Key Decrease key of node x in binomial heap H. n n Suppose x is in binomial tree Bk. Bubble node x up the tree if x is too small. Running time. O(log N) n Proportional to depth of node x log 2 N . 8 depth = 3 x 30 23 32 24 22 48 29 10 31 17 3 6 44 37 18 H 50 55 38
Binomial Heap: Delete node x in binomial heap H. n Decrease key of x to -. n Delete min. Running time. O(log N) 39
Binomial Heap: Insert a new node x into binomial heap H. n H' Make. Heap(x) n H Union(H', H) Running time. O(log N) 8 45 30 23 32 24 22 48 29 10 31 17 3 6 44 37 18 H x H' 50 55 40
Binomial Heap: Sequence of Inserts Insert a new node x into binomial heap H. If N =. . . . 0, then only 1 steps. n n If N =. . . 01, then only 2 steps. n If N =. . . 011, then only 3 steps. n If N =. . 0111, then only 4 steps. 48 29 10 31 17 3 6 44 37 x 50 Inserting 1 item can take (log N) time. If N = 11. . . 111, then log 2 N steps. n But, inserting sequence of N items takes O(N) time! n (N/2)(1) + (N/4)(2) + (N/8)(3) +. . . 2 N n Amortized analysis. n Basis for getting most operations down to constant time. 41
Priority Queues Heaps Operation Linked List Binary Binomial Fibonacci * Relaxed make-heap 1 1 1 insert 1 log N 1 1 find-min N 1 log N 1 1 delete-min N log N union 1 N log N 1 1 decrease-key 1 log N 1 1 delete N log N is-empty 1 1 1 just did this 42
- Slides: 42