Presentation is loading. Please wait.

Presentation is loading. Please wait.

15-211 Fundamental Structures of Computer Science March 02, 2006 Ananda Guna Binomial Heaps.

Similar presentations


Presentation on theme: "15-211 Fundamental Structures of Computer Science March 02, 2006 Ananda Guna Binomial Heaps."— Presentation transcript:

1 15-211 Fundamental Structures of Computer Science March 02, 2006 Ananda Guna Binomial Heaps

2 In this Lecture z Binomial Trees y Definition y properties z Binomial Heaps y efficient merging z Implementation z Operations z About Midterm

3 Binary Heaps z Binary heap is a data structure that allows y insert in O(log n) y deleteMin in O(log n) y findMin in O(1) z How about merging two heaps y complexity is O(n) z So we discuss a data structure that allows merge in O(log n)

4 Applications of Heaps z Binary Heaps y efficient findMin, deleteMin y many applications z Binomial Heaps y Efficient merge of two heaps y Merging two heap based data structures z Binomial Heap is build using a structure called Binomial Trees

5 Binomial Trees z A Binomial Tree B k of order k is defined as follows y B 0 is a tree with one node  B k is a pair of B k-1 trees, where root of one B k-1 becomes the left most child of the other (for all k ≥ 1) B0B0 B1B1 B2B2 B3B3

6 Merging two binomial trees z Merging two equal binomial trees of order j + = New tree has order j + 1

7 Properties of Binomial trees zThe following properties hold for a binomial tree of order k y B k has 2 k nodes y The height of B k is k y B k has k C i nodes at level i for i = 0,1,…k y The root of B k has k-children B 0, B 1, …B k-1 (in that order) where the i th child is a binomial tree of order i. y If binomial tree of order k has n nodes, then k ≤ log n

8 Proofs Lemma 1: B K has 2 k nodes Proof: (by induction). True for k=0, assume true for k=r. Consider B r+1 B r+1 has 2 r + 2 r = 2 r+1 nodes Lemma 2: B k has height k Proof: homework Lemma 3: B k has k C i nodes at level i for i = 0,1,…k Proof: Let T(k,i) be the number of nodes at depth i. Then T(k,i) = T(k-1,i) + T(k-1,i-1) = k-1 C i + k-1 C i-1 = k C i

9 Binomial Heap z Binomial Heap is a collection of binomial trees that satisfies the following properties y No two binomial trees in the collection have the same size y Each node in the collection has a key y Each binomial tree in the collection satisfies the heap order property y Roots of the binomial trees are connected and are in increasing order

10 Example z A binomial heap of n=15 nodes containing B 0, B 1, B 2 and B 3 binomial trees z What is the connection between n and the binomial trees in the heap?

11 Lemma Given any integer n, there exists a binomial heap that contain n nodes Proof:

12 implementation

13 Implementation – Binomial Tree Node z Fields in a binomial tree node y Key y number of children (or degree) y Left most child y Right most sibling y A pointer P to parent

14 Implementation – Binomial Heap head root1 root2 root3root4

15 Operations

16 Operations on Binomial Heaps z Merge is the key operation on binomial heaps y merge() y insert() y findMin() x find the min of all children O(log n) y deleteRoot() y deleteNode() y decreaseKey()

17 Merging two binomial heaps z Suppose H 1 and H 2 are two binomial heaps z Merge H 1 and H 2 into a new heap H z Algorithm: y Let A and B be pointers to H 1 and H 2 y for all orders i x If there is one order i tree, merge it to H x If there are two order i trees, merge them into a new tree of order i+1 and store them in a temp tree T x If there are three order i trees in H 1,H 2 and T, merge two of them, store as T and add the remainder to H

18 Example

19 Binary Heap Operations z Insert y make a new heap H 0 with the new node y Merge(H 0, H) z FindMin y min is one of the children connected to the root x cost is O(log n)

20 Binary Heap Operations z DeleteRoot() y Find the tree with the given root y Split the heap into two heaps H 1 and H 2

21 Binary Heap Operations z DeleteRoot() ctd.. y Rearrange binomial trees in heap H 2 y Merge the two heaps v v

22 Example

23 DeleteNode() z To delete a node, decrease its key to -∞, percolate up to root, then delete the root z DecreaseKey(): Decrease the key and percolate up until heap order property is satisfied

24 Summary z Two heaps y Binary Heaps x deleteMin() y Binomial Heaps x mergeHeaps() z Next Week y Midterm on Tuesday y Strings and Tries on thursday


Download ppt "15-211 Fundamental Structures of Computer Science March 02, 2006 Ananda Guna Binomial Heaps."

Similar presentations


Ads by Google