Download presentation
Presentation is loading. Please wait.
Published bySteven Lamb Modified over 6 years ago
1
Binomial Heaps On the surface it looks like Binomial Heaps are great if you have no remove mins. But, in this case you need only keep track of the current min item using O(1) space per PQ. Insert, meld, and find min each take O(1) actual time. Binomial heap is a step to Fibonacci heaps. Could be useful if you have infrequent remove mins and frequent insert/meld.
2
Min Binomial Heap Collection of min trees. 6 5 9 2 8 7 4 8 7 3 1 4 9 5
Way in which operations are performed constrains the tree structure. 9 5 9 5 6
3
Node Structure Degree Child Sibling Data Number of children.
Pointer to one of the node’s children. Null iff node has no child. Sibling Used for circular linked list of siblings. Data Space requirements are similar to those of a leftist tree. Now we have a degree field instead of an s-value field. As we shall see, degree (like s-values) is O(log n).
4
Binomial Heap Representation
Circular linked list of min trees. 6 4 9 5 8 7 3 1 2 Degree fields not shown.
5
Insert 10 Add a new single-node min tree to the collection.
Update min-element pointer if necessary. 6 4 9 5 8 7 3 1 2 A 10 New tree is added next to tree with min element. This takes O(1) time.
6
Meld Combine the 2 top-level circular lists. Set min-element pointer.
4 8 7 3 1 B 9 5 7 A O(1) time. Combine the 2 top-level circular lists. Set min-element pointer.
7
Meld 4 1 5 7 C 3 7 9 8
8
Remove Min Empty binomial heap => fail.
9
Nonempty Binomial Heap
Remove a min tree. Reinsert subtrees of removed min tree. Update binomial heap pointer. 10 6 4 9 5 8 7 3 1 2 A
10
Remove Min Tree Same as remove a node from a circular list.
b c d e A d No next node => empty after remove. Otherwise, copy next-node data and remove next node. Don’t really need this trick either as later pairwise combine will need to go through all top-level trees in any case. So, can get same asymptotic complexity by doing the remove in conjunction with the pairwise combine.
11
Reinsert Subtrees Combine the 2 top-level circular lists. B A subtrees
9 5 7 A 4 8 3 1 B subtrees Combine the 2 top-level circular lists. Same as in meld operation.
12
Update Binomial Heap Pointer
Must examine roots of all min trees to determine the min value.
13
Complexity Of Remove Min
Remove a min tree. O(1). Reinsert subtrees. Update binomial heap pointer. O(s), where s is the number of min trees in final top-level circular list. s = O(n). Overall complexity of remove min is O(n). Since our remove min operation has to walk through original top-level circular list, we may as well do the removal of the min tree during this walk (instead of using the copy from forward node trick). If we use algorithms as described, no tree has more than 1 node and the amortized cost of a remove min is O(n)!
14
Enhanced Remove Min During reinsert of subtrees, pairwise combine min trees whose roots have equal degree. This is essential to get stated amortized bounds and so is the correct way to remove from a Binomial heap. The simple remove min described earlier does not result in the desired amortized complexity and so is incorrect for Binomial heaps.
15
Pairwise Combine Examine the s = 7 trees in some order.
10 6 4 9 5 8 7 3 1 2 One of the top-level circular lists is the original top-level list (minus the removed min tree); the other is the list of the removed min tree’s subtrees. Examine the s = 7 trees in some order. Determined by the 2 top-level circular lists.
16
Pairwise Combine Use a table to keep track of trees by degree. 1 2 3 4
10 6 4 9 5 8 7 3 1 2 This example does not come from previous slide by doing a remove min. Treat this as an independent example. 1 2 3 4 tree table Use a table to keep track of trees by degree.
17
Pairwise Combine 10 6 4 9 5 8 7 3 1 2 1 2 3 4 tree table
18
Pairwise Combine Combine 2 min trees of degree 0.
10 6 4 9 5 8 7 3 1 2 1 2 3 4 tree table Combine 2 min trees of degree 0. Make the one with larger root a subtree of other.
19
Pairwise Combine Update tree table. 1 2 3 4 tree table 10 6 4 9 5 8 7
Update tree table.
20
Pairwise Combine Combine 2 min trees of degree 1.
10 6 4 9 5 8 7 3 1 2 1 2 3 4 tree table Combine 2 min trees of degree 1. Make the one with larger root a subtree of other.
21
Pairwise Combine Update tree table. 1 2 3 4 tree table 10 6 4 9 5 8 7
Update tree table.
22
Pairwise Combine 10 6 4 9 5 8 7 3 1 2 1 2 3 4 tree table
23
Pairwise Combine Combine 2 min trees of degree 2.
10 6 4 9 5 8 7 3 1 2 1 2 3 4 tree table Combine 2 min trees of degree 2. Make the one with larger root a subtree of other.
24
Pairwise Combine Combine 2 min trees of degree 3.
6 9 5 8 7 3 1 2 4 10 1 2 3 4 tree table Combine 2 min trees of degree 3. Make the one with larger root a subtree of other.
25
Pairwise Combine 1 2 3 4 tree table 6 9 5 8 7 10 Update tree table.
26
Pairwise Combine 1 2 3 4 tree table 6 9 5 8 7 10
27
Pairwise Combine Create circular list of remaining trees. 1 2 3 4
tree table 6 9 5 8 7 10 Create circular list of remaining trees.
28
Complexity Of Remove Min
Create and initialize tree table. O(MaxDegree). Done once only. Examine s min trees and pairwise combine. O(s). Collect remaining trees from tree table, reset table entries to null, and set binomial heap pointer. Overall complexity of remove min. O(MaxDegree + s). Total #combines is at most s-1 as each combine reduces #trees by 1.
29
Binomial Trees … Bk is degree k binomial tree. B0 Bk , k > 0, is:
Order of subtrees is not important.
30
Examples B3 B2 B0 B1
31
Number Of Nodes In Bk … Nk = number of nodes in Bk. B0 N0 = 1
Bk , k > 0, is: … B0 B1 B2 Bk-1 Nk = N0 + N1 + N2 + …+ Nk-1 + 1 = 2k.
32
Equivalent Definition
Bk , k > 0, is two Bk-1s. One of these is a subtree of the other. B2 B1 B3
33
Nk And MaxDegree N0 = 1 Nk = 2Nk-1 = 2k.
If we start with zero elements and perform operations as described, then all trees in all binomial heaps are binomial trees. So, MaxDegree = O(log n).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.