Sei sulla pagina 1di 29

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 1

Chapter 19: 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(H
1
, H
2
): merge H
1
and H
2
, creating a new heap.
Decrease-Key(H, x, k): decrease x.key (x is a node in H)
to k. (Its assumed that k s x.key.)
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 2
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
3 2
1
2 3
Different Trees
Base Case: B
0
= single node is a binomial tree.

Inductive Step:

B
k
= is a binomial tree.
B
k-1

B
k-1

5 4 5 4
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 3
Examples
B
0

B
1

B
2

B
3

B
4

depth # nodes
0 1
1 4
2 6
3 4
4 1
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 4
Another Way to Look at B
k






B
k-1

B
k-2

B
2

B
1

B
0

B
k

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 5
Properties of Binomial Trees
Lemma 19.1: For the binomial tree B
k
,
1. There are 2
k
nodes.
2. Tree height is k.
3. nodes at depth i, i = 0, 1, , k [binomial coefficients].
4. Root has degree k, other nodes have smaller degree. i
th

child of root is root of subtree B
i
, where i = k1, k2, ,
0 [k1 is LM, 0 is RM].
|
|
.
|

\
|
i
k
Proof:

1. Induction on k.
k
1 k
1 - k
k
2
) 2(2
) B in nodes 2(#
B in nodes #
=
=
=

2. Induction on k.
k
1) (k 1
B of height 1
B of height
1 - k
k
=
+ =
+ =
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 6
Proof (Continued)
B
k-1

B
k-1

B
k

depth i in B
k

depth i in this B
k-1

depth i1 in this B
k-1
3. Let D(k,i) = # nodes at depth i in B
k
. Want to show D(k,i) = . |
|
.
|

\
|
i
k
i)! (k 1)! (i
1)! (k
i)! 1 (k i!
1)! (k
hyp. ind. by ,
1 i
1 k
i
1 k
1) i 1, D(k i) 1, D(k
i) D(k,
So,


+

=
|
|
.
|

\
|

+
|
|
.
|

\
|

=
+ =
|
|
.
|

\
|
=

+
=

+
=
i
k
i)! (k i!
1)! i(k 1)! i(k k!
i)! (k i!
1)! i(k i) (k 1)! (k
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 7
Proof (Continued)
4. Root degree of B
k
= 1 + root degree of B
k-1

= 1 + k1 , induction hypothesis
= k


For other properties, see figures.
Corollary 20.2: The maximum degree in an n-node binomial tree is
lg n.
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 8
Binomial Heaps
Set of binomial trees satisfying binomial-heap
properties:
1 Heap ordered: In each binomial tree, key of a node is at
least parents 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: (b
lg n
, , b
0
,, i.e.,



B.H contains B
i
iff b
i
is 1.

=
=
n lg
1 i
i
i
2 b n
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 9
Representing Binomial Heaps
10
25
18
12
1
27
17
38
11
29
14
8
6
parent
key
degree
child sibling
Each node is
represented by a
structure like this
10
25
18
12
1
27
17
38
11
29
14
8
6
called the root list
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 10
Operations on Binomial Heaps
Minimum(H)
y := NIL;
x := head[H];
min := ;
while x = NIL do
if key[x] < min then
min := key[x];
y := x
fi;
x := sibling[x]
od;
return y
Time is O(lg n).
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 11
Linking Two Binomial Heaps
Link(y,z)
p[y] := z;
sibling[y] := child[z];
child[z] := y;
degree[z] := degree[z] + 1
B
k-1

B
k-1

B
k-1
B
k-1

y
y
z
z
Link
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 12
Union
Union
H
1
, H
2
H
1
H
2







H
1
=
H
2
=






First, simply merge the two root lists by root degree (like merge sort).
Remaining Problem: Can have two trees with the same root degree.
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 13
Union (Continued)






prev-x x next-x
Union traverses the new root list like this:
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.
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 14
Analogy
37
3
50
31
17
48
44
10
29
6
55
32
24
45
22
23
30
8
33
41
28
15
25
7
head[H]
18
12
prev-x x
12
33
41
28
15
25
7 head[H
1
] 18
37
3
50
31
17
48
44
10
29
6
55
32
24
45
22
23
30
8
head[H
2
]
Union
Like binary addition:

1 1 1 (carries)
0 0 1 1 1
1 0 0 1 1
1 1 0 1 0

temporarily have three
trees of this degree
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 15
Code for Union
Union(H
1
, H
2
)
H := new heap;
head[H] := merge(H
1
, H
2
); /* 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;
x := next-x;
else
if key[x] s key[next-x] then
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
Link(x, next-x);
x := next-x
fi
fi;
next-x := sibling[x]
od;
return H
Cases 1,2
Case 3
Case 4
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 16
Cases
a
b c
d
prev-x x next-x sibling[next-x]
a
b c
d
prev-x x next-x sibling[next-x]
a
b c
d
prev-x x next-x sibling[next-x]
a
b c
d
prev-x x next-x sibling[next-x]
B
k

B
k

B
k
B
k

B
k

B
k
B
k

B
k

B
l

B
l

B
l

key[x] > key[next[x]]
key[x] s key[next[x]]
Case 3
Case 4
Case 2
Case 1
a
b c
d
prev-x x next-x
B
k

B
l

a
b c
d
prev-x x next-x
B
k

B
k
B
k

a
c
b
d
prev-x x next-x
B
k

B
k

B
l

B
k+1

a
b
c
d
B
k

B
k

B
l

B
k+1

prev-x x next-x
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 17
Union Example
12
33
41
28
15
25
7 head[H
1
] 18
37
3
50
31
17
48
44
10
29
6
55
32
24
45
22
23
30
8
head[H
2
]
18
37
3
50
31
17
48
44
10
29
6
55
32
24
45
22
23
30
8
12
33
41
28
15
25
7 head[H]
Merge
x next-x
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 18
Union Example (Continued)
18
37
3
50
31
17
48
44
10
29
6
55
32
24
45
22
23
30
8
12
33
41
28
15
25
7 head[H]
Case 3
x next-x
37
3
50
31
17
48
44
10
29
6
55
32
24
45
22
23
30
8
33
41
28
15
25
7 head[H]
18
12
x next-x
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 19
Union Example (Continued)
Case 2
37
3
50
31
17
48
44
10
29
6
55
32
24
45
22
23
30
8
33
41
28
15
25
7 head[H]
18
12
x next-x
37
3
50
31
17
48
44
10
29
6
55
32
24
45
22
23
30
8
33
41
28
15
25
7 head[H]
18
12
prev-x x next-x
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 20
Union Example (Continued)
Case 4
37
3
50
31
17
48
44
10
29
6
55
32
24
45
22
23
30
8
33
41
28
15
25
7 head[H]
18
12
prev-x x next-x
37
3
50
31
17
48
44
10
29
6
55
32
24
45
22
23
30
8
33
41
28
15
25
7
head[H]
18
12
prev-x
x next-x
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 21
Union Example (Continued)
Case 3
37
3
50
31
17
48
44
10
29
6
55
32
24
45
22
23
30
8
33
41
28
15
25
7
head[H]
18
12
prev-x
x next-x
37
3
50
31
17
48
44
10
29
6
55
32
24
45
22
23
30
8
33
41
28
15
25
7
head[H]
18
12
prev-x x next-x
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 22
Union Example (Continued)
37
3
50
31
17
48
44
10
29
6
55
32
24
45
22
23
30
8
33
41
28
15
25
7
head[H]
18
12
prev-x x next-x = NIL
terminates
37
3
50
31
17
48
44
10
29
6
55
32
24
45
22
23
30
8
33
41
28
15
25
7
head[H]
18
12
prev-x x next-x
Case 1
Note: Union is
O(lg n).
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 23
Insert and Extract-Min
Insert(H, x)
H' := Make-B-H();
p[x] := NIL;
child[x] := NIL;
sibling[x] := NIL;
degree[x] := 0;
head(H') := x;
H := Union(H, H')
Extract-Min(H)
remove minimum key root x from
Hs root list;
H' := Make-B-H();
root list of H' = xs children in
reverse order;
H := Union(H, H');
return x
Both are O(lg n).
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 24
Extract-Min Example
41
37
42
23
18
26
25
12
16
1
27
17
38
11
29
14
8
6
13
77
28
10
head[H]
41
37
42
23
18
26
25
12
16
1
27
17
38
11
29
14
8
6
13
77
28
10
head[H]
x
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 25
Extract-Min Example (Continued)
41
37
42
23
18 26
25 12 16
27
17
38
11
29
14
8
6
13
77
28
10
head[H] head[H']
25
27
17
38
11
29
14
8
6
42
23
77
26
13
28
16
10
18
41
37
12
head[H]
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 26
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
O(lg n)
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 27
Decrease-Key Example
25
27
17
38
11
29
14
8
6
42
23
77
26
13
28
16
10
18
41
37
12
head[H]
Decrease key 26 to 7
25
27
17
38
11
29
14
8
6
42
23
77
7
13
28
16
10
18
41
37
12
head[H]
y
z
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 28
Decrease-Key Example (Continue)
25
27
17
38
11
29
14
8
6
42
23
77
16
13
28
7
10
18
41
37
12
head[H]
y
z
25
27
17
38
11
29
14
8
6
42
23
77
16
13
28
10
7
18
41
37
12
head[H]
y
z
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 29
Delete
Delete(H, x)
Decrease-Key(H, x, );
Extract-Min(H)
Time is O(lg n)
Will this always work?

Potrebbero piacerti anche