100% found this document useful (3 votes)
2K views

Binomial & Fibonacci Heap Advanced)

Binomial heaps are collections of binomial trees that satisfy two properties: 1) no two trees have the same degree, and 2) each node's key is less than or equal to its parent's key. A binomial tree is recursively defined, with Bk consisting of two Bk-1 trees linked together. Operations like finding the minimum key, inserting a key, and deleting the minimum key take O(log n) time due to the heap structure. Two binomial heaps can be united by merging their root lists in O(log n) time.

Uploaded by

api-3801329
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
2K views

Binomial & Fibonacci Heap Advanced)

Binomial heaps are collections of binomial trees that satisfy two properties: 1) no two trees have the same degree, and 2) each node's key is less than or equal to its parent's key. A binomial tree is recursively defined, with Bk consisting of two Bk-1 trees linked together. Operations like finding the minimum key, inserting a key, and deleting the minimum key take O(log n) time due to the heap structure. Two binomial heaps can be united by merging their root lists in O(log n) time.

Uploaded by

api-3801329
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 60

Binomial Heap

Shahid Iqbal
(05-0978)
Binomial Heap (Vuillemin, 1978)

A binomial heap is a collection of binomial trees that satisfy the


following binomial-heap properties:

3. No two binomial trees in the collection have the same


degree (number of children of root node).

2. Each binomial tree in the collection is heap-ordered in the


sense that each non-root has a key strictly less than or equal
to the key of its parent.
Structure of Binomial Heap

B0 B1 B3

4 5 1

8 3 4 6

7 9 10
i a l
11
B nom
i
a l id
a v eap
Still H
B2 is missing
Binomial Tree

A binomial tree is an ordered tree that is defined


recursively
•B0 consists of a single node.
•Bk consists of two trees ( Bk-1, Bk-1) that are linked by
making one as the leftmost child of the other.
B2
Or B3
de
of red
se ev m
qu e e B2
en ry an
fro ce no s c
m B de hil
lef k-1 … are dre
tt
o r .B in n
ig 1 , B
ht 0
Properties of Binomial Tree
1. Let ‘n’ be total number of nodes in the tree, then
n = 2k,

B0 20 = 1 B1 21 = 2 B3 23 = 8

Co
n str he l
uc t
is mia
tiv ‘ ’
k ino
ep e
er of b
roo h
W ree
f
deg
tree
Contd.
Properties of Binomial Tree
2. Height of a binomial tree is equal to ‘k’, where k
is degree of the binomial tree ( No. of children of
Root ).
• In case of melding two trees, their degree must be
same.
• Whenever two trees are merged/melded, Height
of the resultant tree is one greater that that of
Al original trees.
tre so, B2
“lo e is heig l
e ,y to
g n ex ht s i v
r , B0
” act of B B1 c u
re s , B 0
ly B1
k
eq ino i n
Th ld tre ……
e . B k-1
ua mi
l t al
o Me 2, B 3
, B
B1
Contd.
Properties of Binomial Tree
k 
3. There are exactly   nodes at ith level.
i 
• A property useful for naming the data structure.
B3  3
  = 1
Level 0  0

 3
Level 1   = 3
 1

Level 2  3
  = 3
 2
Level 3  3
  = 1
 3
Contd.
Properties of Binomial Tree

4. Deleting root of a Binomial tree Bk yields


binomial trees Bk-1, … , B0 in sequence.

B2 B1 B0
B3

r e d
r d e tree
O ial
n o m
Bi
Binomial Heap

B0 B1 B2 B3
H

Head P
ointer
to the f k of Heap
irst n=o 3
de of t points
list onl he Roo
y. t

Tree Roots linked together using singly linked list,


sorted by their degrees, in ascending order
Operations on Binomial Heap

Find Minimum Key

Uniting two Binomial Heaps

Insert new Key

Delete Minimum Key

Decrease Key

Delete Key
Find Minimum Key

Algorithm

3. If heap is empty, simply stop execution.

5. Else, assign the key of the first root in the Root


list to Min variable.

7. Move till the end of Root list by comparing keys


with Min and update Min if needed.
Find Minimum Key

Min = 10 Min = 7 Min = 1


B0 B1 B2 B3
10 7 1 5

9 2 3 6 7 8
Ke
sm Kyeyin
alle incu
sm 4 50
allre, s cruren 12 11
r,ismp rretnn
imlpy t onde
‘m lyupd odeis
‘min’ upate is 20
in’ dateth
tehe
Key in current
Clearly it takesnode
“logisn”
greater,
time tonofind
need to key,
min change.
As
Also can
there Rootbelist is ended “log
maximum so value of ‘Min’trees
n” binomial is thein
minimum thevalue
Rootoflist
the heap.
Uniting two Binomial Heaps

Algorithm

4. Merge the Root lists of both the heaps using


Merge routine of Merge Sort algorithm

6. Ensure the property of Binomial Heap, there can


not be two Binomial trees of same degree in root
list.

Step 1 may introduce the violation of Binomial


heap property, therefore Step2 is needed.
Uniting two Binomial Heaps cont.

1. Merge the Root lists of both the heaps using


Merge algorithm of Merge sort

H1 10 7 5

9 6 7 8

12 11 50

20
H2 50 19 1 15

21 2 3 16 17 18

4 22 31 50

60
Merge cont.

Heap after Merge routine

10 7 5
H1
9 6 7 8

12 11 50

20

H2 R 50 19 1 15
As oot
cen s a 21 2 3 16 17 18
r
din e m
g o er
rde ged it takes “log n” time
de Clearly, 4 22 31 50
to merge two heaps.
gre r b in
es y t Where log n = log n + log 60 n2
he 1
ir
Uniting two Binomial Heaps cont.

2. Ensure the property of Binomial Heap, there can not be


two Binomial trees of same degree in root list.

10 50 7 19 1 5 15

9 21 2 3 6 7 8 16 17 18

De 4 12 11 50 22 31 50
gre
eo 20 60
sam f tw
et ot
o M ree
eld s m
the ust
m be
Ensuring property of Binomial Heap cont.

There are two nodes of


Degree ‘0’, lets meld them

10 50 7 19 1 5 15

9 21 2 3 6 7 8 16 17 18

No 4 12 11 50 22 31 50
de
wi ’10
ll b ’ 20 60
eco is s
me mal
the ler,
roo so i
t… t
Ensuring property of Binomial Heap cont.

There are three nodes of


Degree ‘1’, lets meld them

B1 B1 B1
10 7 19 1 5 15
50 9 21 2 3 6 7 8 16 17 18
If
dhee tNheorte 4 12 11 50 22 31 50
g
an arepe, ,t :awreh
d m hsiem ‘i3le’ 20 60
elodf repm l
m
tre
s
o y aly eldsi
a
mi thmee eaevx onfg s
n a r tdw eistt tawm
s r egor, h3e oe
oo eme tfrier
t… ak sets
ing
Ensuring property of Binomial Heap cont.

There are two nodes of


Degree ‘2’, lets meld them
B2 B2
10 7 1 5 15

50 19 9 2 3 6 7 8 16 17 18

Sim 21 4 12 11 50 22 31 50
ply
mo mak 20 60
st e m
ch
ild ax-tr
of ee Now there are three trees of
oth as
l ef
er degree ‘3’. Leave first one
t
and merge other two
Ensuring property of Binomial Heap cont.

10 1 5

50 7 2 3 15 6 7 8

19 9 4 16 17 18 12 11 50
Fin
all 21 22 31 50 20
y,
it h
Bin as b 60
om eco
ial m
He e a v
ap stepalalso take log n time, therefore total time
This id
of both the steps is “log n + log n” which is O (log
n)
Insert Key

Let us assume that H2 is an existing Binomial Heap


and we want to insert ‘x’ in it.

Algorithm,

5. Create a single node Binomial Heap H1


consisting of ‘x’.

7. Union( H1, H2),


Insert Key cont.

H2 10
Let x = 10

H1 50 19 1 15

21 2 3 16 17 18
SteCre
Bip1:ate a 4 22 31 50
)
co nomM e sin , H 2
nsi ial rge gle 60 1
sti He tw no
ng n(H
ap o Hde n i o
of H1 ea U
‘Recall
x’. theps 2 steps of Union w
N o
Insert Key cont.

Union ( H1, H2 ) continued…

H
B0 B01 B12 B23
B B3
4

10 10
50 19
10 1 15
50 21
50 2 3 17 18
19 16
Ste
p2 4
:E 21 22 31 50
nsu
re
Pro Bin 60
pe om
rty ial
Maximum
He cost in Insert operation is of Union,
ap
Final also
therefore, Insert Binomial
takes Heap
O (log n) time
Delete Minimum Key
Let us assume that H1 is an existing Binomial Heap
and we want to delete minimum key.

Algorithm,

5. Find minimum key using Find Minimum routine.


6. Extract Binomial tree containing the Minimum
key, from the Root list of Binomial Heap
7. Connect all the children of Root of Extracted
Binomial tree in ascending order of their degrees
and delete the Root. Now it is a Binomial heap,
say H2.
8. Union( H1, H2),
Delete Minimum Key cont.

Find Minimum key and Extracting min Binomial tree


Min = 10 Min = 7 MinMinimum
=1 key

B0 B1 B2 B3
H1 10 7 1 5

Ke Ke 9 2 3 6 7 8
sm y inyc in 1
sailmle urrecur
prly, s nt nren
uipm odet n 4 12 11 50
Ex d aptley is os de
H 2tra
cti
‘m thuepd m2al is 3
in’ ‘maite ler,
ng n’ t h
Mi e 20
n,
Bi
no 4
mi No need to update
al
tr ee
Deleting root node (minimum)
of extracted
tree and linking children to make heap
Delete Minimum Key cont.

Union ( H1 , H2)
B0 B011 B1 B12 B3

10 3 210 77 5

4 99 6 7 8

12 11 50
M
Ene
He srguir 2 3
ap ngg ni
s twBi 20
pro onBom
pe inial 4
rty omH
Time complexity
Time eaAnd = Time
iacomplexity = to nfind
log + 1min-tree
+ log n ++log
Time
n to
p finally we have a
extract minl tree + Time to link children of extracted
Binomial
=> O (Heap
log n two
) heaps
tree + Time to Union
Decrease Key
Let us assume that H1 is an existing Binomial Heap
and we want to decrease key ‘X’.

Algorithm,

5. If new key is greater or equal to X (key to be


decreased), simple stop.

7. Else, updated X with new key and check for


Min-Binomial Heap property.
Decrease Key cont.

Let ‘X’ is key to be decreased and new key is ‘2’

H1 50 19 15

21 16 17 18

Ste 2 31 50
22 n t
thaSt p 1 i t
tnstare
n ‘ep : ne tX
a e
hrsrenp
X’2: w 24 w ni
i pait
, cE k e
X y
i t shp
n e
on su y i X yk tist
owi t ap
t
pro inrue Ms s we t
o
m ne ekXXt ensw
t
peWe e e i n a l e
a r
i ftbe
r X
e a rd
know
rty xe H ler that height of Binomial tree can
l c
aSphhpifa pan
cu ea o m
PoSmof
“logXn” tiisopin
in worst
the case,
Root list therefore
of Binomial time
CC
n
DecreaseHeap, key is stop
simply O ( log n )
Delete Key

Assume that H1 is an existing Binomial Heap and we


want to Delete key ‘X’.

Algorithm,

• Decrease Key ‘X’ to -∞

• Delete Minimum Key


Delete Key cont.

Let ‘X’ is key we want to delete

1 15
H1
2 16 17 18

-∞
22 31 50
n d
a
24 -∞ rty
Ste
X i th ope
p 1: ’ w pr
De ‘X ap
cre e y He
ase k in
e
Ke
y l ac e M
e p ur
R ns
e
Delete Key cont.

Let ‘X’ is key we want to delete

1 -∞ H2
H1
2 15 17 18

16
22 31 50
en
d r
il
Ste 24 h
c)
p2 tiHs2
:D k,
ele ilHn1 H2
te n d( k e
Mi iaon ma
nK Xn
ey leteU to
De
Delete Key cont.

Let ‘X’ is key we want to delete

H1
18 1 17 15

2 50 16
22 31

24

Ste 2
)
p2 H
:D ,
eleWe know that Decrease key operation takes ( H1
te Final Heap n
M
“log in” i o
n Ktime and Delete min also take “log
U nn”
ey
time therefore Delete key takes O ( log n )
Fibonacci Heap
Fibonacci Heap (Fredman and Tarjan in
1986)

A Fibonacci heap is a collection of min-trees, like Binomial


heap but less structured.

3. Head pointer of heap always point to the tree with


minimum key in the Root list
4. Root list of heap is implemented using Circular Doubly
linked list
5. Every min-tree in the collection is min-heap-ordered in the
sense that each non-root has a key strictly less than or equal
to the key of its parent.
6. There could be some marked nodes, other than Roots.
Structure of Fibonacci Heap

F0 F3
H F1

4 1 5

8
3 4 6

Marked node
9 10
Min Tree
A Min tree is an unordered binomial tree that is
defined recursively

3.key(parent) ≤ key(child)
4.Degree of the min tree is k, where k is “ number of
B2
children of root node ”
3. Fk may have children ( Fk-1, Fk-2 …F0) in any
arbitrary order 1
Ev F3
tre ery B
e b in
ut om 3 4 6
vic ia
e v l tre
be ers e i
tru a m s M 3 9 10
e. ay in
no
t 9
Fibonacci Heap

F1 F2 F0 F0 F3 H
17 24 23 7 3

30 26 46 18 52 41

Th 35 39 44
ere
are
Fib 5 m
on in
acc tre 2 trees of degree ‘0’,
i h es i
eap n t
he No constraint
Operations on Fibonacci Heap

Find Minimum Key

Uniting two Binomial Heaps

Insert new Key

Delete Minimum Key

Decrease Key

Delete Key
Find Minimum Key

Algorithm

3. If head pointer of heap is pointing to NULL, stop


execution.

5. Else, return the key of node pointed by head, as


head of Fibonacci heap always points to min-key
in the heap (Heap property)
Find Minimum Key

H
17 24 23 7 3

30 26 46 18 52 41

35 39 44
StSet
po pep1 2 min = 3
int : :h r
ed eeatdu
bNy rpno
UhLe itnhe
Lad t iks e
po nyoCleary, it takes O( 1 ) time
int t
er
Uniting two Fibonacci Heaps

Algorithm

4. Concatenate Root lists ( circular doubly linked


lists ) of both the heaps.

6. Updated head pointer if needed. Recall that head


always points to the min-key
Uniting two Binomial Heaps cont.

1. Concatenating two Root lists…


2. Setting Head of resultant heap to the min-key
H1 H

23 24 17 7 3 21

H2
30 26 46 18 52 41

35 7 39 3 44 21

Set head Cleary, it takes O(1) time,52as 41


Splicing 2 circular,we
doubly 18
to are only resetting pointers
linked list
min-key of 4 nodes39 44
Insert Key

Let us assume that H is an existing Fibonacci Heap


and we want to insert ‘x’ in it.

Algorithm,
4. Create a new node consisting of key‘x’.

6. If head pointer of Fibonacci heap points to


NULL, simply set the head pointer to newly
created node.

8. Else, add the new node to the left of node pointed


by head pointer.

10. Update the head pointer to min-key if needed.


Insert Key cont.

3
Let x = 3

F1 F2 F0 F0 F3 H
17 24 23 7

30 26 46 18 52 41

SSSttteSetpe 35 39 44
ep
ptohpieoil p213p:::4cca:hdUd
cnotneentfret r recapkndea
by t,aoinftfon tefnw otThere
eer nh are 4 steps in this algorithm. All takes
hean intm o whoeendaaed
deeod gisdikneNe-pkconstant
fetdh yUoe‘Lyinit oddteo time, therefore total time is O ( 1 )
e h xL’ fed
eap
Delete Minimum Key
Let us assume that H is an existing Fibonacci Heap
and we want to delete minimum key.

Algorithm,

5. Delete the min-key and add the child list of min-


key into Root list.

7. Update min-key pointer ( head pointer of heap )

9. Consolidate the Root list so that there must be


only one Min tree of each degree
Delete Minimum Key cont.
Step 1- 2: Delete min-key, add children into Root list
and update min-key
H H

15 1 7 18 38

72 35 88
24 17 23 21 39 41

30 52

ey
- k
in
a tem
d
Up
Delete Minimum Key cont.
Step3: Consolidate Root list
H
15 35 88 7 18 38
15

72 18 38 72
24 17 23 21 39 41

1
2 ’,,
21
52 39 41
s 3
‘0‘0’t,
30 i
t is s1e’,)
o
t roootaediyys ‘g hne
r e nnt rlnor-eoktr lohlistt s
52
u r rreismt ari nO t eo(ott ter
c curerm r e n o=i etoebr in
. . . . . e
f
oof cifnurtothmpeetoerignR po
re e o f
rot t sitd
0
t m p
a t e
0 1 2 3 4 e p
ggrreeenSrtshdt ta zseetdor1eli pd a
Degree
Degree
Define an array of DDeeg t 2301 ris
ofofmin-key
pointers, current isnef‘3’,
troot so isset u‘2’,
D b u oCeHeap e
ro an d
Final Fibonacci m t es
set 2 toApointer
nd h
roughly of log n 3size
rd
pointer min-key
tre
Decrease Key
Let us assume that H is an existing Fibonacci Heap
and we want to decrease key ‘X’ to some new
key.

Three cases:

5. Min heap property is not violated

7. Min heap property is violated but parent is


unmarked

9. Min heap property is violated and parent is


already marked
Decrease Key Case 0

Min heap property is not violated

5. Decrease the X (key to be decreased) to new key.

7. Quit execution.
Decrease Key cont.

Case 0: Let X is key to be decreased to 45

H
7 18 38

24 17 23 21 39 41

26 45
46 30 52
t to
a n
35 88 88 e w “
X k y w 45
e to “
’ is ase
46 ecre
Parent of X is still lesser, means no’violation
occurred. Simply quit d
Decrease Key Case 1

Min heap property is violated but parent is


unmarked

4. Decrease the X (key to be decreased) to new key.

6. Mark parent of X.

8. Cut off link between X and parent of X.

10. Add tree rooted at X into Root list of Heap and


updated min-key if needed.
Decrease Key cont.

Case 1: Let X is key to be decreased to 15

H
Parent[x] 7 18 38

24
24 17 23 21 39 41

26 15
46 30 52
t o n
n t
X i t s
w at ith yut
a
35 88 88 eted w5e-dk“eb
X k e y w
o o
y X“ ri1rn ed
ut rk
rke tcofcm
s e
i arer asneooren ma
Mark parent of X ifand 6 ’ pt rteioefpta un
Update min-key 4 d
’ oddelcae l t is
m
cut offtime
Clearly, the link
needed CAVioth en
betweenis O(1)
complexity
p ar
them
Decrease Key Case 2
Min heap property is violated and parent is already
marked

4. Decrease the X (key to be decreased) to new key.

6. Cut off link between X and parent of X and add tree rooted
at X into Root list , unmark X and updated min-key if
needed.

8. Cut off link between parent[X ] and parent[parent[X ]]and


add tree rooted at parent[X] into Root list, unmake
parent[X] and updated min-key if needed.

10. If parent[parent[X ]] is unmarked, simply mark and quit,


Else repeat the process till some unmarked node or root
arrives.
Decrease Key cont.
Case 2: Let X is key to be decreased to 5
H
15 7 18 38

72
24 17 23 21 39 41
Parent

X 26 30 52
r to
ey o
n t
iead g tey
35
5 88 o
palr inlyk dd
d
ais eygimoepw ‘5d’ a
e
hnt n-ek,ys n toan
e
atare hmekiekXd tsoeodff list
d
p P s tasre eaut ot
Parent of Parent is already marked, simply U ’ imea cr, c o
5 r ek
3 ec e dar he R
unmark, cut off and add to the Root list
Unlink X from its parent and add in Root list ‘ D bnm to t
u
Delete Key
Let us assume that H is an existing Fibonacci Heap
and we want to decrease key ‘X’ to some new
key.

Algorithm:

• Decrease X to -∞

• Delete Minimum key


Delete Key cont.

Step1: Decrease X to -∞
H
15 7 18 38

72
24 17 23 21 39 41
X
26
-∞ 30 52

r tog to
35 88 t e in
o i go -∞
n
d pey to
e a e ke X
e y ted
e h th ans-kele
d at’ iscm
rei e d
Mark parent of X, Unlink X from its parent Up‘26De b
and add in Root list
Delete Key cont.
Step2: Delete min-key
H H
15 -∞ 7 18 38

72 35 88
24 17 23 21 39 41

30 52
to
r
nte
oi
p
e ad ey
e h n-k
d at mi
Delete the min-key and add its children in the U p
Root list
Delete Key cont.
Step2: Delete min-key

H
15 35 88 7 18 38
15

72 18 38 72
24 17 23 21 39 41

21
52 39 41
30 e y
e k
n )
e
a y
sg
o
n -
r k
e
( l i s
e ty
52 c
eiO otet lk
omd
+ o e )
. . . . . mt )
oe(1 teodRge ln
f r
mO dat(ol
r
T t
=i
0 1 2 3 4
S =tae omlieO
e
t i m nTsi=
Final Fibonacci tai m
l C+Heapo
t
T aol
To
Thanks

You might also like