Binomial Heap
Binomial Heap
This chapter and Chapter 20 present data structures known as mergeable heaps,
which support the following five operations.
MAKE-HEAP) creates and returns a new heap containing no elements.
INSERT(H, x) inserts node x, whose key field has already been filled in., into
heap H
MINIMUM(H) returns a pointer to the node in heap H whose key is minimum.
EXTRACT-MIN(H) deletes the node from heap H whose key is minimum, return-
ing a pointer to the node.
UNION(H1, H2) creates and returns a new heap that contains all the nodes of heaps
Hi and H2. Heaps H1 and H2 are "destroyed" by this operation.
In addition, the data structures in these chapters also support the following two
operations.
DECREASE-KEY (H, x, k) assigns to node x within heap H the new key valuek,
which is assumed to be no greater than its current key value.
DELETE(H, x) deletes node x from heap H.
As the table in Figure 19.1 shows, if we don't need the UNION Operation, ordi-
nary binary heaps, as used in heapsort (Chapter 6), work well. Operations other
than UNION run in worst-case time O(lgn) (or better) on a binary heap. If the
UNION operation must be supported, however, binary heaps perfom poorly. By
concatenating the two arrays that hold the binary heaps to be merged and then run-
ning MIN-HEAPIFY (see Exercise 6.2-2), the UNION operation takes O(n) timein
the worst case.
'As mentioned in the introduction to Part V, our default mergeable heaps are mergeable min-
heaps, and so the operations MINIMUM, ExTRacT-MIN, and DECREASE-KEY apply. Alterna
tively, we could define a mergeable mar-heap with the operations MAXIMUM, EXTRACT-MAX,
and INCREASE-KEY.
19.1 Binomial trees and binomial
heaps 457
Chapter 19 Binomial Heaps
Binary heap Binomial heap Fibonacci heap 19.1 Binomial trees and binomial
heaps
Procedure (Worst-case)(worst-case) (amortized)_
(1) e(1) A binomial
MAKE-HEAP e(1) heap is a collection of binomial trees, so this section starts by defining
INSERT edgn) Olgn) e(1) binomial trees and proving some key
O(1) and show how
properties. We then define binomial heaps
MINIMUM O(1) Odgn) they can be
Olgn)
represented.
EXTRACT-MIN lgn) edgn)
UNION en) 0dgn) O1) 19.1.1 Binomial trees
DECREASE-KEY edgn) elgn) (1)
dgn) edgn) Odgn) The binomial tree B is an ordered tree (see Section
DELETE B.5.2) defined recursively.
As shown in Figure 19.2(a), the binomial tree Bo consists of a
single node. The
Running times for uperations three implementations of mergeable heaps. The binomial tree B; consists of two binomial trees B-1 that are linked
Figure 19.1 on
together: the
number of items in the heap(s) at the time of an operation is denoted by n.
root of one is the leftmost child of the root of the other.
Figure 19.2(b) shows the
binomial trees Bo through B4.
In this chapter, we examine "binomial heaps," whose worst-case time bounds are Some properties of binomial trees are given by the following lemma.
also shown in Figure 19.1. In particular, the UNION operation takes only O(lgn)
time to merge two binomial heaps with a total of n elements. Lemma 19.1 (Properties of binomial trees)
In Chapter 20, we shall explore Fibonacci heaps, which have even better time For the binomial tree B,
bounds for some operations. Note, however, that the running times for Fibonacci
1. there are 2 nodes,
heaps in Figure 19.1 are amortized time bounds, not worst-case per-operation time
bounds. 2. the height of the tree is k,
This chapter ignores issues of allocating nodes prior to insertion and freeing 3. there are exactly () nodes at depth i for i = 0, 1 , , k, and
nodes following deletion. We assume that the code that calls the heap procedures
deals with these details. 4. the root has degree k. which is greater than that of any other node; moreoverif
the children ofthe root are numbered from leftto right byk - 1,k - 2 . 0 ,
Binary heaps, binomial heaps, and Fibonacci heaps are all inefficient in their
support of the operation SEARCH; it can take a while to find a node with a given child i is the root of a subtree B;.
key. For this reason, operations such as DECREASE-KEY and DELETE that refer
to a given node require a pointer to that node as part of their input. As in our the basis is the binomial
discussion of priority queues in Section 6.5, when we use a mergeable heap in Proof The proof is by induction on k. For each property,
tree Bo. Verifying that each property holds for Bo is trivial.
an application, we often store a handle to the corresponding application object
For the inductive step, we assume that the lemma holds for B-1.
in each mergeable-heap element, as well as a handle to corresponding mergeable-
heap element in each applic tion object. The exact nature of these handles depends 1. Binomial tree B; consists oftwo copies of B-1, and so B;
has 2-l+2-l =
2
on the application and its implementation. nodes.
Section 19.1 defines binomial heaps after first defining their constituent binomial
of B-i are linked to form B, the
trees. It also introduces a particular representation of binomial heaps. Section 19.2 2. Because of the way in which the two copies
the maximum depth in B-1.
shows how we car. implenient operations on binomial heaps in the time bounds maximum depth of a node in B; is one greater than
maximum depth is (k 1) +l=k.
-
Corollary 19.2
The maximum degree of any node in an n-node binomial tree is lgn.
()
Bp-1 Proof Immediate from properties 1 and 4 of Lemma 19.1.
Bx-
Bk depth The term "binomial tree" comes from property 3 of Lemma 19.1, since the
0 terms )are the binomial coefficients. Exercise 19.1-3 gives furtherjustification
for the term.
2
(b)
3 19.1.2 Binomial heaps
Bo B: B2 B3 Ba
A binomial heap H is a set of binomial trees that satisies the following
heap properties.
of node
binomial-
1. Each binomial tree in H obeys the min-heap property.
cf We that
the key
each
a
such tree is
is
greater than or equal to the key its parent. say
min-heap-ordered.
(C) 2. For any nonnegative integer k, there is at most one binomial tree in H whose
A
Bo root has degree k.
tree contains the
The first property tells us that the root of a min-heap-ordered
Br-2 smallest key in the tree.
Bk-1 H consists of at most
The second property implies that an n-node binomial heap
B
Llgn]+1 binomial trees. To see why, observe that the binary representation of n
has lgn] +1 bits, say (bignj. blgn)-1. bo),
-..,
so that n =
b2. By
therefore, binomial tree B; appears in H if and only if
Figure 19.2 (a) The recursive definition of the binomial tree Bg. Triangles represent rooted sub- property 1l of Lemma 19.1,
binomial trees.
trees. (b) The binomial trees Bo through B4. Node depths in B4 are shown. (c) Another way of bit b; =1. Thus, binomial heap H contains at most Llg n] +1
looking at the binomial tree Bg. H with 13 nodes. The binary represen-
Figure 19.3(a) shows a binomial heap
binomial trees B, B2,
tation of 13 is (110I), and H consists of min-heap-ordered
the number of nodes at depth i - 1 in B-1. Thus,
and Bo, having8, 4,and1 nodes respectively,
for a total of 13nodes.
Dk, i) = Dk- 1,i) + Dk - 1,i - 1) (by the inductive hypothesis)
Representing binomial heaps
-(7) =) (by Exercise C.1-7)
As shown in Figure 19.3(b), each binomiaB tree
of
within a binomial heap is stored
Section 10.4. Each node has a key
in the left-child, right-sibling representation
-() field and any other satellite
information required by the application.
ure 19.2(c) shows, from left to right, the children of the root of number of children of x.
B-1 are roots field degree[x), which is the trees within a
binomial
of B-2, B1-3,.., Bo. When B-1 is linked to B-1, therefore, the children
of As Figure 19.3 also shows, the
roots of the binomial
The degrees
theresulting root are roots of B;-1, Bs-2,..., Bo. as the root list.
linked list, which we refer to
heap are organized in a
19.2 Operations on binomial heaps 461
Chapter 19 Binomial Heaps
460
(a) head[H]>(10- (1
12
J01) 101 110
OO1)O10) O110 001 1019 1100
O001) 0010 0100 1000
O000
Figure 19.4 The binomial tree B4 with nodes labeled in binary by a postorder walk.
19.1
Suppose we 'abel the nodes of binomial tree B, in binary by a postorder walk, as
in Figure 19.4. Consider a noder labeled I at depth i, and let j =k-i. Show
that x hasj l's in its binary representation. How many binary k-strings are there
Figure 19.3 A binomial heap H with n = 13 nodes. (a) The heap consists of binomial trees Bo, B2 that contain exactlyj l's? Show that the degree of x is equal to the number of l's
and B3, which have 1, 4, and 8 nodes respectively, totaling n = 13 nodes. Since each binomial tree to the right of the rightmost 0 in the binary representation
of l.
is min-heap-ordered, the key of any node is no less than the key of its parent. Also shown is the root
list, which is a linked list of roots in order of increasing degree. (b) A more detailed representation
of binomial heap H. Each binomial tree is stored in the left-child, right-sibling representation, and
each node stores its degree.
19.2 Operations on binomial heaps
of the roots strictly increase as we traverse the root list, By the second binomial- binomial heaps in the time
In this section, we show how to perform operations on
heap property, in an n-node binomial heap the degrees of the roots are a subset bounds; the lower
bounds shown in Figure 19.1. We shall only show the upper
of (0, 1,... lgnj}. The sibling field has a different meaning for roots than for bounds are lett as Exercise 19.2-10.
nonroots. Ifx is a root, then siblinglx] points to the next root in the root list. (As
usual, siblinglx] = NIL ifx is the last root in the root list.)
Creating a new binomial heap
A given binomial heap H is accessed by the field head[H), which is
simply a MAKE-BINOMIAL-HEAP procedure sim-
pointer to the first root in the root list of H. If binomial heap H has no elements, To make an empty binomial heap, the
where head[H] NIL. The running time
then head[H] ply allocates and returms an object H,
=
= NIL.
is (1).
19.2 Operatlons on binomial heaps 463
Binomial Heaps
Chapter 19
The following procedure unites binomial heaps H1 and H2, returning the
the minimum key sulting heap. It destroys the re-
Finding representations of Hi and H2 in the process. Be-
BINOMIAL-HEAP-MINIMUM
returns a pointer to the node with t sides BINOMIAL-LINK, the procedure uses an auxiliary procedure BINOMIAL-
The procedure assumes that HEAP-MERGE that merges the root lists of Hi and Hh into a
H. This implementation single linked list that
minimum key in an n-node binomial heap is sorted by degree into
with value oo. (See
Exercise 19.2-5.) monotonically increasing order. The BINOMIAL-HEAP
there are no keys MERGE procedure, whose
pseudocode we leave as Exercise 19.2-1, is similar to
BINOMIAL-HEAP-MINIMUM (H) the MERGE procedure in Section 2.3.1.
1 ply] Z
Figure 19.5 shows an example of
given in the pseudocode occur. has two phases. The first phase, per-
2 siblingly]child[z] The BINOMIAL-HEAP-UNION Procedure
3 childlz]y the root lists of binomial
formed by the call of BINOMIAL-HEAP-MERGE, merges into monotoni*
4 degreelz]degreelz] +1 heaps H and H2 into a single linked
list H that is sorted by degree
no more) of each
be as many as two roots (but
The BINOMIAL-LINK procedure makes node y the new head of the linked list cally increasing order. There might until at most one
links roots of equal degree
of node z's children in O(1) time. It works because the left-child, right-sibling degree, however, so the second phase linked list H is sorted by degree,
we can
in root remains of each degree. Because the
representation of each binomial tree matches the ordering propety of the tree:
a B tree, the leftmost child of the root is the root of a Br-I tree. perform all the link operations quickly.
19.2 Operations on binomial heaps
465
Chapter 19 Binomial Heaps
464
prev-x next-x
(d) headH 12
(a) headH, 12 0- 15 headH,18
2833 3 25
BINOMIAL-HEAP-MERGE
$0 Case 4
S5 55
prev-x next-x
next-x
(e) headH 12 15
b) head[H 1 2 1 8
8 18
18 25 18
vialbe
Case 2 Case 41 50
BINOMIAL-HEAP-UNION. (a) Binomial heaps H and H2. (b) Bi- The BINOMIAL-HEAP-UNION procedure next initializes some pointers into the
Figure 19.5 The execution of
x is the first root on
nomial heap H is the output of BiINOMIAL-HEAP-MERGE(H1, H2). Initially, root list of H. First, it simply returns in lines 4-5 if it happens to be uniting two
< keylnext-x], case 3 applies.
the root list of H. Because both x and next-x have degree 0 and key[x] o1 beini. empty binomial heaps. From line 6 on, therefore, we know that H has at least one
with the same degree, so case 2 applies. (d) After
(c) After the link occurs, is the first of three roots root. Throughout the procedure, we maintain three pointers into the root list:
x is the first of two
all the pointers move down one position in the root list, case 4 applies, since
roots of equal degree. (e) After the link occurs, case 3 applies. () After another link, case 1 applies,
because x has degree 3 and next-x has degree 4. Th iteration of the while loop is the last, because points tothe root currently being examined,
I7O111t
after the pointers move down one position in the root list, nexr-r = NIL. prev-x points to the root preceding r on the root list: siblinglprev-x] = x (since
initially x has no predecessor, we start with prev-x set to NIL), and
In detail, the procedure works as follows. Lines 1-3 start by merging the root next-x points to the root following r on the root list: siblinglx] = next-x.
lists of binomial heaps H, and Hz into a single root list H. The root lists of Hi
and Hy are sorted by strictly increasing degree, and BINOMIAL HEAP- MERGE re- Initially, there are at most two roots on the root list H of a given degree: because
tuns a root list H that is sorted by monotonically increasing degree. If the root lists Hi and H2 were binomial heaps, they each had at most one root of a given degree.
of H and Hz have m roots altogether, BINOMIAL-HEAP-MERGE runs in O(m) Moreover, BINOMIAL-HEAP-MERGE guarantees us that if two roots in H have
time by repeatedly xamining the roots at the heads of the two root lists and ap- the same degree, they are adjacent in the root list.
In fact, during the execution of BINOMIAL-HEAP-UNION, there may be three
pending the root with the lower degree to the output root list, removing it from its roots of a given degree appearing on the root list H at some time. We shall see
input root list in the precess.
19.2 Operations on binomial heaps 467
Chapter 19 Binomial Heaps
and the
on whether x is the first root on the list (line 17) or is not (line 18). Line 19 then degree[sibling[next-x]]. Again, the pointers move one position farther down the list,
next iteration executes either case 3 or case 4. (c) Case 3: degreelx] = degreelnext-x] #
makes x the leftmost child of next-x, and line 20 updatesx for the next iteration. and link it
degree[sibling[next-x]] and < keylnext-x]. We remove next-x from the root list
keylx]
Following either case 3 or case 4, the setup for the next iteration of the while degree[sibling[next-x]] and
to x, creating a Bx+1-tree. (d) Case 4: degreelx] degree|next-x] #
=
loop is the same. We have just linked two B-trees to form a B+1-tree, which x key\next-x] < key[x). We remove x from the root list and link it to next-X, again creating Bk+1-tree.
a
now points to. There were already zero, one, or two other B+1-trees on the root
list resulting from BINOMIAL-HEAP-MERGE, SO x is now the first of either one,
H2 contain n2 nodes, so that n =n1 +n2. Then Hj contains
at
two, or three Br+1-trees on the root list. If x is the only one, then we enter case 1 contain n nodes and
contains at
in the next iteration: degree[x] # degree[next-x]. Ifx is the first of two, then we most llg n1l+l H2 contains at most lg n2l+l roots, and so H
roots and
BINOMIAL-HEAP-MERGE is
that we enter case 2 in the next iteration. of BINOMIAL-HEAP-MERGE. The time to perform
The running time of BINOMIAL-HEAP-UNION is O(lgn), where n is the total thus O(lgn). Each iteration of the while loop takes O(1) time, and there are at
because each iteration either advances the
number of nodes in binomial heaps Hi and H2. We can see this as follows. Let most + llgn2] +2 iterations
g nil
H
19.2 Operations on binomial heaps 469
Binomial Heaps
Chapter 19
(a) head[H]
list ofH or removes a root from the root list.
pointers oneposition down the root
The procedure simply makes a one-node binomial heap H' in O(1) time and unites
it with the n-node binomial heap H in Odgn) time. The call to BINOMIAL-HEAP-
UNION takes care of freeing the temporary binomial heap H'. (A direct implemen-
tation that does not call BINOMIAL-HEAP-UNION is given as Exercise 19.2-8.)
(d) headH 2
Extracting the node with minimum key
The following procedure extracts the node with the minimum key from binomial
heap H and returns a pointer to the extracted node.
BINOMIAL-HEAP-EXTRACT-MIN(H)
I find the root x with the minimum key in the root list of H,
and remove x from the root list of H
Figure 19.7 The action of BINOMIAL-HEAP-EXTRACT-MIN. (a) A binomial heap H. (b) The
2 H' MAKE-BINOMIAL-HEAP() root x with minimum key is removed from the root list of H. (c) The linked list of x's children is
3 reverse the order of the linked list of x's children, reversed, giving another binomial heap H'. (d) The result of uniting H and H'.
and set head[H'] to point to the head of
the-resulting list
4
H-BINOMIAL-HEAP-UNION (H, H') in x's tree except for x itself. Because x's tree was removed from H in line 1, the
5 return x
binomial heap that results from uniting H and H' in line 4, shown in Figure 19.7(d),
This procedure works as shown in Figure 19.7. The contains all the nodes originally in H except for r. Finally, line 5 returns x.
input binomial heap H is Since each of lines 1-4 takes O (lg n) time if H has n nodes, BINOMIAL-HEAP
shown in Figure 19.7(a). Figure 19.7(b) shows the situation after
line 1: the root x EXTRACT-MIN runs in O(lgn) time.
with the minimum key has been removed from the root list
of H. If x is the root
of a B-tree, then by property 4 of Lemma 19.1, x's
children, from left to
are roots of
B-1", B:-2",. Bo-trees. Figure 19.7(c) shows that by reversingright,
,
the
list of x's children in line 3, we have a binomial
heap H' that contains every nod
19.2 Operations on hinomial heaps
Binomial Heaps 471
Chapter 19
(a) headH]-
Decreasing a key
heap H to a
node x in a binomial
The following procedure
decreases the key of a
than x's curent key.
error if k is greater
n e w valuek. t signals an
BINOMIAL-HEAP-DECREASE-KEY (H, x, k)
1 ifk>keylx] current key"
2 then error "new key is greater than
3 keylx]+k headH
4 y
5 z ply]
6 while z # NIL and key[y] < keylz]
do exchange keyly] +> key[z]
and z have satellite fields, exchange
them, too.
If y
9 y Z
10 zplyl
decreases a key in the same
manner (c)headi
As shown in Figure 19.8, this procedure After ensuring
the key in the heap.
as in a binary min-heap: by "bubbling up"
than the current key and then assigning the
that the new key is in fact no greater
node x.
new key to x, the procedure goes up
the tree, with y initially pointing to
is checked against the
In each iteration of the while loop of lines 6-10, key[y]
the binomial tree is now
key of y's parent z. Ify is the root key[y] 2 key[z],
or
and so its key is
min-heap-ordered. Otherwise, node y violates min-heap ordering,
other satellite information.
exchanged with the key of its parent z, along with any
and continues with
The procedure then sets y to z, going up one level in the tree, vitoemoodiow Figure 19.8iteration
Theactionofwhile
BINOMIAL-HEAP-DECREASE-KEY. (a) Thesituation just before line 6
the next iteration. ni viiogro) i1oot he hrst ofthe loop. Node y has had its key decreased to 7,
which is less than the
The BINOMIAL-HEAP-DECREASE-KEY procedure takes O(lgn) time. By key of y's parent z. (b) The keys of the two nodes are exchanged, and the situation just before line 6
of the second iteration is shown. Pointers y and z have moved up one level in the tree, but min-heap
so the while loop of
property 2 of Lemma 19.1, the maximum depth of x [lgn],
is
order is still violated. (c) After another exchange and moving pointers y andz up one more level, we
lines 6-10 iterates at most [lgnj times. find that min-heap order is satisfied, so the while loop terminates.
Deleting a key deals with the situation in which -oo cannot appear as a key, even temporarily.) It
and satellite information from binomial heap H then bubbles this key and the associated satellite information up to a root by calling
It is easy to delete a node x's key
in O(lgn) time. The following implementation assumes that no node currently in BINOMIAL-HEAP-DECREASE-KEY. This root is then removed from H by a call
the binomial heap has a key of-0o. 2ihs o f BINOMIAL-HEAP-EXTRACT-MIN.
The BINOMIAL-HEAP-DELETE procedure takes O(lgn) time.
BINOMIAL-HEAP-DELETE (H, x)
1 BINOMIAL-HEAP-DECREASE-KEY (H, x, -0o) Exercises
2 BINOMIAL-HEAP-EXTRACT-MIN(H)
19.2-1
The BINOMIAL-HEAP-DELETE procedure makes node x have the unique mini- Write pseudocode for BINOMIAL-HEAP-MERGE.
mum key in the entire binomial heap by giving it a key of -oo. (Exercise 19.2-6