Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.

Similar presentations


Presentation on theme: "Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University."— Presentation transcript:

1 Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

2 2 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.

3 3 Binomial Trees A binomial heap is a collection of binomial trees A binomial tree B k is an ordered tree defined recursively B 0 consists of a single node B k consists of two binomial tree B k-1, with root of one is the leftmost child of the root of the other

4 4 Properties of Binomial Trees For a binomial tree B k There are 2 k 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 B i with i = k-1, k-2, …, 0.  Deleting root yields binomial trees B k-1, B k-2, …, B 0. Proof by induction on k

5 5 Binomial Trees: Why It’s Named So

6 6 Proof of the “Binomial” Property

7 7 Properties of Binomial Trees

8 8 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

9 9 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

10 10 Binomial Heap: Properties Properties of N-node binomial heap Min key contained in roots of B k-1, B k-2, …, B 0 Contains B i if b i =1 for binary representation b k-1 b k-2 …b 2 b 0. At most binomial trees Height ≤

11 11 Binomial Heap: Proof of Properties Properties of N-node binomial heap Assume its binary representation is b k-1 b k-2 …b 1 b 0, with b k-1 ≠ 0  At most binomial trees  Height = k-1 ≤ 111…1 100…0

12 12 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 B k-1, B k-2, …, B 0 Keep min-heap order

13 13 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

14 14 Binomial Heap: Union 55 45 32 30 24 2322 50 483117 44 82910 6 37 318 41 33 28 15 25 712 +

15 15 Binomial Heap: Union 55 45 32 30 24 2322 50 483117 44 82910 6 37 3 41 33 28 15 25 7 + 12 18 12

16 16 55 45 32 30 24 2322 50 483117 44 82910 6 37 3 41 33 28 15 25 7 + 12 18 25 37 7 3 18 12 18 12

17 17 55 45 32 30 24 2322 50 483117 44 82910 6 37 3 41 33 28 15 25 7 12 + 18 25 37 7 3 41 283325 37 157 3 18 12 18 12

18 18 55 45 32 30 24 2322 50 483117 44 82910 6 37 3 41 33 28 15 25 7 + 18 12 41 283325 37 157 3 12 18 25 37 7 3 41 283325 37 157 3 18 12

19 19 55 45 32 30 24 2322 50 483117 44 82910 6 37 3 41 33 28 15 25 7 + 18 12 41 283325 37 157 3 12 18 25 37 7 3 41 283325 37 157 3 55 45 32 30 24 2322 50 483117 44 82910 6 18 12

20 20 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 0011 1001+ 0111 11 1 1 0 1 19 + 7 = 26

21 21 3 37 618 55 45 32 30 24 2322 50 483117 44 82910 H 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) Comparison: O(1) for finding min if we always keep a pointer to the min

22 22 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) 55 45 32 30 24 2322 50 483117 37 618 44 82910 H H'

23 23 3 37 618 55 x 32 30 24 2322 50 483117 44 82910 H Binomial Heap: Decrease Key Decrease key of node x in binomial heap H. n Suppose x is in binomial tree B k. n 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 . depth = 3

24 24 Binomial Heap: Delete 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)

25 25 Binomial Heap: Insert Insert a new node x into binomial heap H. n H'  MakeHeap(x) n H  Union(H', H) Running time. O(log N) 3 37 618 55 45 32 30 24 2322 50 483117 44 82910 H x H'

26 26 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 log 2 N steps. But, inserting sequence of N items takes O(N) time! n (N/2)(1) + (N/4)(2) + (N/8)(3) +...  2N n Amortized analysis. n Basis for getting most operations down to constant time. 50 483117 44 2910 3 37 6 x

27 27 Priority Queues make-heap Operation insert find-min delete-min union decrease-key delete 1 Binary log N 1 M*log(M+N) log N 1 Binomial log N 1 1 Fibonacci * 1 1 log N 1 1 1 Relaxed 1 1 log N 1 1 1 Linked List 1 N N 1 1 N is-empty11111 Heaps just did this With extra book keeping M is the size of the smaller heap


Download ppt "Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University."

Similar presentations


Ads by Google