You are on page 1of 56

Quality Content for Outcome based Learning

Design & Analysis of Algorithms


Day 16

Ver. No.: 1.1 Copyright © 2021, ABES Engineering College


General Guideline

© (2021) ABES Engineering College.


This document contains valuable confidential and proprietary
information of ABESEC. Such confidential and proprietary information
includes, amongst others, proprietary intellectual property which can be
legally protected and commercialized. Such information is furnished
herein for training purposes only. Except with the express prior written
permission of ABESEC, this document and the information contained
herein may not be published, disclosed, or used for any other purpose.

2
Copyright © 2021, ABES Engineering College
Session Plan - Day 16

 Introduction to Binomial Heap and Binomial Tree

 Properties of Binomial Heap

 Operations on Binomial Heap

3
Copyright © 2021, ABES Engineering College
Quality Content for Outcome based Learning

Advance Data Structures:


Binomial Heap

Ver. No.: 1.1 Copyright © 2021, ABES Engineering College


Overview of Binomial Heap:

This section present data structures known as merge-able heaps, which support the
following seven operations.

• MAKE-BINOMIAL-HEAP
• BINOMIAL-HEAP-INSERT
• BINOMIAL-HEAP-MINIMUM
• BINOMIAL-HEAP-EXTRACT-MIN
• BINOMIAL-HEAP-UNION
• BINOMIAL-HEAP DECREASE-KEY
• BINOMIAL-HEAP-DELETE

Copyright © 2021, ABES Engineering College


Binomial Heap
• Binomial heap was introduced in 1978 by Jean Vullemin.

• Jean Vullemin is a professor in mathematics and computer science.

• The other name of Binomial Heap is Mergeable heaps.

• A binomial heap is a collection of binomial trees.


– Lets learn What is Binomial Tree?

Copyright © 2021, ABES Engineering College


Binomial Tree
• Binomial tree is an ordered tree defined recursively.
• The binomial tree has one node.
• The binomial tree consists of two binomial trees and they are connected such that the root of
one tree is the leftmost child of the other.

B2
B1
B1 B0
B1

B0 B1 B2 B3

Copyright © 2021, ABES Engineering College


Binomial Tree
• Binomial tree is an ordered tree defined recursively.
• The binomial tree has one node.
• The binomial tree consists of two binomial trees and they are connected such that the root of one tree is
the leftmost child of the other.

B3
B2 B0

B1

B4

Copyright © 2021, ABES Engineering College


Binomial Tree
• Binomial tree is an ordered tree defined recursively.
• The binomial tree has one node.
• The binomial tree consists of two binomial trees and they are connected such that the root of one
tree is the leftmost child of the other.

B1 Bo
B2
Bk-2
Bk-1
(Fig: A General View of B-Tree)

Bk
Copyright © 2021, ABES Engineering College
Binomial Tree
Binomial tree properties:
• has nodes
• has height
• There are exactly nodes at depth i for i=0, 1, 2,…,k.
For Example:
Lets check in and depth 2()

Hence 6 numbers of nodes are available in depth 2 of


• The root has degree k which is greater than other node in the tree. Each of the root’s
child is the root of a subtree Bi.

Copyright © 2021, ABES Engineering College


Binomial Heap (Property)
A Binomial Heap H is a set of binomial trees that satisfies the following properties:

• Each binomial tree in H obeys the min heap property.


(i.e. key of a node is greater or equal to the key of its parent. Hence the root has the
smallest key in the tree).
• For any non negative integer k, there is at most one binomial tree whose root has
degree .
(e.g. it implies that an n node Binomial heap H consists of at most binomial Tree.
• The binomial trees in the binomial heap are arranged in increasing order of degree

Copyright © 2021, ABES Engineering College


Binomial Heap (Property)
Example:

Head[H]
10 1 6

B0 12 25 8 14 29

18
B2 11 17 38

27
B3

Here n=13
So =(So at most 4)

The Above Figure of Binomial Heap consists of , and

Copyright © 2021, ABES Engineering College


Binomial Heap (Representation)
• Each binomial tree within a binomial heap is stored in the left-child, right-
sibling representation.

• Each node contains POINTERS


• to its parent
• to its key value
• to its leftmost child
• to its immediately right sibling
• to its degree value (i.e. denotes the number of children of X)

Copyright © 2021, ABES Engineering College


Binomial Heap (Representation)

a) p c)
key
degree
child sibling
NIL NIL
2 1
0 2
head[H] NIL NIL

b)
10 12
head[H] 2 1 1 0
NIL NIL

10 12

15
15
0
NIL NIL

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations)
Binomial Heap support the following operations:

1. MAKE-HEAP() creates and returns a new heap containing no elements.

2. MINIMUM(H) returns a pointer to the node in heap H whose key is minimum.

3. UNION(H1, H2) creates and returns a new heap that contains all the nodes of
heaps H1 and H2. Heaps H1 and H2 are "destroyed" by this operation.

4. EXTRACT-MIN(H) deletes the node from heap H whose key is minimum, returning
a pointer to the node.

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations)
5. INSERT(H, x) inserts node x, whose key field has already been filled in, into heap
H.

6. DECREASE-KEY(H, x, k) assigns to node x within heap H the new key value k,


which is assumed to be no greater than its current key value.

7. DELETE(H, x) deletes node x from heap H

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_1)
1. MAKE-HEAP() : creates and returns a new heap containing no elements.

To make an empty binomial heap, the MAKE-BINOMIAL-HEAP procedure simply


allocates and returns an object H , where head[H ] = NIL.

The running time is Θ(1).

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_2)
2. MINIMUM(H) : The procedure BINOMIAL-HEAP-MINIMUM() returns a pointer to the
node with the minimum key in an n-node binomial heap H. This implementation
assumes that there are no keys with value ∞.

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_2)
2. MINIMUM(H) : Since the binomial heap is a min-heap-order, the minimum key of each
binomial tree must be at the root. This operation checks all the roots to find the minimum
key.
Pseudocode: This implementation assumes that there are no keys with value ∞.

Head[H]
20 15 8

35 10 28

12

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_2)
2. MINIMUM(H) : Since the binomial heap is a min-heap-order, the minimum key of each binomial tree
must be at the root. This operation checks all the roots to find the minimum key.
Pseudocode: This implementation assumes that there are no keys with value ∞.
BINOMIAL-HEAP-MINIMUM(H)
1 y ← NIL Head[H]
2 x ← head[H] 20 15 8

3 min ← ∞
35 10 28
4 while x ≠ NIL
5 do if key[x] < min 12
6 then min ← key[x]
7 y←x
8 x ← sibling[x]
9 return y

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_2)
2. MINIMUM(H) : Since the binomial heap is a min-heap-order, the minimum key of each binomial
tree must be at the root. This operation checks all the roots to find the minimum key.
Pseudocode: This implementation assumes that there are no keys with value ∞.
BINOMIAL-HEAP-MINIMUM(H)
1 y ← NIL Head[H]
2 x ← head[H] 20 15 8

3 min ← ∞
35 10 28
4 while x ≠ NIL
5 do if key[x] < min 12
After Execution
6 then min ← key[x]
Return y=8
7 y←x
8 x ← sibling[x]
9 return y

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_2)
Since,binomial heap is Heap-ordered and the minimum key must reside in a ROOT
node. The BINOMIAL-HEAP-MINIMUM(H) checks all roots in .
Because,

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
3. UNION(H1, H2)
This operation consists of the following steps
• Merge two binomial heaps H1 and H2. The resulting heap has the roots in
increasing order of degree
• For each tree in the binomial heap H, if it has the same order with another tree,
link the two trees together such that the resulting tree obeys min-heap-order.

For this there is an requirement of 3 pointers into the root list


x = points to the root currently being examined
prev-x = points to the root PRECEDING x on the root list sibling [prev-x] = x
next-x = points to the root FOLLOWING x on the root list sibling [x] = next-x

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
This operation perform by the help of 4(four) number of cases.
Case 1:

x=next x
Example:
Head[H] next → 𝑥 Head[H] x prev → 𝑥
x

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
This operation perform by the help of 4(four) number of cases.
Case 2:

x=next x
Example:

Head[H] next → 𝑥sibling [ next → 𝑥 ]


x
prev → 𝑥 𝑥 next → 𝑥

Head[H]

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
This operation perform by the help of 4(four) number of cases.
Case 3: ) and (key[x]key[next])
sibling[x]=sibling[next x]
Binomial Link(next x, x)
Example:
x next → 𝑥
5 8 25 5 25
Head[H] Head[H]
75 80 8 75 80

100 100

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
This operation perform by the help of 4(four) number of cases.

Case 4: ) and (key[x]key[next])


if(prevx== Null)
Head[H]= next x
else
sibling[prev x]= next x
Binomial Link(x, next x)

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
prev → 𝑥 𝑥 next → 𝑥
12 7 3 15
Head[H] 18 25 37 28 33

41
Ca
se
4 prev → 𝑥 𝑥 next → 𝑥
12 3 15
Head[H] 18 7 37 28 33

25 41

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
prev → 𝑥 𝑥 next → 𝑥
12 7 3 15
Head[H] 18 25 37 28 33 Ca
se
4

41 prev → 𝑥 𝑥 next → 𝑥
12 3 15
Head[H] 18 7 37 28 33

12 3
25 41
Head[H] 18 15 7 37

28 33 se
3
25 Ca

41

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
BINOMIAL-LINK(y, z)
1. p[y] = z
2. sibling[y] = child[z]
3. child[z] = y
4. degree[z] = degree[z] + 1
z 𝑦 𝑦
5 8 25 5 25
Head[H] Head[H]
75 80 z 8 75 80

100 100

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
BINOMIAL-LINK(y, z)
1 p[y] = z
2 sibling[y] = child[z]
3 child[z] = y
4 degree[z] = degree[z] + 1
z 𝑦 𝑦
5 8 25 5 25
Head[H] Head[H]
75 80 z 8 75 80

100 100

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
Example 1: Merge the following two Binomial heap H1 and H2.

H1 12 7 15 H2 18 3 6

25 28 33 37 29 10 44

48 31
41 17

56

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
Example 1: Merge the following two Binomial heap H1 and H2.

H1 12 7 15 H2 18 3 6

25 28 33 37 29 10 44

48 31
41 17

56

After Merging…….

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
Example 1: Merge the following two Binomial heap H1 and H2.

H 12 18 7 3 15 6

25 37 28 33 29 10 44

48 31
41 17

56

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
Example 1: Merge the following two Binomial heap H1 and H2.

H 12 18 7 3 15 6

25 37 28 33 29 10 44

48 31
41 17

56
Fix Pointer

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
Example 1: Merge the following two Binomial heap H1 and H2.

x next x
H 12 18 7 3 15 6

25 37 28 33 29 10 44

48 31
41 17

56

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
Example 1: Merge the following two Binomial heap H1 and H2.
x next x
H 12 18 7 3 15 6

25 37 28 33 29 10 44

Ca 48 31
se 4 41 17

56

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
Example 1: Merge the following two Binomial heap H1 and H2.
x next x
H 12 18 7 3 15 6

25 37 28 33 29 10 44

Ca
se 4 48 31
41 17

56
next x
H 12 7 3 15 6

x 18 25 37 28 33 29 10 44

48 31
41 17

56

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
Example 1: Merge the following two Binomial heap H1 and H2.
x next x Sibling[next x]
H 12 7 3 15 6

18 25 37 28 33 29 10 44

48 31
41 17

56

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
Example 1: Merge the following two Binomial heap H1 and H2.
x next x Sibling[next x]
H 12 7 3 15 6

18 25 37 28 33 29 10 44
Ca
se 2 48 31
41 17

56

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
Example 1: Merge the following two Binomial heap H1 and H2.
x next x Sibling[next x]
H 12 7 3 15 6

18 25 37 28 33 29 10 44
Ca
se 2 48 31
41 17

56
prevx x next x
H 12 7 3 15 6

18 25 37 28 33 29 10 44

48 31
41 17

56

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)

Example 1: Merge the following two Binomial heap H1 and H2.

prevx x next x
H 12 7 3 15 6

18 25 37 28 33 29 10 44

48 31
41 17

56

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)

Example 1: Merge the following two Binomial heap H1 and H2.

prevx x next x
H 12 7 3 15 6
Ca
se 4 18 37 28 33 29 10 44
25

48 31
41 17

56

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
Example 1: Merge the following two Binomial heap H1 and H2.
prevx x next x
H 12 7 3 15 6

18 25 37 28 33 29 10 44
Ca
se 4 48 31
41 17

56
prevx next x
H 12 3 15 6
x
18 7 37 28 33 29 10 44

25 48 31
41 17

56

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
Example 1: Merge the following two Binomial heap H1 and H2.

x next x
H 12 3 15 6

18 7 37 28 33 29 10 44

25 48 31
41 17

56

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
Example 1: Merge the following two Binomial heap H1 and H2.

x next x
H 12 3 15 6

18 7 37 28 33 29 10 44
Ca
se 48
3 25 41 31 17

56

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
Example 1: Merge the following two Binomial heap H1 and H2.
x next x
H 12 3 15 6

18 7 37 28 33 29 10 44
Ca
se 48
3 25 41 31 17

56
H 12 15 6

18 3 28 33 29 10 44

7 37 48 31
41 17

25 56

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)

Example 1: Merge the following two Binomial heap H1 and H2.


x next x
H 12 15 6

18 3 28 33 29 10 44

7 37 48 31
41 17

25 56

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
Example 1: Merge the following two Binomial heap H1 and H2.

x next x
H 12 15 6

18 3 28 33 29 10 44
Ca
se 3
7 37 48 31
41 17

25 56

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)

Example 1: Merge the following two Binomial heap H1 and H2.

H 12 6

18 15 29 10 44

3 28 33 48 31 17

7 37 41 56

25

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)

Example 1: Merge the following two Binomial heap H1 and H2.

H 12 6

18 15 29 10 44

3 28 33 48 31 17

7 37 41 56

25

Final Binomial
Heap Tree

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)

BINOMIAL-HEAP-UNION(H1, H2)
1 H = MAKE-BINOMIAL-HEAP()
2 head[H] = BINOMIAL-HEAP-MERGE(H1, H2)
3 free the objects H1 and H2 but not the lists they point to
4 if head[H] = NIL
5 then return H
6 prev-x ← NIL
7 x ← head[H]
8 next-x ← sibling[x]

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
9 while next-x ≠ NIL
10 do if (degree[x] ≠ degree[next-x]) or
(sibling[next-x] ≠ NIL and degree[sibling[next-x]] = degree[x])
11 then prev-x ← x ▹ Cases 1 and 2
12 x ← next-x ▹ Cases 1 and 2
13 else if key[x] ≤ key[next-x]
14 then sibling[x] ← sibling[next-x] ▹ Case 3
15 BINOMIAL-LINK(next-x, x) ▹ Case 3
16 else if prev-x = NIL ▹ Case 4
17 then head[H] ← next-x ▹ Case 4
18 else sibling[prev-x] ← next-x ▹ Case 4
19 BINOMIAL-LINK(x, next-x) ▹ Case 4
20 x ← next-x ▹ Case 4
21 next-x ← sibling[x]
22 return H

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
Analysis of BINOMIAL-HEAP-UNION(H1, H2)
The running time of BINOMIAL-HEAP-UNION is O(lg n), where n is the total
number of nodes in binomial heaps H1 and H2.
We can see this as follows.
• Let H1 contain n1 nodes and H2 contain n2 nodes,
Hence, n = n1 + n2.
• Then H1 contains at most ⌊lg n1⌋+1 roots.
• and H2 contains at most ⌊lg n2⌋+1 roots,
• Hence H contains at most ⌊lg n1⌋+⌊lg n2⌋+2 ≤ 2⌊lg n⌋+2 = O(lg n) roots
immediately after the call of BINOMIAL-HEAP-MERGE.
• The time required to perform BINOMIAL-HEAP-MERGE is thus O(lg n).

Copyright © 2021, ABES Engineering College


Binomial Heap (Operations_3)
Analysis of BINOMIAL-HEAP-UNION(H1, H2)
• Each iteration of the while loop takes O(1) time, and there are at most ⌊lg n1⌋ +
⌊lgn2⌋ + 2 iterations.

(because each iteration either advances the pointers one position down the
root list of H or removes a root from the root list. )

• Hence the total time required to execute BINOMIAL-HEAP-UNION is O(log n).

Copyright © 2021, ABES Engineering College


Copyright © 2021, ABES Engineering College

You might also like