MHB Proving Correctness of Heap Building Algorithm

  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Algorithm
Click For Summary
The discussion centers on proving the correctness of the BUILDHEAP algorithm, specifically focusing on the assertion that at the start of each iteration of the for loop, every node from i+1 to n is the root of a max-heap. It is emphasized that proving this statement is sufficient for establishing the overall correctness of the algorithm. The reasoning is that if each node satisfies the max-heap property, then the entire structure will also satisfy it after the heapify process is applied. The mention of the "sift down" algorithm clarifies that this method effectively transforms a subtree into a heap, starting from the root, which simplifies the proof process. The participants agree that the correctness of the heapify operation is crucial, as it ensures that the properties of the heap are maintained throughout the iterations.
evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Nerd)

We are given the following algorithm:

Code:
1.BUILDHEAP(A) 
2.    for (i=floor(size(A))/2; i>=0; i--)
3.          HEAPIFY(A,i);

according to my notes, we could prove its correctness, proving the following sentence:

At the beginning of each iteration of the for loop at the lines 2-3, each node $i+1, i+2, \dots, n $ is the root of a max-heap.

Could you explain me why it suffices to prove the above sentence?

Also, how could we prove it? (Thinking)
 
Technology news on Phys.org
You ignored "heapify". Assuming this is the standard "sift down" algorithm, I don't see that there's much of anything to prove. Sift down produces a heap, starting at the "root".
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
14
Views
3K
  • · Replies 30 ·
2
Replies
30
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K