ppt on binomial trees

29
SHRADDHA GUPTA 3 RD CSE 113/05

Upload: api-3844034

Post on 14-Nov-2014

152 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Ppt on Binomial Trees

SHRADDHA GUPTA

3RD CSE

113/05

Page 2: Ppt on Binomial Trees

COMPARISON OF EFFICIENCY

make-heap

Operation

insert

find-min

delete-min

union

decrease-key

delete

1

Binary

log N

1

log N

N

log N

log N

1

Binomial

log N

log N

log N

log N

log N

log N

1

Fibonacci *

1

1

log N

1

1

log N

1

Relaxed

1

1

log N

1

1

log N

1

Linked List

1

N

N

1

1

N

is-empty 1 1 1 11

Heaps

Page 3: Ppt on Binomial Trees

Binomial TreeRecursive definition:

The Binomial Tree Bk is an

ordered tree defined recursively.Bk consists of 2 Bk-1 trees

linked together,root of 1 is child of the other

Bk-1

Bk-1

B0 Bk

B0 B1 B2 B3 B4

Page 4: Ppt on Binomial Trees

Useful properties of order k binomial tree Bk. Number of nodes = 2k. Height = k. Nodes at depth i= Degree of root = k. Root has degree k which is

greater than degree of any

other node.

Proof. By induction on k.

B1

Bk

Bk+

1

B2B0

i

k

Page 5: Ppt on Binomial Trees

PROOF BY INDUCTION

Let’s assume lemma holds for Bk-11. Bk has 2k nodes:

Bk-1 has 2k-1 nodes

Bk ---> 2 copies of Bk-1 binomial trees

== 2k-1 + 2k-1 nodes == 2k nodes Proved

Page 6: Ppt on Binomial Trees

2. Height(Bk) =k

Let us assume height(Bk-1)=k-1

Bk-1

Bk-1

Bk

Root of 1 tree becomes the left child of the other tree

So, maximum depth of Bk is 1 greater than that of Bk-1

height(Bk) =(k-1 ) +1 =k

Page 7: Ppt on Binomial Trees

3. A property useful for naming the data structure. Bk has nodes at depth i.

B4

i

k

62

4

depth 2

depth 3

depth 4

depth 0

depth 1

At depth 2

Page 8: Ppt on Binomial Trees

Let D(k,i) be the number of nodes at depth i of Bk. Since Bk is composed of 2 Bk-1, a node at depth i in Bk-1 appears in Bk once at depth i and once at i+1.

Nodes at depth i in Bk= nodes at depth i in Bk-1

i

k

+ nodes at depth i-1 in Bk-1D(k,i) =D(k-1,i) + D(k-1,i-1)

=k-1Ci + k-1Ci-1

i

k=

3. Bk has nodes at depth i.

Page 9: Ppt on Binomial Trees

Binomial HeapSequence of binomial trees that satisfy binomial heap property.

each tree is min-heap ordered0 or 1 binomial tree of order k (at most 1 binomial tree

in H has root of degree k.

B4 B0B1

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

37

3 18 H

Page 10: Ppt on Binomial Trees

BINOMIAL HEAP OF GIVEN NUMBER OF NODES

Find binary representation of number Ex : 13= 1101 Number of bits used =floor(lg(13))+1 =4Set of binomial trees present in binomial

heap =B0, B2, B3 as bit 1 is present at the positions 0,2,3

Page 11: Ppt on Binomial Trees

Representing Binomial Heaps

Pointer to parent key degree pointer to pointer to child sibling

Node x;

Parent(x)Key(x)Degree(x)Child(x)Sibling(x)

Page 12: Ppt on Binomial Trees

A binomial heap

282

71

110

50

28 H1515

0

5

H

117

Page 13: Ppt on Binomial Trees

OPERATIONS ON BINOMIAL HEAP

Creation of a new Heap Search for minimum key Union of 2 Binomial trees Insertion of a node Extract minimum Decrease key of a node Delete a node

Page 14: Ppt on Binomial Trees

Creating a new Binomial Heap Head [H]= NIL Simply allocates and returns an object, H is the newly created Binomial Heap.

Running time = (1)

Page 15: Ppt on Binomial Trees

Finding the minimum key

x=head[H], min=infinity, y=NILWhile x!=NIL do if key(x)<min then min=key(x) y=x x=sibling(x)Return y

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

37

318H

O(lg n) running time

Page 16: Ppt on Binomial Trees

UNITING 2 BINOMIAL HEAPS

Linking 2 Bk-1 trees rooted at y and z

zy

25

3716

5

z

yy>z

Linking

Linking

37

5

25

16

Bk-1Bk-1 Bk-1

Bk-1

O(1) running time

Page 17: Ppt on Binomial Trees

Binomial-Link(y,z)1 parent[y]=z2 sibling[y]=child[z]3 child[z]=y4 degree[z]++

y

z

parent

x

Child(z)

Sibling

Child(z)

w

Procedure takes O(1) time.

Page 18: Ppt on Binomial Trees

UNITING HI AND H2 HEAPS

STEP 1 Make Binomial Heap (empty binomial

heap with head= NULL) STEP 2 MERGE the binomial heaps (merging occurs as in merge sort , the

root with the lower degree is appended to root of output root list)

Page 19: Ppt on Binomial Trees

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

37

318H2

41

3328

15

25

712

H1

HEAP 1

HEAP2

Page 20: Ppt on Binomial Trees

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

37

318H2

41

3328

15

25

712

H1

H

12 18

25

7

37

3

41

3328

15

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

H

12 18

25

7

37

3

41

3328

15

Page 21: Ppt on Binomial Trees

After Merging STEP 3 IF HEAD(H) = null RETURN H STEP 4 x=HEAD(H) CASE1: degree(x)<degree(next-x) MOVE AHEAD CASE2: degree(x)=degree(next-x) =degree(sibling(next-

x)) MOVE AHEAD

Page 22: Ppt on Binomial Trees

CASE 3:Degree(x)=degree(next-x) != degree(sibling(next(x))

If( key(x)< key(next(x)) BINOMIAL-LINK(next-x,x)Else BINOMIAL-LINK(x,next-x)

Page 23: Ppt on Binomial Trees

H

12 18

25

7

37

3

41

3328

15

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6x Next-x

Degree(x)=degree(next-x) != degree(sibling(next(x))

If( key(x)< key(next(x)) BINOMIAL-LINK(next-x,x)

CASE 3

Page 24: Ppt on Binomial Trees

H

12

18 25

7

37

3

41

3328

15

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6x Next-x

CASE2: degree(x)=degree(next-x) =degree(sibling(next-x)) MOVE AHEAD

Page 25: Ppt on Binomial Trees

H

12

1825

7

37

3

41

3328

15

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6Prex x x Next x

CASE 3:Degree(x)=degree(next-x) != degree(sibling(next(x))

BINOMIAL-LINK(x,next-x)

25

7

x Next x

Page 26: Ppt on Binomial Trees

H

12

1837

3

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6Prev x

41

3328

15

25

7

x

Next x

UNION OF 2 BINOMIAL HEAPS

Page 27: Ppt on Binomial Trees

INSERT INTO BINOMIAL HEAP(H1)

1 Make a new Binomial Heap H12 H <- BINOMIAL HEAP UNION (H, H1)

O(lg n) running time

H

NullH1

Page 28: Ppt on Binomial Trees

EXTRACT MINIMUM NODE

1 Search minimum node ‘x’ and remove it from root list

2 H1 <- make binomial heap3 reverse the order of the linked list of

x’s children and set head(H1) to point to the head of resulting list

4 H <- BINOMIAL HEAP UNION(H,H1)Return x

Page 29: Ppt on Binomial Trees