Professional Documents
Culture Documents
Optimization Algorithms
MICHAEL L. FREDMAN
AND
Abstract. In this paper we develop a new data structure for implementing heaps (priority queues). Our
structure, Fibonacci heaps (abbreviated F-heaps), extends the binomial queues proposed by Vuillemin
and studied further by Brown. F-heaps support arbitrary deletion from an n-item heap in qlogn)
amortized time and all other standard heap operations in o( 1) amortized time. Using F-heaps we are
able to obtain improved running times for several network optimization algorithms. In particular, we
obtain the following worst-case bounds, where n is the number of vertices and m the number of edges
in the problem graph:
( 1) O(n log n + m) for the single-source shortest path problem with nonnegative edge lengths, improved
from O(mlogfmh+2)n);
(2) O(n*log n + nm) for the all-pairs shortest path problem, improved from O(nm lo&,,,+2,n);
(3) O(n*logn + nm) for the assignment problem (weighted bipartite matching), improved from
O(nmlog0dn+2)n);
(4) O(mj3(m, n)) for the minimum spanning tree problem, improved from O(mloglo&,,.+2,n), where
j3(m, n) = min {i 1log% 5 m/n). Note that B(m, n) 5 log*n if m 2 n.
Of these results, the improved bound for minimum spanning trees is the most striking, although all the
results give asymptotic improvements for graphs of appropriate densities.
Categories and Subject Descriptors: E.l [Data]: Data Structures--trees; graphs; F.2.2 [Analysis of
Algorithms and Problem Complexity]: Nonnumerical Algorithms and Problems-computations on
discrete structures; sorting and searching; G.2.2 [Discrete Mathematics]: Graph Theory-graph algo-
rithms; network problems; trees
General Terms: Algorithms, Theory
Additional Key Words and Phrases: Heap, matching, minimum spanning tree, priority queue, shortest
Pati
’ A preliminary version of this paper appeared in the Proceedings of the 25th IEEE Symposium on the
Foundations of Computer Science (Singer Island, Fla., Oct. 24-26). IEEE, New York, 1984, pp. 338-
346, 0 IEEE. Any portion of this paper that appeared in the preliminary version is reprinted with
permission.
This research was supported in part by the National Science Foundation under grant MCS 82-0403 1.
Authors’ addresses: M. L. Fredman, Electrical Engineering and Computer Science Department, Uni-
versity of California, San Diego, La Jolla, CA 92093; R. E. Tarjan, AT&T Bell Laboratories, Murray
Hill, NJ 07974.
Permission to copy without fee all or part of this material is granted provided that the copies are not
made or distributed for direct commercial advantage, the ACM copyright notice and the title of the
publication and its date appear, and notice is given that copying is by permission of the Association for
Computing Machinery. To copy otherwise, or to republish, requires a fee and/or specific permission.
0 1987 ACM 0004-541 l/87/0700-0596 $1.50
Journal ofthe Association for Computing Machinery, Vol. 34, No. 3, July 1987, Pages596-615.
Fibonacci Heaps and Improved Network Optimization Algorithms 597
1. Introduction
A heap is an abstract data structure consisting of a set of items, each with a real-
valued key, subject to the following operations:
make heap: Return a new, empty heap.
insert (i, h): Insert a new item i with predefined key into heap h.
find min (h): Return an item of minimum key in heap h. This operation
does not change h.
delete min (h): Delete an item of minimum key from heap h and return
it.
In addition, the following operations on heaps are often useful:
meld (h,, h2): Return the heap formed by taking the union of the item-
disjoint heaps h, and hz. This operation destroys h, and
h
decrease key (A, i, h): Decrease the key of item i in heap h by subtracting the
nonnegative real number A. This operation assumes that
the position of i in h is known.
delete (i, h): Delete arbitrary item i from heap h. This operation assumes
that the position of i in h is known.
Note that other authors have used different terminology for heaps. Knuth [ 151
called heaps “priority queues.” Aho et al. [l] used this term for heaps not subject
to melding and called heaps subject to melding “mergeable heaps.”
In our discussion of heaps, we assume that a given item is in only one heap at a
time and that a pointer to its heap position is maintained. It is important to
remember that heaps do not support efficient searching for an item.
Remark. The heap parameter h in decreasekey and delete is actually redundant,
since item i determines h. However, our implementations of these operations
require direct accessto h, which must be provided by an auxiliary data structure if
h is not a parameter to the operation. If melding is allowed, a data structure for
disjoint set union [26] must be used for this purpose; otherwise, a pointer from
each item to the heap containing it suffices.
Vuillemin [27] invented a class of heaps, called binomial queues, that support
all the heap operations in O(logn) worst-case time. Here n is the number of items
in the heap or heaps involved in the operation. Brown [2] studied alternative
representations of binomial heaps and developed both theoretical and experimental
running-time bounds. His results suggest that binomial queues are a good choice
in practice if meldable heaps are needed, although several other heap implemen-
tations have the same O(logn) worst-case time bound. For further discussion of
heaps, see [ 11, [2], [ 151,and [24].
In this paper we develop an extension of binomial queues called Fibonacci heaps,
abbreviated F-heaps. F-heaps support delete min and delete in O(log n) amortized
time, and all the other heap operations, in particular decreasekey, in 0( 1) amortized
time. For situations in which the number of deletions is small compared to the
total number of operations, F-heaps are asymptotically faster than binomial queues.
Heaps have a variety of applications in network optimization, and in many such
applications, the number of deletions is relatively small. Thus we are able to use
F-heaps to obtain asymptotically faster algorithms for several well-known network
optimization problems. Our original purpose in developing F-heaps was to speed
598 M. L. FREDMAN AND R. E. TARJAN
up Dijkstra’s algorithm for the single-source shortest path problem with non-
negative length edges [5]. Our implementation of Dijkstra’s algorithm runs in
O(nlogn + m) time, improved from Johnson’s O(mlog~,,,+z,n) bound [ 13, 241.
Various other network algorithms use Dijkstra’s algorithm as a subroutine, and
for each of these, we obtain a corresponding improvement. Thus we can solve both
the all-pairs shortest path problem with possibly negative length edges and the
assignment problem (bipartite weighted matching) in O(n210gn + nm) time,
improved from O(nm 10&~,~+~)n) [24].
We also obtain a faster method for computing minimum spanning trees. Our
bound is O(m@)(m, n)), improved from O(m loglogcm,n+2)n)[4, 241, where
P(m, n) = min(i ] log% 5 m/n).
All our bounds for network optimization are asymptotic improvements for
graphs of intermediate density (n c m << n2). Our bound for minimum spanning
trees, which is perhaps our most striking result, is an asymptotic improvement for
sparse graphs as well.
The remainder of the paper consists of five sections. In Section 2 we develop
and analyze F-heaps. In Section 3 we discuss variants of F-heaps and some
additional heap operations. In Section 4 we use F-heaps to implement Dijkstra’s
algorithm. In Section 5 we discuss the minimum spanning tree problem. In
Section 6 we mention several more recent results and remaining open problems.
2. Fibonacci Heaps
To implement heaps we use heap-ordered trees. A heap-ordered tree is a rooted
tree containing a set of items, one item in each node, with the items arranged in
heap order: If x is any node, then the key of the item in x is no less than the key
of the item in its parent p(x), provided x has a parent. Thus the tree root contains
an item of minimum key. The fundamental operation on heap-ordered trees is
linking, which combines two item-disjoint trees into one. Given two trees with
roots x and y, we link them by comparing the keys of the items in x and y. If the
item in x has the smaller key, we make y a child of x; otherwise, we make x a child
of y. (See Figure 1.)
A Fibonacci heap (F-heap) is a collection of item-disjoint heap-ordered trees.
We impose no explicit constraints on the number or structure of the trees; the only
constraints are implicit in the way the trees are manipulated. We call the number
of children of a node x its rank r(x). There is no constant upper bound on the
rank of a node, although we shall seethat the rank of a node with n descendants is
O(logn). Each node is either marked or unmarked; we shall discuss the use of
marking later.
In order to make the correctness of our claimed time bounds obvious, we assume
the following representation of F-heaps: Each node contains a pointer to its parent
(or to a special node null if it has no parent) and a pointer to one of its children.
The children of each node are doubly linked in a circular list. Each node also
contains its rank and a bit indicating whether it is marked. The roots of all the
trees in the heap are doubly linked in a circular list. We access the heap by a
pointer to a root containing an item of minimum key; we call this root the
minimum node of the heap. A minimum node of null denotes an empty heap. (See
Figure 2.) This representation requires space in each node for one item, four
pointers, an integer, and a bit. The double linking of the lists of roots and children
makes deletion from such a list possible in 0( 1) time. The circular linking makes
concatenation of lists possible in O( 1) time.
Fibonacci Heaps and Improved Network Optimization Algorithms
(a)
048
(b)
FIG. 1. Linking two heapordered trees. (In this and most of the succeed-
ing figures, we do not distinguish between items and their keys.) (a) Two
trees. (b) After linking.
We shall postpone a discussion of decrease key and delete until later in the
section. The remaining heap operations we carry out as follows: To perform make
heap, we return a pointer to null. To perform find min (h), we return the item in
the minimum node of h. To carry out insert (i, h), we create a new heap consisting
of one node containing i, and replace h by the meld of h and the new heap. To
carry out meld (hl , h2), we combine the root lists of hl and hZ into a single list, and
return as the minimum node of the new heap either the minimum node of hl or
the minimum node of hz, whichever contains the item of the smaller key. (In the
caseof a tie, the choice is arbitrary.) All of these operations take 0( 1) time.
The most time-consuming operation is delete min (h). We begin the deletion by
removing the minimum node, say, x, from h. Then we concatenate the list of
children of x with the list of roots of h other than x, and repeat the following step
until it no longer applies.
Linking Step. Find any two trees whose roots have the same rank, and link
them. (The new tree root has rank one greater than the ranks of the old tree roots.)
Once there are no two trees with roots of the same rank, we form a list of the
remaining roots, in the processfinding a root containing an item of minimum key
600 M. L. FREDMAN AND R. E. TARJAN
FIG. 2. Pointers representing an F-heap. The four pointem in each node indicate the
left sibling, the parent, some child, and the right sibling. The middle field in each node
is its key. Ranks and mark bits are not shown.
to serve as the minimum node of the modified heap. We complete the deletion by
saving the item in x, destroying x, and returning the saved item. (See Figure 3.)
The delete min operation requires finding pairs of tree roots of the same rank to
link. To do this we use an array indexed by rank, from zero up to the maximum
possible rank. Each array position holds a pointer to a tree root. When performing
a delete min operation, after deleting the minimum node and forming a list of the
new tree roots, we insert the roots one by one into the appropriate array positions.
Whenever we attempt to insert a root into an already occupied position, we perform
a linking step and attempt to insert the root of the new tree into the next higher
position. After successfully inserting all the roots, we scan the array, emptying it.
The total time for the delete min operation is proportional to the maximum rank
of any of the nodes manipulated plus the number of linking steps.
The data structure we have so far described is a “lazy melding” version of
binomial queues. If we begin with no heaps and carry out an arbitrary sequence of
heap operations (not including delete or decreasekey), then each tree ever created
is a binomial tree, defined inductively as follows: A binomial tree of rank zero
consists of a single node; a binomial tree of rank k > 0 is formed by linking two
binomial trees of rank k - 1. (See Figure 4.) A binomial tree of rank k contains
exactly 2k nodes, and its root has exactly k children. Thus every node in an n-item
heap has rank at most logn.’
We can analyze the amortized running times of the heap operations by using the
“potential” technique of Sleator and Tajan [ 19, 251. We assign to each possible
collection of heaps a real number called the potential of the heaps. We define the
amortized time of a heap operation to be its actual running time plus the net
increase it causes in the potential. (A decrease in potential counts negatively and
thus makes the amortized time less than the actual time.) With this definition, the
actual time of a sequence of operations is equal to the total amortized time plus
the net decrease in potential over the entire sequence.
To apply this technique, we define the potential of a collection of heaps to be
the total number of trees they contain. If we begin with no heaps, the initial
potential is zero, and the potential is always nonnegative. Thus the total amortized
time of a sequence of operations is an upper bound on the total actual time. The
amortized time of a make heap, find min, insert, or meld operation is 0( 1): An
insertion increases the number of trees by one; the other operations do not affect
’ All logarithms in this paper for which a base is not explicitly specified are base 2.
Fibonacci Heaps and Improved Network Optimization Algorithms 601
012
the number of trees. If we charge one unit of time for each linking step, then a
delete min operation has an amortized time of O(logn), where n is the number of
items in the heap: Deleting the minimum node increases the number of trees by at
most logn; each linking step decreasesthe number of trees by one.
Our goal now is to extend this data structure and its analysis to include the
remaining heap operations. We implement decrease key and delete as follows: To
cany out decrease key (A, i, h), we subtract A from the key of i, find the node x
containing i, and cut the edge joining x to its parent p(x). This requires removing
x from the list of children of p(x) and making the parent pointer of x null. The
effect of the cut is to make the subtree rooted at x into a new tree of h, and requires
decreasing the rank of p(x) and adding x to the list of roots of h. (See Figure 5.) (If
x is originally a root, we carry out decrease key (A, i, h) merely by subtracting A
from the key of i.) If the new key of i is smaller than the key of the minimum
node, we redefine the minimum node to be x. This method works because A is
nonnegative; decreasing the key of i preserves heap order within the subtree rooted
at x, though it may violate heap order between x and its parent. A decrease key
operation takes 0( 1) actual time.
The delete operation is similar to decrease key. To carry out delete (i, h), we find
the node x containing i, cut the edge joining x and its parent, form a new list of
roots by concatenating the list of children of x with the original list of roots, and
602 M. L. FREDMAN AND R. E. TARJAN
h-4
(b) BI 62 B3 04
0 15
(b) 6
15
8
FIG. 5. The decreasekey and delete operations. (a) The original heap.
(b) After reducing key 10 to 6. The minimum node is still the node
containing 3. (c) After deleting key 7.
Fibonacci Heaps and Improved Network Optimization Algorithms 603
FIG. 6. Cascading cuts. (a) Just before decreasing key 10. Nodes 4, 5, and 9 are assumed
to have previously lost a child via a cut. (b) After decreasing key 10 to 7. The original cut
separates 10 from 9. Cascading cuts separate 9 from 5, 5 from 4, and 4 from 2.
destroy node x. (See Figure 5.) (If x is originally a root, we remove it from the list
of roots rather than removing it from the list of children of its parent; if x is the
minimum node of the heap, we proceed as in delete min.) A delete operation takes
0( 1) actual time, unless the node destroyed is the minimum node.
There is one additional detail of the implementation that is necessary to obtain
the desired time bounds. After a root node x has been made a child of another
node by a linking step, as soon as x loses two of its children through cuts, we cut
the edge joining x and its parent as well, making x a new root as in decrease key.
We call such a cut a cascading cut. A single decrease key or delete operation in the
middle of a sequence of operations can cause a possibly large number of cascading
cuts. (See Figure 6.)
The purpose of marking nodes is to keep track of where to make cascading cuts.
When making a root node x a child of another node in a linking step, we unmark
x. When cutting the edgejoining a node x and its parent p(x), we decreasethe rank
of p(x) and check whether p(x) is a root. If p(x) is not a root, we mark it if it is
unmarked and cut the edge to its parent if it is marked. (The latter case may lead
to further cascading cuts.) With this method, each cut takes 0( 1) time.
This completes the description of F-heaps. Our analysis of F-heaps hinges on
two crucial properties: (1) Each tree in an F-heap, even though not necessarily a
binomial tree, has a size at least exponential in the rank of its root; and (2) the
number of cascading cuts that take place during a sequence of heap operations is
bounded by the number of decrease key and delete operations. Before proving
604 M. L. FREDMAN AND R. E. TARJAN
these properties, we remark that cascading cuts are introduced in the manipulation
of F-heaps for the purpose of preserving property (1). Moreover, the condition for
their occurrence, namely, the “loss of two children” rule, limits the frequency
of cascading cuts as described by property (2). The following lemma implies
property ( 1):
LEMMA 1. Let x be any node in an F-heap. Arrange the children of x in the
order they were linked to x, from earliest to latest. Then the ith child of x has a
rank of at least i - 2.
PROOF. Let y be the ith child of x, and consider the time when y was linked to
x. Just before the linking, x had at least i - 1 children (some of which it may have
lost after the linking). Since x and y had the same rank just before the linking, they
both had a rank of at least i - 1 at this time. After the linking, the rank of y could
have decreased by at most one without causing y to be cut as a child of x. Cl
COROLLARY 1. A node of rank k in an F-heap has at least Fk+2L @kdescendants,
including itself; where Fk is the kth Fibonacci number (FO= 0, F, = 1, Fk = Fkm2+
Fk-, for k L 2), and C#I= (1 + A)/2 is the golden ratio. (See Figure 7.)
PROOF. Let Sk be the minimum possible number of descendants of a node of
rank k. Obviously, SO= 1, and Sr = 2. Lemma 1 implies that Sk L C!Zi Si + 2 for
k 2 2. The Fibonacci numbers satisfy Fk+z = & Fi + 2 for k L 2, from which
Sk 2 Fk+2 for k L 0 follows by induction on k. The inequality Fk+z 1 4k is well
known [ 141. Cl
Remark. This corollary is the source of the name “Fibonacci heap.”
To analyze F-heaps we need to extend our definition of potential. We define the
potential of a collection of F-heaps to be the number of trees plus twice the number
of marked nonroot nodes. The 0( 1) amortized time bounds for make heap, find
min, insert, and meld remain valid, as does the O(logn) bound for delete min;
delete min (h) increases the potential by at most 1.4404 log n minus the number of
linking steps, since, if the minimum node has rank k, then 4k 5 n and thus
k I log n/log 4 5 1.4404 log n.
Let us charge one unit of time for each cut. A decrease key operation causes the
potential to increase by at most three minus the number of cascading cuts, since
the first cut converts a possibly unmarked nonroot node into a root, each cascading
cut converts a marked nonroot node into a root, and the last cut (either first or
cascading) can convert a nonroot node from unmarked to marked. It follows that
decreasekey has an 0( 1) amortized time bound. Combining the analysis of decrease
key with that of delete min, we obtain an O(log n) amortized time bound for delete.
Thus we have the following theorem:
THEOREM 1. Zf we begin with no F-heaps and perform an arbitrary sequence of
F-heap operations, then the total time is at most the total amortized time, where
the amortized time is O(log n) for each delete min or delete operation and 0( 1)for
each of the other operations.
We close this section with a few remarks on the storage utilization of F-heaps.
Our implementation of F-heaps usesfour pointers per node, but this can be reduced
to three per node by using an appropriate alternative representation [2] and even
to two per node by using a more complicated representation, at a cost of a constant
factor in running time. Although our implementation uses an array for finding
roots of the same rank to link, random-access memory is not actually necessary
Fibonacci Heaps and Improved Network Optimization Algorithms 605
RANK 0 1 2 3 4 5
SIZE 1 2 3 5 0 13
FIG. 7. Trees of minimum possible size for a &ven rank in a Fibonacci heap.
for this purpose. Instead, we can maintain a doubly linked list of rank nodes
representing the possible ranks. Each node has a rank pointer to the rank node
representing its rank. Since the rank of a node is initially zero and only increases
or decreasesby one, it is easy to maintain the rank pointers. When we need to
carry out linking steps, we can use each rank node to hold a pointer to a root of
the appropriate rank. Thus the entire data structure can be implemented on a
pointer machine [22] with no loss in asymptotic efficiency.
LEMMA 2. Afind min operation takes O(l(log(n/l) + 1)) amortized time, where
1 is the number of vacant nodes destroyed and n is the number of nodes in the heap;
if1 = 0, the amortized time is 0( 1).
PROOF. If I = 0, the lemma is obvious. Thus suppose I 2 1. The amortized time
of the find min operation is at most a constant times log n plus the number of new
trees created by the destruction of vacant nodes. Let x be any destroyed vacant
node, and suppose that x has k nonvacant children before its destruction. By
Lemma 1, at least one of these, say, y, has a rank of at least k - 2, which means
M. L. FREDMAN AND R. E. TARJAN
14
816
FIG. 8. A find min operation on a heap with vacant nodes. (a) The original
heap. (b) Afterfind min. Subtrces with roots 15, 12,9, and 8 become trees, and
then linking takes place.
that y is the root of a subtree containing at least @2 nodes. These bkm2nodes can
be uniquely associated with x. In other words, if the 1destroyed vacant nodes have
k,, k2, . . . , ki nonvacant children, then xi=, @j-2 5 n. Subject to this constraint,
the sum of the kis, which counts the number of new trees created, is maximized
when all the kis are equal. This implies that xf=r ki = O(l(log(n/Z) + l)), giving the
lemma. Cl
Lazy deletion is especially useful for applications in which the deleted items can
be identified implicitly, as, for example, if there is a predicate that specifies whether
an item is deleted. The main drawback with lazy deletion is that it may use extra
space if individual items are inserted and deleted many times. We can avoid this
drawback by changing the data structure slightly, giving our second variant of
F-heaps, called F-heaps with good and bad trees.
In the new variant, we avoid the use of vacant nodes. Instead, we divide the trees
comprising the F-heap into two groups: good trees and bad trees. We maintain the
minimum node to be a root of minimum key among the good trees. When inserting
an item, the corresponding one-node tree becomes a good tree in the heap. When
Fibonacci Heaps and Improved Network Optimization Algorithms 607
melding two heaps,we combine their setsof good trees and their setsof bad trees.
When performing a decrease key operation, all the new trees formed by cascading
cuts become good trees. We carry out a delete operation as described in Section 2
except that, if the deleted item is the minimum node of the heap, all the subtrees
rooted at its children become bad trees; we delay any linking until the next jind
min. (This includes the case of a delete min operation.) To carq out a find min,
we check whether there are any bad trees. If not, we merely return the minimum
node. If so, we link trees whose roots have equal rank until there are no two trees
of equal rank, make all trees good, update the minimum node, and return it.
With this variant of F-heaps, we obtain the following amortized time bounds
(using an analysis similar to that above): 0( 1) for find min, insert, meld, and
decreasekey; and O(log(n/l) + 1) per delete or delete min operation for a sequence
of I such operations not separatedby a find min. The advantage of this variant is
that it requires no space for vacant nodes. The disadvantage is that it does not
support implicit deletion.
Our third variant of F-heaps uses only a single tree to represent each heap. In
this one-tree variant, we mark each node that is not a root as either good or bad;
the node type is determined when the linking operation that makes the node a
nonroot is done. These marks are in addition to the marks used for cascadingcuts.
We define the rank of a node to be its number of good children (i.e., we do not
count the bad children).
To meld two heaps, we link the corresponding trees, making the root that
becomes a child bad. This melding does not change the rank of any node. To
perform find min, we return the root of the tree representing the heap. To perform
delete min, we delete the tree root, link pairs of trees whose roots have equal rank
until no such linking is possible, making each node that becomesa nonroot good,
and then link all the remaining trees in any order, making each node that becomes
a nonroot bad. To perform decrease key, we perform the appropriate cuts (using
mark bits as before), producing a set of trees. We link these trees in any order,
making each root that becomesa nonroot bad. To perform an arbitrary deletion,
we do the appropriate cuts, discard the node to be deleted, and combine the
remaining trees using links as in delete min, first combining trees with roots of
equal rank and marking the new nonroots good, then combining trees of unequal
rank and marking the new nonroots bad.
The one-tree variant of F-heaps has the following properties: Corollary 1 still
holds; that is, any node with k good children has at least F~+z > 4“ descendants.
The number of bad children created during the running of the algorithm is at most
one per insert or meld, one per cut (and hence 0( 1) per decrease key), and at most
O(logn) per delete min or delete. Combining this with the previous analysis of
F-heaps, we obtain an amortized time bound of 0( 1) for insert, meld, find min,
and decrease key, and O(log n) for delete min and delete.
Our fourth and last variant of F-heaps, &led F-heaps with implicit keys, is
designedto support the following additional heap operation:
increase all keys (A, h): Increase the keys of all items in heap h by the arbitrary
real number A.
To implement increase all keys, we represent the keys of the items implicitly
rather than explicitly, using a separatedata structure. A suitable variant of com-
pressedtrees [23] sufficesfor this purpose. We maintain a compressedtree for each
heap. Each node in the tree contains a value and possibly an item. Any node
608 M. L. FREDMAN AND R. E. TARJAN
(4
A B C D
containing an item has no children. The values represent the keys as follows:
If x is any node containing an item i, the sum of the values of the ancestors
of x (including x itself) is the key of i. (See Figure 9.)
We manipulate this data structure as follows: When executing make heap, we
construct a new one-node compressed tree to represent the heap. The new node
has value zero. When executing insert (i, h), we create a new compressed tree node
x containing i, make the root of the compressed tree representing h the parent p(x)
of x, and give x a value defined by value(x) = key(i) - value( p(x)). When deleting
an item i from a heap, we destroy the compressed tree node containing i. When
executing decrease key(A, i, h), we subtract A from the value of the compressed
tree node containing i. To perform increase all keys(A, h), we add A to the value
of the root of the compressed tree representing h.
Fibonacci Heaps and Improved Network Optimization Algorithms 609
FIG. 1I. Combining two compressed trees. Ranks are outside the
roots.
04
3
B
@
A
Whenever we need to evaluate the key of an item i (as in a find min operation
or a linking step), we locate the compressed tree node x containing i and follow
the path from x through its ancestors up to the tree root. Then we walk back down
the path from the root to x, compressing it as follows: When we visit a node y that
is not a child of the root, we replace value(y) by value(y) + vaZue(p( v)) and
redefine p(y) to be the root. (See Figure 10.) This compression makes every node
along the path a child of the root and preserves the relationship between values
and keys. After the compression, we return value(x) + valuep(x) as the key of i.
The last operation we must consider is melding. (If no melding takes place, then
the depth of every compressed tree is at most one, and each operation on a
compressed tree takes 0( 1) time.) To facilitate melding we maintain for each
compressed tree root a nonnegative integer rank. (This rank should not be confused
with the rank of a heap-ordered tree node; it does not count the number of
children.) A newly created compressed tree root has rank zero. When executing
meld(h, , hz), we locate the roots, say, x and y, of the compressed trees representing
hi and hZ. If rank(x) > rank(v), we make x the parent of y and redefine value(y)
to be value(y) - value(x). If rank(x) < rank(v), we make y the parent of x and
redefine value(x) to be value(x) - value(y). Finally, if rank(x) = rank(y), we
increase rank(x) by one and proceed as in the case of rank(x) > rank(y). (See
Figure 1I .)
To implement this data structure, we need one compressed tree node for each
make heap operation plus one node per item, with room in each node for an item
or rank, a value, and a parent pointer. The total time for compressed tree operations
is O(m +&(m + f, n)), where m is the number of heap operations, n is the number
of make heap and insert operations, fis the number of key evaluations, and (Yis a
functional inverse of Ackerman’s function [23, 261. In most applications of heaps
requiring use of the increase all keys operation, the time for manipulating heap-
ordered trees will dominate the time for manipulating compressed trees. Note that
the two data structures are entirely separate; we can use compressed trees in
combination with any implementation of heaps that is based on key comparison.
4. Shortest Paths
In this section we use F-heaps to implement Dijkstra’s shortest path algorithm [5]
and explore some of the consequences. Our discussion of Dijkstra’s algorithm is
based on Tarjan’s presentation [24]. Let G be a directed graph, one of whose
vertices is distinguished as the source s, and each of whose edges (v, w) has a
610 M. L. FREDMAN AND R. E. TARJAN
nonnegative length I(v, w). The length of a path in G is the sum of its edgelengths;
the distance from a vertex v to a vertex w is the minimum length of a path from v
to w. A minimum-length path from v to w is called a shortest path. The singZe-
source shortest path problem is that of finding a shortest path from s to v for every
vertex v in G.
We shall denote the number of vertices in G by n and the number of edgesby
m. We assumethat there is a path from s to any other vertex; thus m L n - 1.
Dijkstra’s algorithm solves the shortest path problem using a tentative distance
function d from vertices to real numbers with the following properties:
(1) For any vertex v such that d(v) is finite, there is a path from s to v of
length d(v);
(2) when the algorithm terminates, d(v) is the distance from s to v.
Initially d(s) = 0 and d(v) = 00for v # s. During the running of the algorithm, each
vertex is in one of three states:unlabeled, labeled, or scanned. Initially s is labeled
and all other vertices are unlabeled. The algorithm consists of repeating the
following step until there are no labeled vertices (every vertex is scanned):
Scan. Select a labeled vertex v with d(v) minimum. Convert v from labeled
to scanned, For each edge (v, w) such that d(v) + Z(v, w) < d(w), replace d(w)
by d(v) + l(v, w) and make w labeled.
The nonnegativity of the edge lengths implies that a vertex, once scanned, can
never become labeled, and further that the algorithm computes distancescorrectly.
To implement Dijkstra’s algorithm, we use a heap to store the set of labeled
vertices. The tentative distance of a vertex is its key. Initialization requires one
make heap and one insert operation. Each scanning step requires one delete min
operation. In addition, for each edge (v, w) such that d(v) + I(v, w) < d(w), we need
either an insert operation (if d(w) = 00)or a decrease key operation (if d(w) < 00).
Thus there is one make heap operation, n insert and n delete min operations, and
at most m decrease key operations. The maximum heap size is n - 1. If we use an
F-heap, the total time for heap operations is O(n logn + m). The time for other
tasks is O(n + m). Thus we obtain an O(nlogn + m) running time for Dijkstra’s
algorithm. This improves Johnson’s bound of O(m logt,,,ln+z)n)[ 13, 241 based on
the use of implicit heapswith a branching factor of m/n + 2.
By augmenting the algorithm, we can make it compute shortest paths as well as
distances: For each vertex v, we maintain a tentative predecessor pred (v) that
immediately precedes v on a tentative shortest path from s to v. When replacing
d(w) by d(v) + I(v, w) in a scanning step, we define pred (w) = v. Once the algorithm
terminates, we can find a shortest path from s to v for any vertex v by following
predecessorpointers from v back to s. Augmenting the algorithm to maintain
predecessorsadds only O(m) to the running time.
Severalother optimization algorithms use Dijkstra’s algorithm as a subroutine,
and for each of these,we obtain a corresponding improvement. We shall give three
examples:
(1) The all-pairs shortest path problem, with or without negative edgelengths, can
be solved in O(nm) time plus n iterations of Dijkstra’s algorithm [ 13,241. Thus
we obtain a time bound of O(n*log n + nm), improved from O(nm logtmin+2)n).
(2) The assignment problem (bipartite weighted matching) can also be solved in
O(nm) time plus n iterations of Dijkstra’s algorithm [24]. Thus we obtain a
bound of O(n210gn + nm), improved from O(nm logt,,,,n+2jn).
Fibonacci Heaps and Improved Network Optimization Algorithms 611
(3) Knuth’s generalization [ 161of Dijkstra’s algorithm to compute minimum-cost
derivations in a context-free language runs in O(nlogn + t) time, improved
fromO(mlogn + t), where n is the number of nonterminals, m is the number
of productions, and t is the total length of the productions. (There is at least
one production per nonterminal; thus m 2 n.)
REFERENCES
1. AHO, A. V., HOPCROFT,J. E., AND ULLMAN, J. D. The Design and Analysis of Computer
Algorithms. Addison-Wesley, Reading, Mass. 1974.
2. BROWN,M. R. Implementation and analysis of binomial queue algorithms. SIAM J. Comput. 7,
3 (Aug. 1978), 298-319.
3. CAMERINI,P. M., FRATTA,L., AND MAFFIOLI,F. A note on finding optimum branchings. Networks
9,4 (Winter 1979), 309-3 12.
4. CHERITON,D., AND TARJAN, R. E. Finding minimum spanning trees. SIAM J. Comput. 5,4 (Dec.
1976), 724-742.
5. DIJKSTRA,E. W. A note on two problems in connexion with graphs. Numer. Math. I, 4 (Sept.
1959), 269-27 1.
6. EDMONDS,J., AND KARP, R. M. Theoretical improvements in algorithmic efficiency for network
flow problems. J. ACM 19,2 (Apr. 1972), 248-264.
7. GABOW,H. N., AND STALLMAN,M. Efficient algorithms for the parity and intersection problems
on graphic matroids. In Automata, Languages, and Programming, 12th Colloquium, Lecture Notes
in Computer Science, Vol. 194, W. Brauer, Ed. Springer-Verlag, New York, 1985, pp. 210-220.
8. GABOW, H. N., AND TARJAN, R. E. Efficient algorithms for a family of matroid intersection
problems. J. Algorithms 5, 1 (Mar. 1984), 80-131.
9. GABOW,H. N., GALIL, Z., AND SPENCER,T. Efficient implementation of graph algorithms using
contraction. In Proceedings of the 25th Annual IEEE Symposium on Foundations of Computing.
(Singer Island, Fla., Oct. 24-26). IEEE Press, New York, 1984 pp. 338-346.
10. GABOW, H. N., GALIL, Z., SPENCER,T., AND TARJAN, R. E. EtXcient algorithms for finding
minimum spanning trees in undirected and directed graphs. Combinatorics 6,2 (1986), 109-122.
11. GRAHAM,R. L., AND HELL, P. On the history of the minimum spanning tree problem. Ann. Hist.
Comput. 7, 1 (Jan. 1985), 43-57.
12. JARNIK, V. 0 jistCm probltmu minimalnim. Prdca Moravska P?irodovt?deck&SpoleEnosti 6 (1930),
57-63 (in Czechoslovakian).
13. JOHNSON,D. B. Efficient algorithms for shortest paths in sparse networks. J. ACM 24, 1
(Jan. 1977), l-13.
14. KNUTH, D. E. The Art of Computer Programming. Vol. 1, Fundamental Algorithms, 2nd ed.
Addison-Wesley, Reading, Mass. 1973.
15. KNUTH, D. E. The Art of Computer Programming. Vol. 3, Sorting and Searching. Addison-
Wesley, Reading, Mass., 1973.
16. KNUTH, D. E. A generalization of Dijkstra’s algorithm. Znf Process. Lett. 6, 1 (Feb. 1977), l-5.
17. KOML&, J. Linear verification for spanning trees. In Proceedings of the 25th Annual IEEE
Symposium on Foundations of Computing. (Singer Island, Fla., Oct. 24-26). IEEE Press,New York,
1984 pp. 201-206.
18. PRIM, R. C. Shortest connection networks and some generalizations. Bell Syst. Tech. J. 36, 6
(Nov. 1957), 1389-1401.
19. SLEATOR,D. D., AND TARJAN, R. E. Self-adjusting heaps. SIAM J. Comput. IS, 1 (Feb. 1986),
52-69.
Fibonacci Heaps and Improved Network Optimization Algorithms 615
20. SUURBALLE, J. W., AND TARJAN,R. E. A quick method for finding shortest pairs of paths.
Networks 14 (1984),325-336.
21. TARIAN,R. E. Finding optimum branchings.Networks 7, 1 (Spring 1977),25-35.
22. TARIAN,R. E. A class of algorithms which require nonlinear time to maintain disjoint sets.
J. Comput. Syst. Sci. 18,2 (Apr. 1979),110-127.
23. TASUAN,R. E. Applications of path compressionon balancedtrees.J. ACM 24 4 (Oct. 1979),
690-715.
24. TAIIJAN,R. E. Data Structures and Network Algorithms. Society for Industrial and Applied
Mathematics,Philadelphia,Pa., 1983.
25. TARJAN,R. E. Amortized computational complexity. SIAM J. Algebraic Discrete Methods 6, 2
(Apr. 1985),306-318.
26. TARIAN,R. E., ANDVANLEEUWEN, J. Worst-caseanalysisof setunion algorithms.J. ACM 31,2
(1984),245-281.
27. VUILLEMIN,J. A data structure for manipulating priority queues. Commun. ACM 21, 4
(Apr. 1978),309-315.
28. YAO, A. An CJ(( E 1 log log 1Y I) algorithm for finding minimum spanning trees.InJ Process.
Lett. 4, 1 (Sept.1975),21-23.
RECEIVED
NOVEMBER
1984, REVISED
FEBRUARY
1986; ACCEPTED
JUNE1986
Journal
of the A.sociation for Computing Machinery, Vol.34,No.3,July 1987.