Download presentation
Presentation is loading. Please wait.
1
Binomial Heaps
2
Min Binomial Heap Collection of min trees. 6 4 9 5 8 7 3 1 9 5 6 5 9 2 8 6 7 4
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
4
Binomial Heap Representation Circular linked list of min trees. 6 4 9 5 8 7 3 1 9 5 6 5 9 2 8 6 7 4 A Degree fields not shown.
5
Insert 10 Add a new single-node min tree to the collection. 10 Update min-element pointer if necessary. 6 4 9 5 8 7 3 1 9 5 6 5 9 2 8 6 7 4 A
6
Meld Combine the 2 top-level circular lists. 9 5 7 A 4 8 7 3 1 B Set min-element pointer.
7
Meld 9 5 7 4 8 7 3 1 C
8
Delete 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 9 5 6 5 9 2 8 6 7 4 A
10
Remove Min Tree Same as remove a node from a circular list. abcde A No next node => empty after remove. Otherwise, copy next-node data and remove next node. d
11
Reinsert Subtrees Combine the 2 top-level circular lists. Same as in meld operation. 9 5 7 A 4 8 7 3 1 B subtrees
12
Update Binomial Heap Pointer Must examine roots of all min trees to determine the min value.
13
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).
14
Enhanced Delete Min During reinsert of subtrees, pairwise combine min trees whose roots have equal degree.
15
Pairwise Combine 10 6 4 9 5 8 7 3 1 9 5 6 5 9 2 8 6 7 4 Examine the s = 7 trees in some order. Determined by the 2 top-level circular lists.
16
Pairwise Combine 10 6 4 9 5 8 7 3 1 9 5 6 5 9 2 8 6 7 4 Use a table to keep track of trees by degree. 1 2 3 4 tree table 0
17
Pairwise Combine 10 6 4 9 5 8 7 3 1 9 5 6 5 9 2 8 6 7 4 1 2 3 4 tree table 0
18
Pairwise Combine 10 6 4 9 5 8 7 3 1 9 5 6 5 9 2 8 6 7 4 1 2 3 4 tree table 0 Combine 2 min trees of degree 0. Make the one with larger root a subtree of other.
19
Pairwise Combine 1 2 3 4 tree table 0 10 6 4 9 5 8 7 3 1 9 5 6 5 9 2 8 6 7 4 Update tree table.
20
Pairwise Combine 1 2 3 4 tree table 0 10 6 4 9 5 8 7 3 1 9 5 6 5 9 2 8 6 7 4 Combine 2 min trees of degree 1. Make the one with larger root a subtree of other.
21
Pairwise Combine 1 2 3 4 tree table 0 10 6 4 9 5 8 7 3 1 9 5 6 5 9 2 8 6 7 4 Update tree table.
22
Pairwise Combine 1 2 3 4 tree table 0 10 6 4 9 5 8 7 3 1 9 5 6 5 9 2 8 6 7 4
23
Pairwise Combine 1 2 3 4 tree table 0 10 6 4 9 5 8 7 3 1 9 5 6 5 9 2 8 6 7 4 Combine 2 min trees of degree 2. Make the one with larger root a subtree of other.
24
Pairwise Combine 1 2 3 4 tree table 0 Combine 2 min trees of degree 3. Make the one with larger root a subtree of other. 6 9 5 8 7 3 1 6 5 9 2 8 6 7 4 10 4 9 5
25
Pairwise Combine 1 2 3 4 tree table 0 6 9 5 8 7 3 1 6 5 9 2 8 6 7 4 10 4 9 5 Update tree table.
26
Pairwise Combine 1 2 3 4 tree table 0 6 9 5 8 7 3 1 6 5 9 2 8 6 7 4 10 4 9 5
27
Pairwise Combine 1 2 3 4 tree table 0 6 9 5 8 7 3 1 6 5 9 2 8 6 7 4 10 4 9 5 Create circular list of remaining trees.
28
Complexity Of Delete 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. O(MaxDegree). Overall complexity of remove min. O(MaxDegree + s).
29
Binomial Trees B k is degree k binomial tree. B0B0 B k, k > 0, is: … B0B0 B1B1 B2B2 B k-1
30
Examples B0B0 B1B1 B2B2 B3B3
31
Number Of Nodes In B k N k = number of nodes in B k. B0B0 B k, k > 0, is: … B0B0 B1B1 B2B2 B k-1 N 0 = 1 N k = N 0 + N 1 + N 2 + …+ N k-1 + 1 = 2 k.
32
Equivalent Definition B k, k > 0, is two B k-1 s. One of these is a subtree of the other. B1B1 B2B2 B3B3
33
N k And MaxDegree N 0 = 1 N k = 2N k-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, MaxDegree = O(log n).
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.