Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamental Structures of Computer Science

Similar presentations


Presentation on theme: "Fundamental Structures of Computer Science"— Presentation transcript:

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

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

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

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

5 Binomial Trees A Binomial Tree Bk of order k is defined as follows
B0 is a tree with one node Bk is a pair of Bk-1 trees, where root of one Bk-1 becomes the left most child of the other (for all k ≥ 1) B0 B3 B1 B2

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

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

8 Proofs Lemma 1: BK has 2k nodes
Proof: (by induction). True for k=0, assume true for k=r. Consider Br+1 Br+1 has 2r + 2r = 2r+1 nodes Lemma 2: Bk has height k Proof: homework Lemma 3: Bk has kCi 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-1Ci + k-1Ci-1 = kCi

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

10 Example A binomial heap of n=15 nodes containing B0, B1, B2 and B3 binomial trees What is the connection between n and number of binary trees in the heap?

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

12 implementation

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

14 Implementation – Binary Heap
head root1 root2 root3 root4

15 Operations

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

17 Merging two binomial heaps
Suppose H1 and H2 are two binomial heaps Merge H1 and H2 into a new heap H Algorithm: Let A and B be pointers to H1 and H2 for all orders i If there is one order i tree, merge it to H 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 If there are three order i trees in H1,H2 and T, merge two of them, store as T and add the remainder to H

18 Example

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

20 Binary Heap Operations
DeleteRoot() Find the tree with the given root Split the heap into two heaps H1 and H2

21 Binary Heap Operations
DeleteRoot() ctd.. Rearrange binomial trees in heap H2 Merge the two heaps v v

22 Example

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

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


Download ppt "Fundamental Structures of Computer Science"

Similar presentations


Ads by Google