MHB Proving Correctness of Heap Building Algorithm

  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Algorithm
AI Thread 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".
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top