download

Matakuliah
Tahun
Versi
: T0026/Struktur Data
: 2005
: 1/1
Pertemuan 20
Binomial Heap
1
Learning Outcomes
Pada akhir pertemuan ini, diharapkan mahasiswa
akan mampu :
• Mahasiswa dapat merumuskan program
modular untuk mengimplementasikan
Binomial dan fibonacci heap
2
Outline Materi
•
•
•
•
•
•
Why binomial heaps
Binomial trees
Binomial max heaps
Insert
Merging two binomial heaps
Delete max
3
<<ISI>>
4
<< CLOSING>>
5
6
Why Binomial Heaps?
• The growth rates for MaxHeap
operations are:
– find-max() is O(1)
– insert(k ), delete-max() is O(lgn)
– merge(heap1, heap2)
– 2 methods:
• “append” the heaps and then use fast-buildheap. O(n) where n is the total number of
elements
• “append” the heaps and then percolate the
nodes in the smaller heap
– If number elements in small heap proportional to n,
7
Why Binomial Heaps?
• Binomial Heaps have improved runtime
for some of the primary operations of
heaps:
– find-max() is O(1)
– delete-max() is O(lgn)
– insert(k ) amortized O(1) and worst case is
O(lg n)
B1
B3
– merge(heap1, heap2) with a total of n
elements is O(lgn)
– Example of a Binomial heap shown below
8
Binomial Trees
• The i th binomial tree, Bi , i  0 has a root
with
i
children,
B
,
…
,
B
0
i-1
B
B
B
B
0
1
2
3
9
The Number of Nodes
• The number of nodes
at depth
i  i 
i





,
,...
 0  1
i

0,1,2, …, i are
   
 
depth n
0
1
2
3
4
4

0

 
4

1 

 
4

2

 
B4
4

3

 
4

4

 
10
Binomial (Maximum) Heap
• Is a collection of binomial trees of distinct
sizes each of which has the heap property.
• The roots of the binomial trees are
connected (as a doubly linked list) in the
order of increasing size.
B
B
B
• There
29 is a pointer
35 to the largest
23 key.
0
1
21
3
9
14
1
20
5
17
8
11
Structure of a binomial heap
• There is a relation between the binary
representation of a number, and the
structure of a binomial heap
• Any number n can be represented
uniquely as a binary number with
0 bits
1
2
last
lgbn+1
n 
2

b
2

b
2

...

b
2
0
1
2
last
last  lg n
• where
The representation
is:
12
Structure of a binomial heap
• Any binomial heap with n items can be
represented with at most  lg n+1
binomial trees
• Let bi = 1 if Bi is in the binomial heap and
bi = 0 otherwise
• nLet
of 
nodes
the
 |B
b0i| denote
| B0 | the
b1 |number
B1 | ...
blast in
|B
last
binomiallast
tree  lg n 
where
13
|
Merging same size binomial
heaps into one binomial tree
• Link trees - Make the root of the tree with
the smaller max value, the i+1 child of the
binomial tree with the larger max value.
15
– O(1)
12
7
5
+
9
2
2

3
15
5
12
7
2
2
9
3
14
Insert New
Note: The correspondence between “count”
and inserting a new key into a binomial
heap
1) Convert item to be inserted into a B*0
2) Set i to 0
3) If H includes a Bi
A) Remove Bi from H.
B) Link Bi with B*i to form B*i+1
C) Set i to i +1
D) Repeat step 3
4) link B*i with H and update max pointer.
15
Example for insertion
B0
B1
B4
B2
4 trees
23 nodes
merge with
B*0
i = 0. Remove B0 and link it with B0* getting
B
B1* B*
1
1
i =0
i = 1. Remove B1, link B1* with B1 getting
B
B
B2*B*
2
4
2
i =1
16
Example Continued
I = 2. Remove B2, link B2* with B2 getting
B
B3* B*
4
3
i =2
i = 3. H does not include B3. link B*3 with
B4 .B*Binomial heap has 2 binomial trees
B
and 24 nodes..
3
4
i =3
2 trees
24 nodes
17
Doing a sequence of
insertions
Insert 10
Insert 20
20
10
10
18
Doing a sequence of
insertions
Insert 3
3
20
10
19
Doing a sequence of
insertions
3
20
Insert 8
20
10
10
8
First 3 is removed
Then 3 and 8 are joined
Then the two B1s are joined
3
20
Doing a sequence of
insertions
Insert 30
30
20
10
8
3
21
Doing a sequence of
insertions
Insert 15
30
15
20
10
8
3
22
Amortized analysis for insert
new
Assume n=2k inserts
• For n/2 inserts the binomial heap does not
have a B0 and 1 link is done
• For n/4 inserts the binomial heap has B0
but no B1 and 2 links are done
• For n/8 inserts the binomial heap has B0
and B1 but not B2 and 3 links are done
• ...
• For n/2k inserts the binomial heap has B0 ,
23
B1,…Bk-2 and either not Bk-1 or Bk-1 and k
Merge 2 Binomial Heaps,
H1 and H2 into Result
Note: The correspondence between adding
two binary numbers and merging two
binomial heaps
Merge H1 and H2 into a new binomial heap,
result.
stage 0:
1. If neither contain B0 do nothing
2. If only one binomial heap includes a B0
put it into the result.
3. If both include B0 , link them into B1 and24
Merge 2 Binomial Heaps,
H1 and H2 into Result
stage i (i = 1,…, lg n):
There may be 0 to 3 Bi ‘s, one from H1,
one from H2 and one from the previous
stage.
1. If no Bi do nothing
2. If there is only one Bi put it into the
result.
3. Otherwise link two Bi ‘s into Bi+1 and
save for next stage.
4. If there is still a Bi put it in the result.
25
deleting-max
1) Remove the Bi containing the max from
H, joining remaining parts into a new
binomial heap H1.
2) Remove root of Bi .link the binomial
subtrees of the root into a new binomial
heap H2.
3) Merge H1 and H2.
Time:
1) O(1)
26