Professional Documents
Culture Documents
FibonacciHeaps PDF
FibonacciHeaps PDF
‣ preliminaries
‣ insert
‣ extract the minimum
‣ decrease key
‣ bounding the rank
‣ meld and delete
Fibonacci heap
operation linked list binary heap binomial heap
†
2
Fibonacci heaps
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- 3
rithms; network problems; trees
Fibonacci heaps
History.
・Ingenious data structure and application of amortized analysis.
・Original motivation: improve Dijkstra’s shortest path algorithm
from O(m log n) to O(m + n log n).
・Also improved best-known bounds for all-pairs shortest paths,
assignment problem, minimum spanning trees.
4
F IBONACCI H EAPS
‣ structure
‣ insert
‣ extract the minimum
‣ decrease key
‣ bounding the rank
‣ meld and delete
SECTION 19.1
Fibonacci heaps
Basic idea.
・Similar to binomial heaps, but less rigid structure.
・Binomial heap: eagerly consolidate trees after each INSERT;
implement DECREASE-KEY by repeatedly exchanging node with its parent.
6
Fibonacci heap: structure
heap-ordered tree
17 24 23 7 3 root
30 26 46
18 52 41
heap H
35
39 44
7
Fibonacci heap: structure
min
17 24 23 7 3
30 26 46
18 52 41
heap H marked
35
39 44
8
Fibonacci heap: structure
Heap representation.
・Store a pointer to the minimum node.
・Maintain tree roots in a circular, doubly-linked list.
min
17 24 23 7 3
30 26 46
18 52 41
heap H
35
39 44
9
Fibonacci heap: representation
min
17 24 23 7 3 rank = 3
30 26 46
18 52 41
children are in a
heap H
35 circular doubly-linked list
39 44
10
Fibonacci heap: representation
min
17 24 23 7 3 rank = 3
30 26 46
18 52 41
heap H
35
39 44
11
Fibonacci heap: notation
notation meaning
n number of nodes
rank = 1 17 24 23 7 3 rank = 3
30 26 46
18 52 41
heap H
35
39 44
12
Fibonacci heap: potential function
Potential function.
17 24 23 7 3
30 26 46
18 52 41
heap H
35
39 44
13
F IBONACCI H EAPS
‣ preliminaries
‣ insert
‣ extract the minimum
‣ decrease key
‣ bounding the rank
‣ meld and delete
SECTION 19.2
Fibonacci heap: insert
insert 21
21
min
17 24 23 7 3
30 26 46
18 52 41
heap H
35
39 44
15
Fibonacci heap: insert
insert 21
min
17 24 23 7 21 3
30 26 46
18 52 41
heap H
35
39 44
16
Fibonacci heap: insert analysis
min
17 24 23 7 21 3
30 26 46
18 52 41
heap H
35
39 44
17
F IBONACCI H EAPS
‣ preliminaries
‣ insert
‣ extract the minimum
‣ decrease key
‣ bounding the rank
‣ meld and delete
SECTION 19.2
Linking operation
15 3 3
56 24 33 18 52 41 15 18 52 41
77 39 44 56 24 33 39 44
77
still heap-ordered
19
Fibonacci heap: extract the minimum
・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.
min
7 24 23 17 3
30 26 46
18 52 41
35 39 44
20
Fibonacci heap: extract the minimum
・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.
min 7 24 23 17 18 52 41
30 26 46 39 44
35
21
Fibonacci heap: extract the minimum
・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.
current
min 7 24 23 17 18 52 41
30 26 46 39 44
35
22
Fibonacci heap: extract the minimum
・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.
rank
0 1 2 3
current
min 7 24 23 17 18 52 41
30 26 46 39 44
35
23
Fibonacci heap: extract the minimum
・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.
rank
0 1 2 3
current
min 7 24 23 17 18 52 41
30 26 46 39 44
35
24
Fibonacci heap: extract the minimum
・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.
rank
0 1 2 3
current
min 7 24 23 17 18 52 41
30 26 46 39 44
35
25
Fibonacci heap: extract the minimum
・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.
rank
link 23 to 17 0 1 2 3
current
min 7 24 23 17 18 52 41
30 26 46 39 44
35
26
Fibonacci heap: extract the minimum
・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.
rank
link 17 to 7 0 1 2 3
current
min 7 24 17 18 52 41
30 26 46 23 39 44
35
27
Fibonacci heap: extract the minimum
・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.
rank
link 24 to 7 0 1 2 3
current
min
24 7 18 52 41
26 46 17 30 39 44
35 23
28
Fibonacci heap: extract the minimum
・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.
rank
0 1 2 3
current
min
7 18 52 41
24 17 30 39 44
26 46 23
35
29
Fibonacci heap: extract the minimum
・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.
rank
0 1 2 3
current
min
7 18 52 41
24 17 30 39 44
26 46 23
35
30
Fibonacci heap: extract the minimum
・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.
rank
0 1 2 3
current
min
7 18 52 41
24 17 30 39 44
26 46 23
35
31
Fibonacci heap: extract the minimum
・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.
rank
link 41 to 18 0 1 2 3
current
min
7 18 52 41
24 17 30 39 44
26 46 23
35
32
Fibonacci heap: extract the minimum
・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.
rank
0 1 2 3
current
min
7 52 18
24 17 30 41 39
26 46 23 44
35
33
Fibonacci heap: extract the minimum
・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.
rank
0 1 2 3
current
min
7 52 18
24 17 30 41 39
26 46 23 44
35
34
Fibonacci heap: extract the minimum
・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.
min
7 52 18
24 17 30 41 39
26 46 23 44
35
35
Fibonacci heap: extract the minimum analysis
Amortized cost. O(log n).
・ĉi = ci + ∆Φ = O(rank(H)) + O(rank(Hʹ)).
・The rank of a Fibonacci heap with n elements is O(log n).
Fibonacci lemma
(stay tuned)
36
Fibonacci heap vs. binomial heaps
Observation. If only INSERT and EXTRACT-MIN operations, then all trees are
binomial trees.
we link only trees of equal rank
B0 B1 B2 B3
Binomial heap property. This implies rank(H) ≤ log2 n.
Fibonacci heap property. Our DECREASE-KEY implementation will not preserve
this property, but we will implement it in such a way that rank(H) ≤ logφ n.
37
F IBONACCI H EAPS
‣ preliminaries
‣ insert
‣ extract the minimum
‣ decrease key
‣ bounding the rank
‣ meld and delete
SECTION 19.3
Fibonacci heap: decrease key
decrease-key of x from 30 to 7
8 29 10 44
30 23 22 48 31 17
45 32 24 50
55
39
Fibonacci heap: decrease key
decrease-key of x from 23 to 5
6 7
8 29 10 44 45 32
23 22 48 31 17 55
24 50
40
Fibonacci heap: decrease key
decrease-key of 22 to 4
decrease-key of 48 to 3
decrease-key of 31 to 2 6 7 5
decrease-key of 17 to 1
8 29 10 44 45 32 24
22 48 31 17 55
50
41
Fibonacci heap: decrease key
6 7 5 4 3 2 1
8 29 10 44 45 32 24 50
rank = 4, nodes = 5
55
42
Fibonacci heap: decrease key
min
7 18 38
marked node:
24 17 23 21 39 41
one child already cut
26 46 30 52
35 88 72
43
Fibonacci heap: decrease key
decrease-key of x from 46 to 29
min
7 18 38
24 17 23 21 39 41
26 46 30 52
35 88 72
44
Fibonacci heap: decrease key
decrease-key of x from 46 to 29
min
7 18 38
24 17 23 21 39 41
26 29 30 52
35 88 72
45
Fibonacci heap: decrease key
decrease-key of x from 29 to 15
min
7 18 38
24 17 23 21 39 41
p
26 29 30 52
35 88 72
46
Fibonacci heap: decrease key
decrease-key of x from 29 to 15
min
7 18 38
24 17 23 21 39 41
p
26 15 30 52
35 88 72
47
Fibonacci heap: decrease key
decrease-key of x from 29 to 15
min
7 18 38
24 17 23 21 39 41
p
26 15 30 52
35 88 72
48
Fibonacci heap: decrease key
decrease-key of x from 29 to 15
min
x 15 7 18 38
72 24 17 23 21 39 41
p
26 30 52
35 88
49
Fibonacci heap: decrease key
decrease-key of x from 29 to 15
min
x 15 7 18 38
72 24 17 23 21 39 41
p
26 30 52
35 88
50
Fibonacci heap: decrease key
decrease-key of x from 35 to 5
min
15 7 18 38
72 24
24 17 23 21 39 41
p 26 30 52
x 35 88
51
Fibonacci heap: decrease key
decrease-key of x from 35 to 5
min
15 7 18 38
72 24
24 17 23 21 39 41
p 26 30 52
x 5 88
52
Fibonacci heap: decrease key
decrease-key of x from 35 to 5
x min
15 5 7 18 38
72 24
24 17 23 21 39 41
p 26 30 52
88
53
Fibonacci heap: decrease key
decrease-key of x from 35 to 5
x min
15 5 7 18 38
72 24
24 17 23 21 39 41
second child cut
p 26 30 52
88
54
Fibonacci heap: decrease key
decrease-key of x from 35 to 5
x min p
15 5 26 7 18 38
72 88 24
24 17 23 21 39 41
30 52
55
Fibonacci heap: decrease key
decrease-key of x from 35 to 5
x min p
15 5 26 7 18 38
72 88 p′ 24
24 17 23 21 39 41
56
Fibonacci heap: decrease key
decrease-key of x from 35 to 5
x min p p′ p″
15 5 26 24 7 18 38
but don’t
72 88 mark parent 17 23 21 39 41
if it’s a root
30 52
57
Fibonacci heap: decrease key analysis
・ΔΦ ≤ c + 2 ⋅ (-c + 2) = 4 – c.
Amortized cost. ĉi = ci + ∆Φ = O(1).
58
F IBONACCI H EAPS
‣ preliminaries
‣ insert
‣ extract the minimum
‣ decrease key
‣ bounding the rank
‣ meld and delete
SECTION 19.4
Analysis summary
Insert. O(1).
Delete-min. O(rank(H)) amortized.
Decrease-key. O(1) amortized.
Fibonacci lemma. Let H be a Fibonacci heap with n elements.
Then, rank(H) = O(log n).
number of nodes is
exponential in rank
60
Bounding the rank
Lemma 1. Fix a point in time. Let x be a node of rank k, and let y1, …, yk
denote its current children in the order in which they were linked to x.
Then:
x
0 B7 i = 1
rank(yi )
i 2 B7 i 2
y1 y2 … yk
Pf.
・When yi was linked into x, x had at least i – 1 children y1, …, yi–1.
・Since only trees of equal rank are linked, at that time
rank(yi) = rank(x) ≥ i – 1.
・Since then, yi has lost at most one child (or yi would have been cut).
・Thus, right now rank(yi) ≥ i – 2. ▪
61
Bounding the rank
Lemma 1. Fix a point in time. Let x be a node of rank k, and let y1, …, yk
denote its current children in the order in which they were linked to x.
Then:
x
0 B7 i = 1
rank(yi )
i 2 B7 i 2
y1 y2 … yk
T0 T1 T2 T3 T4 T5
F2 = 1 F3 = 2 F4 = 3 F5 = 5 F6 = 8 F7 = 13
62
Bounding the rank
Lemma 1. Fix a point in time. Let x be a node of rank k, and let y1, …, yk
denote its current children in the order in which they were linked to x.
Then:
x
0 B7 i = 1
rank(yi )
i 2 B7 i 2
y1 y2 … yk
T4 T5 T6
F6 = 8 F7 = 13 F8 = F6 + F7 = 8 + 13 = 21
63
Bounding the rank
64
Bounding the rank
65
Fibonacci fact 1
66
Fibonacci fact 2
≥ φk – 1 + φk – 2 (inductive hypothesis)
= φk – 2 (1 + φ) (algebra)
= φk – 2 φ2 (φ2 = φ + 1)
= φk. ▪ (algebra)
67
Fibonacci numbers and nature
pinecone
cauliflower
68
F IBONACCI H EAPS
‣ preliminaries
‣ insert
‣ extract the minimum
‣ decrease key
‣ bounding the rank
‣ meld and delete
min min
23 24 17 7 3 21
30 26 46
18 52 41
heap H1 heap H2
35
39 44
70
Fibonacci heap: meld
min
23 24 17 7 3 21
30 26 46
18 52 41
35 heap H
39 44
71
Fibonacci heap: meld analysis
min
23 24 17 7 3 21
30 26 46
18 52 41
35 heap H
39 44
72
Fibonacci heap: delete
73
Priority queues performance cost summary
Fibonacci heap
operation linked list binary heap binomial heap
†
† amortized
Accomplished. O(1) INSERT and DECREASE-KEY, O(log n) EXTRACT-MIN.
74
P RIORITY Q UEUES
‣ binary heaps
‣ d-ary heaps
‣ binomial heaps
‣ Fibonacci heaps
‣ advanced topics
Heaps of heaps
・b-heaps.
・Fat heaps.
・2–3 heaps.
・Leaf heaps.
・Thin heaps.
・Skew heaps.
・Splay heaps.
・Weak heaps.
・Leftist heaps.
・Quake heaps.
・Pairing heaps.
・Violation heaps.
・Run-relaxed heaps.
・Rank-pairing heaps.
・Skew-pairing heaps.
・Rank-relaxed heaps.
・Lazy Fibonacci heaps. 76
Brodal queues
Q. Can we achieve same running time as for Fibonacci heap but with
worst-case bounds per operation (instead of amortized)?
Theory. [Brodal 1996] Yes.
Practice. Ever implemented? Constants are high (and requires RAM model).
77
Strict Fibonacci heaps
Q. Can we achieve same running time as for Fibonacci heap but with
worst-case bounds per operation (instead of amortized) in pointer model?
Theory. [Brodal–Lagogiannis–Tarjan 2012] Yes.
79
Pairing heaps
80
Pairing heaps
Practice. As fast as (or faster than) the binary heap on some problems.
Included in GNU C++ library and LEDA.
RESEARCHCONlRlWlIONS
Algorithms and
Data Structures Pairing Heaps:
Experiments and Analysis
G. Scott Graham
Editor
ABSTRACT: The pairing heap has recently been and practical importance from their use in solving a
introduced as a new data structure for priority queues. wide range of combinatorial problems, including job
Pairing heaps are extremely simple to implement and scheduling, minimal spanning tree, shortest path,
seem to be very efficient in practice, but they are difficult and graph traversal.
to analyze theoretically, and open problems remain. It Priority queues support the operations insert,
has been conjectured that they achieve the same find-min, and delete-min; additional operations often
amortized time bounds as Fibonacci heaps, namely, include decrease-key and delete. The insert(t, v) opera-
O(log n) time for delete and delete-min and O(1) for tion adds item t with key value v to the priority
all other operations, where n is the size of the priority queue. The find-min operation returns the item
queue at the time of the operation. We provide empirical with minimum key value. The delete-min operation
evidence that supports this conjecture. The most returns the item with minimum key value and
promising algorithm in our simulations is a new variant removes it from the priority queue. The decrease-
of the twopass method, called auxiliary twopass. We key(t, d) operation reduces item t’s key value by d.
prove that, assuming no decrease-key operations are The delete(t) operation removes item t from the
performed, it achieves the same amortized time bounds as priority queue. The decrease-key and delete opera-
Fibonacci heaps. tions require that a pointer to the location in the
priority queue of item t be supplied explicitly, since
1. INTRODUCTION priority queues do not support searching for arbi-
A priority queue is an abstract data type for main- trary items by value. Some priority queues also sup-
taining and manipulating a set of items based on port the merge operation, which combines two item-
priority [I]. Prio’rity queues derive great theoretical disjoint priority queues. 81
Support was provided in part by NSF research grant DCR-84-03613, an NSF
We will concentrate on the insert, delete-min, and
Presidential Young Investigator Award, an IBM Faculty Development Award, decrease-key operations because they are the opera-
Priority queues performance cost summary
† amortized
82
Priority queues with integer priorities
ARTICLE IN PRESS
Abstract
We consider Fibonacci heap style integer priority queues supporting find-min, insert, and decrease key
operations in constant time. We present a deterministic linear space solution that with n integer keys
supports delete in Oðlog log nÞ time. If the integers are in the range ½0; NÞ; we can also support delete in
Oðlog log NÞ time.
Even for the special case of monotone priority queues, where the minimum has to be non-decreasing, the
best previous bounds on delete were Oððlog nÞ1=ð3$eÞ Þ and Oððlog NÞ1=ð4$eÞ Þ: These previous bounds used
both randomization and amortization. Our new bounds are deterministic, worst-case, with no restriction to
monotonicity, and exponentially faster. 83
As a classical application, for a directed graph with n nodes and m edges with non-negative integer
Priority queues with integer priorities
Theorem. [Thorup 2004] There exists a priority queue that supports INSERT,
FIND-MIN, and DECREASE-KEY in constant time and EXTRACT-MIN and DELETE-KEY
in either O(log log n) or O(log log C) time.
Corollary 1. Can implement Dijkstra’s algorithm in either O(m log log n) or
O(m log log C) time.
84
Soft heaps
elements inserted 0 11 22 44 55
88 11 22 99 44 33 77 66 0 55
57 66 77 88 99
soft heap
corrupted
85
Soft heaps
86
Soft heaps
Theorem. [Chazelle 2000] Starting from an empty soft heap, any sequence
of n INSERT, MIN, EXTRACT-MIN, MELD, and DELETE operations takes O(n) time
and at most 10% of its elements are corrupted at any given time.
BERNARD CHAZELLE
Princeton University, Princeton, New Jersey, and NEC Research Institute
Abstract. A simple variant of a priority queue, called a soft heap, is introduced. The data structure
supports the usual operations: insert, delete, meld, and findmin. Its novelty is to beat the logarithmic
bound on the complexity of a heap in a comparison-based model. To break this information-theoretic
barrier, the entropy of the data structure is reduced by artificially raising the values of certain keys.
Given any mixed sequence of n operations, a soft heap with error rate ! (for any 0 ! ! " 1/2) ensures
that, at any time, at most !n of its items have their keys raised. The amortized complexity of each
operation is constant, except for insert, which takes O(log 1/!) time. The soft heap is optimal for any
value of ! in a comparison-based model. The data structure is purely pointer-based. No arrays are
used and no numeric assumptions are made on the keys. The main idea behind the soft heap is to
move items across the data structure not individually, as is customary, but in groups, in a
data-structuring equivalent of “car pooling.” Keys must be raised as a result, in order to preserve the
heap ordering of the data structure. The soft heap can be used to compute exact or approximate
medians and percentiles optimally. It is also useful for approximate sorting and for computing
minimum spanning trees of general graphs.
Categories and Subject Descriptors: E.1 [Data Structures]: Nonnumerical Algorithms and Problems 87
General Terms: Theory
Soft heaps
88
Soft heaps
Theorem. [Chazelle 2000] There exists an O(m α(m, n)) time deterministic
algorithm to compute an MST in a graph with n nodes and m edges.
BERNARD CHAZELLE
Princeton University, Princeton, New Jersey, and NEC Research Institute
Abstract. A deterministic algorithm for computing a minimum spanning tree of a connected graph is
presented. Its running time is O(m ! (m, n)), where ! is the classical functional inverse of
Ackermann’s function and n (respectively, m) is the number of vertices (respectively, edges). The
algorithm is comparison-based: it uses pointers, not arrays, and it makes no numeric assumptions on
the edge costs.
Categories and Subject Descriptors: F.2.2 [Analysis of Algorithms and Problem Complexity]:
Nonnumerical Algorithms and Problems.
General Terms: Theory
Additional Key Words and Phrases: Graphs, matroids, minimum spanning trees 89