Binomial Heaps and Trees for Efficient Memory Allocation
E N D
Presentation Transcript
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.
Binomial Trees • A binomial heap is a collection of binomial trees • A binomial tree Bk is an ordered tree defined recursively • B0 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
Properties of Binomial Trees • For a binomial tree Bk • There are 2k nodes • Height = 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, …, B0 . Proof by induction on k
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
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
Binomial Heap: Properties • Properties of N-node binomial heap • Min key contained in roots of Bk-1, Bk-2, …, B0 • Contains Bi if bi=1 for binary representation bk-1bk-2…b2b0. • At most binomial trees • Height ≤
Binomial Heap: Proof of Properties • Properties of N-node binomial heap • Assume its binary representation is bk-1bk-2…b1b0, with bk-1 ≠ 0 • At most binomial trees • Height = k-1 ≤ 100…0 111…1
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, …, B0 Keep min-heap order
Binomial Heap: Union • Union of two binomial heaps • Reference: http://www.cs.princeton.edu/~wayne/cs423/lectures/heaps.ppt Just like adding two binary numbers
Binomial Heap: Union 6 3 18 37 8 29 10 44 30 23 22 48 31 17 15 7 12 45 32 24 50 25 28 33 + 55 41
Binomial Heap: Union 12 18 6 3 18 37 8 29 10 44 30 23 22 48 31 17 15 7 12 45 32 24 50 25 28 33 + 55 41
3 12 18 7 37 25 6 3 18 37 8 29 10 44 30 23 22 48 31 17 15 7 12 45 32 24 50 25 28 33 + 55 41 12 18
3 3 12 18 15 7 7 37 37 28 33 25 25 41 6 3 18 37 8 29 10 44 30 23 22 48 31 17 15 7 12 45 32 24 50 25 28 33 + 55 41 12 18
3 3 12 18 15 7 7 37 37 28 33 25 25 41 6 3 18 37 8 29 10 44 30 23 22 48 31 17 15 7 12 45 32 24 50 25 28 33 + 55 41 3 12 15 7 37 18 28 33 25 41
3 3 12 18 15 7 7 37 37 28 33 25 25 41 6 3 18 37 8 29 10 44 30 23 22 48 31 17 15 7 12 45 32 24 50 25 28 33 + 55 41 6 3 12 8 29 10 15 7 44 37 18 30 23 22 48 31 17 28 33 25 45 32 24 50 41 55
Binomial Heap: Union • Create heap H that is union of heaps H' and H''. • Analogous to binary addition. • Running time. O(log N) • Proportional to number of trees in root lists 1 1 1 1 0 0 1 1 + 0 0 1 1 1 19 + 7 = 26 1 1 0 1 0
Binomial Heap: Delete Min 3 6 18 44 8 29 10 37 30 23 22 48 31 17 H 45 32 24 50 55 • Delete node with minimum key in binomial heap H. • Find root x with min key in root list of H, and delete • H' broken binomial trees • H Union(H', H) • Running time. O(log N) Comparison: O(1) for finding min if we always keep a pointer to the min
Binomial Heap: Delete Min H' • Delete node with minimum key in binomial heap H. • Find root x with min key in root list of H, and delete • H' broken binomial trees • H Union(H', H) • Running time. O(log N) 6 18 44 8 29 10 37 30 23 22 48 31 17 H 45 32 24 50 55
Binomial Heap: Decrease Key • Decrease key of node x in binomial heap H. • Suppose x is in binomial tree Bk. • Bubble node x up the tree if x is too small. • Running time. O(log N) • Proportional to depth of node x log2 N . 3 6 18 depth = 3 44 8 29 10 37 30 23 22 48 31 17 H x 32 24 50 55
Binomial Heap: Delete • Delete node x in binomial heap H. • Decrease key of x to -. • Bubble up to the root. • Delete min. • Running time. O(log N)
Binomial Heap: Insert • Insert a new node x into binomial heap H. • H' MakeHeap(x) • H Union(H', H) • Running time. O(log N) 3 6 18 x 44 8 29 10 37 30 23 22 48 31 17 H H' 45 32 24 50 55
Binomial Heap: Sequence of Inserts • Insert a new node x into binomial heap H. • If N = .......0, then only 1 steps. • If N = ......01, then only 2 steps. • If N = .....011, then only 3 steps. • If N = ....0111, then only 4 steps. • Inserting 1 item can take (log N) time. • If N = 11...111, then log2 N steps. • But, inserting sequence of N items takes O(N) time! • (N/2)(1) + (N/4)(2) + (N/8)(3) + . . . 2N • Amortized analysis. • Basis for getting most operationsdown to constant time. 3 6 x 37 29 10 44 48 31 17 50
Priority Queues With extra book keeping Heaps Operation Linked List Binary Binomial Fibonacci * Relaxed make-heap 1 1 1 1 1 insert 1 log N log N 1 1 find-min N 1 1 1 1 delete-min N log N log N log N log N union 1 M*log(M+N) log N 1 1 decrease-key 1 log N log N 1 1 delete N log N log N log N log N is-empty 1 1 1 1 1 M is the size of the smaller heap just did this