Sie sind auf Seite 1von 30

A SEMINAR ON

BINOMIAL HEAPS
PRESENTED BY:PRAMOD TRIPATHI M.TECH 1ST SEM E.NO.112109

Introduction
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. (Its assumed that k e x.key.)

Jim Anderson

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 2 4 5 3 Different Trees 3 4 1 2 5

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


Bk-1
Jim Anderson Binomial Heaps - 3

Bk-1

is a binomial tree.

Examples
B0
B1 B2

B3 depth # nodes 0 1 1 2 3 4
Jim Anderson

B4

4 6 4 1
Binomial Heaps - 4

Another Way to Look at Bk

Bk-2 Bk-1 Bk

B2

B1

B0

Jim Anderson

Binomial Heaps - 5

Properties of Binomial Trees


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 = k1, k2, , 0. Proof: 1. Induction on k.
# nodes in B k ! 2(# nodes in Bk -1 ) ! 2(2 k 1 )
Jim Anderson

2. Induction on k.
height of B k ! 1  height of B k -1 ! 1  (k  1) !k
Binomial Heaps - 6

! 2k

Proof (Continued)
3. Let D(k,i) = # nodes at depth i in Bk. Want to show D(k,i) =
depth i in this Bk-1 Bk Bk-1 depth i1 in this Bk-1 So, Bk-1 depth i in Bk
k i

D(k, i) ! D(k  1, i)  D(k  1, i  1) k  1 k  1 ! , by ind. hyp. i  i 1 (k  1)! (k  1)! !  i!(k  1  i)! (i  1)!(k  i)!
Jim Anderson

(k  1)!(k  i)  i(k  1)! ! i!(k  i)! k!i(k  1)!i(k  1)! ! i!(k  i)! k ! i
Binomial Heaps - 7

Proof (Continued)
4. Root degree of Bk = 1 + root degree of Bk-1 = 1 + k1 , induction hypothesis =k

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

Jim Anderson

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

Jim Anderson

Binomial Heaps - 9

Representing Binomial Heaps


10 1 25 6

called the root list

12 18

8 17

14 38

29

11 27

Each node is represented by a structure like this

parent key degree child sibling

10

1 25

12 18

8 17

14 38

29

11 27

Jim Anderson

Binomial Heaps - 10

Operations on Binomial Heaps


Minimum(H){ y := NIL; x := head[H]; min := g; while x { NIL do{ if key[x] < min then min := key[x]; y := x } x := sibling[x] } return y Time is O(lg n).
Jim Anderson 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

y Bk-1

z Bk-1 Link

z y Bk-1 Bk-1

Jim Anderson

Binomial Heaps - 12

Union
H 1, H 2 H1 = Union H1 H2 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 Binomial Heaps - 13

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 Binomial Heaps - 14

Analogy
head[H1]
12 7 25 15 33

head[H2]

18

3 37

28 41

8 22 48 50

29 31

10

44

30 32

23 24

17

45

Union
55

prev-x head[H]
12 18 3 37 8 25 30 41 45 55 32 23 24 22 48 50 29 31

x
6

Like binary addition: 1 1 1 (carries) 00111 10011 11010 temporarily have three trees of this degree
Binomial Heaps - 15

15 33

10

44

28

17

Jim Anderson

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] e key[next-x] then sibling[x] := sibling[next-x]; Case 3 Link(next-x, x) else Case 4 if prev-x = NIL then head[H] := next-x else sibling[prev-x] := next-x fi Link(x, next-x); x := next-x } } next-x := sibling[x] Jim Anderson } return H Binomial Heaps - 16

prev-x

next-x sibling[next-x]

Cases
Case 1 a

prev-x

next-x

b
Bk

c
Bl

b
Bk

c
Bl
x

prev-x

next-x sibling[next-x]

b
Bk

c
Bk

d
Bk

Case 2

prev-x

next-x

b
Bk

c
Bk
x

d
Bk
next-x

prev-x

next-x sibling[next-x]

Case 3

prev-x

a c

b
Bk Bk Bk+1

d
Bl

Bl Bk Bk key[x] e key[next[x]]
prev-x

prev-x

next-x sibling[next-x]

Case 4

next-x

a b

c
Bk Bk

d
Bl

Bl Bk Bk key[x] > key[next[x]]


Jim Anderson

Bk+1 Binomial Heaps - 17

Union Example
head[H1]
12 7 25 15 33

head[H2]

18

3 37

28 41

8 22 48 50

29 31

10

44

30 32

23 24

17

45 55

Merge
x head[H]
12

next-x
18 7 25 3 37 15 33 8 41 30 32 23 24 22 48 50 29 31 6

28

10

44

17

45 55

Jim Anderson

Binomial Heaps - 18

Union Example (Continued)


x head[H]
12

next-x
18 7 25 3 37 15 33 8 41 30 32 23 24 22 48 50 29 31 6

28

10

44

17

45 55

Case 3
x head[H]
12 18

next-x
7 25 3 37 28 41 30 32 23 24 22 48 50 15 33 8 29 31 6

10

44

17

45 55

Jim Anderson

Binomial Heaps - 19

Union Example (Continued)


x head[H]
12 18

next-x
7 25 3 37 15 33 8 41 30 32 23 24 22 48 50 29 31 6 28

10

44

17

45 55

Case 2
prev-x head[H]
12 18

x
7 25

next-x
3 37 15 33 8 41 30 32 23 24 22 48 50 29 31 6 28 44

10

17

45 55

Jim Anderson

Binomial Heaps - 20

Union Example (Continued)


prev-x head[H]
12 18

x
7 25

next-x
3 37 15 33 8 41 30 32 23 24 22 48 50 29 31 6 28 44

10

17

45

Case 4

55

prev-x head[H]
12 18

x
3 37 28 41

next-x
15 33 8 29 31 6 44

7 25

10

30 32

23 24

22

48 50

17

45 55

Jim Anderson

Binomial Heaps - 21

Union Example (Continued)


prev-x head[H]
12 18

x
3 37 28 41

next-x
15 33 8 29 31 6 44

7 25

10

30 32

23 24

22

48 50

17

45

Case 3

55

prev-x head[H]
12 18

x
3 37 8 25 30 23 24 22 48 50 29 31

next-x
6 44

15 33

10

28 41

17

45 55

32

Jim Anderson

Binomial Heaps - 22

Union Example (Continued)


prev-x head[H]
12 18

x
3 37 8 25 30 23 24 22 48 50 29 31

next-x
6 44

15 33

10

28 41

17

45

32

Case 1

55

prev-x head[H]
12 18 3 37 8 25 30 41 45 55 32 23 24 22 48 50 29 31

x
6

next-x = NIL terminates

15 33

10

44

28

17

Note: Union is O(lg n).


Binomial Heaps - 23

Jim Anderson

Insert and Extract-Min


Insert(H, x) Hd := Make-B-H(); p[x] := NIL; child[x] := NIL; sibling[x] := NIL; degree[x] := 0; head(Hd) := x; H := Union(H, Hd) Extract-Min(H) remove minimum key root x from Hs root list; Hd := Make-B-H(); root list of Hd = xs children in reverse order; H := Union(H, Hd); return x

Both are O(lg n).

Jim Anderson

Binomial Heaps - 24

Extract-Min Example
head[H]
37 41 28 77 8 17 14 38 29 26 42 10 13 6 16 23 1 25 12

18

11 27

head[H]

x
37 41 28 77 8 17 14 38 29 26 42 10 13 6 16 23 1 25 12

18

11 27

Jim Anderson

Binomial Heaps - 25

Extract-Min Example (Continued)


head[H]
37 41 28 77 10 13

head[Hd]

25

12

16 23

6 29

18

26 42

8 17

14 38

11 27

head[H]

25 37 41

12 18 10 13 11 27 8 17

6 29

14

16 23

28 77

38

26 42

Jim Anderson

Binomial Heaps - 26

Decrease-Key
Decrease-Key(H, x, k)
if k > key[x] then error ;} 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] }

O(lg n)
Jim Anderson Binomial Heaps - 27

Decrease-Key Example
head[H]
25 37 41 16 23 28 77 13 11 27 12 18 10 8 17 6 29 14 38

26 42

Decrease key 26 to 7

head[H]

25 37 41

12 18 10 8 17

6 29

14

z y
7 42

16 23

28 77

13

11 27

38

Jim Anderson

Binomial Heaps - 28

Decrease-Key Example (Continue)


head[H]
25 37 41 12 18 6

z
7 23 28 77

10 13 11 27

8 17

14

29

y
16 42

38

head[H]

z
25 37 41 10 23 28 77 13 11 27 12 18 6

y
7

8 17

14

29

38

16 42

Jim Anderson

Binomial Heaps - 29

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

Time is O(lg n)

Jim Anderson

Binomial Heaps - 30

Das könnte Ihnen auch gefallen