Fibonacci Heap
Fibonacci Heap
make-heap 1 1 1 1 1
is-empty 1 1 1 1 1
union 1 n log n 1 1
find-min n 1 log n 1 1
make-heap 1 1 1 1 1
is-empty 1 1 1 1 1
union 1 n log n 1 1
find-min n 1 log n 1 1
3
Fibonacci Heaps
Basic idea.
Similar to binomial heaps, but less rigid structure.
Binomial heap: eagerly consolidate trees after each insert.
4
Fibonacci Heaps: Structure
17 24 23 7 3
30 26 46
18 52 41
Heap H 35
39 44
5
Fibonacci Heaps: Structure
Fibonacci heap.
Set of heap-ordered trees.
Maintain pointer to minimum element.
Set of marked nodes.
find-min takes O(1) time
min
17 24 23 7 3
30 26 46
18 52 41
Heap H 35
39 44
6
Fibonacci Heaps: Structure
Fibonacci heap.
Set of heap-ordered trees.
Maintain pointer to minimum element.
Set of marked nodes.
min
17 24 23 7 3
30 26 46
18 52 41
Heap H 35 marked
39 44
7
Fibonacci Heaps: Notation
Notation.
n = number of nodes in heap.
rank(x) = number of children of node x.
rank(H) = max rank of any node in heap H.
trees(H) = number of trees in heap H.
marks(H) = number of marked nodes in heap H.
17 24 23 7 3
30 26 46
18 52 41
Heap H 35 marked
39 44
8
Fibonacci Heaps: Potential Function
17 24 23 7 3
30 26 46
18 52 41
Heap H 35 marked
39 44
9
Insert
10
Fibonacci Heaps: Insert
Insert.
Create a new singleton tree.
Add to root list; update min pointer (if necessary).
insert 21
21
min
17 24 23 7 3
30 26 46
18 52 41
Heap H 35
39 44
11
Fibonacci Heaps: Insert
Insert.
Create a new singleton tree.
Add to root list; update min pointer (if necessary).
insert 21
min
17 24 23 7 21 3
30 26 46
18 52 41
Heap H 35
39 44
12
Fibonacci Heaps: Insert Analysis
min
17 24 23 7 21 3
30 26 46
18 52 41
Heap H 35
39 44
13
Delete Min
14
Linking Operation
15 3 3
56 24 18 52 41 15 18 52 41
77 39 44 56 24 39 44
tree T1 tree T2
77
tree T'
15
Fibonacci Heaps: Delete Min
Delete min.
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
16
Fibonacci Heaps: Delete Min
Delete min.
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
17
Fibonacci Heaps: Delete Min
Delete min.
Delete min; meld its children into root list; update min.
Consolidate trees so that no two roots have same rank.
min
current
7 24 23 17 18 52 41
30 26 46 39 44
35
18
Fibonacci Heaps: Delete Min
Delete min.
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
min
current
7 24 23 17 18 52 41
30 26 46 39 44
35
19
Fibonacci Heaps: Delete Min
Delete min.
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
min
current
7 24 23 17 18 52 41
30 26 46 39 44
35
20
Fibonacci Heaps: Delete Min
Delete min.
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
min
7 24 23 17 18 52 41
30 26 46 current 39 44
35
21
Fibonacci Heaps: Delete Min
Delete min.
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
min
7 24 23 17 18 52 41
30 26 46 current 39 44
35
link 23 into 17
22
Fibonacci Heaps: Delete Min
Delete min.
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
min
7 24 17 18 52 41
30 26 46 current 23 39 44
35
link 17 into 7
23
Fibonacci Heaps: Delete Min
Delete min.
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
24 7 18 52 41
26 46 17 30 39 44
35 23
link 24 into 7
24
Fibonacci Heaps: Delete Min
Delete min.
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
25
Fibonacci Heaps: Delete Min
Delete min.
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
26
Fibonacci Heaps: Delete Min
Delete min.
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
27
Fibonacci Heaps: Delete Min
Delete min.
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
link 41 into 18
35
28
Fibonacci Heaps: Delete Min
Delete min.
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
29
Fibonacci Heaps: Delete Min
Delete min.
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
30
Fibonacci Heaps: Delete Min
Delete min.
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
stop
35
31
Fibonacci Heaps: Delete Min Analysis
Delete min.
(H) = trees(H) + 2 marks(H)
potential function
32
Fibonacci Heaps: Delete Min Analysis
B0 B1 B2 B3
33
Decrease Key
34
Fibonacci Heaps: Decrease Key
min
7 18 38
marked node:
one child already cut
24 17 23 21 39 41
26 46 30 52
35 88 72
35
Fibonacci Heaps: Decrease Key
min
7 18 38
24 17 23 21 39 41
26 46
29 30 52
x
decrease-key of x from 46 to 29
35 88 72
36
Fibonacci Heaps: Decrease Key
min
7 18 38
24 17 23 21 39 41
26 29 30 52
x
decrease-key of x from 46 to 29
35 88 72
37
Fibonacci Heaps: Decrease Key
min
7 18 38
24 17 23 21 39 41
p
26 29
15 30 52
x
decrease-key of x from 29 to 15
35 88 72
38
Fibonacci Heaps: Decrease Key
min
7 18 38
24 17 23 21 39 41
p
26 15 30 52
x
decrease-key of x from 29 to 15
35 88 72
39
Fibonacci Heaps: Decrease Key
x min
15 7 18 38
72 24 17 23 21 39 41
p
26 30 52
decrease-key of x from 29 to 15
35 88
40
Fibonacci Heaps: Decrease Key
x min
15 7 18 38
72 24 17 23 21 39 41
p
mark parent
26 30 52
decrease-key of x from 29 to 15
35 88
41
Fibonacci Heaps: Decrease Key
min
15 7 18 38
72 24 17 23 21 39 41
p 26 30 52
decrease-key of x from 35 to 5
x 35
5 88
42
Fibonacci Heaps: Decrease Key
min
15 7 18 38
72 24 17 23 21 39 41
p 26 30 52
decrease-key of x from 35 to 5
x 5 88
43
Fibonacci Heaps: Decrease Key
min
x
15 5 7 18 38
72 24 17 23 21 39 41
p 26 30 52
decrease-key of x from 35 to 5
88
44
Fibonacci Heaps: Decrease Key
min
x
15 5 7 18 38
p 26 30 52
decrease-key of x from 35 to 5
88
45
Fibonacci Heaps: Decrease Key
min
x p
15 5 26 7 18 38
72 88 24 17 23 21 39 41
30 52
decrease-key of x from 35 to 5
46
Fibonacci Heaps: Decrease Key
min
x p
15 5 26 7 18 38
72 88 p' 24 17 23 21 39 41
decrease-key of x from 35 to 5
47
Fibonacci Heaps: Decrease Key
min
x p p' p''
15 5 26 24 7 18 38
72 88 don't mark 17 23 21 39 41
parent if
it's a root
30 52
decrease-key of x from 35 to 5
48
Fibonacci Heaps: Decrease Key Analysis
Decrease-key.
(H) = trees(H) + 2 marks(H)
potential function
49
Analysis
50
Analysis Summary
Insert. O(1)
Delete-min. O(rank(H)) †
Decrease-key. O(1) †
† amortized
51
Fibonacci Heaps: Bounding the Rank
Lemma. Fix a point in time. Let x be a node, and let y1, …, yk denote
its children in the order in which they were linked to x. Then:
x
0 if i 1
rank (yi )
i 2 if i 1
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(xi) i - 1.
Since then, yi has lost at most one child.
Thus, right now rank(yi) i - 2.
or yi would have been cut
52
Fibonacci Heaps: Bounding the Rank
Lemma. Fix a point in time. Let x be a node, and let y1, …, yk denote
its children in the order in which they were linked to x. Then:
x
0 if i 1
rank (yi )
i 2 if i 1
y1 y2 … yk
Def. Let Fk be smallest possible tree of rank k satisfying property.
F0 F1 F2 F3 F4 F5
1 2 3 5 8 13
53
Fibonacci Heaps: Bounding the Rank
Lemma. Fix a point in time. Let x be a node, and let y1, …, yk denote
its children in the order in which they were linked to x. Then:
x
0 if i 1
rank (yi )
i 2 if i 1
y1 y2 … yk
Def. Let Fk be smallest possible tree of rank k satisfying property.
F4 F5 F6
8 13 8 + 13 = 21
54
Fibonacci Heaps: Bounding the Rank
Lemma. Fix a point in time. Let x be a node, and let y1, …, yk denote
its children in the order in which they were linked to x. Then:
x
0 if i 1
rank (yi )
i 2 if i 1
y1 y2 … yk
Def. Let Fk be smallest possible tree of rank k satisfying property.
55
Fibonacci Numbers
56
Fibonacci Numbers: Exponential Growth
1 if k 0
Fk 2 if k 1 slightly non-standard definition
F F if k 2
k-1 k-2
pinecone
cauliflower
58
Union
59
Fibonacci Heaps: Union
min min
23 24 17 7 3 21
30 26 46
18 52 41
min
23 24 17 7 3 21
30 26 46
18 52 41
35 Heap H
39 44
61
Fibonacci Heaps: Union
min
23 24 17 7 3 21
30 26 46
18 52 41
35 Heap H
39 44
62
Delete
63
Fibonacci Heaps: Delete
Delete node x.
(H) = trees(H) + 2 marks(H)
decrease-key of x to -.
potential function
delete-min element in heap.
64
Priority Queues Performance Cost Summary
make-heap 1 1 1 1 1
is-empty 1 1 1 1 1
union 1 n log n 1 1
find-min n 1 log n 1 1
65