Tải bản đầy đủ (.pdf) (5 trang)

phân tích va thiết kế giải thuật dương tuấn anh appendix a heap bottom up construction sinhvienzone com

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (182.65 KB, 5 trang )

om

nh
Vi
en

Zo

ne

.C

Appendix A: Heap Bottomup Construction

Si

Course: Algorithm Analysis and Design

SinhVienZone.com

/>
1


Heap bottom up construction

Si

nh
Vi
en



Zo

ne

.C

om

This method views every position in the array as the root of a
small heap and uses downheap procedure for such small
heaps.

Figure 1: The heap created from the array of characters: A, S, O, R, T, I,
N, G, E, X, A, M, P, L, E.
SinhVienZone.com

/>
2


nh
Vi
en

Zo

ne

.C


procedure build_heap;
begin
for k:= M div 2 downto 1 do
downheap(k);
end

om

Heap bottom-up construction procedure

Si

M: the number of elements in the heap.
The keys in a[ (M div 2)+1 .. M] each form heaps of one
element, so they satisfy the heap condition and don’t
need to be checked.

SinhVienZone.com

/>
3


Property: Bottom-up heap construction is lineartime.
For example: To build a heap of 127 elements, the
method calls downheap on
- 64 heaps of size 1
- 32 heaps of size 3
- 16 heaps of size 7

- 8 heaps of size 15
- 4 heaps of size 31
- 2 heaps of size 63
- 1 heaps of size 127
So the method needs 64.0 + 32.1 + 16.2 + 8.3 + 4.4 +
2.5 + 1.6 = 120 “promotions”.
0.26 + 1.25 + 2.24 + 3.23 + 4.22 + 5.21 + 6.20

Si

nh
Vi
en

Zo

ne

.C

om



SinhVienZone.com

/>
4



om

0.26 + 1.25 + 2.24 + 3.23 + 4.22 + 5.21 + 6.20
(M= 127 = 27 -1) m = 7

Si

nh
Vi
en

Zo

ne

.C

For M = 2m, an upper bound on the number of comparisions is
(1-1)2m-1 + (2-1)2m-2 + (3 -1)2m-3+… + (k-1)2m-k + … (m-1-1)2m-(m-1)
+ (m -1)2m-m.
= 1.2m-2 + 2.2m-3 +3.2m-4+ 4.2m-5 +…+ (k-1)2m-k + …
(m-2)21 + (m -1)20
= (2m-2 + 2m-3 + …+ 20) + (2m-3 + …+ 20) + (2m-4 + …+ 20) …+(22 + 21
+ 20) + (21 + 20) + 1
= (2m-1 -1)+ (2m-2 -1) + … (23-1) + (22-1) +(21-1)
= (2m-1 + 2m-2 + … 22 + 21)– m +1
= (2m-1 + 2m-2 + … 22 + 21+1) – m
= (2m -1) – m < M
So the complexity of heap bottom-up building is O(M).


SinhVienZone.com

/>
5



×