You are on page 1of 42

Properties and Data Structure

By:-
KAVITA GUPTA
Roll No.9
M.Sc.Ist yr.
BINOMIAL HEAP
COMPLEXITY TABLE

Procedure Binomial Heap


(Worst case)

Make-Heap (1)

Insert O(lgn)

Minimum O(lgn)

Extract-Min (lgn)

Union O(lgn)

Decrease-Key (lgn)

Delete (lgn)
Binomial Heaps
A Binomial Heap is collection of Binomial trees.
The Binomial tree Bk consists of two Binomial trees Bk-1 that are
linked together and root of one is the leftmost child of the root of
the other

Properties :- B0 Bk-1
There are 2k nodes
Bk-1
Height of the tree is k Bk
exactly kCi nodes at depth i for i=0,1,...,k.
Root has the highest degree k and o numbering children
from left to right as k-1, k-2, ...0, then child i is the root of
a subtree Bi.
B0
B0

B1
B0

B1
B2

B0

B1

B1
B2

B0

B1 B1

B1
B2

B0

B1 B1

B1
B2 B3

B0

B1 B1

B1 B2
B2 B3

B0

B1 B1
B2

B1 B2
B2 B3
Depth
0

B0 1

B1 B1 2
B2

B1
3
B2
B2 B3
Depth
0

B0 1

B1 B1 2
B2

B1
3
B2

Proofs :-
1 Assume : Binomial tree Bk consists of 2 copies of Bk-1
Bk has 2k-1+ 2k-1 = 2k nodes.

2 Maximum depth of Bk is (k-1)+1= k , as max depth of Bk is one greater than


that of Bk-1...

3 Let D(k,i), no. of nodes at depth i of tree Bk . A node at depth i in Bk-1 occurs
twice in Bk, Once at depth i and once at depth i-1.
D(k,i) = D(k-1,i) + D(k-1,i-1)
= k-iCi + k-1Ci-1
= kCi
4. If Bk-1 has degree k-1 than degree of Bk is k. Now, in B1 there is 1
child B0, in B2 there are 2 children one B1 and one B0 , in B3 there
are 3 children: one B2, one B1 & one B0.
Similarly, children of the root of Bk are roots of
Bk-1, Bk-2, Bk-3, ........., B0, from left.

Binomial Heap

A Binomial Heap H is a set of Binomial trees that satisfies the


following property:-

1. Each tree obeys minimum heap property.

2. For any non-negative integer k, there is at most one binomial tree


in H whose root has degree k.
Representing Binomial Heaps

1. Each node x has a key field.

2. Each node x contains pointers p[x] to its parent,


child[x] to its leftmost child
and sibling[x] to its immediately right sibling.

3. If node x is a root, then p[x] = NIL,


if node x has no children, then child[x] = NIL,
if x is the rightmost child of its parent, then sibling[x] = NIL.

4. Each node contains field degree[x], no. of children of x.

5. Binomial heap is H accessed by the head[H], a pointer to the first root in


the root list of H. It is NIL, if the heap H has no elements.
Heap[H] 10 1 6

12 25 8 14 29

18 11 17 38

27
10 6
1
Head[H]

key

degree

child sibling
10 6
1
Head[H]

12 25 8 14 29

key

degree

child sibling
10 6
1
Head[H]

12 25 8 14 29

18 17 38
11

key

degree

child sibling
10 6
1
Head[H]

12 25 8 14 29

18 17 38
11

p
27
key

degree

child sibling
10 6
1
Head[H]

12 25 8 14 29

18 17 38
11

p
27
key

degree

child sibling
10 6
1
0
Head[H]

12 25 8 14 29
0 0

18 17 38
11
0 0 0

p
27
key 0

degree

child sibling
10 6
1
0
Head[H]

12 25 8 14 29
1 0 2 1 0

18 17 38
11
0 0 0
1

p
27
key 0

degree

child sibling
10 6
1
0 3
2
Head[H]

12 25 8 14 29
1 0 2 1 0

18 17 38
11
0 0 0
1

p
27
key 0

degree

child sibling
Example: Heap[H2] 7 11 17
Heap[H1] 1 10
25 18 26
22 B3
19

Binomial-Heap-merge

Heap[H] 1 7 10 11 17

x next-x
B3
22 25 18 26

Case 3 19
x next-x

Heap[H] 1 10 11 17

7 22 25 18 26 B3
Case 2
19

prev-x x next-x

Heap[H] 1 10 11 17

7 22 25 18 26 B3
Case 4

19
prev-x x next-x

Heap[H] 1 10 17

7 11 22 18 26 B3
Case 3
25 19

prev-x x next-x

Heap[H] 1 10

7 17 11 22 B3

Case 1
18 26 25

19
Deletion in Binomial Heaps

Extract-Min
Decrease-Key
Delete

Name : Kirti Dhruva


Roll No: 10
Extract the minimum key node
Extract-Min(H) //H is the heap
1. find the minimum key root x & separate it from the root
list.
2. H’  Make-Binomial-Heap() //empty heap
3. reverse the order of the pointers of children of x & set
head[H’] to point to the head of the resulting list.
4. H  Binomial-Heap-Union(H,H’)
5. return x
Example of Extract-Min

x
Initial
Heap[H] 37 10 1

41 28 13 6 16 12 25

77 8 14 29 26 23 18

11 17 38 42

27
Heap[H] 37 10
x
1
41 28 13
6 16 12 25
77
8 14 29 26 23 18

11 17 38 42

27
Heap[H] 37 10

41 28 13

77
Children of x are reversed

Heap[H’] 25 12 16 6

18 26 23 8 14 29

42 11 17 38

27
Heap-Union(H,H’)

Final
Heap[H] 25 12 6

37 18 10 8 14 29

41 16 28 13 11 17 38

26 23 77 27

42
Analysis of Extract-Min
Step 1: A ‘n’ node binomial heap’s root list has at
most [lg n]+1 roots hence finding smallest
root takes O(lg n) & removing it from heap
takes O(1) time.
Step 2: Creating a empty heap takes O(1) time.
Step 3: Reversing the children of x takes O(lg n)
Step 4: Union of two heaps takes O(lg n) time.

Result: Extract-Min takes O(lg n) time in the worst


case.
Decrease a node’s key
//x is the node & k is its new key
Decrease-Key(H,x,k)
1. If k > key[x] then
2. error “can not increase key”
3. key[x]  k
4. y  x , z  p[y]
5. while z !=Nil & key[y] < key[z] //bubbling up
6. do exchange key[y]  key[z]
7. y  z , z  p[y]
Example of Decrease-Key

Initial
Heap[H] 25 12 6

37 18 8 14 29

41 11 17 38

x 27
Heap[H] 25 12 6

37 18 8 14 29

41 z 11 17 38

x 7 y

Function call Decrease-Key(H,x,7) does this


Heap[H] 25 12 6

37 18 z 8 14 29

41 y 7 17 38

11
Bubbling-up node pointed by ‘y’
min heap property restored

Heap[H] 25 12 6

y
37 18 7 14 29

41 8 17 38

11
Analysis of Decrease-Key

Step 1 to 4: takes O(1) i.e. constant time.


Steps 5 to 7: are executed for lg n times since
in the worst case ‘y’ would be a leaf node
whose depth is lg n hence the while loop
takes O(lg n) time.
Result: Decrease-Key takes O(lg n) in
its worst case.
Deleting a node
// x is the node to delete
Delete(H,x)
1. Decrease-Key(H,x,k)
2. Extract-Min(H)

/* here k is assumed to be the smallest key


that a node can have & no other node can have it
*/
Analysis of Delete

Step 1: calls function Decrease-Key which takes O(lg


n) time in its worst case.

Step 2: calls function Extract-Min which takes


O(lg n) time in its worst case.

Result: Delete takes O(lg n) in its worst case.

You might also like