Binomial & Fibonacci Heap Advanced)
Binomial & Fibonacci Heap Advanced)
Shahid Iqbal
(05-0978)
Binomial Heap (Vuillemin, 1978)
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
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
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
Decrease Key
Delete Key
Find Minimum Key
Algorithm
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
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.
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.
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.
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.
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.
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
Algorithm,
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.
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,
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,
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
Algorithm,
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.
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.
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)
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
Decrease Key
Delete Key
Find Minimum Key
Algorithm
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
23 24 17 7 3 21
H2
30 26 46 18 52 41
35 7 39 3 44 21
Algorithm,
4. Create a new node consisting of key‘x’.
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,
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:
7. Quit execution.
Decrease Key cont.
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
6. Mark parent of X.
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
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.
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 -∞
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