0% found this document useful (0 votes)
2 views29 pages

14.2.1 Binheap

The document discusses binomial heaps, a data structure useful for implementing mergeable heaps, particularly in scheduling and graph algorithms. It details operations such as Make-Heap, Insert, Minimum, Extract-Min, Union, and Decrease-Key, along with the properties and structure of binomial trees. Additionally, it provides examples and code snippets for operations like Union and linking two binomial heaps.

Uploaded by

kamal deep garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views29 pages

14.2.1 Binheap

The document discusses binomial heaps, a data structure useful for implementing mergeable heaps, particularly in scheduling and graph algorithms. It details operations such as Make-Heap, Insert, Minimum, Extract-Min, Union, and Decrease-Key, along with the properties and structure of binomial trees. Additionally, it provides examples and code snippets for operations like Union and linking two binomial heaps.

Uploaded by

kamal deep garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

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(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.)

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 1


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:

Bk = Bk-1 is a binomial tree.


Bk-1
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 2
Examples
B0

B1

B2

B3

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


B0
B1
B2
Bk-2
Bk-1

Bk

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 4


Properties of Binomial Trees
Lemma
Lemma19.1: 19.1:For
Forthethebinomial
binomialtree treeBBkk,,
1.1.There
Thereare are22knodes.
k
nodes.
2.2.Tree
Tree
k
height
heightisisk.k.
3.3.  i  nodes
nodesatatdepth
depthi,i,ii==0,0,1,1,…,
…,kk[binomial
[binomialcoefficients].
coefficients].
4.4.RootRoothas hasdegree
degreek,k,other
othernodes
nodeshavehavesmaller
smallerdegree.
degree.iith
th

child
childof ofroot
rootisisroot
rootof
ofsubtree
subtreeBBi,i,where
whereii==k–1,
k–1,k–2,
k–2,…,…,
00[k–1 [k–1isisLM,
LM,00isisRM].
RM].
Proof:
1. Induction on k. 2. Induction on k.
# nodes in B k height of Bk
2(# nodes in B k -1 ) 1  height of Bk -1
2(2 k  1 ) 1  (k  1)
2 k k
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 5
Proof (Continued)
 k
3. Let D(k,i) = # nodes at depth i in Bk. Want to show D(k,i) =  
i
.

depth i in this Bk-1

Bk
Bk-1 depth i in Bk
Bk-1
depth i–1 in this 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
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 6
Proof (Continued)
4. Root degree of Bk = 1 + root degree of Bk-1
= 1 + k–1 , induction hypothesis
=k

For other properties, see figures.

Corollary
Corollary20.2:
20.2:The
Themaximum
maximumdegree
degreein
inan
ann-node
n-nodebinomial
binomialtree
treeisis
lg
lgn.n.

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 7


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: blg n, …, b0, i.e.,

B.H contains Blgi niff


 b is 1.
n   bi 2
i i

i 1

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 8


Representing Binomial Heaps
10 1 6 called the root list
12 25 14 29
8

18 17 38
11

27

Each node is parent


represented by a key
structure like this
degree
child sibling

10 1 6

12 25 14 29
8

18 17 38
11

27

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 9


Operations on Binomial Heaps
Minimum(H)
Minimum(H)
yy:=
:=NIL;
NIL;
xx:=
:=head[H];
head[H];
min
min:= :=;;
whilexx
while NIL
NILdodo
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).


Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 10
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

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 11


Union
H1, H 2 Union H1  H 2

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.

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 12


Union (Continued)
Union traverses the new root list like this:

prev-x x next-x

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 - 13


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

prev-x x Like binary addition:


head[H] 12 3 6

18
15 7 37
10 44 1 1 1 (carries)
8 29
00111
25
28 33
30 23 22 48 31 17
10011
41
24 50
11010
45 32

55 temporarily have three


trees of this degree
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 14
Code for Union
Union(H
Union(H1,1,HH2)2)
HH:=:=newnewheap;heap;
head[H]
head[H] :=merge(H
:= merge(H1,1,HH2);2); /*/*simple
simplemerge
mergeofofroot
rootlists
lists*/*/
ififhead[H]
head[H]==NIL NILthenthenreturn
returnHHfi;fi;
prev-x
prev-x:=:=NIL; NIL;
xx:=:=head[H];
head[H];
next-x
next-x:=:=sibling[x];
sibling[x];
while next-xNIL
while next-x NILdo do
ifif(degree[x] 
(degree[x]  degree[next-x])or
degree[next-x]) or
(sibling[next-x] 
(sibling[next-x]  NIL and degree[sibling[next-x]]==degree[x])
NIL and degree[sibling[next-x]] degree[x])then
then
Cases 1,2 prev-x :=
prev-x := x; x;
xx:=:=next-x;
next-x;
else
else
key[x]key[next-x]
ififkey[x] key[next-x]then then
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
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 15
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

prev-x x next-x sibling[next-x] prev-x x next-x


a b c d Case 2 a b c d

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
Jim Anderson Comp 750, Fall 2009 Bk+1 Binomial Heaps - 16
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

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 17


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

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 18


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

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 19


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

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 20


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

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 21


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

prev-x x next-x = NIL


head[H] 12 3 6  terminates
18 7 37 44
15 29 10
8

33 25 17
28 22 48 31
30 23

41
45 32 24 50 Note: Union is
O(lg n).
55

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 22


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
listof
ofH
H==x’sx’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

Both are O(lg n).

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 23


Extract-Min Example
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

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 24


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

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 25


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];
whilezzNIL
while 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)

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 26


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

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 27


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

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 28


Delete
Delete(H,
Delete(H,x)
x)
Decrease-Key(H,x,x,);
Decrease-Key(H, );
Extract-Min(H)
Extract-Min(H)

Time is O(lg n)

Will this always work?

Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 29

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy