Algorithm Search⌘K

Proofs: Kinetic heap

These are the human proofs of Kinetic heap, at full length.

The kinetic heap reports the minimum at every time, and processes exactly Φ events in the worst case full

This proof shows: correctness.

We use the definitions of the page. Each item ee has the key ke(t)=ae+betk_e(t) = a_e + b_e t. The order just after time ss is: e≺sfe \prec_s f if ke(s)<kf(s)k_e(s) < k_f(s), or ke(s)=kf(s)k_e(s) = k_f(s) and be<bfb_e < b_f. Write e⪯sfe \preceq_s f if e≺sfe \prec_s f or the two keys and slopes are equal. The invariants at the end of each operation are (I1) H[parent(i)]⪯nowH[i]H[parent(i)] \preceq_{now} H[i] for every i>0i > 0; (I2) every position i>0i > 0 has a current certificate whose failure time is fail(i)fail(i) as defined on the page, and every finite one is larger than nownow.

Lemma 1 (a certificate). Let pp and cc be two items with p⪯scp \preceq_s c. If bp≤bcb_p \le b_c, then kp(t)≤kc(t)k_p(t) \le k_c(t) for all t≥st \ge s. If bp>bcb_p > b_c, let τ=(ac−ap)/(bp−bc)\tau = (a_c - a_p)/(b_p - b_c); then τ≥s\tau \ge s, kp(t)≤kc(t)k_p(t) \le k_c(t) exactly for t≤τt \le \tau, and c≺τpc \prec_\tau p.

Proof. The difference d(t)=kc(t)−kp(t)=(ac−ap)+(bc−bp)td(t) = k_c(t) - k_p(t) = (a_c - a_p) + (b_c - b_p) t is linear, and d(s)≥0d(s) \ge 0. If bc≥bpb_c \ge b_p, it does not decrease, so d(t)≥0d(t) \ge 0 for t≥st \ge s. If bc<bpb_c < b_p, it decreases and has its only zero at τ\tau. Since d(s)≥0d(s) \ge 0, τ≥s\tau \ge s, and d(t)≥0d(t) \ge 0 exactly when t≤τt \le \tau. At τ\tau the keys are equal and bc<bpb_c < b_p, so c≺τpc \prec_\tau p. □\square

Lemma 2 (between events). If (I1) and (I2) hold at time nownow and no finite failure time is in [now,t][now, t], then H[0]H[0] has a smallest key at every time in [now,t][now, t], and (I1) and (I2) hold at time tt.

Proof. By Lemma 1, for each edge either the certificate never fails, or it fails after tt; in both cases the parent key is at most the child key at all times in [now,t][now, t]. Then by induction on the depth, kH[0](s)≤kH[i](s)k_{H[0]}(s) \le k_{H[i]}(s) for all ii and all such ss. For (I1) at time tt: if bp≤bcb_p \le b_c, the keys at tt are in order and, if equal, the slopes are in order. If bp>bcb_p > b_c, the failure time is larger than tt, so kp(t)<kc(t)k_p(t) < k_c(t). The certificates depend only on the items at the positions, which do not change, so (I2) holds, with all finite failure times larger than tt. □\square

Lemma 3 (an event). Suppose that before line L5 the heap satisfies H[parent(j)]⪯τH[j]H[parent(j)] \preceq_\tau H[j] for every edge except possibly the edges whose certificates have failure time τ\tau, and every such edge has c≺τpc \prec_\tau p with equal keys. Process one of them at position ii. Afterwards the same statement holds, with one edge less of this kind or with new ones only of the same kind, and every certificate is correct.

Proof. Let pp be the item at q=parent(i)q = parent(i) and cc the item at ii; by Lemma 1, kp(τ)=kc(τ)k_p(\tau) = k_c(\tau) and bc<bpb_c < b_p. After the swap, cc is at qq and pp at ii, and c≺τpc \prec_\tau p, so the edge from qq to ii is in order, and its certificate never fails. The edges that changed their items are those at qq and at ii; line L6 computes their certificates again. The edge from qq to its other child ww: before, p⪯τwp \preceq_\tau w or the edge was a failing one; now cc is the parent, with kc(τ)=kp(τ)k_c(\tau) = k_p(\tau) and bc<bpb_c < b_p, so if p⪯τwp \preceq_\tau w then c≺τwc \prec_\tau w. The edges from ii to its children xx: before, c⪯τxc \preceq_\tau x or a failing edge; now pp is the parent, and the new certificate is computed by line L6; if x≺τpx \prec_\tau p, the edge has failure time τ\tau by the formula (the keys of pp and cc are equal at τ\tau, so kx(τ)≥kp(τ)k_x(\tau) \ge k_p(\tau), and x≺τpx \prec_\tau p means equality and bx<bpb_x < b_p). The edge above qq: the parent gg had g⪯τpg \preceq_\tau p; now the child is cc, with the same key as pp at τ\tau; if c≺τgc \prec_\tau g, then the keys are equal and the slope of cc is smaller, so the failure time is τ\tau. In all cases an edge is either in order just after τ\tau, or has failure time τ\tau with equal keys, as claimed. □\square

Lemma 4 (the potential). Let Φ′\Phi' be the number of pairs of items (f,g)(f, g) such that ff is at a proper ancestor of the position of gg and bg<bfb_g < b_f. Each event lowers Φ′\Phi' by at least 1. So, if no item is inserted or deleted, ADVANCE processes at most Φ′\Phi' events, and this is at most Φ=∑idepth(i)\Phi = \sum_i depth(i).

Proof. This is Lemma 2 of da Fonseca and de Figueiredo, written for a min-heap. Let the event swap pp at qq and cc at ii, with bc<bpb_c < b_p. Let ww be the other child of qq. Split the other items into three sets. For an item that is not in the subtree of qq, its set of descendants is the same set of items before and after, and so is its set of ancestors. For an item in the subtree of ii below ii, its ancestors are the same set of items: pp and cc just exchange places. For an item xx in the subtree of ww, the ancestor pp is replaced by cc; the pair counts after only if bx<bcb_x < b_c, which implies bx<bpb_x < b_p, so the count for xx does not grow. The pairs with pp or cc as the descendant: the ancestors of qq are ancestors of both before and after. The pairs with pp or cc as the ancestor are covered by the cases above, except the pair of pp and cc itself: before, pp is above cc and bc<bpb_c < b_p, so it counts; after, cc is above pp and bp<bcb_p < b_c is false, so it does not. So Φ′\Phi' drops by at least 1. It is never negative, so the number of events is at most its initial value. Each item at depth dd is in at most dd pairs as the descendant, so Φ′≤Φ\Phi' \le \Phi. □\square

Lemma 5 (the lower bound). For ks(t)=−2st+s2k_s(t) = -2 s t + s^2, s=0,1,…,n−1s = 0, 1, \ldots, n - 1, and a start time t0<−nt_0 < -n, ADVANCE to t1>2nt_1 > 2n processes exactly Φ\Phi events.

Proof. ks(t)+t2=(t−s)2k_s(t) + t^2 = (t - s)^2, so at time tt the item with the smallest key is the one with ss nearest to tt. At t=st = s this is the item ss alone, so every item is at the root at some time in [t0,t1][t_0, t_1]. An event moves one item up by one level and one item down. So an item that starts at depth dd needs at least dd events in which it moves up, and these are different events for different items. So there are at least Φ\Phi events, and by Lemma 4 at most Φ\Phi. □\square

Theorem. After each operation, (I1) and (I2) hold, and FIND-MIN returns an item with the smallest key at time nownow. With no inserts and deletes, the number of events is at most Φ≤n⌊lg⁡n⌋\Phi \le n \lfloor \lg n \rfloor, and this is tight.

Proof. INSERT and DELETE are the usual sift operations of a binary heap under the order ≺now\prec_{now}, so (I1) holds after them, and lines L10 and L12 compute the certificate of every edge whose items changed; by Lemma 1 every finite failure time is at least nownow, and it equals nownow only if the child is before the parent in ≺now\prec_{now}, which (I1) excludes. So (I2) holds. In ADVANCE, the loop takes the earliest failure time τ\tau. By Lemma 2 the heap is valid up to τ\tau. By Lemma 3, after the events at time τ\tau that the loop processes, every edge is in order just after τ\tau or has failure time τ\tau; the loop goes on while there is one with failure time τ≤t\tau \le t, and by Lemma 4 the number of events is finite, so the loop leaves time τ\tau with (I1) and (I2) at τ\tau. Then Lemma 2 applies again up to the next failure time or tt. When the loop ends, Lemma 2 gives (I1) and (I2) at tt, and by (I1) and induction on depth, H[0]H[0] has a smallest key at nownow. The bound on the events is Lemma 4 and its tightness is Lemma 5; the depth of each of the nn positions is at most ⌊lg⁡n⌋\lfloor \lg n \rfloor. ■\blacksquare

Back to Kinetic heap