Algorithm Search⌘K

Proofs: Heap delete

These are the human proofs of Heap delete, at full length.

HEAP-DELETE removes the key at index i and leaves a max-heap full

This proof shows: correctness, termination.

Write ss for A.heap-sizeA.\mathit{heap\text{-}size} after line L4, so s=n−1s = n - 1, and p(j)=⌊j/2⌋p(j) = \lfloor j/2 \rfloor. An edge is a pair (p(j),j)(p(j), j) with 2≤j≤s2 \le j \le s, and it holds when A[p(j)]≥A[j]A[p(j)] \ge A[j]. Let C(i)C(i) be the set of children of ii with index at most ss.

We use two lemmas whose proofs are on other pages, for a max-heap in place of a min-heap.

  • Lemma U (sift-up). If each edge (p(j),j)(p(j), j) with j≠ij \ne i holds, and, when i≥2i \ge 2, A[p(i)]≥A[c]A[p(i)] \ge A[c] for each c∈C(i)c \in C(i), then SIFT-UP(A,i)(A, i) stops after at most ⌊lg⁡i⌋\lfloor \lg i \rfloor swaps and afterwards each edge holds. This is the invariant proof of heap increase-key, whose loop is lines L9 to L11.
  • Lemma D (sift-down). If each edge (p(j),j)(p(j), j) with p(j)≠ip(j) \ne i holds, and, when i≥2i \ge 2, A[p(i)]≥A[c]A[p(i)] \ge A[c] for each c∈C(i)c \in C(i), then MAX-HEAPIFY(A,i)(A, i) stops after at most ⌊lg⁡(s/i)⌋\lfloor \lg(s/i) \rfloor swaps and afterwards each edge holds. This is the lemma of heap extract-max, whose loop is lines L12 to L17.

Both only swap keys.

Theorem. Let A[1…n]A[1 \ldots n] be a max-heap and 1≤i≤n1 \le i \le n. Then HEAP-DELETE(A,i)(A, i) returns the old A[i]A[i], and afterwards A[1…n−1]A[1 \ldots n - 1] is a max-heap whose multiset of keys is the old one with one copy of the old A[i]A[i] removed. It stops. An index out of range gives an error.

Proof. Line L1 handles a bad index. Let xx be the old A[i]A[i] and yy the old A[n]A[n]. Lines L2 to L4 save xx, write yy into the cell ii and set s=n−1s = n - 1. The keys of A[1…s]A[1 \ldots s] are now the old keys with one copy of xx removed: if i=ni = n, the cell nn was dropped; else the cell ii lost xx and got yy, and the cell nn, which held yy, was dropped.

Case i=ni = n. Then i>si > s, line L5 skips the repair, and A[1…s]A[1 \ldots s] is the old heap without its last cell. Each of its edges is an old edge, so it holds.

Case i≤si \le s. Each edge with no end at ii joins two unchanged cells, so it holds. The edges with an end at ii are (p(i),i)(p(i), i) when i≥2i \ge 2, and (i,c)(i, c) for c∈C(i)c \in C(i). For each c∈C(i)c \in C(i) we have A[c]≤xA[c] \le x, because the edge (i,c)(i, c) held before, and if i≥2i \ge 2, then x≤A[p(i)]x \le A[p(i)], because the edge (p(i),i)(p(i), i) held before.

  • Sub-case: line L6 calls SIFT-UP, so i≥2i \ge 2 and A[p(i)]<yA[p(i)] < y. Then for each c∈C(i)c \in C(i), A[c]≤x≤A[p(i)]<yA[c] \le x \le A[p(i)] < y, so each edge (i,c)(i, c) holds. Each edge (p(j),j)(p(j), j) with j≠ij \ne i holds, and A[p(i)]≥x≥A[c]A[p(i)] \ge x \ge A[c] for each c∈C(i)c \in C(i). So the hypothesis of Lemma U holds, and afterwards each edge holds.
  • Sub-case: line L7 calls MAX-HEAPIFY, so i=1i = 1 or A[p(i)]≥yA[p(i)] \ge y. Then the edge (p(i),i)(p(i), i) holds if i≥2i \ge 2, and each edge with p(j)≠ip(j) \ne i holds. If i≥2i \ge 2, A[p(i)]≥x≥A[c]A[p(i)] \ge x \ge A[c] for each c∈C(i)c \in C(i). So the hypothesis of Lemma D holds, and afterwards each edge holds.

In each case the sift only swaps, so the multiset of keys stays the one above, and the sift stops by its lemma. Line L8 returns xx. ■\blacksquare

Back to Heap delete