BINOMIAL HEAPS JyhShing Roger Jang CSIE Dept National
BINOMIAL HEAPS Jyh-Shing Roger Jang (張智星) CSIE Dept, National Taiwan University
MEMORY ALLOCATION IN CLASSES The binary heap is good for simple operations such as insertion, deletion, and min extraction, but it is bad for union operation. Union operation requires building up a new heap from scratch using the elements from the old heaps, which is expensive Binomial heaps support union operations more efficiently. 2
BINOMIAL TREES A binomial heap is a collection of binomial trees A binomial tree Bk is an ordered tree defined recursively B 0 consists of a single node Bk consists of two binomial tree Bk-1, with root of one is the leftmost child of the root of the other 3
PROPERTIES OF BINOMIAL TREES For a binomial tree Bk There are 2 k nodes Height = k Proof by induction on k Degree of root = k There are exactly nodes at depth i for i = 0, 1, …, k The root has a k subtrees, with each subtree being Bi with i = k-1, k-2, …, 0. Deleting root yields binomial trees Bk-1, Bk-2, …, B 0. 4
BINOMIAL TREES: WHY IT’S NAMED SO 5
PROOF OF THE “BINOMIAL” PROPERTY 6
PROPERTIES OF BINOMIAL TREES 7
BINOMIAL HEAP Binomial heap by Vuillemin, 1978 Composed of a sequence of binomial trees Each tree is min-heap ordered 0 or 1 binomial tree of order k 8
BINOMIAL HEAP: IMPLEMENTATION Common implementation of binomial heaps Represent trees via 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 9
BINOMIAL HEAP: PROPERTIES Properties of N-node binomial heap Min key contained in roots of Bk-1, Bk-2, …, B 0 Contains Bi if bi=1 for binary representation bk-1 bk-2…b 2 b 0. At most binomial trees Height ≤ 10
BINOMIAL HEAP: PROOF OF PROPERTIES Properties of N-node binomial heap Assume its binary representation is bk-1 bk-2…b 1 b 0, with bk-1 ≠ 0 100… 0 At most 111… 1 binomial trees Height = k-1 ≤ 11
BINOMIAL HEAP: UNION OF SAME-ORDER BINOMIAL TREES Create Binomial Tree H that is union of H’ and H’’ Easy if H’ and H’’ are of the same order (degree) Connect roots of H’ and H’’, with smaller key to be root of H Choose smaller Min key contained in roots of Bk-1, Bk-2, …, B 0 Keep min-heap order 12
BINOMIAL HEAP: UNION Union of two binomial heaps Reference: http: //www. cs. princeton. edu/~wayne/cs 423/lectures/heaps. ppt Just like adding two binary numbers 13
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 14
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 15
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 16
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 17
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 18
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 19
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 19 + 7 = 26 + 1 1 0 0 1 1 1 1 1 0 20
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) Comparison: O(1) for finding min if we always keep a pointer to the min 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 21
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' 22
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 23
Binomial Heap: Delete node x in binomial heap H. n Decrease key of x to -. n Bubble up to the root. n Delete min. Running time. O(log N) 24
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 25
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. 26
Priority Queues With extra book keeping Heaps Operation Linked List Binary Binomial Fibonacci * Relaxed make-heap 1 1 1 insert 1 log N 1 1 find-min N 1 1 delete-min N log N union 1 M*log(M+N) log N 1 1 decrease-key 1 log N 1 1 delete N log N is-empty 1 1 1 M is the size of the smaller heap just did this 27
- Slides: 27