Algorithm Search⌘K

Proofs: Fenwick tree

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

The Fenwick tree keeps its invariant and returns prefix sums full

This proof shows: correctness, termination.

Let (G,+,0)(G, +, 0) be an abelian group. For i≥1i \ge 1, lowbit⁡(i)\operatorname{lowbit}(i) is the largest power of two that divides ii, and R(i)=(i−lowbit⁡(i),i]∩ZR(i) = (i - \operatorname{lowbit}(i), i] \cap \mathbb{Z}. The invariant is

(F) T[i]=∑k∈R(i)A[k]T[i] = \sum_{k \in R(i)} A[k] for each 1≤i≤n1 \le i \le n.

Lemma 1 (dyadic ranges). Let lowbit⁡(i)=2t\operatorname{lowbit}(i) = 2^t. Then i=(2m+1)2ti = (2m + 1) 2^t for an integer m≥0m \ge 0, and R(i)=(2m⋅2t,(2m+1)2t]R(i) = (2m \cdot 2^t, (2m + 1) 2^t]. For any i,j≥1i, j \ge 1, the ranges R(i)R(i) and R(j)R(j) are disjoint, or one is a subset of the other.

Proof. The first part is the definition of lowbit⁡\operatorname{lowbit}: i/2ti / 2^t is odd. Each range has the form (a2t,(a+1)2t](a 2^t, (a + 1) 2^t] with integers a≥0a \ge 0 and t≥0t \ge 0. Take two such ranges with t≤ut \le u, say (a2t,(a+1)2t](a 2^t, (a+1)2^t] and (b2u,(b+1)2u](b 2^u, (b+1) 2^u]. The points b2ub 2^u and (b+1)2u(b+1) 2^u are multiples of 2t2^t, so they are not strictly inside the first range. Hence the first range lies entirely inside the second one or entirely outside it. □\square

Lemma 2 (the PREFIX chain). Let 0≤i≤n0 \le i \le n, and let i0=ii_0 = i and is+1=is−lowbit⁡(is)i_{s+1} = i_s - \operatorname{lowbit}(i_s) while is>0i_s > 0. The chain reaches 0 after exactly pp steps, where pp is the number of set bits of ii, and (0,i](0, i] is the disjoint union of R(i0),R(i1),…,R(ip−1)R(i_0), R(i_1), \ldots, R(i_{p-1}).

Proof. Subtracting lowbit⁡(is)\operatorname{lowbit}(i_s) clears the lowest set bit of isi_s and changes no other bit. So the number of set bits drops by exactly one at each step, and the chain reaches 0 after pp steps. By definition R(is)=(is+1,is]R(i_s) = (i_{s+1}, i_s]. These ranges are adjacent and in decreasing order, so their union is (ip,i0]=(0,i](i_p, i_0] = (0, i], and they are disjoint. □\square

Lemma 3 (the ADD chain). Let 1≤i≤n1 \le i \le n, and let c0=ic_0 = i and cs+1=cs+lowbit⁡(cs)c_{s+1} = c_s + \operatorname{lowbit}(c_s). Then lowbit⁡(cs+1)≥2lowbit⁡(cs)\operatorname{lowbit}(c_{s+1}) \ge 2 \operatorname{lowbit}(c_s), and for 1≤j≤n1 \le j \le n: i∈R(j)i \in R(j) if and only if j=csj = c_s for some ss.

Proof. Let c=csc = c_s and lowbit⁡(c)=2t\operatorname{lowbit}(c) = 2^t. Write c=P+(2r−1)2tc = P + (2^r - 1) 2^t, where r≥1r \ge 1 is the number of consecutive set bits of cc from bit tt upwards, so bit t+rt + r of cc is 0 and PP is a multiple of 2t+r+12^{t + r + 1}. Then c+2t=P+2t+rc + 2^t = P + 2^{t+r}, so lowbit⁡(cs+1)=2t+r≥2t+1\operatorname{lowbit}(c_{s+1}) = 2^{t+r} \ge 2^{t+1}. Also cs+1−lowbit⁡(cs+1)=P≤c−2tc_{s+1} - \operatorname{lowbit}(c_{s+1}) = P \le c - 2^t, so R(cs)⊆R(cs+1)R(c_s) \subseteq R(c_{s+1}).

If. i∈R(c0)i \in R(c_0), and the ranges grow along the chain, so i∈R(cs)i \in R(c_s) for each ss.

Only if. Let i∈R(j)i \in R(j) with 1≤j≤n1 \le j \le n. Then j≥i=c0j \ge i = c_0. The chain increases strictly, so either j=csj = c_s for some ss, or there is an ss with cs<j<cs+1c_s < j < c_{s+1}. Assume the second case. The point ii lies in R(cs)R(c_s) and in R(j)R(j), so by Lemma 1 one holds the other. R(j)R(j) cannot lie inside R(cs)R(c_s), because j∈R(j)j \in R(j) and j>cs=max⁡R(cs)j > c_s = \max R(c_s). So R(cs)⊊R(j)R(c_s) \subsetneq R(j), which forces lowbit⁡(j)>lowbit⁡(cs)=2t\operatorname{lowbit}(j) > \operatorname{lowbit}(c_s) = 2^t, so jj is a multiple of 2t+12^{t+1}. But csc_s is an odd multiple of 2t2^t, so cs+1=cs+2tc_{s+1} = c_s + 2^t is the least multiple of 2t+12^{t+1} that is larger than csc_s. Then cs<j<cs+1c_s < j < c_{s+1} is impossible. □\square

Theorem 1 (PREFIX). If (F) holds, PREFIX(T,i)(T, i) returns A[1]+⋯+A[i]A[1] + \cdots + A[i] for 0≤i≤n0 \le i \le n, after at most ⌊lg⁡n⌋+1\lfloor \lg n \rfloor + 1 rounds.

Proof. The loop of lines L5 to L7 visits the chain of Lemma 2 and stops when i=0i = 0. Its invariant is s=∑k∈(i,i0]A[k]s = \sum_{k \in (i, i_0]} A[k] for the current ii: it holds with s=0s = 0 and i=i0i = i_0, and a round adds T[i]=∑k∈R(i)A[k]T[i] = \sum_{k \in R(i)} A[k] by (F) and moves ii to the lower end of R(i)R(i). At the end i=0i = 0, so s=∑k∈(0,i0]A[k]s = \sum_{k \in (0, i_0]} A[k]. The number of rounds is the number of set bits of i0≤ni_0 \le n, which is at most ⌊lg⁡n⌋+1\lfloor \lg n \rfloor + 1. □\square

Theorem 2 (ADD). If (F) holds for AA, then after ADD(T,n,i,δ)(T, n, i, \delta) it holds for A′A', where A′[i]=A[i]+δA'[i] = A[i] + \delta and A′[k]=A[k]A'[k] = A[k] for k≠ik \ne i. The loop runs at most ⌊lg⁡n⌋+1\lfloor \lg n \rfloor + 1 times.

Proof. For each jj, ∑k∈R(j)A′[k]\sum_{k \in R(j)} A'[k] is ∑k∈R(j)A[k]+δ\sum_{k \in R(j)} A[k] + \delta if i∈R(j)i \in R(j), and is unchanged otherwise. Lines L1 to L3 add δ\delta to T[cs]T[c_s] for each cs≤nc_s \le n of the chain of Lemma 3, and to no other cell. By Lemma 3 these are exactly the j≤nj \le n with i∈R(j)i \in R(j). By Lemma 3 again, lowbit⁡(cs)≥2s\operatorname{lowbit}(c_s) \ge 2^s, and cs≥lowbit⁡(cs)c_s \ge \operatorname{lowbit}(c_s), so cs>nc_s > n once 2s>n2^s > n. So at most ⌊lg⁡n⌋+1\lfloor \lg n \rfloor + 1 indices of the chain are at most nn. □\square

Theorem 3 (RANGE). If (F) holds, RANGE(T,l,r)(T, l, r) returns A[l]+⋯+A[r]A[l] + \cdots + A[r].

Proof. By Theorem 1, line L9 computes ∑k=1rA[k]−∑k=1l−1A[k]=∑k=lrA[k]\sum_{k=1}^{r} A[k] - \sum_{k=1}^{l-1} A[k] = \sum_{k=l}^{r} A[k]. The subtraction is defined because GG is a group. □\square

Theorem 4 (BUILD). After BUILD(A,n)(A, n), (F) holds.

Proof. For j≤nj \le n, call i<ji < j a child of jj if i+lowbit⁡(i)=ji + \operatorname{lowbit}(i) = j. We first show that R(j)={j}∪⋃i child of jR(i)R(j) = \{j\} \cup \bigcup_{i \text{ child of } j} R(i), with a disjoint union. Let lowbit⁡(j)=2u\operatorname{lowbit}(j) = 2^u. If u=0u = 0, then R(j)={j}R(j) = \{j\}, and jj has no child, because i+lowbit⁡(i)i + \operatorname{lowbit}(i) is even for each ii. If u≥1u \ge 1, the children of jj are j−2vj - 2^v for 0≤v<u0 \le v < u: for such i=j−2vi = j - 2^v, lowbit⁡(i)=2v\operatorname{lowbit}(i) = 2^v and i+2v=ji + 2^v = j, and conversely i+lowbit⁡(i)=ji + \operatorname{lowbit}(i) = j with lowbit⁡(i)=2v\operatorname{lowbit}(i) = 2^v gives i=j−2vi = j - 2^v and needs v<uv < u. Then R(j−2v)=(j−2v+1,j−2v]R(j - 2^v) = (j - 2^{v+1}, j - 2^v], and these ranges for v=0,…,u−1v = 0, \ldots, u - 1 together with {j}\{j\} are disjoint and cover (j−2u,j]=R(j)(j - 2^u, j] = R(j).

Line L10 sets T[j]=A[j]T[j] = A[j]. In the loop, T[j]T[j] changes only at line L13, in the round of a child ii of jj, and each child has i<ji < j. So after round j−1j - 1, the cell T[j]T[j] never changes again. We claim that when round ii starts, T[i]=∑k∈R(i)A[k]T[i] = \sum_{k \in R(i)} A[k], by strong induction on ii. When round ii starts, the rounds of all children i′i' of ii are done, and each of them added the value T[i′]T[i'] that it had at the start of its own round, which is ∑k∈R(i′)A[k]\sum_{k \in R(i')} A[k] by the hypothesis. No other round has changed T[i]T[i]. So T[i]=A[i]+∑i′ child of i∑k∈R(i′)A[k]T[i] = A[i] + \sum_{i' \text{ child of } i} \sum_{k \in R(i')} A[k]. By the decomposition, this is ∑k∈R(i)A[k]\sum_{k \in R(i)} A[k]. After the last round, each T[i]T[i] keeps this value, so (F) holds. □\square

Theorem 5 (SEARCH). Assume (F) and A[k]≥0A[k] \ge 0 for each kk, in an ordered abelian group. Let s0s_0 be the input ss. SEARCH returns the least i∈{1,…,n}i \in \{1, \ldots, n\} with PREFIX(i)≥s0(i) \ge s_0, or n+1n + 1 if there is none.

Proof. Let 2K2^K be the largest power of two at most nn. Write P(i)P(i) for PREFIX(i)(i). Since A≥0A \ge 0, PP does not decrease. Let qq be the largest index in {0,…,n}\{0, \ldots, n\} with P(q)<s0P(q) < s_0, which exists if s0>0s_0 > 0; if s0≤0s_0 \le 0, put q=0q = 0. The answer is q+1q + 1. We show by a loop invariant that pos=qpos = q at the end. Before the test with step=2tstep = 2^t:

(S) pospos is a multiple of 2t+12^{t+1}, pos≤q<pos+2t+1pos \le q < pos + 2^{t+1}, and s=s0−P(pos)s = s_0 - P(pos).

At the start, pos=0pos = 0, t=Kt = K, and q≤n<2K+1q \le n < 2^{K+1}, so (S) holds. Assume (S) before a test. Since pospos is a multiple of 2t+12^{t+1}, lowbit⁡(pos+2t)=2t\operatorname{lowbit}(pos + 2^t) = 2^t and R(pos+2t)=(pos,pos+2t]R(pos + 2^t) = (pos, pos + 2^t]. So, if pos+2t≤npos + 2^t \le n, then T[pos+2t]=P(pos+2t)−P(pos)T[pos + 2^t] = P(pos + 2^t) - P(pos), and the test T[pos+2t]<sT[pos + 2^t] < s is the test P(pos+2t)<s0P(pos + 2^t) < s_0.

  • If pos+2t≤npos + 2^t \le n and P(pos+2t)<s0P(pos + 2^t) < s_0, then q≥pos+2tq \ge pos + 2^t by the choice of qq, and line L17 sets pos←pos+2tpos \leftarrow pos + 2^t and keeps s=s0−P(pos)s = s_0 - P(pos).
  • If pos+2t≤npos + 2^t \le n and P(pos+2t)≥s0P(pos + 2^t) \ge s_0, then q<pos+2tq < pos + 2^t, because PP does not decrease, and pospos does not change.
  • If pos+2t>npos + 2^t > n, then q≤n<pos+2tq \le n < pos + 2^t, and pospos does not change.

In each case pospos is now a multiple of 2t2^t and pos≤q<pos+2tpos \le q < pos + 2^t, which is (S) for the next value t−1t - 1 of the exponent. After the test with step=1step = 1, this gives pos≤q<pos+1pos \le q < pos + 1, so pos=qpos = q, and line L19 returns q+1q + 1. The loop makes K+1=⌊lg⁡n⌋+1K + 1 = \lfloor \lg n \rfloor + 1 tests. ■\blacksquare

Back to Fenwick tree