Binomial Heaps Min Binomial Heap Collection of min
Binomial Heaps
Min Binomial Heap • Collection of min trees. 2 4 6 5 7 1 4 9 7 6 5 5 8 6 9 9 8 3
Node Structure • Degree § Number of children. • Child § Pointer to one of the node’s children. § Null iff node has no child. • Sibling § Used for circular linked list of siblings. • Data
Binomial Heap Representation • Circular linked list of min trees. 2 4 6 5 7 A 1 4 9 7 6 5 3 8 5 8 6 9 9 Degree fields not shown.
Insert 10 • Add a new single-node min tree to the collection. • Update min-element pointer if necessary. 2 4 5 1 4 9 7 10 6 7 6 5 5 8 6 9 9 8 3 A
Meld 5 A 9 4 1 7 B 7 3 8 • Combine the 2 top-level circular lists. • Set min-element pointer.
Meld 5 9 4 1 7 C 7 8 3
Delete Min • Empty binomial heap => fail.
Nonempty Binomial Heap • Remove a min tree. • Reinsert subtrees of removed min tree. • Update binomial heap pointer. 2 4 5 A 1 4 9 7 10 6 7 6 5 5 8 6 9 9 8 3
Remove Min Tree • Same as remove a node from a circular list. a b dc d e A • No next node => empty after remove. • Otherwise, copy next-node data and remove next node.
Reinsert Subtrees 5 A 7 9 4 1 7 8 3 B subtrees • Combine the 2 top-level circular lists. § Same as in meld operation.
Update Binomial Heap Pointer • Must examine roots of all min trees to determine the min value.
Complexity Of Delete Min • Remove a min tree. § O(1). • Reinsert subtrees. § O(1). • 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).
Enhanced Delete Min • During reinsert of subtrees, pairwise combine min trees whose roots have equal degree.
Pairwise Combine 2 4 5 1 4 9 7 10 6 7 6 5 5 8 6 9 9 Examine the s = 7 trees in some order. Determined by the 2 top-level circular lists. 8 3
Pairwise Combine 4 2 4 6 5 7 9 10 5 6 5 1 9 7 6 3 8 8 tree table Use a table to keep track of trees by degree. 4 3 2 1 0 9
Pairwise Combine 4 2 4 6 5 7 6 9 10 5 9 6 5 1 7 3 8 8 tree table 4 3 2 1 0 9
Pairwise Combine 4 2 4 6 5 7 6 9 10 5 9 6 5 1 7 3 8 8 tree table Combine 2 min trees of degree 0. Make the one with larger root a subtree of other. 4 3 2 1 0 9
Pairwise Combine 2 4 6 5 7 6 9 4 5 10 9 6 7 3 8 8 tree table Update tree table. 5 1 4 3 2 1 0 9
Pairwise Combine 2 4 6 5 7 6 9 4 5 10 9 6 5 1 7 3 8 8 tree table Combine 2 min trees of degree 1. Make the one with larger root a subtree of other. 4 3 2 1 0 9
Pairwise Combine 4 2 4 6 5 7 6 9 10 6 5 7 9 8 8 tree table Update tree table. 5 1 3 4 3 2 1 0 9
Pairwise Combine 4 2 4 6 5 7 6 9 10 6 5 1 5 7 9 8 8 tree table 3 4 3 2 1 0 9
Pairwise Combine 4 2 4 6 5 7 6 9 10 6 5 1 5 7 9 8 8 tree table Combine 2 min trees of degree 2. Make the one with larger root a subtree of other. 3 4 3 2 1 0 9
Pairwise Combine 6 2 4 6 8 5 7 6 9 5 1 7 4 10 5 3 8 9 tree table Combine 2 min trees of degree 3. Make the one with larger root a subtree of other. 4 3 2 1 0 9
Pairwise Combine 1 2 4 6 5 7 7 4 9 6 10 5 6 3 Update tree table. 9 8 9 tree table 8 5 4 3 2 1 0
Pairwise Combine 1 2 4 6 8 5 7 6 7 4 9 10 5 6 5 3 9 8 9 tree table 4 3 2 1 0
Pairwise Combine 1 2 4 6 5 7 6 7 4 9 10 5 6 5 3 9 8 9 tree table 8 Create circular list of remaining trees. 4 3 2 1 0
Complexity Of Delete Min • Create and initialize tree table. § O(Max. Degree). § 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. § O(Max. Degree). • Overall complexity of remove min. § O(Max. Degree + s).
Binomial Trees • Bk is degree k binomial tree. B 0 • Bk , k > 0, is: … B 0 B 1 B 2 Bk-1
Examples B 0 B 1 B 2 B 3
Number Of Nodes In Bk • Nk = number of nodes in Bk. N 0 = 1 B 0 • Bk , k > 0, is: … B 0 B 1 B 2 • Nk = N 0 + N 1 + N 2 + …+ Nk-1 + 1 = 2 k. Bk-1
Equivalent Definition • Bk , k > 0, is two Bk-1 s. • One of these is a subtree of the other. B 1 B 2 B 3
Nk And Max. Degree • N 0 = 1 • Nk = 2 Nk-1 = 2 k. • If we start with zero elements and perform operations as described, then all trees in all binomial heaps are binomial trees. • So, Max. Degree = O(log n).
- Slides: 33