Title: Chapter 20 Binomial Heaps
1Chapter 20 Binomial Heaps
- ??? ????
- ??????? ???
- 2000. 12. 1
2Preface
- Binomial Heap
- introduced in 1978 by Jean Vuillemin
- A data structure for manipulating priority
queues - communications of the ACM, 21(4)309-315
-
- studied their properties in detail by Mark R.
Brown - The Analysis of a Practical and Nearly Optimal
Priority Queue - Implementation and analysis of binomial queue
algorithms
3Preface
- Mergeable Heaps
- ?
- Binomial coefficients
- binomial expansion
- xy1
- ex) n4 ? 24 ( 1 4 6 4 1 )
Bk-1
Bk-1
Bk-1
Bk-1
Bk
4Mergeable Heaps
- Five operations
- Make-Heap()
- ??? Heap ??
- Insert(H,x)
- Heap H? Node x ??
- Minimum(H)
- Heap H? ?? ?? Key? Pointer ??
- Extract-Min(H)
- ?? ?? Key? Node? ? ?
- Union(H1, H2)
- Heap H1, H2? ??, ??? Heap ??
5Mergeable Heaps
- Additional two operation in Binomial heaps
- Decrease-Key(H, x, k)
- Heap H? Node x? ??? ? ??? ? k? ??
- Delete(H,x)
- Heap H??? Node x? ??
6Running times for operations
- Binary Binomial Fibonacci
- Procedure heap heap heap
- Make-Heap
- Insert
- Minimum
- Extract-Min
- Union
- Decrease-Key
- Delete
7Figure 20.2 Binomial trees
(a)
Depth 0 1 2 3 4
B0
Bk-1
Bk
Bk-1
(b)
B0
B1
B2
B3
B4
(c)
B0
B1
B2
Bk
Bk-2
Bk-1
8Binomial trees
- Lemma 20.1(Properties of binomial trees)
- For the binomial tree Bk,
- 1.There are 2k nodes
- Binomial tree Bk consists of two copies of Bk-1,
so Bk has 2k-1 2k-1 2k noes - 2.The height of the tree is k
- Because the maximum depth of a node in Bk is one
greater than the maximum depth in Bk-1, this
maximum depth is (k-1)1k
9Binomial trees
- 3.There are exactly nodes at depth i for
i0,1,,k - Let D(k,i) be the number of nodes at depth i of
binomial tree Bk - D(k,i) D(k-1,i) D(k-1,i-1)
-
-
-
- ? k-combinations of an n-set
10Binomial trees
- 4.The root has degree k, which is greater than
that of any other node moreover if the
children of the root are numbered from left to
right by k-1,k-2,,0, child i is the root of a
subtree Bi - The only node with greater degree in Bk than in
Bk-1 is the root, which has one more child than
in Bk-1 - Bk Bk-1 Bk-1
- Bk Bk-1 (Bk-2 Bk-3 B0 )
- Therefore, the root of Bk has Bk-1, Bk-2, ,
B1, B0.
11Figure 20.3 Binomial heap
(a)
headH
12Figure 20.3 Binomial heap
(b)
13Binomial heaps
- Binomial heap H is a set of binomial trees that
satisfies the following properties. - Binomial-heap properties
- 1. Each binomial tree in H is heap-ordered the
key of a node is greater than or equal to the key
of its parent. - The root of a heap-ordered tree contains the
smallest key in the tree
14Binomial Heaps
- 2. There is at most one binomial tree in H whose
root has a given degree. - An n-node binomial heap H consists of at most
- binomial trees.
- The binary representation of n has
bits, - say , so that
- ex) the binary representation of 13 is lt1101gt
- ? H consists of heap-ordered binomial trees B3,
B2, and B0, having 8, 4, and 1 nodes
respectively, for a total of 13 nodes
15Operations on binomial heaps
- Creating a new binomial heap
- Finding the minimum key
- Uniting two binomial heaps
- Inserting a node
- Extracting the node with minimum key
- Decreasing a key
- Deleting a key
16Creating a new binomial heap
- Allocates and returns an object H
- headH Nil
- running time is
17Finding the minimum key
- Binomial-Heap-Minimum(H)
- y ? Nil
- x ? headH
- min ? 8
- while x ? Nil
- do if keyx lt min
- then min ? keyx
- y ? x
- x ? siblingx
- return y
- running time is
- Since a binomial heap is heap-ordered, the
minimum key must reside in a root node at most
18Uniting two binomial heaps
- Binomial-Link(y,z) running time
is O(1) - py ? z
- siblingy ? childz
- childz ? y
- degreez ? degreez 1
- Binomial-Heap-Merge(H1,H2)
- merges the root lists of H1 and H2 into a single
linked list that is sorted by degree into
monotonically increasing order. - If the root lists of H1 and H2 have m roots
altogether, running time is O(m) - The running time of Binomial-Heap-Union is
19Figure 20.6 Four cases
- In each case, x is the root of a Bk-tree and l gt
k - (a) Case 1 degreex?degreenext-x
- pointers move one position further down the root
list - (b) Case 2 degreexdegreenext-xdegreesiblin
gnext-x - pointers move one position further down the root
list - (c) Case 3 degreexdegreenext-x?
degreesiblingnext-x - and keyxkeynext-x
- remove next-x from the root list and link it to
x, - creating a Bk1-tree
- (d) Case 4 degreexdegreenext-x?
degreesiblingnext-x - and keynext-xkeyx
- remove x from the root list and link it to
next-x, - creating a Bk1-tree
20Figure 20.5 Binomial-Heap-Union
- (a) Binomial heaps H1 and H2
- (b) case 3 both x and next-x have degree 0
- and keyx lt keynext-x
- (c) case 2 x is the first of three roots
- with the same degree
- (d) case 4 x is the first of two roots of equal
degree - (e) case 3 both x and next-x have degree 2
- and keyx lt keynext-x
- (f) case 1 x has degree 3 and next-x has degree
4
21Binomial-Heap-Union (1/2)
- Binomial-Heap-Union(H1,H2)
- H ? Make-Binomial-Heap()
- headH ? Binomial-Heap-Merge(H1,H2)
- free the objects H1 and H2 but not the lists they
point to - if headH Nil
- then return H
- prev-x ? Nil
- x ? headH
- next-x ? siblingx
- while next-x ? Nil
22Binomial-Heap-Union (2/2)
- do if (degreex ? degreenext-x) or
(siblingnext-x ? Nil - and degreesiblingnext-x degreex)
- then prev-x ? x
? Case 1 and 2 - x ? next-x
? Case 1 and 2 - else if keyx keynext-x
- then siblingx ? siblingnext-x ? Case
3 - Binomial-Link(next-x,x) ?
Case 3 - else if prev-x Nil
? Case 4 - then headH ? next-x ? Case 4
- else siblingprev-x ? next-x ? Case 4
- Binomial-Link(x,next-x) ? Case 4
- x ? next-x ?
Case 4 - next-x ? siblingx
- return H
23Inserting a node
- Binomial-Heap-Insert(H,x)
- H ? Make-Binomial-Heap()
- px ? Nil
- childx ? Nill
- siblingx ? Nil
- degreex ? 0
- headH ? x
- H ? Binomial-Heap-Union(H,H)
- Running time is
- unites the new heap H with the n-node binomial
heap H in
24Extracting the node with minimum key
- Binomial-Heap-Extract-Min(H)
- find the root x with the minimum key in the root
list of H, and remove x from the root list of H - H ? Make-Binomial-Heap()
- reverse the order of the linked list of xs
children, and set headH to point to the head
of the resulting list - H ? Binomial-Heap-Union(H,H)
- return x
- Running Time is
25Figure 20.7 Binomial-Heap-Extract-Min
- A binomial heap H
- The root x with minimum key is removed from the
root list of H - The linked list of xs children is reversed,
giving another binomial heap H - The result of uniting H and H
26Decreasing a key
- Binomial-Heap-Decrease-Key(H,x,k)
- if k gt keyx
- then error new key is greater than current key
- keyx ? k
- y ? x
- z ? py
- while z ? Nil and keyy lt keyz
- do exchange keyy ? keyz
- ?If y and z have satellite fields, exchange
them, too. - y ? z
- z ? py
- Running Time is
- becase following the root path, the while loop
iterates at most times
27Figure 20.8 Binomial-Heap-Decrease-Key
- The first iteration of the while loop. Node y has
had its key decreased to 7, which is less than
the key of ys parent z - The keys of the two nodes are exchanged, and
pointers y and z have moved up one level in the
tree, but heap order is still violated - After another exchange and moving pointers y and
z up one more level, the while loop terminates.
28Deleting a key
- Binomial-Heap-Delete(H,x)
- Binomial-Heap-Decrease-Key(H,x,-8)
- Binomial-Heap-Extract-Min(H)
- Running Time is
- Binomial-Heap-Decrease-Key
- Binomial-Heap-Extract-Min