0% found this document useful (0 votes)
9 views33 pages

Lec34 CS345

Uploaded by

shivna0809
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views33 pages

Lec34 CS345

Uploaded by

shivna0809
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Design and Analysis of Algorithms

CS345

Lecture 34
Fibonacci Heap - I

1
REPRESENTING ANY TREE

as a binary tree

2
Representing any tree as a binary tree

Any arbitrary rooted tree 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

Any arbitrary rooted tree 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

Necessity is the mother of invention…

5
Two fundamental graph problems

Minimum Spanning Tree Single Source Shortest Paths


Prim’s Algorithm Dijkstra’s Algorithm

Heap

• O() time
Decrease-Label operations
O()
• Extract-smallest-label operation O() time

O() Optimal algorithm for

6
Operations on a Heap
Binary heap Binomial heap Binomial heap Fibonacci Heap

Find-min() O() O() O() O()

Insert(, ) O() O() O() O()

Extract-min() O() O() O() O()


amortized amortized

Decrease-key(,,) O() O() O() O()


amortized

Merge-heaps(,) O() O() O() O()

Inspiration :
Binary numbers

7
BINOMIAL TREE

Binomial tree of degree

8
What is ?

𝑩 𝑘 −1

𝑩 𝑘 −1
𝑩𝑘

𝑩0 𝑩1 𝑩2 𝑩3
9
What is ?

Height of = ? 𝑘
𝒉 ( 𝑘−1 ) + 1 𝑩 𝑘 −1

No. of nodes in = ? 2𝑘 𝑩 𝑘 −1
𝟐 𝑵 ( 𝑘−1 ) 𝑩𝑘

No. of nodes at depth in = ? ( )


𝑘
𝑖
+

𝑩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

Inspiration: How to fix this problem ?


Addition of binary numbers

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

The only problem is that there are multiple binomial


trees of same degree in the root list.
18
Merging two Binomial Heaps
𝑛=11 𝑛=5 𝑛=16
4

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

Can you exploit this similarity to


perform Extract-min efficiently ?
Do you notice any similarity between
the root list of a binomial heap and
children list of any node?
Head
3 6 1

2 4 9
11

7 23 67
𝑩0 𝑩1 𝑩3
19

The children list, after reversal, can be seen as a


binomial heap.
25
Extract-min on a Binomial Heap

O() time

Merge the two


heaps

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

Merge the two


heaps

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

Insert(, ) O() O() O()

Extract-min() O() O() O()


amortized

Decrease-key(,,) O() O() O() Do these


Operations
Merge-heaps(,) O() O() O() lazily

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()

Merge the two


heaps

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 

You might also like