binomial & fibonacci heap advanced)
TRANSCRIPT
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:
1. 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.
5 1
8 64
10
3
97
11
B3
4
B0 B1
B2 is missing
Structure of Binomial Heap
Still a valid Binomial
Heap
Binomial Tree
A binomial tree is an ordered tree that is defined recursively 1.B0 consists of a single node.
2.Bk consists of two trees ( Bk-1, Bk-1) that are linked
by making one as the leftmost child of the other.
B2
B3
B2Ordered means children
of every node are in
sequence Bk-1 …
.B1 , B
0
from left to right
Properties of Binomial Tree
1. Let ‘n’ be total number of nodes in the tree, then n = 2k,
B3B0 B1
Where ‘k’ is the
degree of binomial
tree
20 = 1 21 = 2 23 = 8
Constructive proof
Properties of Binomial Tree Contd.
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 original trees.
Think recursively to
Meld trees, B 0
, B 0,
B 1, B 2
, B 3…….B k-1
B1 B1
B2
Also, height of Binomial
tree is exactly equal to
“log n”
Properties of Binomial TreeContd.
3. There are exactly nodes at ith level.
• A property useful for naming the data structure.
ki
B3
3
0
3
1
3
2
3
3
= 1
= 3
= 3
= 1
Level 0
Level 1
Level 2
Level 3
Properties of Binomial TreeContd.
4. Deleting root of a Binomial tree Bk yields binomial trees Bk-1, … , B0 in sequence.
B3
B1B2B0
Ordered
Binomial tree
Binomial Heap
B2B0 B1 B3
H
k = 3
Tree Roots linked together using singly linked list, sorted by their degrees, in ascending order
Head Pointer of Heap points to the first node of the Root list only.
Find Minimum Key
Uniting two Binomial Heaps
Insert new Key
Delete Minimum Key
Decrease Key
Delete Key
Operations on Binomial Heap
Find Minimum Key
Algorithm
1. If heap is empty, simply stop execution.
2. Else, assign the key of the first root in the Root list to Min variable.
3. Move till the end of Root list by comparing keys with Min and update Min if needed.
Find Minimum Key
7
50
5
8
12
20
6
11
B2
7
9
10
2
4
1
3
B0 B1 B3
Min = 10 Min = 7
Key in current node is
smaller, simply update the
‘min’
Min = 1
Key in current node is
smaller, simply update the
‘min’Key in current node is greater, no need to change.
Also Root list is ended so value of ‘Min’ is the minimum value of the heap.
Clearly it takes “log n” time to find min key, As there can be maximum “log n” binomial trees in
the Root list
Uniting two Binomial Heaps
Algorithm
1. Merge the Root lists of both the heaps using Merge routine of Merge Sort algorithm
2. 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
17
50
15
18
22
60
16
31
19
21
50
2
4
1
3
7
50
5
8
12
20
6
11
7
9
10H1
H2
17
50
15
18
22
60
16
31
2
4
1
3
50 19
21
7
50
5
8
12
20
6
11
10 7
9
Heap after Merge routine
Merge cont.
Roots are merged in
Ascending order by their
degrees
H1
H2
Clearly, it takes “log n” time to merge two heaps. Where log n = log n1 + log n2
17
50
15
18
22
60
16
31
19
21
50
2
4
1
3 7
50
5
8
12
20
6
11
7
9
10
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.
Degree of two trees must be
same to Meld them
17
50
15
18
22
60
16
31
19
21
50
2
4
1
3 7
50
5
8
12
20
6
11
7
9
10
Ensuring property of Binomial Heap cont.
There are two nodes of Degree ‘0’, lets meld them
Node ’10’ is smaller, so it
will become the root…
17
50
15
18
22
60
16
31
19
21 2
4
1
3 7
50
5
8
12
20
6
11
7
9
Ensuring property of Binomial Heap cont.
There are three nodes of Degree ‘1’, lets meld them
Note: while melding two
heap, there may exist 3 trees
of same degree
50
10
B1 B1 B1
If there are ‘3’ trees of same
degree, simply leave the first
and meld other two, making
min as root…
Ensuring property of Binomial Heap cont.
17
50
15
18
22
60
16
31
2
4
1
3 7
50
5
8
12
20
6
11
19
21
7
950
10
There are two nodes of Degree ‘2’, lets meld them
Simply make max-tree as left
most child of other
B2 B2
Now there are three trees of degree ‘3’. Leave first one
and merge other two
17
50
15
18
22
60
16
31
19
21
50 2
4
1
3 7
50
5
8
12
20
6
11
7
9
10
Ensuring property of Binomial Heap cont.
Finally, it has become a valid
Binomial HeapThis step also take log n time, therefore total time 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,
1. Create a single node Binomial Heap H1 consisting of ‘x’.
2. Union( H1, H2),
Insert Key cont.
17
50
15
18
22
60
16
31
19
21
50
2
4
1
3
10
H1
H2 Let x = 10
Create a single node
Binomial Heap H1
consisting of ‘x’. Now Union( H
1, H2)
Recall the 2 steps of Union
Step1: Merge two Heaps
17
50
15
18
22
60
16
31
19
21
50
2
4
1
3
10
Insert Key cont.
Step2: Ensure Binomial Heap
Property
Union ( H1, H2 ) continued…
B0 B0
50
10
B1 B1B2 B2
19
21
50
10
B3 B3
Final Binomial Heap
B4
H
Maximum cost in Insert operation is of Union, therefore, Insert also takes 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,
1. Find minimum key using Find Minimum routine.2. Extract Binomial tree containing the Minimum
key, from the Root list of Binomial Heap3. 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.
4. Union( H1, H2),
7
50
5
8
12
20
6
11
B2
7
9
10
2
4
1
3
B0 B1 B3
Min = 10 Min = 7
Key in current node is
smaller, simply update the
‘min’
Min = 1
Key in current node is smaller,
simply update the ‘min’
No need to update
Delete Minimum Key cont.
Find Minimum key and Extracting min Binomial treeMinimum key
Extracting Min, Binomial treeDeleting root node (minimum) of extracted tree and linking children to make heap
2
4
1
3
H1
H2
7
50
5
8
12
20
6
11
7
9
10
2
4
3
Delete Minimum Key cont.
Union ( H1 , H2)
Merging two Binomial
Heaps
7
50
5
8
12
20
6
11
7
9
10 2
4
3
Ensuring Binomial Heap
property
B0 B0B1 B1B1
And finally we have a Binomial Heap
B1 B2 B3
Time complexity = Time to find min-tree + Time to extract min tree + Time to link children of
extracted tree + Time to Union two heaps
Time complexity = log n + 1 + log n + log n
=> O ( log n )
Decrease Key
Let us assume that H1 is an existing Binomial Heap and we want to decrease key ‘X’.
Algorithm,
1. If new key is greater or equal to X (key to be decreased), simple stop.
2. 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’
17
50
15
18
22
16
31
19
21
50H1
24
X
Step 1: new key is smaller
than ‘X’, continue executionPlac
e new
key in
XStep 2: Ensure Min Heap
property
2
X
Compare key
X w
ith its
parent
Shift X
to its
parent
Compare X
with
its pare
nt
and s
wap
X
Shift X
to its
parent
X is in the Root list of Binomial Heap, simply stop
We know that height of Binomial tree can be “log n” in worst case, therefore time of
Decrease key is O ( log n )
Delete Key
Assume that H1 is an existing Binomial Heap and we want to Delete key ‘X’.
Algorithm,
1. Decrease Key ‘X’ to -∞
2. Delete Minimum Key
17
50
15
18
22
16
31
1
2H1
24
X
-∞
X
X
Let ‘X’ is key we want to delete
Step 1: Decrease KeyRep
lace k
ey ‘X
’ with
-∞ an
d
ensu
re M
in Hea
p property
Delete Key cont.
Let ‘X’ is key we want to delete
Delete Key cont.
17
50
-∞
18
1
2H1
24
22
15
3116
Step 2: Delete Min KeyDele
te X an
d link its
child
ren
to mak
e H2
X
H2
Union ( H1,
H2 )
Let ‘X’ is key we want to delete
Delete Key cont.
17
50
18 1
2
H1
24
22
15
3116
Step 2: Delete Min KeyUnion ( H
1, H2 )
Final HeapWe know that Decrease key operation takes
“log n” time and Delete min also take “log n” 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.
1. Head pointer of heap always point to the tree with minimum key in the Root list
2. Root list of heap is implemented using Circular Doubly linked list
3. 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.
4. There could be some marked nodes, other than Roots.
Structure of Fibonacci Heap
HF0 F3 F1
Marked node
1
64
10
3
9
4 5
8
Min Tree
A Min tree is an unordered binomial tree that is defined recursively
1.key(parent) ≤ key(child) 2.Degree of the min tree is k, where k is “ number of children of root node ”3. Fk may have children ( Fk-1, Fk-2 …F0) in any
arbitrary order
B2
F3
1
64
10
3
93
9
Every Binomial tree is Min
tree but vice versa may not
be true.
Fibonacci Heap
H
3
4152
44
18
39
24 72317
30 4626
35There are 5 min trees in the
Fibonacci heap
F1 F0F0F2 F3
2 trees of degree ‘0’,
No constraint
Find Minimum Key
Uniting two Binomial Heaps
Insert new Key
Delete Minimum Key
Decrease Key
Delete Key
Operations on Fibonacci Heap
Find Minimum Key
Algorithm
1. If head pointer of heap is pointing to NULL, stop execution.
2. 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
3
4152
44
18
39
24 72317
30 4626
35Step 1: head point is not
NULL
min = 3
Cleary, it takes O( 1 ) time
Step 2: return the key
pointed by head pointer
Uniting two Fibonacci Heaps
Algorithm
1. Concatenate Root lists ( circular doubly linked lists ) of both the heaps.
2. Updated head pointer if needed. Recall that head always points to the min-key
Uniting two Binomial Heaps cont.
1. Concatenating two Root lists…
H1
H2
24 1723
30 4626
35 3 21
4152
44
18
39
7
Splicing 2 circular, doubly linked list
3 21
4152
44
18
39
7
H
Set head to
min-key
2. Setting Head of resultant heap to the min-key
Cleary, it takes O(1) time, as we are only resetting pointers
of 4 nodes
Insert Key
Let us assume that H is an existing Fibonacci Heap and we want to insert ‘x’ in it.
Algorithm,1. Create a new node consisting of key‘x’.
2. If head pointer of Fibonacci heap points to NULL, simply set the head pointer to newly created node.
3. Else, add the new node to the left of node pointed by head pointer.
4. Update the head pointer to min-key if needed.
Insert Key cont.
Let x = 3
H
7
4152
44
18
39
24
3
2317
30 4626
35
F1 F0F2 F3
Step1: create new node
containing key ‘x’
Step2: check for head
pointer, if it is NULL
Step3: add new node to
the left of node pointed
by head of the heap
F0
Step4: Update head
pointer to min-key if
needed
There are 4 steps in this algorithm. All takes constant time, therefore total time is O ( 1 )
Delete Minimum Key
Let us assume that H is an existing Fibonacci Heap and we want to delete minimum key.
Algorithm,
1. Delete the min-key and add the child list of min-key into Root list.
2. Update min-key pointer ( head pointer of heap )
3. Consolidate the Root list so that there must be only one Min tree of each degree
Delete Minimum Key cont.
38
41
18
39
7
2317
30
21
52
15
72
H
248835
1
24
Update m
in-key
H
Step 1- 2: Delete min-key, add children into Root list and update min-key
Delete Minimum Key cont.
Step3: Consolidate Root list
38
41
18
3921
52
15
72
8835 7
2317
30
2424
Start fr
om min-key
Consolidate
Root list
H
. . . . .0 1 2 3 4
38
41
18
3921
52
15
72
Final Fibonacci HeapAmortized
time =
O( lo
g n )
Define an array of pointers, roughly of log n size
Degree of min-key is ‘3’, set 3rd pointer to min-key
Degree of current root is ‘2’, set 2nd pointer
Degree of current ro
ot is ‘1
’,
set 1st pointer
Degree
of curre
nt root is
‘1’,
but 1st pointer
is al
ready se
t,
therefore
merge b
oth the
trees
and update
pointers
Degree
of curre
nt root is
‘2’,
but 2nd pointer
is al
ready se
t,
therefore
merge b
oth the
trees
and update
pointers
Degree
of curre
nt root is
‘3’,
but 3rd pointer
is al
ready se
t,
therefore
merge b
oth the
trees
and update
pointers
Degree
of curre
nt root is
‘0’,
set 0
th pointer
Degree
of curre
nt root is
‘0’,
but 0th pointer
is al
ready se
t,
therefore
merge b
oth the
trees
and update
pointers
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:
1. Min heap property is not violated
2. Min heap property is violated but parent is unmarked
3. Min heap property is violated and parent is already marked
Decrease Key Case 0
Min heap property is not violated
1. Decrease the X (key to be decreased) to new key.
2. Quit execution.
Decrease Key cont.
Case 0: Let X is key to be decreased to 45
H
38
41
18
3924
7
2317
304626
88
21
52
’46’ is k
ey we want to
decrease to “ 45 “
45
Parent of X is still lesser, means no violation occurred. Simply quit
35 88X
Decrease Key Case 1
Min heap property is violated but parent is unmarked
1. Decrease the X (key to be decreased) to new key.
2. Mark parent of X.
3. Cut off link between X and parent of X.
4. 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
38
41
18
3924
7
2317
304626
88
21
52
’46’ is k
ey we want to
decrease to “ 15 “
15
35 88X
Compare key X with its
parent
Violation occurre
d but
parent is unmarked
Mark parent of X and cut off the link between
them
24
Parent[x]
Add tree r
ooted at
X on
the left
of min-key
15
88
Update min-key if neededClearly, time complexity is O(1)
Decrease Key Case 2
Min heap property is violated and parent is already marked
1. Decrease the X (key to be decreased) to new key.
2. 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.
3. 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.
4. 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
38
41
18
39
7
2317
3026
88
21
52
15
72
X
24
35
H
‘35’ is th
e key
going to
be decr
eased
to ‘5
’
Decreas
e X to
new key5
Unlink X from its parent and add in Root list
Parent
Parent is
alrea
dy
marked
, simply
unmark, c
ut off a
nd add
to the R
oot list
26
88
26
Parent of Parent is already marked, simply unmark, cut off and add to the Root list
24
Update head
pointer to
min-key
Delete Key
Let us assume that H is an existing Fibonacci Heap and we want to decrease key ‘X’ to some new key.
Algorithm:
1. Decrease X to -∞
2. Delete Minimum key
Delete Key cont.
Step1: Decrease X to -∞
38
41
18
39
7
2317
3026
88
21
52
15
72
X
35
H
24
‘26’ is th
e key
going to
be dele
ted
Decreas
e X to
-∞
-∞
8835
-∞
Update head
pointer to
min-key
Mark parent of X, Unlink X from its parent and add in Root list
24
Delete Key cont.
Step2: Delete min-key
38
41
18
39
7
2317
30
21
52
15
72
H
248835
-∞
24
Delete the min-key and add its children in the Root list
Update head
pointer to
min-key
H
Delete Key cont.
Step2: Delete min-key
38
41
18
3921
52
15
72
8835 7
2317
30
2424
Start fr
om min-key
Consolidate
Root list
H
. . . . .0 1 2 3 4
38
41
18
3921
52
15
72
Final Fibonacci HeapTotal
time =
Time t
o decreas
e key
+ Time t
o delete
key
Total tim
e = O
(1) + O
(log n)
= O
( log n )
Thanks