1

"Data Structures and Network Algorithms" by Tarjan states the heapify function in leftiest heaps as following:

heap function heapify (list q);
  do |q| ≥ 2 → := q[3..] & meld (q(1), q(2)) od;
  return if q = [ ] → null | q != [ ] → q(1) fi
end heapify;

q is being the queue of heaps we would like to heapify together. meld(h1, h2) is being a function for merging two heaps and restoring the heap property. meld(h1, h2) is having the complexity O(logn), where n is the total number of nodes in both heaps. This makes the complexity for one pass through the queue as following:

enter image description here(Eq. 1)

which is plausible. What I do not get is the time for the entire heapify as it is:

enter image description here(Eq. 2)

k here is the number of original heaps and n the total number of items they contain. There are also two constrains mentioned beforehand:

enter image description here(Eq. 3)

Can somebody help me understand how the Eq. 2 is derived? (how one interprets the left expression of the equality in Eq. 2)

Alexander Weber
  • 789
  • 4
  • 16

1 Answers1

0

I figured out the solution, which turned out to be pretty obvious:

in each iteration, the number of queue elements is halved. This makes it k/2 per iteration. Looking at all iterations, the number is exponentially shrinking to the base of 2, i.e. for i=0 we have k; i=1 k/2; i=2 k/4; i=3 k/8 and s.o. That is why the sum goes up to lg k and the number of heaps is decreasing by k/(2^i). The sum in Eq. 3 tells us, that all elements are spread between the heaps of the current run. As we just figured out, the number of heaps per iteration is being k/(2^i). That is why the maximum for the meld per queue run is being n_i = n/(k/(2_i)) = n*(2_i)/k. All this together explains the Eq. 2 and after dropping the constants we get the equation on the right side.

Alexander Weber
  • 789
  • 4
  • 16