0% found this document useful (0 votes)
84 views39 pages

Binomial Heaps: Sachin Tripathi

Binomial heaps are a data structure for implementing mergeable priority queues. They are composed of binomial trees that satisfy two properties: 1) each node's key is greater than or equal to its children's keys, and 2) there is at most one tree of each degree. Common operations on binomial heaps include insertion, deletion, merging, and decreasing a key. Trees are represented using left-child/right-sibling pointers and merged by connecting roots while preserving heap order.

Uploaded by

Mohit Tripathi
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)
84 views39 pages

Binomial Heaps: Sachin Tripathi

Binomial heaps are a data structure for implementing mergeable priority queues. They are composed of binomial trees that satisfy two properties: 1) each node's key is greater than or equal to its children's keys, and 2) there is at most one tree of each degree. Common operations on binomial heaps include insertion, deletion, merging, and decreasing a key. Trees are represented using left-child/right-sibling pointers and merged by connecting roots while preserving heap order.

Uploaded by

Mohit Tripathi
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/ 39

Binomial Heaps

Sachin Tripathi
IIT(ISM), Dhanbad

Slides from Prof. Jim Anderson, California State University, Northridge


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.

Base Case: B0 = single node is a binomial tree.

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

depth i in this Bk-1

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

The maximum degree in an n-node binomial tree is lg n.


Binomial Heaps
 Set of binomial trees satisfying binomial-heap properties:
1 Heap ordered:
 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 Bi iff bi is 1.

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

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

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

55 temporarily have three


trees of this degree
Cases
prev-x x next-x sibling[next-x] 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
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

prev-x x next-x = NIL


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

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

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