Download presentation

Presentation is loading. Please wait.

Published byKayleigh Bendel Modified over 2 years ago

1
1 Appendix A: Heap Bottom- up Construction Course: Algorithm Analysis and Design

2
2 Heap bottom up construction 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.

3
3 Heap bottom-up construction procedure procedure build_heap; begin for k:= M div 2 downto 1 do downheap(k); end 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.

4
4 Property: Bottom-up heap construction is linear- time. 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.2 6 + 1.2 5 + 2.2 4 + 3.2 3 + 4.2 2 + 5.2 1 + 6.2 0

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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google