Vous êtes sur la page 1sur 29

SHRADDHA GUPTA

3RD CSE
113/05
COMPARISON OF EFFICIENCY
Heaps
Operation Linked List Binary Binomial Fibonacci * Relaxed
make-heap 1 1 1 1 1
insert 1 log N log N 1 1
find-min N 1 log N 1 1
delete-min N log N log N log N log N
union 1 N log N 1 1
decrease-key 1 log N log N 1 1
delete N log N log N log N log N
is-empty 1 1 1 1 1
Binomial Tree
 Recursive definition: B0 Bk
The Binomial Tree Bk is an
ordered tree defined recursively. Bk-1
Bk consists of 2 Bk-1 trees Bk-1
linked together,root of 1 is child
of the other

B0 B1 B2 B3 B4
Useful properties of order k binomial tree Bk.
 Number of nodes = 2k.
 Height = k.
k 
 Nodes at depth i=   B
i
Degree of root = k.
k+1

 Root has degree k which is


B1 B0
greater than degree of any B2
Bk
other node.

Proof.

 By induction on k.
PROOF BY INDUCTION
Let’s assume lemma holds for Bk-1
• 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
2. Height(Bk) =k
Let us assume height(Bk-1)=k-1

Bk
Bk-1

Bk-1

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


So, maximum depth of Bk is 1 greater than that of Bk
height(Bk) =(k-1 ) +1 =k
3. A property useful for naming the data
structure.
 Bk has  k  nodes at depth i.
 
i

4
 =6 At depth 2
2

depth 0

depth 1

depth 2

depth 3

depth 4 B4
k 
3. Bk has   nodes at depth i.
i

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


+ nodes at depth i-1 in Bk-1
D(k,i) =D(k-1,i) + D(k-1,i-1)
=k-1Ci + k-1Ci-1
= k 
 
i
Binomial Heap
Sequence of binomial trees that satisfy
binomial heap property.
each tree is min-heap ordered
0 or 1 binomial tree of order k (at most 1 binomial tree

in H has root of degree k.


6 3 18 H

8 29 10 44 37

30 23 22 48 31 17

45 32 24 50

55
B4 B1 B0
BINOMIAL HEAP OF GIVEN
NUMBER OF NODES
 Find binary representation of number
Ex : 13= 1101
Number of bits used =floor(lg(13))+1
=4
Set of binomial trees present in binomial
heap =B0, B2, B3 as bit 1 is present at
the positions 0,2,3
Representing Binomial Heaps

Node x;

Parent(x) Pointer to parent


Key(x)
Degree(x)
key
Child(x) degree
Sibling(x)
pointer to pointer to
child sibling
A binomial heap
H

15 28
15 H 0 2
28

7 11
7 11
1 0

5
0
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
Creating a new Binomial Heap
 Head [H]= NIL
Simply allocates and returns an object,
H is the newly created Binomial Heap.

Running time = (1)


Finding the minimum key

x=head[H], min=infinity, y=NIL


While x!=NIL
do if key(x)<min
then min=key(x)
y=x
x=sibling(x)
Return y 6
H 18 3

8 29 10 44
37

30 23 22 48 31 17
O(lg n) running time
45 32 24 50

55
UNITING 2 BINOMIAL HEAPS

Linking 2 Bk-1 trees rooted at y and z z


y z y>z
y
Linking Bk-1
Bk-1 Bk-1
Bk-1

16 5

Linking 16 37
25
37
25
O(1) running time
Binomial-Link(y,z)
1 parent[y]=z
2 sibling[y]=child[z]
3 child[z]=y z
n t
r e
4 degree[z]++ pa
( z ) Child(z)
d
C hil
y
x
Sibling

Procedure takes O(1) time.


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)
H2 18 3 6

37 8 29 10 44
HEAP 1

30 23 22 48 31 17

45 32 24 50
H1
55
12 7 15

HEAP2
25 28 33

41
H2 18 3
6
H1
37 8 29 10
12 7 15 44

30 23 22 48 31 17
25 28 33

45 32 24 50
41

55 6
H
H 15
12 18 7 3 15 29
12 8 10 44
18 7 3
28 33
25 37
30 23 28 22 48
33 31 17
25 41 37

45 32 24 41 50
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
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)
H x Next-x 6
15
12 18 7 3

28 33 8 29 10 44
25 37

41
30 23 22 48 31 17

45 32 24 50

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

If( key(x)< key(next(x))


BINOMIAL-LINK(next-x,x)
H x Next-x 6
15
12 7 3

28 33 8 29 10 44
18 25 37

41
30 23 22 48 31 17

45 32 24 50

55

CASE2: degree(x)=degree(next-x)
=degree(sibling(next-x))
MOVE AHEAD
H Prex x x x x Next x 6
Next
15
12 7 3

28 33 8 29 10 44
25
7 37
18
41
30 23 22 48 31 17
25

45 32 24 50

CASE 3: 55
Degree(x)=degree(next-x)
!= degree(sibling(next(x))
BINOMIAL-LINK(x,next-x)
Next x
H Prev x x 6
3
12

37 8 29 10 44
18 15 7

30 23 22 48 31 17
28 33 25

45 32 24 50
41

55

UNION OF 2 BINOMIAL HEAPS


INSERT INTO BINOMIAL HEAP(H1)

1 Make a new Binomial Heap H1


2 H <- BINOMIAL HEAP UNION (H, H1)
H
H1 Null

O(lg n) running time


EXTRACT MINIMUM NODE
1 Search minimum node ‘x’ and remove
it from root list
2 H1 <- make binomial heap
3 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

Vous aimerez peut-être aussi