11 Fibonacci
11 Fibonacci
1
Priority Queues
Heaps
Operation Linked List Binary Binomial Fibonacci † Relaxed
make-heap 1 1 1 1 1
insert 1 log N log N 1 1
find-min N 1 log N 1 1
delete-min N log N log N log N log N
union 1 N log N 1 1
decrease-key 1 log N log N 1 1
delete N log N log N log N log N
is-empty 1 1 1 1 1
† amortized
this time
2
Fibonacci Heaps
Fibonacci heap history. Fredman and Tarjan (1986)
Ingenious data structure and analysis.
Original motivation: O(m + n log n) shortest path algorithm.
– also led to faster algorithms for MST, weighted bipartite matching
Still ahead of its time.
3
Fibonacci Heaps: Structure
Fibonacci heap.
Set of min-heap ordered trees.
min
17 24 23 7 3
30 26 46
marked 18 52 41
35
H 39 44
4
Fibonacci Heaps: Implementation
Implementation.
Represent trees using left-child, right sibling pointers and circular,
doubly linked list.
– can quickly splice off subtrees
Roots of trees connected with circular doubly linked list.
– fast union
Pointer to root of tree with min element.
– fast find-min
min
17 24 23 7 3
30 26 46
18 52 41
35
H 39 44
5
Fibonacci Heaps: Potential Function
Key quantities.
Degree[x] = degree of node x.
Mark[x] = mark of node x (black or gray).
t(H) = # trees.
m(H) = # marked nodes.
(H) = t(H) + 2m(H) = potential function.
t(H) = 5, m(H) = 3
(H) = 11
degree = 3 min
17 24 23 7 3
30 26 46
18 52 41
35
H 39 44
6
Fibonacci Heaps: Insert
Insert.
Create a new singleton tree.
Add to left of min pointer.
Update min pointer.
Insert 21
21
min
17 24 23 7 3
30 26 46
18 52 41
35
H 39 44
7
Fibonacci Heaps: Insert
Insert.
Create a new singleton tree.
Add to left of min pointer.
Update min pointer.
Insert 21
min
17 24 23 7 21 3
30 26 46
18 52 41
35
H 39 44
8
Fibonacci Heaps: Insert
Insert.
Create a new singleton tree.
Add to left of min pointer.
Update min pointer.
17 24 23 7 21 3
30 26 46
18 52 41
35
H 39 44
9
Fibonacci Heaps: Union
Union.
Concatenate two Fibonacci heaps.
Root lists are circular, doubly linked lists.
min min
23 24 17 7 3 21
H' H'
30 26 46 '
18 52 41
35
39 44
10
Fibonacci Heaps: Union
Union.
Concatenate two Fibonacci heaps.
Root lists are circular, doubly linked lists.
23 24 17 7 3 21
H' H'
30 26 46 '
18 52 41
35
39 44
11
Fibonacci Heaps: Delete Min
Delete min.
Delete min and concatenate its children into root list.
Consolidate trees so that no two roots have same degree.
min
7 24 23 17 3
30 26 46 18 52 41
35 39 44
12
Fibonacci Heaps: Delete Min
Delete min.
Delete min and concatenate its children into root list.
Consolidate trees so that no two roots have same degree.
current
min
7 24 23 17 18 52 41
30 26 46 39 44
35
13
Fibonacci Heaps: Delete Min
Delete min.
Delete min and concatenate its children into root list.
Consolidate trees so that no two roots have same degree.
0 1 2 3
current
min
7 24 23 17 18 52 41
30 26 46 39 44
35
14
Fibonacci Heaps: Delete Min
Delete min.
Delete min and concatenate its children into root list.
Consolidate trees so that no two roots have same degree.
0 1 2 3
current
min
7 24 23 17 18 52 41
30 26 46 39 44
35
15
Fibonacci Heaps: Delete Min
Delete min.
Delete min and concatenate its children into root list.
Consolidate trees so that no two roots have same degree.
0 1 2 3
min
7 24 23 17 18 52 41
30 26 46 39 44
current
35
16
Fibonacci Heaps: Delete Min
Delete min.
Delete min and concatenate its children into root list.
Consolidate trees so that no two roots have same degree.
0 1 2 3
min
7 24 23 17 18 52 41
30 26 46 39 44
current
35
Merge 17 and 23 trees.
17
Fibonacci Heaps: Delete Min
Delete min.
Delete min and concatenate its children into root list.
Consolidate trees so that no two roots have same degree.
0 1 2 3
current
min
7 24 17 18 52 41
30 26 46 23 39 44
35
Merge 7 and 17 trees.
18
Fibonacci Heaps: Delete Min
Delete min.
Delete min and concatenate its children into root list.
Consolidate trees so that no two roots have same degree.
0 1 2 3
min current
24 7 18 52 41
26 46 17 30 39 44
35 23
Merge 7 and 24 trees.
19
Fibonacci Heaps: Delete Min
Delete min.
Delete min and concatenate its children into root list.
Consolidate trees so that no two roots have same degree.
0 1 2 3
min current
7 18 52 41
24 17 30 39 44
26 46 23
35
20
Fibonacci Heaps: Delete Min
Delete min.
Delete min and concatenate its children into root list.
Consolidate trees so that no two roots have same degree.
0 1 2 3
min current
7 18 52 41
24 17 30 39 44
26 46 23
35
21
Fibonacci Heaps: Delete Min
Delete min.
Delete min and concatenate its children into root list.
Consolidate trees so that no two roots have same degree.
0 1 2 3
min current
7 18 52 41
24 17 30 39 44
26 46 23
35
22
Fibonacci Heaps: Delete Min
Delete min.
Delete min and concatenate its children into root list.
Consolidate trees so that no two roots have same degree.
0 1 2 3
min current
7 18 52 41
24 17 30 39 44
26 46 23
Merge 41 and 18 trees.
35
23
Fibonacci Heaps: Delete Min
Delete min.
Delete min and concatenate its children into root list.
Consolidate trees so that no two roots have same degree.
0 1 2 3
min current
7 52 18
24 17 30 41 39
26 46 23 44
35
24
Fibonacci Heaps: Delete Min
Delete min.
Delete min and concatenate its children into root list.
Consolidate trees so that no two roots have same degree.
0 1 2 3
min current
7 52 18
24 17 30 41 39
26 46 23 44
35
25
Fibonacci Heaps: Delete Min
Delete min.
Delete min and concatenate its children into root list.
Consolidate trees so that no two roots have same degree.
min
7 52 18
24 17 30 41 39
26 46 23 44
Stop.
35
26
Fibonacci Heaps: Delete Min Analysis
Notation.
D(n) = max degree of any node in Fibonacci heap with n nodes.
t(H) = # trees in heap H.
(H) = t(H) + 2m(H).
27
Fibonacci Heaps: Delete Min Analysis
Is amortized cost of O(D(n)) good?
Yes, if only Insert, Delete-min, and Union operations supported.
– in this case, Fibonacci heap contains only binomial trees since
we only merge trees of equal root degree
– this implies D(n) log2 N
Yes, if we support Decrease-key in clever way.
– we'll show that D(n) log N, where is golden ratio
2 = 1 +
= (1 + 5) / 2 = 1.618…
– limiting ratio between successive Fibonacci numbers!
28
Fibonacci Heaps: Decrease Key
Decrease key of element x to k.
Case 0: min-heap property not violated.
– decrease key of x to k
– change heap min pointer if necessary
min
7 18 38
24 17 23 21 39 41
26 46
45 30 52
Decrease 46 to 45.
35 88 72
29
Fibonacci Heaps: Decrease Key
Decrease key of element x to k.
Case 1: parent of x is unmarked.
– decrease key of x to k
– cut off link between x and its parent
– mark parent
– add tree rooted at x to root list, updating heap min pointer
min
7 18 38
24 17 23 21 39 41
26 45
15 30 52
Decrease 45 to 15.
35 88 72
30
Fibonacci Heaps: Decrease Key
Decrease key of element x to k.
Case 1: parent of x is unmarked.
– decrease key of x to k
– cut off link between x and its parent
– mark parent
– add tree rooted at x to root list, updating heap min pointer
min
7 18 38
24 17 23 21 39 41
26 15 30 52
Decrease 45 to 15.
35 88 72
31
Fibonacci Heaps: Decrease Key
Decrease key of element x to k.
Case 1: parent of x is unmarked.
– decrease key of x to k
– cut off link between x and its parent
– mark parent
– add tree rooted at x to root list, updating heap min pointer
min
15 7 18 38
72 24 17 23 21 39 41
26 30 52
Decrease 45 to 15.
35 88
32
Fibonacci Heaps: Decrease Key
Decrease key of element x to k.
Case 2: parent of x is marked.
– decrease key of x to k
– cut off link between x and its parent p[x], and add x to root list
– cut off link between p[x] and p[p[x]], add p[x] to root list
If p[p[x]] unmarked, then mark it.
If p[p[x]] marked, cut off p[p[x]], unmark, and repeat.
min
15 7 18 38
72 24 17 23 21 39 41
26 30 52
Decrease 35 to 5.
35
5 88
33
Fibonacci Heaps: Decrease Key
Decrease key of element x to k.
Case 2: parent of x is marked.
– decrease key of x to k
– cut off link between x and its parent p[x], and add x to root list
– cut off link between p[x] and p[p[x]], add p[x] to root list
If p[p[x]] unmarked, then mark it.
If p[p[x]] marked, cut off p[p[x]], unmark, and repeat.
min
15 5 7 18 38
72 24 17 23 21 39 41
parent marked 26 30 52
Decrease 35 to 5.
88
34
Fibonacci Heaps: Decrease Key
Decrease key of element x to k.
Case 2: parent of x is marked.
– decrease key of x to k
– cut off link between x and its parent p[x], and add x to root list
– cut off link between p[x] and p[p[x]], add p[x] to root list
If p[p[x]] unmarked, then mark it.
If p[p[x]] marked, cut off p[p[x]], unmark, and repeat.
min
15 5 26 7 18 38
72 88 24 17 23 21 39 41
parent marked30 52
Decrease 35 to 5.
35
Fibonacci Heaps: Decrease Key
Decrease key of element x to k.
Case 2: parent of x is marked.
– decrease key of x to k
– cut off link between x and its parent p[x], and add x to root list
– cut off link between p[x] and p[p[x]], add p[x] to root list
If p[p[x]] unmarked, then mark it.
If p[p[x]] marked, cut off p[p[x]], unmark, and repeat.
min
15 5 26 24 7 18 38
72 88 17 23 21 39 41
30 52
Decrease 35 to 5.
36
Fibonacci Heaps: Decrease Key Analysis
Notation.
t(H) = # trees in heap H.
m(H) = # marked nodes in heap H.
(H) = t(H) + 2m(H).
37
Fibonacci Heaps: Delete
Delete node x.
Decrease key of x to -.
Delete min element in heap.
38
Fibonacci Heaps: Bounding Max Degree
Definition. D(N) = max degree in Fibonacci heap with N nodes.
Key lemma. D(N) log N, where = (1 + 5) / 2.
Corollary. Delete and Delete-min take O(log N) amortized time.
Lemma. Let x be a node with degree k, and let y1, . . . , yk denote the children
of x in the order in which they were linked to x. Then:
0 if i 1
degree ( yi )
i 2 if i 1
Proof.
When yi is linked to x, y1, . . . , yi-1 already linked to x,
degree(x) = i - 1
degree(yi) = i - 1 since we only link nodes of equal degree
Since then, yi has lost at most one child
– otherwise it would have been cut from x
Thus, degree(yi) = i - 1 or i - 2
39
Fibonacci Heaps: Bounding Max Degree
Key lemma. In a Fibonacci heap with N nodes, the maximum degree of
any node is at most log N, where = (1 + 5) / 2.
k 2
Fact F2. For k 2, Fk 2 Fi
i 0 sk size ( x*)
k
2 size ( yi )
i 2
Consequence. sk Fk . k
k
2 sdeg[ yi ]
This implies that size(x) degree(x) i 2
for all nodes x. k
2 si 2
i 2
k 2
2 si
i 0
41
Golden Ratio
43
Fibonacci Numbers and Nature
Pinecone
Cauliflower
44
Fibonacci Proofs
k 2
For k 2, F 2 Fi
k
Fact F2. i 0
Proof. (by induction on k)
Fk 2 Fk Fk 1
Base cases: k 2
– F2 = 3, F3 = 5
2 Fi Fk 1
i 0
k
Inductive hypotheses:
k 2 2 Fk
Fk 2 Fi i 0
i 0
45
On Complicated Algorithms
"Once you succeed in writing the programs for [these] complicated
algorithms, they usually run extremely fast. The computer doesn't
need to understand the algorithm, its task is only to run the
programs."
R. E. Tarjan
46