The Fenwick tree keeps its invariant and returns prefix sums full
This proof shows: correctness, termination.
Let (G,+,0) be an abelian group. For i≥1, lowbit(i) is the largest power of two that divides i, and R(i)=(i−lowbit(i),i]∩Z. The invariant is
(F) T[i]=∑k∈R(i)A[k] for each 1≤i≤n.
Lemma 1 (dyadic ranges). Let lowbit(i)=2t. Then i=(2m+1)2t for an integer m≥0, and R(i)=(2m⋅2t,(2m+1)2t]. For any i,j≥1, the ranges R(i) and R(j) are disjoint, or one is a subset of the other.
Proof. The first part is the definition of lowbit: i/2t is odd. Each range has the form (a2t,(a+1)2t] with integers a≥0 and t≥0. Take two such ranges with t≤u, say (a2t,(a+1)2t] and (b2u,(b+1)2u]. The points b2u and (b+1)2u are multiples of 2t, so they are not strictly inside the first range. Hence the first range lies entirely inside the second one or entirely outside it. □
Lemma 2 (the PREFIX chain). Let 0≤i≤n, and let i0=i and is+1=is−lowbit(is) while is>0. The chain reaches 0 after exactly p steps, where p is the number of set bits of i, and (0,i] is the disjoint union of R(i0),R(i1),…,R(ip−1).
Proof. Subtracting lowbit(is) clears the lowest set bit of is and changes no other bit. So the number of set bits drops by exactly one at each step, and the chain reaches 0 after p steps. By definition R(is)=(is+1,is]. These ranges are adjacent and in decreasing order, so their union is (ip,i0]=(0,i], and they are disjoint. □
Lemma 3 (the ADD chain). Let 1≤i≤n, and let c0=i and cs+1=cs+lowbit(cs). Then lowbit(cs+1)≥2lowbit(cs), and for 1≤j≤n: i∈R(j) if and only if j=cs for some s.
Proof. Let c=cs and lowbit(c)=2t. Write c=P+(2r−1)2t, where r≥1 is the number of consecutive set bits of c from bit t upwards, so bit t+r of c is 0 and P is a multiple of 2t+r+1. Then c+2t=P+2t+r, so lowbit(cs+1)=2t+r≥2t+1. Also cs+1−lowbit(cs+1)=P≤c−2t, so R(cs)⊆R(cs+1).
If. i∈R(c0), and the ranges grow along the chain, so i∈R(cs) for each s.
Only if. Let i∈R(j) with 1≤j≤n. Then j≥i=c0. The chain increases strictly, so either j=cs for some s, or there is an s with cs<j<cs+1. Assume the second case. The point i lies in R(cs) and in R(j), so by Lemma 1 one holds the other. R(j) cannot lie inside R(cs), because j∈R(j) and j>cs=maxR(cs). So R(cs)⊊R(j), which forces lowbit(j)>lowbit(cs)=2t, so j is a multiple of 2t+1. But cs is an odd multiple of 2t, so cs+1=cs+2t is the least multiple of 2t+1 that is larger than cs. Then cs<j<cs+1 is impossible. □
Theorem 1 (PREFIX). If (F) holds, PREFIX(T,i) returns A[1]+⋯+A[i] for 0≤i≤n, after at most ⌊lgn⌋+1 rounds.
Proof. The loop of lines L5 to L7 visits the chain of Lemma 2 and stops when i=0. Its invariant is s=∑k∈(i,i0]A[k] for the current i: it holds with s=0 and i=i0, and a round adds T[i]=∑k∈R(i)A[k] by (F) and moves i to the lower end of R(i). At the end i=0, so s=∑k∈(0,i0]A[k]. The number of rounds is the number of set bits of i0≤n, which is at most ⌊lgn⌋+1. □
Theorem 2 (ADD). If (F) holds for A, then after ADD(T,n,i,δ) it holds for A′, where A′[i]=A[i]+δ and A′[k]=A[k] for k=i. The loop runs at most ⌊lgn⌋+1 times.
Proof. For each j, ∑k∈R(j)A′[k] is ∑k∈R(j)A[k]+δ if i∈R(j), and is unchanged otherwise. Lines L1 to L3 add δ to T[cs] for each cs≤n of the chain of Lemma 3, and to no other cell. By Lemma 3 these are exactly the j≤n with i∈R(j). By Lemma 3 again, lowbit(cs)≥2s, and cs≥lowbit(cs), so cs>n once 2s>n. So at most ⌊lgn⌋+1 indices of the chain are at most n. □
Theorem 3 (RANGE). If (F) holds, RANGE(T,l,r) returns A[l]+⋯+A[r].
Proof. By Theorem 1, line L9 computes ∑k=1rA[k]−∑k=1l−1A[k]=∑k=lrA[k]. The subtraction is defined because G is a group. □
Theorem 4 (BUILD). After BUILD(A,n), (F) holds.
Proof. For j≤n, call i<j a child of j if i+lowbit(i)=j. We first show that R(j)={j}∪⋃i child of jR(i), with a disjoint union. Let lowbit(j)=2u. If u=0, then R(j)={j}, and j has no child, because i+lowbit(i) is even for each i. If u≥1, the children of j are j−2v for 0≤v<u: for such i=j−2v, lowbit(i)=2v and i+2v=j, and conversely i+lowbit(i)=j with lowbit(i)=2v gives i=j−2v and needs v<u. Then R(j−2v)=(j−2v+1,j−2v], and these ranges for v=0,…,u−1 together with {j} are disjoint and cover (j−2u,j]=R(j).
Line L10 sets T[j]=A[j]. In the loop, T[j] changes only at line L13, in the round of a child i of j, and each child has i<j. So after round j−1, the cell T[j] never changes again. We claim that when round i starts, T[i]=∑k∈R(i)A[k], by strong induction on i. When round i starts, the rounds of all children i′ of i are done, and each of them added the value T[i′] that it had at the start of its own round, which is ∑k∈R(i′)A[k] by the hypothesis. No other round has changed T[i]. So T[i]=A[i]+∑i′ child of i∑k∈R(i′)A[k]. By the decomposition, this is ∑k∈R(i)A[k]. After the last round, each T[i] keeps this value, so (F) holds. □
Theorem 5 (SEARCH). Assume (F) and A[k]≥0 for each k, in an ordered abelian group. Let s0 be the input s. SEARCH returns the least i∈{1,…,n} with PREFIX(i)≥s0, or n+1 if there is none.
Proof. Let 2K be the largest power of two at most n. Write P(i) for PREFIX(i). Since A≥0, P does not decrease. Let q be the largest index in {0,…,n} with P(q)<s0, which exists if s0>0; if s0≤0, put q=0. The answer is q+1. We show by a loop invariant that pos=q at the end. Before the test with step=2t:
(S) pos is a multiple of 2t+1, pos≤q<pos+2t+1, and s=s0−P(pos).
At the start, pos=0, t=K, and q≤n<2K+1, so (S) holds. Assume (S) before a test. Since pos is a multiple of 2t+1, lowbit(pos+2t)=2t and R(pos+2t)=(pos,pos+2t]. So, if pos+2t≤n, then T[pos+2t]=P(pos+2t)−P(pos), and the test T[pos+2t]<s is the test P(pos+2t)<s0.
- If pos+2t≤n and P(pos+2t)<s0, then q≥pos+2t by the choice of q, and line L17 sets pos←pos+2t and keeps s=s0−P(pos).
- If pos+2t≤n and P(pos+2t)≥s0, then q<pos+2t, because P does not decrease, and pos does not change.
- If pos+2t>n, then q≤n<pos+2t, and pos does not change.
In each case pos is now a multiple of 2t and pos≤q<pos+2t, which is (S) for the next value t−1 of the exponent. After the test with step=1, this gives pos≤q<pos+1, so pos=q, and line L19 returns q+1. The loop makes K+1=⌊lgn⌋+1 tests. ■