Title: Fibonacci Heaps
1Fibonacci Heaps
- CS 583
- Analysis of Algorithms
2Outline
- Operations on Binomial Heaps
- Inserting a node
- Extracting the node with minimum key
- Decreasing a key
- Deleting a key
- Fibonacci Heaps
- Definitions
- Amortized analysis
- Inserting a node
- Union
- Self-test
- 20.2-2
3Binomial Heaps Inserting a Node
This procedure inserts node x into binomial heap
H, assuming that x is already allocated with
keyx. Binomial-Heap-Insert(H, x) 1 H2
Make-Binomial-Heap() // create empty heap 2 px
NIL 3 childx NIL 4 siblingx NIL 5
degreex 0 6 headH2 x 7 H
Binomial-Heap-Union(H, H2) The procedure makes a
one node heap H2 in ?(1) time and unites it with
the n-node binomial heap H in O(lg n) time.
4Extracting Minimum Node
This procedure extracts the node with the minimum
key from heap H and returns a pointer to the
extracted node. Binomial-Heap-Extract-Min(H) 1
x ltFind the root with the minimum key
in the root list of H and remove it from Hgt 2 H2
Make-Binomial-Heap() // Remove x (root) from
the tree, and // prepare the linked list as
B0, B1, ... , Bk-1 3 ltreverse the order of xs
childrengt 4 headH2 lthead of xs childrengt 5
H Binomial-Heap-Union(H,H2) 6 return x Each
of lines 1,3,4,5 takes O(lg n) time if H has n
nodes. Hence the above procedure runs in O(lg n)
time.
5Decreasing a Key
This procedure decreases the key of a node x in a
heap H to a new value k. Binomial-Heap-Decrease(H
, x, k) 1 if k gt keyx 2 throw new key is
greater than the current key 3 keyx k 4 y
x 5 z p(y) // Bubble up the key in the
heap 6 while z ltgt NIL and keyy lt keyz 7
ltexchange keyy with keyzgt 8 ltexchange
satellite info y with zgt 9 y z 10 z
py The maximum depth of x is lg n, hence the
while loop 6 iterates at most lg n times. Each
iteration takes ?(1) time, hence the running time
of the algorithm is O(lg n).
6Deleting a Key
Deleting a key is a straightforward combination
of existing procedures. The procedure takes a
heap H, and the node x. Binomial-Heap-Delete(H,
x) // Make xs key the smallest 1
Binomial-Heap-Decrease-Key(H, x, -?) //
Remove x from H 2 Binomial-Heap-Extract-Min(H) B
oth subroutines take O(lg n), hence the running
time of the above procedure is O(lg n).
7Fibonacci Heaps
- Fibonacci heaps support meargeable-heap
operations - Insert, Minimum, Extract-Min, Union,
Decrease-Key, Delete. - They have the advantage over other heap types in
that operations that do not involve deleting an
element run in ?(1) time. - They are theoretically desirable in applications
that involve small number of Extract-Min and
Delete operations relative to the total number of
operations. - Practically, their complexity and constant
factors make them less desirable than binary
heaps for most applications.
8Fibonacci Heaps Definition
- A Fibonacci heap is a collection of min-heap
trees. - The trees are not constrained to be binomial
trees. - The trees are unordered, i.e. children nodes
order does not matter. - Each node x contains the following fields
- px pointer to its parent.
- childx pointer to any of its children.
- all children nodes are linked in a circular
doubly linked list using left. and right.
pointers. - degreex the number of xs children.
- markx indicates whether the node x has lost a
child since the last time x was made a child of
another node.
9Definition (cont.)
- A Fibonacci heap H is accessed by a pointer
minH to the root of a tree containing a minimum
key. - This node is called the minimum node.
- The roots of all trees in the Fibonacci heap H
are linked together into a circular doubly linked
list. - The list is called the root list of H.
- Roots are linked using left and right pointers.
- The order of trees within a root list is
arbitrary. - The number of nodes currently in H is kept in
nH.
10Amortized Analysis
- In amortized analysis, the time required to
perform a sequence of data-structure operations
is averaged over all operations performed. - It differs from the average-case analysis in that
probability is not involved. - It guarantees the average performance of each
operation in the worst case. - The potential method of amortized analysis
represents the prepaid work as potential that
can be released for future operations. - The total work on a data structure is spread
unevenly over the course of an algorithm.
11The Potential Method
- Start with an initial data structure D0 on which
n operations are performed. - Let ci, i 1,...,n be the actual cost of the ith
operation. - Di is the data structure that results after
applying the ith operation on Di-1. - A potential function ? maps each data structure
Di to a real number ?(Di), which is a potential
associated with Di. - The amortized cost cai of the ith operation is
- cai ci ?(Di) - ?(Di-1)
- It is thus an actual cost of operation plus the
increase in potential due to the operation.
12The Potential Method (cont.)
- The total amortized cost of n operations is
- ?i1,ncai ?i1,n(ci ?(Di) - ?(Di-1))
?i1,nci ?(Dn) - ?(D0) - When ?(Dn)gt?(D0), the total amortized cost is an
upper bound on the total actual cost. - Intuitively, when ?(Di)-?(Di-1) gt 0, the
amortized cost of the ith operation represents an
overcharge. - Otherwise, the amortized cost represents an
undercharge, and the actual cost is paid by
decrease in potential. - Different potential functions may yield different
amortized costs, but still be upper bound on the
actual costs.
13Fibonacci Heap Potential Function
- For a given Fibonacci heap H, we denote
- t(H) the number of trees in the root list.
- m(H) the number of marked nodes in H.
- The potential of Fibonacci heap H is defined as
- ?(H) t(H) 2 m(H)
- Assume that a Fibonacci heap application begins
with no heaps. - The initial potential is 0.
- By above equation, the potential gt 0 in all
subsequent time. - Hence an upper bound on total amortized cost is
also an upper bound on total actual cost.
14Mergeable-Heap Operations
- For basic mergeable-heap operations (Make-Heap,
Insert, Minimum, Extract-Min, Union) - Each Fibonacci heap is a collection of
unordered binomial trees. - An unordered binomial tree is like a binomial
tree, and is defined recursively. - The tree U0 contains a single node.
- The tree Uk consists of two unordered binomial
trees Uk-1 for which the root of one is made into
any child of the root of another. The following
property is different from the ordered binomial
trees. - For the tree Uk, the root has degree k, which is
greater than of any other node. The children of
the root are U0,U1,....Uk-1 in some order.
15Creating a New Heap
- The Make-Fib-Heap procedure allocates and returns
the Fibonacci heap object H, where - nH 0
- minH NIL
- there are no trees in H.
- The potential is determined by t(H) 0 and m(H)
0 - ?(H) 0
- Hence the amortized cost is the same as actual
cost, -- ?(1).
16Inserting a Node
This procedure inserts node x into a Fibonacci
heap assuming that x is allocated with a
keyx. Fib-Heap-Insert(H,x) // Create a
linked list containing x only 1 degreex 0 2
px NIL 3 childx NIL 4 leftx x 5
rightx x 6 markx FALSE 7 ltconcatenate
x-list with root list Hgt 8 if minH NIL or
keyx lt keyminH 9 minH x 10 nH
17Inserting Node Performance
- Let H be the input heap, and H2 be the resulting
heap. Then - t(H2) t(H) 1
- m(H2) m(H)
- The increase in potential is
- ((t(H)1) 2 m(H)) (t(H) 2 m(H)) 1
- The amortized cost is
- Actual cost 1 ?(1) 1 ?(1)
18Uniting Two Fibonacci Heaps
This procedure unites heaps H1 and H2, destroying
them in the process. It simply concatenates root
lists and determines the new minimum
node. Fib-Heap-Union(H1,H2) 1 H
Make-Fib-Heap() 2 minH minH1 3
ltconcatenate the root lists H2 and Hgt 4 if
(minH1 NIL) or (minH2 ltgt NIL and
keyminH2 lt keyminH1) 5 minH
minH2 6 nH nH1 nH2 7 ltfree objects
H1 and H2gt 8 return H
19Union Performance
- The components of potential are
- t(H) t(H1) t(H2)
- m(H) m(H1) m(H2)
- The change in potential is
- ?(H) (?(H1) ?(H2)) 0
- The amortized cost is therefore equal to the
actual cost. - The actual cost is ?(1) amortized cost.