Algorithm Search⌘K

Proofs: Chtholly tree

These are the human proofs of Chtholly tree, at full length.

The Chtholly tree keeps its runs correct, and assignments cost O(log n) amortized full

This proof shows: correctness, complexity.

The structure is a set R\mathcal R of runs (s,e,v)(s, e, v) ordered by ss. The invariant is:

(C) The runs are non-empty and disjoint, their union is [0,n)[0, n), and A[k]=vA[k] = v for each run (s,e,v)∈R(s, e, v) \in \mathcal R and each s≤k<es \le k < e.

At the start R\mathcal R holds one run per position, or one run per maximal block of equal values, and (C) holds.

Lemma 1 (SPLIT). Let (C) hold and 0≤x≤n0 \le x \le n. After SPLIT(x)(x), (C) holds for the same array AA, and if x<nx < n some run starts at xx; the result is that run. Every run that started at some point yy before still starts at yy.

Proof. If x=nx = n nothing changes. Otherwise, by (C), exactly one run (s,e,v)(s, e, v) has s≤x<es \le x < e, and it is the run with the largest start ≤x\le x, since the runs are disjoint and sorted by start. If s=xs = x it is returned. Else s<x<es < x < e, and the two runs (s,x,v)(s, x, v) and (x,e,v)(x, e, v) are non-empty, cover the same positions as (s,e,v)(s, e, v), and give them the same value. The other runs do not change. □\square

Lemma 2 (ASSIGN). Let (C) hold and 0≤l<r≤n0 \le l < r \le n. After ASSIGN(l,r,v)(l, r, v), (C) holds for the array A′A' with A′[k]=vA'[k] = v for l≤k<rl \le k < r and A′[k]=A[k]A'[k] = A[k] otherwise.

Proof. By Lemma 1, after line L6 a run starts at rr or r=nr = n, and after line L7 a run starts at ll; the split at ll does not remove the start at rr. The runs with starts in [l,r)[l, r) are then those from lo up to hi, and since the run boundaries include ll and rr, their union is exactly [l,r)[l, r). Line L8 deletes them and line L9 inserts (l,r,v)(l, r, v), which covers the same positions, is non-empty, and has the value vv. The runs outside [l,r)[l, r) keep their positions and values. □\square

Lemma 3 (EACH). After lines L10 and L11, the runs from lo up to hi tile [l,r)[l, r) in order. A change of vv in a visited run changes AA only on that run.

Proof. As in Lemma 2. □\square

Theorem (amortized cost). Let the set be a balanced search tree, and let RR be the number of runs, so R≤nR \le n. Each split costs O(log⁡n)O(\log n). Let an ASSIGN delete kk runs. Its actual cost is O((k+1)log⁡n)O((k + 1) \log n), and it changes RR by at most 2−k+1=3−k2 - k + 1 = 3 - k. With the potential Φ=cRlog⁡n\Phi = c R \log n for a large enough constant cc, the amortized cost is O((k+1)log⁡n)+c(3−k)log⁡n=O(log⁡n)O((k + 1) \log n) + c (3 - k) \log n = O(\log n). An EACH that visits kk runs costs O(k+log⁡n)O(k + \log n) and raises Φ\Phi by at most 2clog⁡n2 c \log n, so its amortized cost is O(k+log⁡n)O(k + \log n). Starting from R0R_0 runs, Φ0=cR0log⁡n\Phi_0 = c R_0 \log n and Φ≥0\Phi \ge 0 always, so mm operations cost O((R0+m)log⁡n+∑kEACH)O((R_0 + m) \log n + \sum k_{\mathrm{EACH}}) in total. □\square

Remark (no bound for EACH). The term ∑kEACH\sum k_{\mathrm{EACH}} cannot be bounded in general: with A[k]=kA[k] = k there are nn runs, and mm calls of EACH(0,n,f)(0, n, f) without assignments visit nmn m runs.

Back to Chtholly tree