XL Fiboheap
XL Fiboheap
XL Fiboheap
A little and a little, collected together, become a great deal; the heap in the barn
consists of single grains, and drop and drop makes an inundation.
Saadi (11841291)
The trees that are slow to grow bear the best fruit.
Molire (16221673)
Promote yourself but do not demote another.
Rabbi Israel Salanter (18101883)
Fall is my favorite season in Los Angeles, watching the birds change color and fall
from the trees.
David Letterman
L Fibonacci Heaps
L.1 Mergeable Heaps
A mergeable heap is a data structure that stores a collection of keys
1
and supports the following operations.
INSERT: Insert a new key into a heap. This operation can also be used to create a new heap
containing just one key.
FINDMIN: Return the smallest key in a heap.
DELETEMIN: Remove the smallest key from a heap.
MERGE: Merge two heaps into one. The new heap contains all the keys that used to be in the old
heaps, and the old heaps are (possibly) destroyed.
If we never had to use DELETEMIN, mergeable heaps would be completely trivial. Each heap just
stores to maintain the single record (if any) with the smallest key. INSERTs and MERGEs require only
one comparison to decide which record to keep, so they take constant time. FINDMIN obviously takes
constant time as well.
If we need DELETEMIN, but we dont care how long it takes, we can still implement mergeable
heaps so that INSERTs, MERGEs, and FINDMINs take constant time. We store the records in a circular
doubly-linked list, and keep a pointer to the minimum key. Now deleting the minimum key takes (n)
time, since we have to scan the linked list to nd the new smallest key.
In this lecture, Ill describe a data structure called a Fibonacci heap that supports INSERTs, MERGEs,
and FINDMINs in constant time, even in the worst case, and also handles DELETEMIN in O(log n) amortized
time. That means that any sequence of n INSERTs, m MERGEs, f FINDMINs, and d DELETEMINs takes
O(n +m+ f +d log n) time.
L.2 Binomial Trees and Fibonacci Heaps
A Fibonacci heap is a circular doubly linked list, with a pointer to the minimum key, but the elements of the
list are not single keys. Instead, we collect keys together into structures called binomial heaps. Binomial
heaps are trees that satisfy the heap propertyevery node has a smaller key than its childrenand have
the following special recursive structure.
A kth order binomial tree, which Ill abbreviate B
k
, is dened recursively. B
0
is a single node. For all
k > 0, B
k
consists of two copies of B
k1
that have been linked together, meaning that the root of one
B
k1
has become a new child of the other root.
1
In the earlier lecture on treaps, I called these keys priorities to distinguish them from search keys.
1
Algorithms Non-Lecture L: Fibonacci Heaps
B
4
B
4
5
B
Binomial trees of order 0 through 5.
Binomial trees have several useful properties, which are easy to prove by induction (hint, hint).
The root of B
k
has degree k.
The children of the root of B
k
are the roots of B
0
, B
1
, . . . , B
k1
.
B
k
has height k.
B
k
has 2
k
nodes.
B
k
can be obtained from B
k1
by adding a new child to every node.
B
k
has
k
d
i=1
s
i2
If we assume inductively that s
i
F
i+2
for all 1 i < d (with the easy base cases s
1
= F
1
and
s
0
= F
2
), we have
s
d
1 +
d
i=1
F
i
= F
d+2
.
(The last step was a practice problem in Homework 0.) By denition, [v[ s
deg(v)
.
You can easily show (using either induction or the annihilator method) that F
k+2
>
k
where
=
1+
_
5
2
1.618 is the golden ratio. Thus, Lemma 1 implies that
deg(v) log
[v[ = O(log[v[).
Thus, since the size of any subtree in an n-node Fibonacci heap is obviously at most n, the degree of any
node is O(log n), which is exactly what we wanted. Our earlier analysis is still good.
L.7 Analyzing Everything Together
Unfortunately, our analyses of DELETEMIN and DECREASEKEY used two different potential functions.
Unless we can nd a single potential function that works for both operations, we cant claim both
amortized time bounds simultaneously. So we need to nd a potential function that goes up a little
during a cheap DELETEMIN or a cheap DECREASEKEY, and goes down a lot during an expensive DELETEMIN
or an expensive DECREASEKEY.
Lets look a little more carefully at the cost of each Fibonacci heap operation, and its effect on
both the number of roots and the number of marked nodes, the things we used as out earlier potential
functions. Let r and m be the numbers of roots and marks before each operation, and let r
/
and m
/
be
the numbers of roots and marks after the operation.
operation actual cost r
/
r m
/
m
INSERT 1 1 0
MERGE 1 0 0
DELETEMIN r +deg(min) r
/
r 0
DECREASEKEY 1 +m m
/
1 +m m
/
m
/
m
In particular, notice that promoting a node in DECREASEKEY requires constant time and increases the
number of roots by one, and that we promote (at most) one unmarked node.
6
Algorithms Non-Lecture L: Fibonacci Heaps
If we guess that the correct potential function is a linear combination of our old potential functions r
and m and play around with various possibilities for the coefcients, we will eventually stumble across
the correct answer:
= r +2m
To see that this potential function gives us good amortized bounds for every Fibonacci heap operation,
lets add two more columns to our table.
operation actual cost r
/
r m
/
m
/
amortized cost
INSERT 1 1 0 1 2
MERGE 1 0 0 0 1
DELETEMIN r +deg(min) r
/
r 0 r
/
r r
/
+deg(min)
DECREASEKEY 1 +m m
/
1 +m m
/
m
/
m 1 +m
/
m 2
Since Lemma 1 implies that r
/
+deg(min) = O(log n), were nally done! (Whew!)
L.8 Fibonacci Trees
To give you a little more intuition about how Fibonacci heaps behave, lets look at a worst-case
construction for Lemma 1. Suppose we want to remove as many nodes as possible from a binomial heap
of order k, by promoting various nodes to the root list, but without causing any cascading promotions.
The most damage we can do is to promote the largest subtree of every node. Call the result a Fibonacci
tree of order k +1, and denote it f
k+1
. As a base case, let f
1
be the tree with one (unmarked) node, that
is, f
1
= B
0
. The reason for shifting the index should be obvious after a few seconds.
Fibonacci trees of order 1 through 6. Light nodes have been promoted away; dark nodes are marked.
Recall that the root of a binomial tree B
k
has k children, which are roots of B
0
, B
1
, . . . , B
k1
. To
convert B
k
to f
k+1
, we promote the root of B
k1
, and recursively convert each of the other subtrees B
i
to f
i+1
. The root of the resulting tree f
k+1
has degree k 1, and the children are the roots of smaller
Fibonacci trees f
1
, f
2
, . . . , f
k1
. We can also consider B
k
as two copies of B
k1
linked together. Its quite
easy to show that an order-k Fibonacci tree consists of an order k 2 Fibonacci tree linked to an order
k 1 Fibonacci tree. (See the picture below.)
5
f
f
f
f
f
7
4
3
f
2
1
f
f
f
5
7
6
B
0 B
1
B
2
B
3
B
4
B
5
B
6
B
5
B
6
B
5
Comparing the recursive structures of B
6
and f
7
.
Since f
1
and f
2
both have exactly one node, the number of nodes in an order-k Fibonacci tree is
exactly the kth Fibonacci number! (Thats why we changed in the index.) Like binomial trees, Fibonacci
trees have lots of other nice properties that easy to prove by induction (hint, hint):
7
Algorithms Non-Lecture L: Fibonacci Heaps
The root of f
k
has degree k 2.
f
k
can be obtained from f
k1
by adding a new unmarked child to every marked node and then
marking all the old unmarked nodes.
f
k
has height |k/2 1.
f
k
has F
k2
unmarked nodes, F
k1
marked nodes, and thus F
k
nodes altogether.
f
k
has
kd2
d1
unmarked nodes,
kd2
d