FibonacciHeaps 207
FibonacciHeaps 207
‣ preliminaries
‣ insert
‣ extract the minimum
‣ decrease key
‣ bounding the rank
‣ meld and delete
† amortized
2
Fibonacci heaps
3
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.
Remark. Height of Fibonacci heap is Θ(n) in worst case, but it doesn’t use
sink or swim operations.
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
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
36
Fibonacci heap vs. binomial heaps
Observation. If only INSERT and EXTRACT-MIN operations, then all trees are
binomial trees.
B0 B1 B2 B3
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 17 23 21 39 41
mark parent
if it’s a root
30 52
57
Fibonacci heap: decrease key analysis
58
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
† amortized
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