I’m trying to understand a simple concept regarding heaps.
I know that BuildHeap using Floyd algorithm takes Theta(n) to build a heap of size n. The way we get this running time is by building the heap from bottom up – in this way the larger quantity of heap do less work.
I had an exerecise about this concept but with one different detail, the question was the following :
“Suppose the known ‘FixHeap’ takes Theta(log(log(n)) instead of
Theta(log(n)). What will be the running time of the BuildHeap
algorithm to build a max heap of size N using the new ‘FixHeap’
algorithm(the one which takes now only Theta(log(log(n)))”
I don’t understand how the running time of the new given FixHeap affects the overall algorithm running time. Can you please help me to understand what the changes will be?
The FixHeap is the known algorithm which compares the parent to its left and right child and put the largest value in the parent. FixHeap on a vertex which is the parent of a leaf will do maximum one replacement, and the parent of this vertex might do two replacement and so on..
EDIT : The current BuildHeap running time is calculated by the following expression:
(n/4) * 1 + (n/2) * 2 + (n/3) * 3 +……+ 1 * logn-1
Just because there are n/4 parent of the leaves which do 1 change at maximum, and there are n/2 which do 2 changes at maximum and so on…
I just can’t see the change in the formula now.
Thank you!
This algorithm will still take Θ(n) time. To see this, we can show that the algorithm is O(n) and Ω(n).
For the O(n) part, note that this algorithm clearly can’t be any asymptotically slower than the version in which FixHeap takes O(log n) time each. Since the heapify algorithm takes O(n) time in that second case, we can get an O(n) time bound for the O(log log n) time case as well.
For the Ω(n) part, note that the heapify algorithm works by executing the FixHeap procedure once for each element in the first half of the array. It takes at least Ω(n) time to iterate over half of an n-element array, giving us the lower bound we need.
Hope this helps!