You are on page 1of 24

Subject Name: Advanced Algorithms

Unit No:3
Unit Name: Advanced Data Structures
Faculty Name : Ms Vanita Mane
Harsha Saxena
Siddhi Kadu.
Module No 3: Advanced Data Structures
 

Lecture No:19
Union operation on Binomial
Heap
 
 
Uniting two binomial heaps

• Uniting two binomial heaps H1 and H2 is equivalent to adding two binary


numbers. In particular, adding the binary representation of |H1| and |H2|.
• In the worst case, every bit addition generates a carry which is equivalent to
creating a new Bi while merging a copy of Bi−1 in H1 and a copy of Bi−1 in
H2.
• Thus, merge incurs O(log n) in the worst case, where n = |H1| + |H2|.

3 Lecture No:19 Union operation on Binomial Heap


 
Binomial-Heap-Union(H1,H2)

• H <- Binomial-Heap-Merge(H1,H2)
• This merges the root lists of H1 and H2 in increasing order of
root degree
• Walk across the merged root list, merging binomial trees of equal
degree. If there are three such trees in a row only merge the last two
together(to maintain property of increasing order of root degree as
we walk the roots)

4 Lecture No:19 Union operation on Binomial Heap


 
Lecture 17: Binomial trees and
5
Binomial Heap
H1 H2
2 60 80 18

32 63
58 19
93

53
69

Stage 1: Start merging root lists of binomial heaps H1 and


H2 into a single root list H. The root list is strictly in
increasing degree

6 Lecture No:19 Union operation on Binomial Heap


 
Example 1

H
60 80 2 18

32 63
58 19
93

53
69

7 Lecture No:19 Union operation on Binomial Heap


 
Lecture 17: Binomial trees and
8
Binomial Heap
Binomial-Heap-Union

Stage 2: Check for empty heap


Stage 3: Initialize prev-x, x and next-x

X next-X Sibling[next-x]
H
60 80 2 18
Prev-X=nil
32 63 58 19
93

53
69

Lecture No:19 Union operation on Binomial Heap


 
Binomial-Heap-Union

Stage 2: Check for empty heap


Stage 3: Initialize prev-x, x and next-x

Prev-X X next-X Sibling[next-x]


H
60 80 2 18

32 63 58 19
93

53
69

Lecture No:19 Union operation on Binomial Heap


 
Case 1
Case 2

Move forward

Case 3

Case 4

Lecture 17: Binomial trees and


11
Binomial Heap
Degree[x]≠Degree[next-x] Move x

Degree[x]=Degree[next-x]=Degree[sibling[next-x]] Move x

X is parent
Degree[x]=Degree[next-x] and key[x]≤key[next-x]

Lecture 17: Binomial trees


Degree[x]=Degree[next-x]
12 andandkey[x]>key[next-x] Next-x is parent
Binomial Heap
Binomial-Heap-Union

Stage 4: decide which sub-trees to link to each other depending types


of cases:
There can be following 4 cases when we traverse the list of roots.
—–Case 1: Orders of x and next-x are not same, we simply move ahead.
In following 3 cases orders of x and next-x are same.
—–Case 2: If the order of next-next-x is also same, move ahead.
—–Case 3: If the key of x is smaller than or equal to the key of next-x, then
make next-x as a child of x by linking it with x.
—–Case 4: If the key of x is greater, then make x as the child of next.
AIM: to merge two trees until there is no more than 1 sub-tree of a
certain degree.

Lecture No:19 Union operation on Binomial Heap


 
Example 1

Prev-x=Nill x Next-X
H
60 80 2 18

32 63
58 19
93

Degree[x] ≠ Degree[Next-X]
53
apply Case 1 move forward
69

14 Lecture No:19 Union operation on Binomial Heap


 
Example 1

Prev-x x Next-X
H
60 80 2 18

32 63
58 19
93

Degree[x] ≠ Degree[Next-X]
53
apply Case 1 move forward
69

15 Lecture No:19 Union operation on Binomial Heap


 
Example 1

Prev-x x Next-X
H
60 80 2 18

32 63
58 19
93

53
69
Degree[x]= Degree[next-x] and sibling[next-x]=nill
Key[x]≤key[next-x]
apply Case 3 make next-x the leftmost child of x

16 Lecture No:19 Union operation on Binomial Heap


 
H
60 80 2

18 32 63
93

58 19 53

Combine trees of same degree using binomial


link, make smaller key the root of the
combined tree
69
17 Lecture No:19 Union operation on Binomial Heap
 
Binomial-Heap-Union-Example

H1 H2

Merge H1 and H2 Heap


H

Lecture No:19 Union operation on Binomial Heap


 
Binomial-Heap-Union-Example

x next-x Sibling[next-x]
H

Prev-x=nill

degree[x] = degree[next-x]≠ degree[sibling[next-x]]


Key[x] ≤ Key[next-x] Apply Case 3-merge Binomial tree x and next-x

Lecture No:19 Union operation on Binomial Heap


 
Binomial-Heap-Union-Example

Sibling[next-x]
x next-x
H

Prev-x=nill

degree[x] = degree[next-x]= degree[sibling[next-x]]


Apply Case 2-move x forward

Prev-x x next-x Sibling[next-x]

Lecture No:19 Union operation on Binomial Heap


 
Binomial-Heap-Union-Example

next-x Sibling[next-x]
Prev-x x
H

degree[x] = degree[next-x]≠ degree[sibling[next-x]]


Key[x] > Key[next-x] Apply Case 4-merge Binomial tree x and next-x

Lecture No:19 Union operation on Binomial Heap


 
Binomial-Heap-Union-Example

prev-x x next-x Sibling[next-x]

degree[x] = degree[next-x] ≠ degree[sibling[next-x]]


Key[x] ≤ Key[next-x] Apply Case 3-merge Binomial tree x and next-x

Lecture No:19 Union operation on Binomial Heap


 
Binomial-Heap-Union-Example

Finally, the union of H1 and H2 will look like this:

 Running time for examining the sum of roots of heaps H1 and H2 :


O(log n )

Lecture No:19 Union operation on Binomial Heap


 
Thank You

You might also like