0% found this document useful (0 votes)
70 views

Binomial Heaps: Manoj Kumar DTU, Delhi

Binomial heaps are a type of mergeable heap data structure that supports five operations: make-heap, insert, minimum, extract-minimum, and union. They are composed of binomial trees that have specific properties like the number of nodes and degree of nodes at each level. Operations like minimum and extract-minimum run in O(log n) time by traversing the binomial trees. The union operation merges two binomial heaps by combining their root lists and linking trees with the same degree.

Uploaded by

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

Binomial Heaps: Manoj Kumar DTU, Delhi

Binomial heaps are a type of mergeable heap data structure that supports five operations: make-heap, insert, minimum, extract-minimum, and union. They are composed of binomial trees that have specific properties like the number of nodes and degree of nodes at each level. Operations like minimum and extract-minimum run in O(log n) time by traversing the binomial trees. The union operation merges two binomial heaps by combining their root lists and linking trees with the same degree.

Uploaded by

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

Binomial Heaps

Manoj Kumar
DTU, Delhi
Binomial Heaps
• Known as mergeable heaps.
• Supports following five operations:
1. MAKE-HEAP(): creates and returns a new heap with
no elements.
2. INSERT(H, x): Inserts node x, into heap H.
3. MINIMUM(H): returns a pointer to the node in heap H
whose key is minimum.
4. EXTRACT-MIN(H): deletes the node from heap H
whose key is minimum, returning pointer o the node.
5. UNION(H1,H2): creates and returns new heap that
contains all the elements of heaps H1 and H2. H1 and
H2 are destroyed in this process.
Binomial Heaps: Definitions
Binomial Heap: Collection of binomial trees
(satisfying some properties).
Binomial Trees

• Definition is inductive.

• These are ordered trees, i.e., order of children is


important.
1 1
Different Trees
2 3 3 2
4 5 4 5
Binomial Trees
• Base Case: B0 = single node is a binomial tree.

• Inductive Step:

Bk-1
Bk = Bk-1 is a binomial tree.
Examples
B0

B1

B2

B3

depth # nodes
0 1
B4 1 4
2 6
3 4
4 1
Another Way to Look at Bk

L B0
B1
B2
Bk-2

Bk-1

Bk
Properties of Binomial Trees
Lemma 1: For the binomial tree Bk,
1. There are 2k nodes.

2. Tree height is k.
k 
3.   nodes at depth i, i = 0, 1, …, k [binomial coefficients].
i

4. Root has degree k, other nodes have smaller degree. ith


child of root is root of subtree Bi, where i = k–1, k–2, …,
0 [Bk–1 is Left Most, B0 is Right Most].
Proof: Inductive
1. Binomial tree Bk consists of two copies of Bk-1, so
Bk has 2k-1 + 2k-1 = 2k nodes.
2. The way in which two copies of are connected,
height of Bk is one greater than height of Bk-1. So the
height of Bk is (k-1)+1= k.
Bk

Bk-1

Bk-1
Proof: Inductive
3. Let D(k,i) be the number of nodes at depth I of binomial tree
Bk. Since Bk is composed of two copies of Bk-1 linked
together, a node at depth i in Bk-1 appears in Bk once at depth
i and once at depth i+1.
Thus number of nodes in Bk at depth i is the number of nodes at
depth i in Bk-1 plus the number of nodes at depth i-1 in Bk-1.
Thus
D(k,i) = D(k-1,i) + D(k-1,i-1)
 k - 1  k - 1 k 
=  i +   =  
   i -1  i
depth i in this Bk-1
Bk
Bk-1 depth i in Bk
depth i–1 in this Bk-1 Bk-1
proof
4. Root degree of Bk = 1 + root degree of Bk-1
= 1 + k–1 , induction hypothesis
=k

Corollary : The maximum degree in an n-node binomial tree


is lg n.
Binomial Heaps
• Binomial heap H is a set of binomial trees satisfying following
binomial-heap properties:
1 Each binomial tree in H is Heap ordered: the key of a
node is greater than or equal to the key of it’s parent.
iImplies root of a binomial tree has the smallest key in that
tree.
2 There is at most one binomial tree in H whose root has a
given degree.
iImplies B.H. with n nodes has at most lg n + 1 B.T.’s.
Think of n in binary: 〈blg n, …, b0〉, i.e.,
lg n 
n= ∑ i
b 2
i =1
i

B.H contains Bi iff bi is 1.


Representing Binomial Heaps
head[H] 10 1 6

12 25 14 29
8

18 17 38
11

27

Each node is parent


represented by a
key
structure like this
degree
child sibling

1 6
head[H] 10

12 25 14 29
8

18 17 38
11

27
Operations on Binomial Heaps
MAKE-BINOMIAL-HEAP(): simply allocates and
return an object H, where head[H] = NIL.
Running time is Θ(1)

MAKE-BINOMIAL-HEAP()
1. Create new head node H
2. head[H] NIL
3. return H
Operations on Binomial Heaps
BINOMIAL-HEAP-MINIMUM(H)
1. y NIL;
2. x head[H];
3. min ∞;
4. while x ≠ NIL do
5. if key[x] < min then
6. min key[x];
7. y x
8. x sibling[x]
9. return y

Time is O(lg n).


Linking Two Binomial Trees
Linking two binomial trees whose roots have same degree.

BINOMIAL-LINK(y,z)
1. p[y] z;
2. sibling[y] child[z];
3. child[z] y;
4. degree[z] degree[z] + 1

z
y z y
Link Bk-1
Bk-1 Bk-1
Bk-1
UNION
H1, H2 Union H1 ∪ H2

H1 = H2 =

First, simply merge the two root lists by root degree (like merge sort).

Remaining Problem: Can have two trees with the same root degree.
UNION…
Union traverses the new root list like this:

prev-x x next-x

Depending on what x, next-x, and sibling[next-x] point to, Union


links trees with the same root degree.

Note: We may temporarily create three trees with the same root
degree.
UNION…
12 7 18 3 6
head[H1] 15 head[H2]
25 28 33 37 10 44
8 29

41 48 31 17
23 22
30

24 50
45 32
Union
55

prev-x x
head[H] 12 3 6

18 7 37 44
15 29 10
8

33 25 17
28 22 48 31
30 23

41 50
32 24
45

55
UNION:Example
head[H1] 12 7 15 head[H2] 18 3 6

25 28 33 37 10 44
8 29

41 48 31 17
23 22
30

24 50
45 32

55

Merge

x next-x
head[H] 12 18 7 3 15 6

25 37 28 33 44
29 10
8

41
22 48 31 17
30 23

24 50
45 32

55
UNION:Example…
x next-x
head[H] 12 18 7 3 15 6

25 37 28 33 44
29 10
8

41
22 48 31 17
30 23

24 50
45 32

55

Case 3

x next-x
head[H] 12 7 3 15 6

18 25 37 28 33 44
29 10
8

41
22 48 31 17
30 23

24 50
45 32

55
UNION:Example…
x next-x
head[H] 12 7 3 15 6

18 25 37 28 33 44
29 10
8

41
22 48 31 17
30 23

24 50
45 32

55
Case 2

prev-x x next-x
head[H] 12 7 3 15 6

18 25 37 28 33 44
29 10
8

41
22 48 31 17
30 23

24 50
45 32

55
UNION:Example…
prev-x x next-x
head[H] 12 7 3 15 6

18 25 37 28 33 44
29 10
8

41
22 48 31 17
30 23

24 50
45 32

Case 4 55

prev-x x next-x
head[H] 12 3 15 6

18 7 37 28 33 44
29 10
8

25 41
22 48 31 17
30 23

24 50
45 32

55
Binomial
Heaps - 22
UNION:Example…
prev-x x next-x
head[H] 12 3 15 6

18 7 37 28 33 44
29 10
8

25 41
22 48 31 17
30 23

24 50
45 32

Case 3 55

prev-x x next-x
head[H] 12 3 6

18 7 37 44
15 29 10
8

33 25 17
28 22 48 31
30 23

41 50
32 24
45

55
UNION:Example…
prev-x x next-x
head[H] 12 3 6

18 7 37 44
15 29 10
8

33 25 17
28 22 48 31
30 23

41 50
32 24
45

Case 1 55

prev-x x next-x = NIL


head[H] 12 3 6 ⇒ terminates
18 7 37 44
15 29 10
8

33 25 17
28 22 48 31
30 23

41
45 32 24 50 Note: Union is
O(lg n).
55
Code for UNION
BINOMIAL-HEAP-UNION(H1, H2)
1. H MAKE-BINOMIAL-HEAP();
2. head[H] BINOMIAL-HEAP-MERGE(H1, H2); /* simple merge of root lists */
3. Free the objects H1 and H2, but not the list 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];
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 & 2
12. x next-x; Cases 1 & 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
prev-x x next-x sibling[next-x] prev-x x next-x
a b c d Case 1 a b c d

Bk Bl Bk Bl

prev-x x next-x sibling[next-x] prev-x x next-x


a b c d Case 2 a b c d

Bk Bk Bk Bk Bk Bk
prev-x x next-x sibling[next-x] prev-x x next-x
a b c d Case 3 a b d
c
Bk Bk Bl Bk Bl
key[x] ≤ key[next[x]]
Bk
Bk+1
prev-x x next-x sibling[next-x] prev-x x next-x
a d Case 4 a d
b c c
b
Bk Bk Bl Bk Bl
key[x] > key[next[x]] Bk
INSERT
Inserts node x into binomial heap H, assuming that node x already
been allocated and key[x] has already been filled.

x parent
BINOMIAL-HEAP-INSERT(H, x)
1. H′ MAKE-BINOMIAL-HEAP(); key
2. p[x] NIL; degree
3. child[x] NIL;
4. sibling[x] NIL; child sibling
5. degree[x] 0;
6. head(H′) x;
7. H BINOMIAL-HAP-UNION(H, H′)

Time O(lg n).


INSERT:Example
head[H’] 2
head[H] 12 7 15

25 28 33

41

head[H] 2 12 7 15

25 28 33

41

head[H] 2 7 15

12 25 28 33

41
INSERT: Example…
head[H] 2 15

7 12 28 33

25 41

head[H] 2

7 12
15

28 33 25

41
Extract-Minimum
• Extracts the node with minimum key from binomial
heap H, and returns a pointer to the extracted node

BINOMIAL-HEAP-EXTRACT-MIN(H)
1. remove minimum key root x from H’s root list;
2. H
H′ Make-B-H();
3. root list of H′′ = x’s children in reverse order;
4. H Union(H, H′);
5. return x

Time O(lg n).


Extract-Min: Example
head[H] 37 10 1

41 28 13 25
16 12
6

77
29 26 23 18
8 14

38 42
11 17

27

x
head[H] 37 10 1

41 28 13 25
16 12
6

77
29 26 23 18
8 14

38 42
11 17

27
Extract-Min: Example
head[H] 37 10 head[H′] 25 12 16 6

41 28 13 18 26 23 14 29
8

77 42 38
11 17

27

head[H] 25 12 6

37 18 29
8 14
10

41
13 11 17 38
16 28

77 27
26 23

42
Decrease-Key
Decreases the key of a node x in a binomial heap H to a
new value k.
BINOMIAL-HEAP-DECREASE-KEY(H, x, k)
1. if k > key[x]
2. then error “new key is greater than current key”
3. key[x] k;
4. y x;
5. z p[y];
6. while z ≠ NIL and key[y] < key[z]
7. do exchange key[y] and key[z];
8. exchange other satellite fields of y and z
9. y z;
10. z p[y]

O(lg n)
Decrease-Key Example
head[H] 25 12 6

37 18 29
8 14
10

41
13 11 17 38
16 28

77 27
26 23

42

Decrease key 26 to 7

head[H] 25 12 6

37 18 29
8 14
10

41
11 17 38
z 16 28 13

27
y 7 23 77

42
head[H] 25 12 6

37 18 z 8 14 29
10

41
y 7 28 13 11 17 38

77 27
16 23

42

z
head[H] 25 12 6

37 18 y 14 29
7 8

41
13 11 17 38
10 28

77 27
16 23

42
DELETE a node
BINOMIAL-HEAP-DELETE(H, x)
1. BINOMIAL-HEAP-DECREASE-KEY(H, x, −∞);
2. BINOMIAL-HEAP-EXTRACT-MIN(H)

Time is O(lg n)

You might also like