Binomial ..Heaps
Binomial ..Heaps
Binomial Heaps
• A way to implement mergeable heaps.
• Useful in scheduling algorithms and graph algorithms.
• 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.
These are ordered trees, i.e., order of children is important.
1 1
Different Trees
2 3 3 2
4 5 4 5
Base Case: B0 = single node is a binomial tree.
Inductive Step:
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
Binomial Heaps
• Set of binomial trees satisfying binomial-heap
properties:
1 Heap ordered: In each binomial tree, key of a node is at
least parent’s key (backwards from definition used in
HeapSort).
Implies root of a binomial tree has the smallest key in that tree.
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 b i 2i
i 1
B.H contains Bi iff bi is 1.
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
Operations on Binomial Heaps
Minimum(H)
Minimum(H)
yy:=
:=NIL;
NIL;
xx:=
:=head[H];
head[H];
min
min:= :=;
;
while
whilexx NIL
NILdo
do
ififkey[x]
key[x]<<min
minthen
then
min
min:=:=key[x];
key[x];
yy:=:=xx
fi;
fi;
xx:=:=sibling[x]
sibling[x]
od;
od;
return
returnyy
Time is O(lg n).
Linking Two Binomial Heaps
Link(y,z)
Link(y,z)
p[y]
p[y]:=
:=z;z;
sibling[y]
sibling[y]:=:=child[z];
child[z];
child[z]
child[z]:=:=y;y;
degree[z]
degree[z]:= :=degree[z]
degree[z]++11
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 (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
1 1 1 (carries)
7 37 44
15 8 29 10
00111
28 33 25
31 17
10011
22 48
30 23
11010
41 50
32 24
45
temporarily have three
55 trees of this degree
Code for Union
Union(H
Union(H1,1,HH2)2)
HH:=:=new
newheap; heap;
head[H]
head[H] :=merge(H
:= merge(H1,1,HH2);2); /*/*simple
simplemerge
mergeofofroot
rootlists
lists*/*/
ififhead[H]
head[H]==NIL NILthen
thenreturn
returnHHfi;fi;
prev-x
prev-x:=:=NIL; NIL;
xx:=:=head[H];
head[H];
next-x
next-x:=:=sibling[x];
sibling[x];
while
while next-xNIL
next-x NILdodo
ifif(degree[x]
(degree[x] degree[next-x])oror
degree[next-x])
(sibling[next-x]
(sibling[next-x]NIL NILand
anddegree[sibling[next-x]]
degree[sibling[next-x]]==degree[x])
degree[x])then
then
Cases 1,2 prev-x :=
prev-x := x; x;
xx:=:=next-x;
next-x;
else
else
ififkey[x]
key[x]key[next-x]
key[next-x]thenthen
Case 3 sibling[x] := sibling[next-x];
sibling[x] := sibling[next-x];
Link(next-x,
Link(next-x,x)x)
else
else
ififprev-x
prev-x==NIL
NILthen
thenhead[H]
head[H]:=:=next-x
next-xelse
elsesibling[prev-x]
sibling[prev-x]:=:=next-x
next-xfifi
Case 4 Link(x,
Link(x,next-x);
next-x);
xx:=:=next-x
next-x
fifi
fi;fi;
next-x
next-x:=:=sibling[x]
sibling[x]
od;
od;
return
returnHH
prev-x x next-x sibling[next-x]
Cases prev-x x next-x
a b c d Case 1 a b c d
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
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 17
28 22 48 31
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 17
28 22 48 31
30 23
41
45 32 24 50 Note: Union is
O(lg n).
55
Insert and Extract-Min
Insert(H,
Insert(H,x)x) Extract-Min(H)
Extract-Min(H)
H
H:=:=Make-B-H();
Make-B-H(); remove
removeminimum
minimumkey keyroot
rootxxfrom
from
p[x]
p[x]:=:=NIL;
NIL; H’s
H’sroot
rootlist;
list;
child[x]
child[x]:=:=NIL;
NIL; H
H:=:=Make-B-H();
Make-B-H();
sibling[x]
sibling[x]:=:=NIL;
NIL; root
rootlist
listofofH
H==x’s
x’schildren
childreninin
degree[x]
degree[x]:=:=0;0; reverse
reverseorder;
order;
head(H)
head(H):=:=x;x; HH:=:=Union(H,
Union(H,H); H);
HH:=:=Union(H,
Union(H,H)H) return
returnxx
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 (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
Decrease-Key
Decrease-Key(H,
Decrease-Key(H,x,x,k)
k)
ififkk>>key[x]
key[x]then
then“error”
“error”fi;fi;
key[x]
key[x]:=:=k;k;
yy:=:=x;x;
zz:=:=p[y];
p[y];
while
whilezzNIL NILand
andkey[y]
key[y]<<key[z]
key[z]do
do
exchange
exchangekey[y]
key[y]and
andkey[z];
key[z];
yy:=:=z;z;
zz:=:=p[y]
p[y]
odod
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
17 38
z 16 28 13 11
27
y 7 23 77
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 14 29
7 8
41
13 11 17 38
10 28
77 27
16 23
42
Delete
Delete(H,
Delete(H,x)x)
Decrease-Key(H,
Decrease-Key(H,x,x,);
);
Extract-Min(H)
Extract-Min(H)
Time is O(lg n)