Presentation is loading. Please wait.

Presentation is loading. Please wait.

Insert in amortized cost O(1), Merge in O(lgn) DeleteMax in O(lgn)

Similar presentations


Presentation on theme: "Insert in amortized cost O(1), Merge in O(lgn) DeleteMax in O(lgn)"— Presentation transcript:

1 Insert in amortized cost O(1), Merge in O(lgn) DeleteMax in O(lgn)
Binomial Heaps Data structure with: Insert in amortized cost O(1), Merge in O(lgn) DeleteMax in O(lgn) 9/20/2018 Cutler/Head

2 Merging two binomial heaps Delete max
This class Why binomial heaps Binomial trees Binomial max heaps Insert Merging two binomial heaps Delete max 9/20/2018 Cutler/Head

3 MaxHeap operations are: find-max() is O(1)
Why not Heaps? MaxHeap operations are: find-max() is O(1) insert(k ), delete-max() is O(lgn) merge(heap1, heap2) is O(n) 2 methods for merge: “append” the heaps and then use fast-build-heap. O(n) where n is the total number of elements “append” the heaps and then percolate the nodes in the smaller heap If number elements in small heap proportional to n, the worst case time complexity is O(n lg n) 9/20/2018 Cutler/Head

4 find-max() is O(lg n) (O(1) if additional pointer)
Why Binomial Heaps? Binomial Heaps have improved runtime for some of the primary operations of heaps: find-max() is O(lg n) (O(1) if additional pointer) delete-max() is O(lgn) insert(k ) amortized O(1) and worst case is O(lg n) merge(heap1, heap2) with a total of n elements is O(lgn) Example of a Binomial heap shown below B1 B3 9/20/2018 Cutler/Head

5 Binomial Trees The i th binomial tree, Bi , i  0 has a root with i children, Bi-1, … , B0 B0 B1 B2 B Bi n i depth i = lgn B0 B1 B2 B3 9/20/2018 Cutler/Head

6 The number of nodes at depth 0,1,2, …, i are
d n B4 1 2 3 4 9/20/2018 Cutler/Head

7 Binomial (Maximum) Heap
Is a collection of binomial trees of distinct sizes each of which has the heap property. The roots of the binomial trees are connected (as a doubly linked list) in the order of increasing size. (There may be a pointer to the largest key). B1 B0 B3 29 35 23 21 20 14 9 17 5 1 8 9/20/2018 Cutler/Head

8 Binary numbers and binomial heaps
Note correspondence of binary representation of n, and the structure of a binomial heap with n nodes n can be represented uniquely as a binary number blast…b1b0 with last =  lg n and  lg n+1 bits A binomial heap for n nodes where n = blast…b1b0 will have a binomial tree for all i where bi =1. For example will have B4-- B3– B0. 9/20/2018 Cutler/Head

9 A node of the binomial heap
9/20/2018 Cutler/Head

10 A binomial heap 15 28 15 28 H 2 7 11 7 11 1 5 5 9/20/2018 Cutler/Head

11 Merging same size binomial heaps Bi into a B i+1
Link trees - Make the root of the tree with the smaller max value, the i+1th child of the binomial tree with the larger max value. O(1) H1 H2 15 12 + 7 5 9 2 2 3 H1 15 12 7 5 9 2 2 9/20/2018 3 Cutler/Head

12 Code for merging same size heaps into 1 binomial tree
JoinSameBinomialTrees(T1, T2) //T1 and T2 are not NULL If degree[T1]=degree[T2] If key[T1]>key[T2] degree[T1] parent[T2] = T sibling[T2]=child[T1] child[T1] = T return T1 else degree[T2] parent[T1] = T sibling[T1]=child[T2] child[T2] = T return T2 else send error message 9/20/2018 Cutler/Head

13 1) Convert item to be inserted into a B*0 2) Set i to 0
Insert New Note: The correspondence between “count” and inserting a new key into a binomial heap H 1) Convert item to be inserted into a B*0 2) Set i to 0 3) If H includes a Bi A) Remove Bi from H. B) Link Bi with B*i to form B*i+1 C) Set i to i +1 D) Repeat step 3 4) Else link B*i with H (and update max pointer). Worst case time is O(lgn) 9/20/2018 Cutler/Head

14 i = 0. Remove B0 and link it with B0* getting B1*
Example for insertion B0 B1 B2 B4 4 trees 23 nodes H merge with B*0 New i = 0. Remove B0 and link it with B0* getting B1* B1 B*1 H i =0 i = 1. Remove B1, link B1* with B1 getting B2* B4 B2 H B*2 i =1 9/20/2018 Cutler/Head

15 I = 2. Remove B2, link B2* with B2 getting B3*
Example Continued I = 2. Remove B2, link B2* with B2 getting B3* B4 H B*3 i =2 i = 3. H does not include B3. link B*3 with B4 . Binomial heap has 2 binomial trees and 24 nodes.. B3 B4 H i =3 2 trees 24 nodes 9/20/2018 Cutler/Head

16 Doing a sequence of insertions
H 10 B0 Insert 20 H 20 20 B*0 H contains B0 so remove B0. Join into B1 and add to H 10 9/20/2018 Cutler/Head

17 Doing a sequence of insertion
3 Insert 3 B*0 H does not contain B0 so add B0* to H H 3 20 B1 B0 10 9/20/2018 Cutler/Head

18 Doing a sequence of insertions
H 3 20 Insert 8 B1 B0 20 8 10 3 H 8 10 B*0 First 3 is removed Then 3 and 8 are joined into a B1* Then the two B1s are joined 9/20/2018 Cutler/Head

19 Doing a sequence of insertions
20 H 10 8 Insert 30 3 30 20 8 10 3 9/20/2018 Cutler/Head

20 Amortized analysis for insert new
Assume n=2k inserts Inserts H Links per insert n/ no B n/ B no B n/ B0, B1 no B ... n/2k = 1 B0 , B1,…Bk-2 no k Bk-1 B0 , B1,…Bk-2 Bk (k+1) no Bk Note correspondence: Flips for binary increment and links for binomial heap insert 9/20/2018 Cutler/Head

21 Amortized analysis for insert new
The total time (link operations) 1*(n/2)+2*(n/4)+3*(n/8)+…+ k*(n/2k)+ (k+1)*1= So amortized cost for insertion is O(1) 9/20/2018 Cutler/Head

22 Merge 2 Binomial Heaps, H1 and H2 into Result
Note: The correspondence between adding two binary numbers and merging two binomial heaps Merge H1 and H2 into a new binomial heap, result. Let n = max(n1, n2). The largest binomial tree in the heaps is Blg n stage 0: 1. If neither contain B0 do nothing 2. If only one binomial heap includes a B0 put it into the result. 3. If both include B0 , link them into B1 and save for next stage. (like carry bit in binary addition) 9/20/2018 Cutler/Head

23 Merge 2 Binomial Heaps, H1 and H2 into Result
stage i (i = 1,…, lg n): There may be 0 to 3 Bi ‘s, one from H1, one from H2 and one from the previous stage. 1. If no Bi do nothing 2. If there is only one Bi put it into the result. 3. Otherwise link two Bi ‘s into Bi+1 and save for next stage. 4. If there is still a Bi put it in the result. If there is a saved B, add to result There are exactly lg n +1 stages. Each stage is O(1). So worst case growth rate is O(lgn) 9/20/2018 Cutler/Head

24 deleting-max 1) Remove the Bi containing the max from H, joining remaining parts into a new binomial heap H1. 2) Remove root of Bi .link the binomial subtrees of the root into a new binomial heap H2. 3) Merge H1 and H2. Time: 1) O(lgn) when no max pointer 2) Since Bi has i children there are i links. Bi contains 2i n nodes, so ilg n and the worst case time is O(lg n) 3) O(lg n) TOTAL O(lg n) 9/20/2018 Cutler/Head


Download ppt "Insert in amortized cost O(1), Merge in O(lgn) DeleteMax in O(lgn)"

Similar presentations


Ads by Google