Lec34 CS345
Lec34 CS345
CS345
Lecture 34
Fibonacci Heap - I
1
REPRESENTING ANY TREE
as a binary tree
2
Representing any tree as a binary tree
a
a
b
b c d e
c
d
f g h f e
g
k Left link : child
h
Right link: sibling
k
This representation has many applications
3
Representing any tree as a binary tree
a
a
b
b c d e
c
d
f g h f e
g
Left link : child
h
Right link: sibling
k
4
EVOLUTION OF HEAPS
5
Two fundamental graph problems
Heap
• O() time
Decrease-Label operations
O()
• Extract-smallest-label operation O() time
6
Operations on a Heap
Binary heap Binomial heap Binomial heap Fibonacci Heap
Inspiration :
Binary numbers
7
BINOMIAL TREE
8
What is ?
𝑩 𝑘 −1
𝑩 𝑘 −1
𝑩𝑘
𝑩0 𝑩1 𝑩2 𝑩3
9
What is ?
Height of = ? 𝑘
𝒉 ( 𝑘−1 ) + 1 𝑩 𝑘 −1
No. of nodes in = ? 2𝑘 𝑩 𝑘 −1
𝟐 𝑵 ( 𝑘−1 ) 𝑩𝑘
𝑩4 10
BINOMIAL HEAP
A collection of
Heap ordered Binomial trees
11
Binomial heap of size
Least Most
significant significant
bit bit
𝑛=19
Head
𝑩0 𝑩1 𝑩4
Note that there is at most one Binomial tree of degree in a Binomial heap. 12
Binomial heap of size
𝑛=19
How to implement
this structure ?
Head
3 5 1
9 2 7 4 9
8 7 6 19 23 67
𝑩0 𝑩1 𝑩4
26 91 12 63
34
13
Binomial heap of size
𝑛=19
Head
3 5 1
9 2 7 4 9
8 7 6 19 23 67
𝑩0 𝑩1 𝑩4
26 91 12 63
34
14
Binomial heap of size
𝑛=19
How to access parent?
Head
3 5 1
9 2 7 4 9
8 7 6 19 23 67
𝑩0 𝑩1 𝑩4
26 91 12 63 Degree
Key
Parent
34
Left Right
15
Structure of a node
MERGING
TWO BINOMIAL HEAPS
Spend some time on your own to find out an efficient way for this operation
16
Merging two Binomial Heaps
𝑛=11 𝑛=5
3 6 1 8 5
7 4 9
11 9 17
19 23 67
𝑩0 𝑩1 𝑩3 𝑩0
21
𝑩2
63
17
Merging two Binomial Heaps
𝑛=11 𝑛=5 𝑛=16
4
3 3
8 63 5 1
7 4 9
8 6 8
11 9 17
19 23 67
𝑩0 𝑩 0111 𝑩 21 21 𝑩2 𝑩3
63
3 35 1
69 8 3 4 9
6 85 17 7
9 17 5 11 6 8 19 23 67
11 𝑩2 𝑩 21
3 𝑩2 𝑩3
21 9 17 11 63
21
19
OTHER OPERATIONS
ON A BINOMIAL HEAP
20
Find-min on a Binomial Heap
O() time
Head
3 6 1
7 4 9
11
19 23 67
𝑩0 𝑩1 𝑩3
63
21
Decrease-key on a Binomial Heap
Head
3 6 1
7 4 9
11
19 23 67
𝑩0 𝑩1 𝑩3
63
2
22
Decrease-key on a Binomial Heap
Head
3 6 1
7 4 9
11
2 23 67
𝑩0 𝑩1 𝑩3
19
23
Decrease-key on a Binomial Heap
O() time
Head
3 6 1
2 4 9
11
7 23 67
𝑩0 𝑩1 𝑩3
19
24
Extract-min on a Binomial Heap
2 4 9
11
7 23 67
𝑩0 𝑩1 𝑩3
19
O() time
Head Head
3 6 9 4 2
67 7 23
11
19
𝑩0 𝑩1 𝑩0 𝑩1 𝑩2
26
Insert() on a Binomial Heap
O() time
Head Head
3 6 1 𝑥
7 4 9
11
19 23 67
𝑩0 𝑩1 𝑩3
63
27
Operations on a Heap
Binary heap Binomial heap Binomial heap
Maintain
Find-min() O() O() O() it explicitly
Keep root list as a doubly linked circular list instead of single link list
Keep siblings in a doubly linked circular list instead of single link list
Wake up only when Extract-min is invoked
28
Insert() on a Binomial Heap
Insert()
Head Head
8 6 5 4
7 15 9
11
19 23 67
63
𝑯
29
Insert() on a Binomial Heap
Insert()
Head Head
8 6 5 4
7 15 9
11
19 23 67
63
𝑯
30
Insert() on a Binomial Heap
Insert()
Insert() O() time
Head
Head
8 6 5 4 19
7 15 9
11
19 23 67
63
𝑯
31
Insert() on a Binomial Heap
Insert()
Insert() O() time
Insert()
Head
8 6 5 4 19 2
7 15 9
11
19 23 67
63
𝑯
32
Insert() on a Binomial Heap
Insert()
Insert() O() time
Insert()
Extract-min()
Head Head
8 6 5 4 19 2
7 15 9
11
19 23 67
63
𝑯
Homework: Make sincere attempt to design ways to implement Extract-min in 33
amortized O() time. We shall discuss it in the next class