Fibonacci Heap
Fibonacci Heap
Fibonacci Heaps
2
Fibonacci Heaps
Introduced by Fredman and Tarjan, 1986
Basic idea.
Similar to binomial heaps, but less rigid structure.
Binomial heap: eagerly consolidate trees after each insert.
Fibonacci heap: lazily defer consolidation until next delete-min.
3
7 23
30
17
35
26 46
24
Heap H
39
41 18 52
3
44
Fibonacci Heaps: Structure
Fibonacci heap.
Set of heap-ordered trees.
Maintain pointer to minimum element.
Set of marked nodes.
roots
heap-ordered tree
each parent smaller than its children
4
7 23
30
17
35
26 46
24
Heap H
39
41 18 52
3
44
Fibonacci Heaps: Structure
Fibonacci heap.
Set of heap-ordered trees.
Maintain pointer to minimum element.
Set of marked nodes.
min
5
7 23
30
17
35
26 46
24
Heap H
39
41 18 52
3
44
Fibonacci Heaps: Structure
Fibonacci heap.
Set of heap-ordered trees.
Maintain pointer to minimum element.
Set of marked nodes.
min
use to keep heaps flat (stay tuned)
marked
6
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.
7 23
30
17
35
26 46
24
39
41 18 52
3
44
rank = 3
min
Heap H
trees(H) = 5 marks(H) = 3
marked
n = 14
7
List of Operations
Make-Heap
Insert
Find-Minimum
Extract-Min
Union
8
Make-Heap
Allocates and returns the Fibonacci heap
object H where
n[H]=0
Min[H]=nil
9
Insert
10
Fibonacci Heaps: Insert
Insert.
Create a new singleton tree.
Add to root list; update min pointer (if necessary).
7 23
30
17
35
26 46
24
39
41 18 52
3
44
21
insert 21
min
Heap H
11
Fibonacci Heaps: Insert
Insert.
Create a new singleton tree.
Add to root list; update min pointer (if necessary).
39
41
7 23
18 52
3
30
17
35
26 46
24
44
21
min
Heap H
insert 21
12
Delete Min
13
Linking Operation
Linking operation. Make larger root be a child of smaller root.
39
41 18 52
3
44
77
56 24
15
tree T
1
tree T
2
39
41 18 52
3
44
77
56 24
15
tree T'
smaller root larger root still heap-ordered
14
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.
39
41 18 52
3
44
17 23
30
7
35
26 46
24
min
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.
39
41 17 23 18 52
30
7
35
26 46
24
44
min
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.
39
41 17 23 18 52
30
7
35
26 46
24
44
min
current
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.
39
41 17 23 18 52
30
7
35
26 46
24
44
0 1 2 3
current
min
rank
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.
39
41 17 23 18 52
30
7
35
26 46
24
44
0 1 2 3
min
current
rank
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.
39
41 17 23 18 52
30
7
35
26 46
24
44
0 1 2 3
min
current
rank
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.
39
41 17 23 18 52
30
7
35
26 46
24
44
0 1 2 3
min
current
rank
link 23 into 17
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.
39
41 17
23
18 52
30
7
35
26 46
24
44
0 1 2 3
min
current
rank
link 17 into 7
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.
39
41 7
30
18 52
17
35
26 46
24
44
0 1 2 3
23
current
min
rank
link 24 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.
39
41 7
30
18 52
23
17
35
26 46
24 44
0 1 2 3
min
current
rank
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.
39
41 7
30
18 52
23
17
35
26 46
24 44
0 1 2 3
min
current
rank
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.
39
41 7
30
18 52
23
17
35
26 46
24 44
0 1 2 3
min
current
rank
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.
39
41 7
30
18 52
23
17
35
26 46
24 44
0 1 2 3
min
current
rank
link 41 into 18
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.
39 41
7
30
18 52
23
17
35
26 46
24
44
0 1 2 3
min
current
rank
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.
7
30
52
23
17
35
26 46
24
0 1 2 3
min
rank
39 41
18
44
current
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.
7
30
52
23
17
35
26 46
24
min
39 41
18
44
stop
30
Decrease Key
31
Intuition for deceasing the key of node x.
If heap-order is not violated, just decrease the key of x.
Otherwise, cut tree rooted at x and meld into root list.
To keep trees flat: as soon as a node has its second child cut,
cut it off and meld into root list (and unmark it).
24
46
17
30
23
7
88
26
21
52
39
18
41
38
72
Fibonacci Heaps: Decrease Key
35
min
marked node:
one child already cut
32
Case 1. [heap order not violated]
Decrease key of x.
Change heap min pointer (if necessary).
24
46
17
30
23
7
88
26
21
52
39
18
41
38
72
Fibonacci Heaps: Decrease Key
29
35
min
x
decrease-key of x from 46 to 29
33
Case 1. [heap order not violated]
Decrease key of x.
Change heap min pointer (if necessary).
24
29
17
30
23
7
88
26
21
52
39
18
41
38
72
Fibonacci Heaps: Decrease Key
35
min
x
decrease-key of x from 46 to 29
34
Case 2a. [heap order violated]
Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
24
29
17
30
23
7
88
26
21
52
39
18
41
38
72
Fibonacci Heaps: Decrease Key
15
35
min
decrease-key of x from 29 to 15
p
x
35
Case 2a. [heap order violated]
Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
24
15
17
30
23
7
88
26
21
52
39
18
41
38
72
Fibonacci Heaps: Decrease Key
35
min
decrease-key of x from 29 to 15
p
x
36
Case 2a. [heap order violated]
Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
24 17
30
23
7
88
26
21
52
39
18
41
38
Fibonacci Heaps: Decrease Key
35
min
decrease-key of x from 29 to 15
p
15
72
x
37
Case 2a. [heap order violated]
Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
24 17
30
23
7
88
26
21
52
39
18
41
38
Fibonacci Heaps: Decrease Key
35
min
decrease-key of x from 29 to 15
p
15
72
x
mark parent
24
38
35
Case 2b. [heap order violated]
Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
24
15
17
30
23
7
88
26
21
52
39
18
41
38
72 24
Fibonacci Heaps: Decrease Key
5
min
x
p
decrease-key of x from 35 to 5
39
5
Case 2b. [heap order violated]
Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
24
15
17
30
23
7
88
26
21
52
39
18
41
38
72 24
Fibonacci Heaps: Decrease Key
min
x
p
decrease-key of x from 35 to 5
40
Fibonacci Heaps: Decrease Key
24 17
30
23
7
26
21
52
39
18
41
38
24
5
88
15
72
decrease-key of x from 35 to 5
x
p
min
Case 2b. [heap order violated]
Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
41
Case 2b. [heap order violated]
Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
24 17
30
23
7
26
21
52
39
18
41
38
24
5
Fibonacci Heaps: Decrease Key
88
15
72
decrease-key of x from 35 to 5
x
p
second child cut
min
42
Case 2b. [heap order violated]
Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
24
26
17
30
23
7
21
52
39
18
41
38
88 24
5
Fibonacci Heaps: Decrease Key
15
72
decrease-key of x from 35 to 5
x p
min
43
Case 2b. [heap order violated]
Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
24
26
17
30
23
7
21
52
39
18
41
38
88 24
5
Fibonacci Heaps: Decrease Key
15
72
decrease-key of x from 35 to 5
x p
p'
second child cut
min
44
Case 2b. [heap order violated]
Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
26
17
30
23
7
21
52
39
18
41
38
88
5
Fibonacci Heaps: Decrease Key
15 24
72
decrease-key of x from 35 to 5
x p p'
min
don't mark
parent if
it's a root
p''
45
Union
46
Fibonacci Heaps: Union
Union. Combine two Fibonacci heaps.
Representation. Root lists are circular, doubly linked lists.
39
41
7 17
18 52
3
30
23
35
26 46
24
44
21
min min
Heap H' Heap H''
47
Fibonacci Heaps: Union
Union. Combine two Fibonacci heaps.
Representation. Root lists are circular, doubly linked lists.
39
41
7 17
18 52
3
30
23
35
26 46
24
44
21
min
Heap H
48
Fibonacci Heaps: Union
Actual cost. O(1)
Change in potential. 0
Amortized cost. O(1)
(H) = trees(H) + 2
marks(H)
potential function
39
41
7 17
18 52
3
30
23
35
26 46
24
44
21
min
Heap H
49
Delete
50
Delete node x.
decrease-key of x to -.
delete-min element in heap.
Fibonacci Heaps: Delete