1 / 24

Binomial Heaps

Binomial Heaps. Data structure with: Insert in amortized cost O(1), Merge in O(lgn) DeleteMax in O(lgn). This class. Why binomial heaps Binomial trees Binomial max heaps Insert Merging two binomial heaps Delete max. Why not Heaps?. MaxHeap operations are: find-max() is O(1)

Télécharger la présentation

Binomial Heaps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. This class • Why binomial heaps • Binomial trees • Binomial max heaps • Insert • Merging two binomial heaps • Delete max Cutler/Head

  3. 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) Cutler/Head

  4. 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 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 B3 ... Bi n 1 2 4 8 ... 2i depth 0 1 2 3 ... i = lgn B1 B3 B0 B2 Cutler/Head

  6. The Number of Nodes • The number of nodes at depth 0,1,2, …, i are dn B4 0 1 2 3 4 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 B3 B0 29 35 23 14 20 21 9 17 1 5 8 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 11001 will have B4-- B3– B0. Cutler/Head

  9. A node of the binomial heap Cutler/Head

  10. A binomial heap 15 28 15 28 H 0 2 7 11 7 11 1 0 5 5 0 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) H2 H1 15 12 + 7 9 2 5 2 3 H1  15 12 7 5 9 2 2 3 Cutler/Head

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

  13. 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*ito 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) Cutler/Head

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

  15. 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 trees24 nodes Cutler/Head

  16. Doing a sequence of insertions Insert 10 10 H B0 Insert 20 H 20 20 B*0 H contains B0 so remove B0. Join into B1 and add to H 10 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 Cutler/Head

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

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

  20. Amortized analysis for insert new Assume n=2kinserts Inserts H Links per insert n/2 no B0 1 n/4 B0 no B1 2 n/8 B0, B1 no B2 3 ... n/2k = 1 B0 , B1,…Bk-2 no k Bk-1 • B0 , B1,…Bk-2 Bk-1 (k+1) no Bk Note correspondence: Flips for binary increment and links for binomial heap insert 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) 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 nothing2. If only one binomial heap includes a B0 put itinto the result.3. If both include B0 , link them into B1 and save for next stage. (like carry bit in binary addition) 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 Bido nothing2. If there is only one Biput it into the result.3. Otherwiselink two Bi‘s into Bi+1 and save for next stage.4. If there is still a Biput 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) 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 pointer2) 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) Cutler/Head

More Related