Prove that binary heap build max comparsion is (2N-2)
Asked Answered
S

1

6

I am trying to prove that for binary heaps, buildHeap does at most (2N-2) comparisons between elements. I find it very difficult to prove this claim.

Septuplicate answered 11/4, 2018 at 11:42 Comment(5)
What have you tried so far?Gassing
I understood the complexity of running time. Sum h=0 -> Log N | ( n / 2^(h+1) ) * O(H)Septuplicate
Possible duplicate of build heap time complexity worst case vs upper bound / tight upper boundGassing
No its not duplicate, i am not talking about time complexity, my question refer to comparisons number.Septuplicate
The focus of the question might be different, yeah. Yet, the complexity analysis involves counting the number of comparisons. In fact, if you refer to the answer to that question, it is quite similar to, if not more detailed than, the answer given to your question.Gassing
I
12

The build-heap algorithm starts at the midpoint and moves items down as required. Let's consider a heap of 127 items (7 levels). In the worst case:

64 nodes (the leaf level) don't move at all
32 nodes move down one level
16 nodes move down two levels
 8 nodes move down three levels
 4 nodes move down four levels
 2 nodes move down five levels
 1 node moves down six levels

So in the worst case you have

64*0 + 32*1 + 16*2 + 8*3 + 4*4 + 2*5 + 1*6
0 + 32 + 32 + 24 + 16 + 10 + 6 = 120 swaps

So in the worst case, build-heap makes fewer than N swaps.

Because build-heap requires that you swap an item with the smallest of its children, it requires two comparisons to initiate a swap: one to find the smallest of the two children, and one to determine if the node is larger and must be swapped.

The number of comparisons required to move a node is 2*(levels_moved+1), and no more than N/2 nodes will be moved.

The general case

We need to prove that the maximum number of comparisons is no more than 2N-2. As I noted above, it takes two comparisons to move a node one level. So if the number of levels moved is less than N (i.e. (N-1) or fewer), then the maximum number of comparisons cannot exceed 2N-2.

I use a full heap in the discussion below because it represents the worst case.

In a full heap of N items, there are (N+1)/2 nodes at the leaf level. (N+1)/4 at the next level up. (N+1)/8 at the next, etc. You end up with this:

(N+1)/2 nodes move 0 levels
(N+1)/4 nodes move 1 level
(N+1)/8 nodes move 2 levels
(N+1)/16 nodes move 3 levels
(N+1)/32 nodes move 4 levels
...

That gives us the series:

((N+1)/2)*0 + ((N+1)/4)*1 + ((N+1)/8)*2 + ((N+1)/16)*3 ...

Let's see what that does for heaps of different sizes:

heap size  levels   levels moved
   1         1          0
   3         2          1
   7         3          2*1 + 1*2 = 4
   15        4          4*1 + 2*2 + 1*3 = 11
   31        5          8*1 + 4*2 + 2*3 + 1*4 = 26
   63        6          16*1 + 8*2 + 4*3 + 2*4 + 1*5 = 57
   127       7          32*1 + 16*2 + 8*3 + 4*4 + 2*5 + 1*6 = 120
         ....

I ran that for heaps up of up to 20 levels (size a million and change), and it holds true: the maximum number of levels moved for a full heap of N items is N-log2(N+1).

Taking the above series as an Arithetico-geometric Sequence, we compute the sum for log2(N + 1) - 1 terms, ignoring the first term as it is zero, to be equal to N - 1. (Recall that a full binary tree has log2(N + 1) levels)

This sum represents the total number of times a siftup operation was performed. The total number of comparisons thus required is 2N - 2 (since each sift up operation requires two comparisons). This is also the upper bound, since a full binary tree always represents the worst case for a given tree depth.

See also https://mcmap.net/q/541270/-build-heap-time-complexity-worst-case-vs-upper-bound-tight-upper-bound, which gives a more formal explanation of the math involved.

Idiocy answered 11/4, 2018 at 18:19 Comment(7)
maybe i miss something but I don't understand why 2*(levels_moved+1)= 2n-2.Septuplicate
@Septuplicate Yes, I think you're missing something. If you know that build-heap always does fewer than N level moves (i.e. levels_moved <= (N-1)), and it takes at most 2 comparisons for every level move, then the maximum number of comparisons is 2*(N-1), which is equal to 2N-2.Idiocy
@JimMischel I can see why number of levels moved for a full heap items is N-log2(N+1) but I don't get what you did next.. how did u calculate the series? how it came to be N-1? can you show your formula maybe? also you can see that if you have full heap with, for example 7 nodes, the maximum number of comparisons is 8.. and 2*7-2=12 .. there is something that I don't understand here :(Munich
@Munich The original problem statement: Prove that the maximum number of comparisons is no more than 2N-2. So we have to prove that the maximum number of moves for a of 7 items is no more than 12. You said yourself that the real number is 8. 8 is no more than 12. What's the problem?Idiocy
Oh I get it.. and what about the sum you did? I don't see how it finally came to N-1?Munich
@Munich You'll have to be more explicit. I showed with the first example that a heap of 127 items will do at most 120 swaps.Idiocy
what I mean is, how did u calculate the Arithetico-geometric Sequence? and WHY with this sum?Munich

© 2022 - 2025 — McMap. All rights reserved.