Algorithm Search⌘K

Proofs: Deap

These are the human proofs of Deap, at full length.

The deap operations keep the two heaps and the partner condition full

This proof shows: correctness, termination.

We use the notation of the page. The keys are in A[2…n]A[2 \ldots n]; the level of ii is k(i)=⌊lg⁡i⌋k(i) = \lfloor \lg i \rfloor; PL(i)=i+2k(i)−1\mathrm{PL}(i) = i + 2^{k(i) - 1} is the place of a min node ii; MAX-PARTNER(i)\mathrm{MAX\text{-}PARTNER}(i) is PL(i)\mathrm{PL}(i) if PL(i)≤n\mathrm{PL}(i) \le n and ⌊PL(i)/2⌋\lfloor \mathrm{PL}(i)/2 \rfloor else; and MIN-PARTNER(j)=j−2k(j)−1\mathrm{MIN\text{-}PARTNER}(j) = j - 2^{k(j) - 1} for a max node jj. The invariants are (L), (R) and (C) of the page. For a max node jj, let S(j)S(j) be the set of min nodes whose partner is jj.

Facts about partners

Fact 1. MAX-PARTNER(i)≤n\mathrm{MAX\text{-}PARTNER}(i) \le n for each min node i≤ni \le n, when n≥3n \ge 3.

Proof. If PL(i)>n\mathrm{PL}(i) > n, the partner is the parent of PL(i)\mathrm{PL}(i), which is at level k(i)−1k(i) - 1. The tree is complete and i≤ni \le n is at level k(i)k(i), so every cell of level k(i)−1k(i) - 1 is at most nn. □\square

Fact 2. Let c′≥4c' \ge 4 be a min node with parent cc. Then MAX-PARTNER(c)\mathrm{MAX\text{-}PARTNER}(c) is MAX-PARTNER(c′)\mathrm{MAX\text{-}PARTNER}(c') or its parent. So, under (R), A[MAX-PARTNER(c)]≥A[MAX-PARTNER(c′)]A[\mathrm{MAX\text{-}PARTNER}(c)] \ge A[\mathrm{MAX\text{-}PARTNER}(c')].

Proof. PL(c)=c+2k(c)−1=⌊PL(c′)/2⌋\mathrm{PL}(c) = c + 2^{k(c) - 1} = \lfloor \mathrm{PL}(c')/2 \rfloor, since 2k(c′)−12^{k(c')-1} is even. If PL(c′)≤n\mathrm{PL}(c') \le n, then PL(c)<PL(c′)≤n\mathrm{PL}(c) < \mathrm{PL}(c') \le n, so the partner of cc is the parent of the partner of c′c'. Else the partner of c′c' is ⌊PL(c′)/2⌋=PL(c)≤n\lfloor \mathrm{PL}(c')/2 \rfloor = \mathrm{PL}(c) \le n, by Fact 1, and that is also the partner of cc. □\square

Fact 3. For a max node j≥6j \ge 6, MIN-PARTNER(⌊j/2⌋)=⌊MIN-PARTNER(j)/2⌋\mathrm{MIN\text{-}PARTNER}(\lfloor j/2 \rfloor) = \lfloor \mathrm{MIN\text{-}PARTNER}(j)/2 \rfloor. And S(j)S(j) is {MIN-PARTNER(j)}\{\mathrm{MIN\text{-}PARTNER}(j)\}, together with the children of MIN-PARTNER(j)\mathrm{MIN\text{-}PARTNER}(j) when jj is a leaf.

Proof. The first part is arithmetic, as in Fact 2. For the second, let m=MIN-PARTNER(j)m = \mathrm{MIN\text{-}PARTNER}(j); then PL(m)=j\mathrm{PL}(m) = j, so m∈S(j)m \in S(j). Another min node ii has the partner jj only if PL(i)>n\mathrm{PL}(i) > n and ⌊PL(i)/2⌋=j\lfloor \mathrm{PL}(i)/2 \rfloor = j, so PL(i)∈{2j,2j+1}\mathrm{PL}(i) \in \{2j, 2j + 1\}, which means ii is a child of mm and jj has no child at PL(i)\mathrm{PL}(i). If jj has a child 2j≤n2j \le n, then 2m+1<2j≤n2m + 1 < 2j \le n and PL(2m+1)=2j+1\mathrm{PL}(2m + 1) = 2j + 1; this is ≤n\le n unless 2j=n2j = n, so a non-leaf jj has the extra member 2m+12m + 1 only when 2j=n2j = n. We record this case: it occurs only when 2j+1=n+12j + 1 = n + 1. For a leaf jj, both children of mm, when they exist, have places 2j2j and 2j+12j + 1 beyond nn, so they are in S(j)S(j). Grandchildren of mm do not exist, since they would be at level k(j)+2k(j) + 2, beyond the level of the children of jj, which do not exist. □\square

Lemma 0. If (L), (R) and (C) hold and n≥3n \ge 3, then A[2]A[2] is a smallest key and A[3]A[3] a largest key. If n=2n = 2, the one key A[2]A[2] is both.

Proof. A min key is at least A[2]A[2] by (L), and at most the key of its partner, which is at most A[3]A[3] by (R). A max key A[j]A[j] is at most A[3]A[3] by (R), and at least A[MIN-PARTNER(j)]A[\mathrm{MIN\text{-}PARTNER}(j)] by (C), which is at least A[2]A[2]. □\square

The sifts

Say that the invariants hold with A[t]=hA[t] = h if they hold for the array in which the cell tt holds the key hh; the code may treat tt as a hole.

Lemma 1. Let tt be a min node, and let the invariants hold with A[t]=hA[t] = h. If x≤hx \le h, then MIN-SIFT-UP(t,x)(t, x) stops after at most k(t)−1k(t) - 1 iterations, and afterwards the invariants hold with the keys of AA changed as follows: xx is in some cell ff on the path from tt up to 2, and the keys on the path from ff to the parent of tt have moved one cell down, so that the multiset is the old one with hh replaced by xx.

Proof. Before each test of line L6, for the current hole cc, the invariants hold with A[c]=hcA[c] = h_c for some hc≥xh_c \ge x; this holds at the start with ht=hh_t = h. A min node has one lower bound, its parent's key, and its upper bounds are the keys of its children and of its partner. If the test is true, y=A[⌊c/2⌋]>xy = A[\lfloor c/2 \rfloor] > x moves into cc. Then yy meets the lower bound of cc by (L) at ⌊c/2⌋\lfloor c/2 \rfloor, and y≤hcy \le h_c meets the upper bounds of cc. The parent cell becomes the hole with h=y>xh = y > x, and no constraint at it changed. If the test is false, then c≤3c \le 3 or x≥A[⌊c/2⌋]x \ge A[\lfloor c/2 \rfloor], and x≤hcx \le h_c meets the upper bounds of cc. Each iteration moves up one level, and the loop stops at level 1. □\square

Lemma 1'. The mirror statement holds for MAX-SIFT-UP(t,x)(t, x) at a max node tt with x≥hx \ge h. A max node has one upper bound, its parent's key, and its lower bounds are the keys of its children and of the nodes of S(t)S(t); the proof is the same with the order reversed.

INSERT

Let the invariants hold for A[2…n−1]A[2 \ldots n - 1], and let nn be the new cell, with the key xx. If n=2n = 2, there is nothing to check.

A max node nn. Let i=MIN-PARTNER(n)i = \mathrm{MIN\text{-}PARTNER}(n). Before the insert, PL(i)=n\mathrm{PL}(i) = n was beyond the end, so the partner of ii was ⌊n/2⌋\lfloor n/2 \rfloor (or nothing if n=3n = 3), and (C) gave A[i]≤A[⌊n/2⌋]A[i] \le A[\lfloor n/2 \rfloor]. After the insert, S(n)={i}S(n) = \{i\}, because ii has no children (they would be at level k(n)+1k(n) + 1, beyond nn). So the invariants hold with A[n]=A[i]A[n] = A[i]: it meets the upper bound A[⌊n/2⌋]A[\lfloor n/2 \rfloor] and the lower bound A[i]A[i].

If x<A[i]x < A[i], line L3 sets A[n]=A[i]A[n] = A[i], and the invariants hold with the hole ii and h=A[i]>xh = A[i] > x. Lemma 1 applies. Else x≥A[i]x \ge A[i], and Lemma 1' applies to MAX-SIFT-UP(n,x)(n, x) with h=A[i]h = A[i].

A min node n≥4n \ge 4. Its place is beyond the end, so its partner is j=⌊PL(n)/2⌋=PL(⌊n/2⌋)j = \lfloor \mathrm{PL}(n)/2 \rfloor = \mathrm{PL}(\lfloor n/2 \rfloor), which is also the partner of the parent of nn, so (C) gives A[⌊n/2⌋]≤A[j]A[\lfloor n/2 \rfloor] \le A[j]. So the invariants hold with A[n]=A[j]A[n] = A[j]: nn has no children, and A[j]A[j] meets its lower bound and its partner bound.

If A[j]<xA[j] < x, line L5 moves A[j]A[j] to nn, and the invariants hold with the hole jj and h=A[j]<xh = A[j] < x, because the only new member of S(j)S(j) is nn, which holds A[j]A[j]. Lemma 1' applies. Else Lemma 1 applies to MIN-SIFT-UP(n,x)(n, x) with h=A[j]≥xh = A[j] \ge x.

Removing the last cell

Lemma 2. If the invariants hold for A[2…n+1]A[2 \ldots n + 1], they hold for A[2…n]A[2 \ldots n].

Proof. (L) and (R) lose one edge. If n+1n + 1 is a min node, no partner changes. If n+1n + 1 is a max node, the only min node whose partner changes is b=MIN-PARTNER(n+1)b = \mathrm{MIN\text{-}PARTNER}(n + 1), whose new partner is q=⌊(n+1)/2⌋q = \lfloor (n + 1)/2 \rfloor, and A[b]≤A[n+1]≤A[q]A[b] \le A[n + 1] \le A[q] by (C) and (R). □\square

EXTRACT-MIN

Let n≥3n \ge 3 be the size after line L10, and xx the key of the removed cell n+1n + 1. By Lemma 0, r=A[2]r = A[2] is a smallest key. By Lemma 2, the invariants hold for A[2…n]A[2 \ldots n] with the old key at 2.

The descent of line L12. Claim: before each test, with the hole at cc, the invariants hold for all cells other than cc, where (L) is read with cc skipped, and the key that cc held before the descent reached it is at least the key now at its parent. When the smaller child c′c' moves up to cc, it is at most its sibling, and at least the key now above cc, which was above c′c' before, by (L). For (C) at cc, Fact 2 gives A[MAX-PARTNER(c)]≥A[MAX-PARTNER(c′)]≥A[c′]A[\mathrm{MAX\text{-}PARTNER}(c)] \ge A[\mathrm{MAX\text{-}PARTNER}(c')] \ge A[c']. The max side does not change. The loop stops at a leaf ii after at most k(n)−1k(n) - 1 iterations.

The placement. Let j=MAX-PARTNER(i)j = \mathrm{MAX\text{-}PARTNER}(i). The key now at ⌊i/2⌋\lfloor i/2 \rfloor, if i≥4i \ge 4, is the old key of ii, which was at most A[j]A[j] by (C) for A[2…n]A[2 \ldots n]. So the invariants hold with A[i]=A[j]A[i] = A[j]. If A[j]<xA[j] < x, line L14 moves A[j]A[j] to ii; the invariants hold with the hole jj and h=A[j]<xh = A[j] < x, since the cell i∈S(j)i \in S(j) holds A[j]A[j], and Lemma 1' applies. Else Lemma 1 applies to MIN-SIFT-UP(i,x)(i, x) with h=A[j]≥xh = A[j] \ge x. For n=2n = 2, line L11 puts xx at 2.

EXTRACT-MAX

Let n≥3n \ge 3 be the size after line L16, xx the key of the removed cell n+1n + 1, and r=A[3]r = A[3], a largest key by Lemma 0. If n+1n + 1 was a max node, let b=MIN-PARTNER(n+1)b = \mathrm{MIN\text{-}PARTNER}(n + 1) and q=⌊(n+1)/2⌋q = \lfloor (n + 1)/2 \rfloor; then A[b]≤x≤A[q]A[b] \le x \le A[q] before the removal, and bb has no children (as in INSERT).

The descent of line L18. Let 3=c0,c1,…,cs=j3 = c_0, c_1, \ldots, c_s = j be the path of the hole, where ct+1c_{t+1} is the larger child of ctc_t, and jj is a leaf. After the descent, ctc_t holds the old key of ct+1c_{t+1} for t<st < s. These keys decrease along the path, and each is at most the old key above it, so (R) holds on the max side with jj as a hole. For (C), consider a min node ii whose partner is some ctc_t with t<st < s. If i=MIN-PARTNER(ct)i = \mathrm{MIN\text{-}PARTNER}(c_t), then by Fact 3, MIN-PARTNER(ct+1)\mathrm{MIN\text{-}PARTNER}(c_{t+1}) is a child of ii, so A[i]≤A[MIN-PARTNER(ct+1)]≤A[i] \le A[\mathrm{MIN\text{-}PARTNER}(c_{t+1})] \le the old A[ct+1]A[c_{t+1}], which is the new A[ct]A[c_t]. Else, by Fact 3, ctc_t is not a leaf, 2ct=n2c_t = n, and ii is the child 2m+12m + 1 with place 2ct+1=n+12c_t + 1 = n + 1; so i=bi = b and ct=qc_t = q. So after the descent, (C) can fail only for bb, and A[b]≤xA[b] \le x.

The choice of line L19. By Fact 3, S(j)S(j) is MIN-PARTNER(j)\mathrm{MIN\text{-}PARTNER}(j) and its children; by (L), its largest key is at the larger child if there is one. So i′i', the node chosen in line L19, holds the largest key of S(j)S(j). The key now at the parent of jj is the old key of jj, which is at least every key of S(j)∖{b}S(j) \setminus \{b\}, by (C) for A[2…n+1]A[2 \ldots n + 1] (for a node of S(j)S(j) other than bb, the partner did not change with the removal).

Case x≥A[i′]x \ge A[i']. The invariants other than (C) at bb hold with A[j]=A[i′]A[j] = A[i'], because A[i′]A[i'] is at least the keys of S(j)S(j) and at most the key above jj, unless i′=bi' = b, where A[b]≤xA[b] \le x also allows h=A[i′]h = A[i']. Lemma 1' applies to MAX-SIFT-UP(j,x)(j, x) with h=A[i′]h = A[i']. Afterwards, the cells of the path from jj up to the final cell ff of xx hold their old keys again, below ff, then xx at ff, then the keys above ff that are at least xx, since the loop stopped. So the partner qq of bb, which is on the path or off it, holds a key that is at least xx or its old key, and the old key was at least xx; in both cases it is at least A[b]A[b], so (C) holds at bb too.

Case x<A[i′]x < A[i']. Then i′≠bi' \ne b, since A[b]≤xA[b] \le x. Line L20 moves A[i′]A[i'] to jj; it is at most the key above jj, as shown, and it is at least the keys of S(j)S(j). The node bb, if it exists, has a partner whose key is now at least A[i′]>x≥A[b]A[i'] > x \ge A[b] if it is on the path, and its old key, which is at least xx, if it is not. So the invariants hold with the hole i′i' and h=A[i′]h = A[i'], which is a min leaf whose partner jj holds hh. Lemma 1 applies to MIN-SIFT-UP(i′,x)(i', x).

For n≤2n \le 2, lines L15 and L17 are correct by Lemma 0 and Lemma 2.

Theorem

Starting from the empty deap, each sequence of INSERT, EXTRACT-MIN and EXTRACT-MAX keeps (L), (R) and (C), stops after O(log⁡n)O(\log n) steps per operation, and keeps in A[2…n]A[2 \ldots n] exactly the current multiset; FIND-MIN and FIND-MAX return a smallest and a largest key.

Proof. The invariants and the multiset are the cases above, with Lemmas 1 and 1' for the sifts; each loop moves one level per iteration. The answers of the finds and extracts follow from Lemma 0. ■\blacksquare

Back to Deap