Binomial Heaps: Sachin Tripathi
Binomial Heaps: Sachin Tripathi
Sachin Tripathi
IIT(ISM), Dhanbad
Operations:
Make-Heap().
Insert(H, x), where x is a node H.
Minimum(H).
Extract-Min(H).
Union(H1, H2): merge H1 and H2, creating a new heap.
Decrease-Key(H, x, k): decrease x.key (x is a node in H) to k. (It’s
assumed that k x.key.)
Definitions
Binomial Heap: Collection of binomial trees (satisfying some properties).
Binomial Trees
Definition is inductive.
Inductive Step:
Bk = is a binomial tree.
Bk-1
Bk-1
Examples B0
B1
B2
B3
depth # nodes
0 1
B4
1 4
2 6
3 4
4 1
Another Way to Look at Bk
B0
B1
B2
Bk-2
Bk-1
Bk
Properties of Binomial Trees
Lemma: For the binomial tree Bk,
1. There are 2k nodes.
2. Tree height is k.
k
3. i nodes at depth i, i = 0, 1, …, k [binomial coefficients].
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 [k–1 is LM, 0 is RM].
Proof:
1. Induction on k. 2. Induction on k.
# nodes in Bk height of Bk
2(# nodes in Bk -1 ) 1 height of Bk -1
2(2 k 1 ) 1 (k 1)
2k k
Proof (Continued)
k
3. Let D(k,i) = # nodes at depth i in Bk. Want to show D(k,i) = .
i
Bk
Bk-1 depth i in Bk
depth i–1 in this Bk-1 Bk-1
So,
D(k, i) (k 1)!(k i) i(k 1)!
D(k 1, i) D(k 1, i 1) i! (k i)!
k 1 k 1 k!i(k 1)!i(k 1)!
, by ind. hyp.
i i 1 i! (k i)!
(k 1)! (k 1)! k
i! (k 1 i)! (i 1)!(k i)! i
Proof (Continued)
4. Root degree of Bk = 1 + root degree of Bk-1
= 1 + k–1 , induction hypothesis
=k
2 Set includes at most one binomial tree whose root is a given degree.
Implies B.H. with n nodes has at most lg n + 1 B.T.’s.
Think of n in binary: blg n, …, b0, i.e.,
lg n
n i
b 2
i 1
i
Representing Binomial Heaps
10 1 6
called the root list
12 25 14 29
8
18 17 38
11
27
10 1 6
12 25 14 29
8
18 17 38
11
27
Binomial Heap: Implementation
Implementation.
Represent trees using left-child, right sibling pointers.
three links per node (parent, left, right)
Roots of trees connected with singly linked list.
degrees of trees strictly decreasing from left to right heap
6 3 18 6 3 18
37
29 10 44 37 29
48 31 17 48 10
50 50 31 17 44
Binomial Heap
Linking Two Binomial Heaps
Link(y,z)
p[y] := z;
sibling[y] := child[z];
child[z] := y;
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.
Binomial Heap: Union
Create heap H that is union of heaps H' and H''.
"Mergeable heaps."
Easy if H' and H'' are each order k binomial trees.
connect roots of H' and H''
choose smaller key to be root of H
8 29 10 44
30 23 22 48 31 17
45 32 24 50
55
H' H''
Binomial Heap: Union
6 3 18
8 29 10 44 37
30 23 22 48 31 17
15 7 12
45 32 24 50
28 33 25
55
+ 41
1 1 1
1 0 0 1 1
19 + 7 = 26 + 0 0 1 1 1
1 1 0 1 0
Binomial Heap: Union
6 3 18
8 29 10 44 37
30 23 22 48 31 17
15 7 12
45 32 24 50
28 33 25
55
+ 41
Binomial Heap: Union 12
18
6 3 18
8 29 10 44 37
30 23 22 48 31 17
15 7 12
45 32 24 50
28 33 25
55
+ 41
3 12
7 37 18
25
6 3 18
8 29 10 44 37
30 23 22 48 31 17
15 7 12
45 32 24 50
28 33 25
55
+ 41
12
18
3 3 12
15 7 37 7 37 18
28 33 25 25
41
6 3 18
8 29 10 44 37
30 23 22 48 31 17
15 7 12
45 32 24 50
28 33 25
55
+ 41
12
18
3 3 12
15 7 37 7 37 18
28 33 25 25
41
6 3 18
8 29 10 44 37
30 23 22 48 31 17
15 7 12
45 32 24 50
28 33 25
55
+ 41
3 12
15 7 37 18
28 33 25
41
3 3 12
15 7 37 7 37 18
28 33 25 25
41
6 3 18
8 29 10 44 37
30 23 22 48 31 17
15 7 12
45 32 24 50
28 33 25
55
+ 41
6 3 12
8 29 10 44 15 7 37 18
30 23 22 48 31 17 28 33 25
45 32 24 50 41
55
Union (Continued)
Union traverses the new root list like this:
prev-x x next-x
Note: We may temporarily create three trees with the same root
degree.
Analogy
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
Union
55
18
15 7 37
8 29 10 44 1 1 1 (carries)
33 25
00111
28 22 48 31 17
30 23 10011
41
45 32 24 50 11010
Bk Bl Bk Bl
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
Bk+1
Code for Union
Union(H1, H2)
H := new heap;
head[H] := merge(H1, H2); /* simple merge of root lists */
if head[H] = NIL then return H fi;
prev-x := NIL;
x := head[H];
next-x := sibling[x];
while next-x NIL do
if (degree[x] degree[next-x]) or
(sibling[next-x] NIL and degree[sibling[next-x]] = degree[x]) then
prev-x := x;
Cases 1,2
x := next-x;
else
if key[x] key[next-x] then
Case 3 sibling[x] := sibling[next-x];
Link(next-x, x)
else
if prev-x = NIL then head[H] := next-x else sibling[prev-x] := next-x fi
Case 4 Link(x, next-x);
x := next-x
fi
fi;
next-x := sibling[x]
od;
return H
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 (Continued)
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 (Continued)
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 (Continued)
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
Union Example (Continued)
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
28 22 48 31 17
30 23
41 50
32 24
45
55
Union Example (Continued)
prev-x x next-x
head[H] 12 3 6
18 7 37 44
15 29 10
8
33 25
28 22 48 31 17
30 23
41 50
32 24
45
Case 1 55
33 25
28 22 48 31 17
30 23
41 50
32 24
45
55
Extract-Min
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 (Continued)
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
Insert and Extract-Min
Insert(H, x) Extract-Min(H)
H := Make-B-H(); remove minimum key root x from
p[x] := NIL; H’s root list;
child[x] := NIL; H := Make-B-H();
sibling[x] := NIL; root list of H = x’s children in
degree[x] := 0; reverse order;
head(H) := x; H := Union(H, H);
H := Union(H, H) return x
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
13 11 17 38
z 16 28
77 27
y 7 23
42
Decrease-Key Example (Continue)
head[H] 25 12 6
37 18 z 10 8 14 29
41
y 7 28 13 11 17 38
77 27
16 23
42
z
head[H] 25 12 6
37 18 y 8 14 29
7
41
13 11 17 38
10 28
77 27
16 23
42
Decrease-Key
Decrease-Key(H, x, k)
if k > key[x] then “error” fi;
key[x] := k;
y := x;
z := p[y];
while z NIL and key[y] < key[z] do
exchange key[y] and key[z];
y := z;
z := p[y]
od
Delete
Delete(H, x)
Decrease-Key(H, x, );
Extract-Min(H)
Thank you