Docsity

Fibonacci heap, Lecture notes of Computer Applications

Toggle sidebar
bg1
Meeting 11 October 4, 2005
Fibonacci Heaps
The Fibonacci heap is a data structure implementing the
priority queue abstract data type, just like the ordinary
heap but more complicated and asymptotically faster for
some operations. We first introduce binomial trees, which
are special heap-ordered trees, and then explain Fibonacci
heaps as collections of heap-ordered trees.
Binomial trees. The binomial tree of height his a tree
obtained from two binomial trees of height h−1, by link-
ing the root of one to the other. The binomial tree of height
0 consists of a single node. Binomial trees of heights up
to 4 are shown in Figure 43. Each step in the construc-
Figure 43: Binomial trees of heights 0, 1, 2, 3, 4. Each tree is
obtained by linking two copies of the previous tree.
tion increases the height by one, increases the degree (the
number of children) of the root by one, and doubles the
size of the tree. It follows that a binomial tree of height h
has root degree hand size 2h. The root has the largest de-
gree of any node in the binomial tree, which implies that
every node in a binomial tree with nnodes has degree at
most log2n.
To store any set of items with priorities, we use a small
collection of binomial trees. For an integer n, let nibe
the i-th bit in the binary notation, so we can write n=
Pi≥0ni2i. To store nitems, we use a binomial tree of
size 2ifor each ni= 1. The total number of binomial trees
is thus the number of 1’s in the binary notation of n, which
is at most log2(n+ 1). The collection is referred to as a
binomial heap. The items in each binomial tree are stored
in heap-order. There is no specific relationship between
the items stored in different binomial trees. The item with
minimum key is thus stored in one of the logarithmically
many roots, but it is not clear in which one. An example is
=+ 10
4
11
1312
15 7
15
98
9
15
10
11
13
15
12
4 7
9
5
8
5
9
Figure 44: Adding the shaded node to a binomial heap consisting
of three binomial trees.
shown in Figure 44 where 1110 = 10112items are stored
in three binomial trees with sizes 8, 2, and 1. In order
to add a new item to the set, we create a new binomial
tree of size 1 and we successively link binomial trees as
dictated by the rules of adding 1 to the binary notation of
n. In the example, we get 10112+ 12= 11002. The new
collection thus consists of two binomial trees with sizes 8
and 4. The size 8 tree is the old one, and the size 4 tree
is obtained by first linking the two size 1 trees and then
linking the resulting size 2 tree to the old size 2 tree. All
this is illustrated in Figure 44.
Fibonacci heaps. AFibonacci heap is a collection of
heap-ordered trees. Ideally, we would like it to be a col-
lection of binomial trees, but we need more flexibility. It
will be important to understand how exactly the nodes of a
Fibonacci heap are connected by pointers. Siblings are or-
ganized in doubly-linked cyclic lists, and each node has a
pointer to its parent and a pointer to one of its children, as
shown in Figure 45. Besides the pointers, each node stores
a key, its degree, and a bit that can be used to mark or un-
34
pf3
pf4

Partial preview of the text

Download Fibonacci heap and more Lecture notes Computer Applications in PDF only on Docsity!

Meeting 11 October 4, 2005

Fibonacci Heaps

The Fibonacci heap is a data structure implementing the priority queue abstract data type, just like the ordinary heap but more complicated and asymptotically faster for some operations. We first introduce binomial trees, which are special heap-ordered trees, and then explain Fibonacci heaps as collections of heap-ordered trees.

Binomial trees. The binomial tree of height h is a tree obtained from two binomial trees of height h − 1 , by link- ing the root of one to the other. The binomial tree of height 0 consists of a single node. Binomial trees of heights up to 4 are shown in Figure 43. Each step in the construc-

Figure 43: Binomial trees of heights 0, 1, 2, 3, 4. Each tree is obtained by linking two copies of the previous tree.

tion increases the height by one, increases the degree (the number of children) of the root by one, and doubles the size of the tree. It follows that a binomial tree of height h has root degree h and size 2 h. The root has the largest de- gree of any node in the binomial tree, which implies that every node in a binomial tree with n nodes has degree at most log 2 n.

To store any set of items with priorities, we use a small collection of binomial trees. For an integer n, let ni be the∑ i-th bit in the binary notation, so we can write n =

i≥ 0 ni^2

i. To store n items, we use a binomial tree of

size 2 i^ for each ni = 1. The total number of binomial trees is thus the number of 1’s in the binary notation of n, which

is at most log 2 (n + 1). The collection is referred to as a binomial heap. The items in each binomial tree are stored in heap-order. There is no specific relationship between the items stored in different binomial trees. The item with minimum key is thus stored in one of the logarithmically many roots, but it is not clear in which one. An example is

  • =

10

4

(^121311)

15 7 15

9 9 8

15

10 (^1311) 15

12

4 7

9

5 8

5 9

Figure 44: Adding the shaded node to a binomial heap consisting of three binomial trees.

shown in Figure 44 where 1110 = 1011 2 items are stored in three binomial trees with sizes 8, 2, and 1. In order to add a new item to the set, we create a new binomial tree of size 1 and we successively link binomial trees as dictated by the rules of adding 1 to the binary notation of n. In the example, we get 10112 + 1 2 = 1100 2. The new collection thus consists of two binomial trees with sizes 8 and 4. The size 8 tree is the old one, and the size 4 tree is obtained by first linking the two size 1 trees and then linking the resulting size 2 tree to the old size 2 tree. All this is illustrated in Figure 44.

Fibonacci heaps. A Fibonacci heap is a collection of heap-ordered trees. Ideally, we would like it to be a col- lection of binomial trees, but we need more flexibility. It will be important to understand how exactly the nodes of a Fibonacci heap are connected by pointers. Siblings are or- ganized in doubly-linked cyclic lists, and each node has a pointer to its parent and a pointer to one of its children, as shown in Figure 45. Besides the pointers, each node stores a key, its degree, and a bit that can be used to mark or un-

min

12 13

5

15

7

9 10

4

8

11

9

Figure 45: The Fibonacci heap representation of the first collec- tion of heap-ordered trees in Figure 44.

mark the node. The roots of the heap-ordered trees are doubly-linked in a cycle, and there is an explicit pointer to the root that stores the item with the minimum key. Figure 46 illustrates a few basic operations that we can perform on a Fibonacci heap. Given two heap-ordered trees, we

merging

linking

unlinking

Figure 46: Cartoons for linking two trees, unlinking a tree, and merging two cycles.

link them by making the root with the bigger key the child of the other root. To unlink a heap-ordered tree or subtree, we remove its root from a doubly-linked cycle. Finally, to merge two cycles, we cut both open and connect them at their ends. Any one of these three operations takes only constant time.

Potential function. A Fibonacci heap supports a vari- ety of operations, including the standard ones for priority queues. We use a potential function to analyze their amor- tized cost applied to an initially empty Fibonacci heap. Letting ri be the number of roots in the root cycle and mi the number of marked nodes, the potential after the i-th operation is Φi = ri + 2mi. When we deal with a collection of Fibonacci heaps we define its potential as the sum of individual potentials. The initial Fibonacci heap is empty, so Φ 0 = 0. As usual we let ci be the actual cost and ai = ci + Φi − Φi− 1 the amortized cost of the i-th

operation. Since Φ 0 = 0 and Φi ≥ 0 for all i, the actual cost is less than the amortized cost: ∑^ n

i=

ci ≤

∑^ n

i=

ai = rn + 2mn +

∑^ n

i=

ci.

For some of the operations, it is fairly easy to compute the amortized cost. We get the minimum by returning the key in the marked root. This operation does not change the potential and its amortized and actual cost is ai = ci = 1. We meld two Fibonacci heaps H 1 and H 2 by first merging the two root circles and second adjusting the pointer to the minimum key. We have

ri(H 1 ) = ri− 1 (H 1 ) + ri− 1 (H 2 ), mi(H 1 ) = mi− 1 (H 1 ) + mi− 1 (H 2 ),

which implies that there is no change in potential. The amortized and actual cost is therefore ai = ci = 1. We insert a key into a Fibonacci heap by first creating a new Fibonacci heap that stores only the new key and second melding the two heaps. We have one more node in the root cycle so the change in potential is Φi − Φi− 1 = 1. The amortized cost is therefore ai = ci + 1 = 2.

Deletemin. Next we consider the somewhat more in- volved operation of deleting the minimum key, which is done in four steps:

Step 1. Remove the node with minimum key from the root cycle. Step 2. Merge the root cycle with the cycle of children of the removed node. Step 3. As long as there are two roots with the same degree link them. Step 4. Recompute the pointer to the minimum key.

For Step 3, we use a pointer array R. Initially, R[i] = NULL for each i. For each root % in the root cycle we execute the following iteration.

i = % → degree; while R[i] 6 = NULL do %′^ = R[i]; R[i] = NULL; % = LINK(%, %′); i++ endwhile ; R[i] = %.

To analyze the amortized cost for deleting the minimum, let D(n) be the maximum possible degree of any node in a Fibonacci heap of n nodes. The number of linking

the cascading cuts in Step 4, is only a constant. Similarly, the actual cost of a delete operation is at most a constant, but Step 2 may increase the potential of the Fibonacci heap by as much as D(n). The rest is bounded from above by a constant, which implies that the amortized cost of the delete operation is O(log n). We summarize the amortized cost of the various operations supported by the Fibonacci heap:

find the minimum O(1) meld two heaps O(1) insert a new item O(1) delete the minimum O(log n) decrease the key of a node O(1) delete a node O(log n)

We will later see graph problems for which the difference in the amortized cost of the decreasekey and the delete op- erations implies a significant improvement in the running time.