Binomial Heaps
description
Transcript of Binomial Heaps
Binomial Heaps
p2.
Binomial trees:B0 has a single node
Bk:
Ex
B1 B2 B3
i
Bk-1
Bk-1
p3.
Lemma1For the binomial tree Bk,
1. there are 2k nodes,2. the height if the tree is k,3. there are exactly nodes at depth i for i= 0, 1, …., k, 4. the root has degree k, which is greater than that of
any other node; moreover if the children of the root are numbered from left to right by k-1, k-2, …, 0, child I is the root of a subtree Bi
Proof:1) By induction, 2k-1 + 2k-1 = 2k
2) By induction, 1 + (k-1) = k3) , by induction
ki
ki
1k1-i
1ki
p4.
Corollary:The maximum degree of any node in an n-node binomial tree is lg(n)
Binomial heaps:H: a set of binomial trees satisfying the following:
1. Each binomial tree in H is heap-ordered:the key of a node is greater than or equal to the key of its parent
2. There is at most one binomial tree in H whose root has a given degree
Note: By 2. an n-node binomial heap H consists of at most binomial trees
1n lg
p5.
Representation of binomial heaps
(a) head[H] 10 1
1711
29148
18
12 25
38
27
6
p6.
/100/
180/ /
121
25
/ /
/12
/63/
270/ /
111
170/
380/ /
82
141
290/ /
(b) head[H]
pkey
degree
child
sibling
p7.
Operations on binomial heaps Creating a new binomial heap
Finding the minimum key Uniting 2 binomial heaps
time:θ(1) NIL,Hhead
}y return
} }
x y key[x] min
{ then min x[k] if
{ do NIL x while
min head[H] x NIL y
{Minimum(H)HeapBinomial
Time: O( lg n)
p8.
Binomial-Heap-Merge
}1 degree[z] degree[z]
child[z] child[z] sibling[y]
z p[y] {
z) Link(y,Binomial
y
Bk-1
Bk-1
Y, z : Bk-1 trees
p9.
12 7 15
28 33
41
25 37
3 6
4410298
1731482223
32 2445
55
18
30
50
(a) head[H1] head[H2]
37
3 6
4410298
1731482223
32 2445
55
18
30
50
(b) head[H] 12 7 15
28 33
41
25
x next-xsorted degree
Output of
Binomial-Heap-Merge
Case 3
p10.
37
3 6
4410298
1731482223
32 2445
55
18
30
50
(c) head[H] 12 7 15
28 33
41
25
x next-x
Case 2
sibling[next-x]
37
3 6
4410298
1731482223
32 2445
55
18
30
50
(d) head[H] 12 7 15
28 33
41
25
x next-x
Case 4
prev-x
p11.
37
3 6
4410298
1731482223
32 2445
55
18
30
50
(d) head[H] 12
7
15
28 33
4125
x next-x
Case 3
prev-x
37
3 6
4410298
1731482223
32 2445
55
18
30
50
(d) head[H] 12
715
28 33
41
25
x next-x
Case 1
prev-x
p12.
.......
{
then ) ]degree[ ]]ling[degree[sib and NIL ]sibling[ (
or ) ]degree[ ]degree[ ( if {
)do NIL ( while
sibling[H] head[H]
NIL H return then NILhead[H] if
to pointthey liststhe not but H and Hfree )H ,Merge(H-Heap-Binomial head[H]
) Heap(-Binomial-Make H {
)H ,Union(HHeapBinomial
21
21
21
next-xxxprev-x
xnext-xnext-x
next-xx
next-x
next-xxprev-x
Case1
p13.
}y return
} ]sibling[
}
) , Link(-Binomial ]sibling[
else head[H]
then ) NIL ( if {else }
) , Link(-Binomial ]sibling[ ]sibling[
{ then ) ]key[ ]key[ ( ifelse }
xnext-x
next-xxnext-xx
next-xprev-x
next-xprev-x
xnext-xnext-xx
next-xx
......
Case 3
Case 4
p14.
….a b c dprev-x x
next-xsibling[next-x]
Bk Bl
….….(a)
Case 1
a b c d
prev-x x next-x
Bk Bl
….
a b c dprev-x x
next-xsibling[next-x]
Bk Bk
….….(b)
Case 2
a b c d
prev-x x next-x
Bk Bk
….
Bk Bk
p15.
a b c dprev-x x
next-xsibling[next-x]
Bk Bk
….….(c)
Case 3
a bc
d
prev-x x next-x
Bk
Bk
….
Bl Bl
a b c dprev-x x
next-xsibling[next-x]
Bk Bk
….….(d)
Case 4
Bk
][][ xnextkeyxkey
][][ xnextkeyxkey
Bk+1
a cb
d
prev-x x
Bk
Bk
….
Bl
Bk+1
p16.
Insert a node
Extracting the node with minimum key
})'
'
'
H H, Union(-Heap-Binomial H x ]head[H NIL sibling[x]
NIL child[x] NIL p[x]
) Heap(-Binomial-Make H {
x) Insert(H,HeapBinomial
}
)x return
H H, Union(-Heap-Binomial H listresulting
the of headthe to point to ]head[H set and children sx' of list linkedthe of orderthe reverse
) Heap(-Binomial-Make H H of list root
the inkey minthe withrootthe remove and find {
Min(H)-ExtractHeapBinomial
'
'
'
z
y xx
p17.
13
10 1
2512166
1823262914
17 3811
27
41
8
42
(a) head[H] 37
28
77
13
10 1
2512166
1823262914
17 3811
27
418
42
(b) head[H] 37
28
77
x
p18.
13
10 25
41
(c) head[H] 37
28
77
6
2914
17 3811
27
8
16
2326
42
12
18
head[H’]
25 6
2914
17 3811
27
8
12
18
41
37
(d)head[H]
16
2326
42
13
10
28
77
p19.
Decreasing a key
Deleting a key
}}
p[y] z z y
fields other and key[z] key[y]exchange {
do ) key[z] key[y] and NIL z ( whilep[y] z x y
k key[x] " key[x] k" error then key[x] k if
{k) x, Key(H,-DecreaseHeapBinomial
} ) H Min(-Extract-Heap-Binomial
) -x, H, Key(-Decrease-Heap-Binomial {
x) Delete(H,HeapBinomial
p20.
25 6
2914
17 3811
27
8
12
18
41
37
(a)head[H]
16
237
42
13
10
28
77
z
y
25 6
2914
17 3811
27
8
12
18
41
37
(b)head[H]
7
2316
42
13
10
28
77
z
y
p21.
25 6
2914
17 3811
27
8
12
18
41
37
(c)head[H]
10
2316
42
13
7
28
77
z
y