Algorithm Search⌘K

Proofs: Two-dimensional Fenwick tree

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

The two-dimensional Fenwick tree keeps its block sums and returns rectangle sums full

This proof shows: correctness, termination.

We use 1-based table indices. For x≥1x \ge 1, g(x)g(x) is the lowest set bit of xx and R(x)=(x−g(x),x]R(x) = (x - g(x), x]. Write M′[a][b]=M[a−1][b−1]M'[a][b] = M[a - 1][b - 1] for 1≤a≤n1 \le a \le n, 1≤b≤m1 \le b \le m. The invariant is

(F)F[i][j]=∑a∈R(i)∑b∈R(j)M′[a][b]for 1≤i≤n, 1≤j≤m.\text{(F)} \qquad F[i][j] = \sum_{a \in R(i)} \sum_{b \in R(j)} M'[a][b] \quad \text{for } 1 \le i \le n,\ 1 \le j \le m.

Lemma 1 (down chains). Let x≥0x \ge 0 and x0=xx_0 = x, xt+1=xt−g(xt)x_{t+1} = x_t - g(x_t) while xt>0x_t > 0. The chain reaches 0 after at most ⌊lg⁡x⌋+1\lfloor \lg x \rfloor + 1 steps when x≥1x \ge 1, and the ranges R(xt)R(x_t) over the nonzero xtx_t are disjoint with union {1,…,x}\{1, \ldots, x\}.

Proof. Subtracting g(xt)g(x_t) clears the lowest set bit, so each step lowers the number of set bits by one, and xx has at most ⌊lg⁡x⌋+1\lfloor \lg x \rfloor + 1 of them. The range R(xt)R(x_t) is (xt+1,xt](x_{t+1}, x_t]. These intervals are adjacent and descend from xx to 00, so they are disjoint and their union is (0,x](0, x]. □\square

Lemma 2 (up chains). Let 1≤p≤N1 \le p \le N and p0=pp_0 = p, ps+1=ps+g(ps)p_{s+1} = p_s + g(p_s). For 1≤y≤N1 \le y \le N: p∈R(y)p \in R(y) if and only if y=psy = p_s for some ss. The chain has at most ⌊lg⁡N⌋+1\lfloor \lg N \rfloor + 1 terms that are at most NN.

Proof. First, g(ps+1)>g(ps)g(p_{s+1}) > g(p_s). Write ps=q⋅2k+1+2kp_s = q \cdot 2^{k+1} + 2^k with g(ps)=2kg(p_s) = 2^k. Then ps+1=(q+1)2k+1p_{s+1} = (q + 1) 2^{k+1}, which is a multiple of 2k+12^{k+1}, so g(ps+1)=2k′g(p_{s+1}) = 2^{k'} with k′≥k+1k' \ge k + 1. So the chain is strictly increasing, and the exponent of gg grows by at least 1 per step; since g(y)≤y≤Ng(y) \le y \le N, at most ⌊lg⁡N⌋+1\lfloor \lg N \rfloor + 1 terms are at most NN.

Every chain term covers pp. Clearly p∈R(p0)p \in R(p_0). We show R(ps)⊆R(ps+1)R(p_s) \subseteq R(p_{s+1}). With the notation above, ps+1−g(ps+1)=(q+1)2k+1−2k′≤(q+1)2k+1−2k+1=q⋅2k+1=ps−g(ps)p_{s+1} - g(p_{s+1}) = (q + 1)2^{k+1} - 2^{k'} \le (q + 1) 2^{k+1} - 2^{k+1} = q \cdot 2^{k+1} = p_s - g(p_s), and ps<ps+1p_s < p_{s+1}. So the interval (ps−g(ps),ps](p_s - g(p_s), p_s] lies inside (ps+1−g(ps+1),ps+1](p_{s+1} - g(p_{s+1}), p_{s+1}]. By induction p∈R(ps)p \in R(p_s) for every ss.

No other index covers pp. Let p∈R(y)p \in R(y), so y≥p=p0y \ge p = p_0. Assume that yy is not a chain term. The chain is strictly increasing and unbounded, so there is an ss with ps<y<ps+1=ps+g(ps)p_s < y < p_{s+1} = p_s + g(p_s). Write y=ps+ty = p_s + t with 0<t<g(ps)0 < t < g(p_s). Since psp_s is a multiple of g(ps)>tg(p_s) > t, the lowest set bit of yy is that of tt, so g(y)=g(t)≤tg(y) = g(t) \le t. Then y−g(y)≥y−t=psy - g(y) \ge y - t = p_s, so ps∉R(y)p_s \notin R(y). But R(y)R(y) is an interval that ends at y>psy > p_s and holds p≤psp \le p_s, so it holds psp_s. This is a contradiction. □\square

Lemma 3 (build). After BUILD, (F) holds.

Proof. Row pass. Fix a row ii and write vj=M′[i][j]v_j = M'[i][j] and Gj=F[i][j]G_j = F[i][j]. We show that after lines L2 and L3, Gj=∑b∈R(j)vbG_j = \sum_{b \in R(j)} v_b for all jj. The loop pushes GjG_j into Gj+g(j)G_{j + g(j)} at step jj. Call j′j' a child of jj when j′+g(j′)=jj' + g(j') = j. Each child j′j' of jj is less than jj, and Gj′G_{j'} receives pushes only from its own children, which are less than j′j', so Gj′G_{j'} is final when it is pushed. So at the end, Gj=vj+∑j′ child of jGj′G_j = v_j + \sum_{j' \text{ child of } j} G_{j'}, and by induction on jj it suffices that the ranges of the children of jj split R(j)∖{j}=(j−g(j),j−1]R(j) \setminus \{j\} = (j - g(j), j - 1]. Let g(j)=2kg(j) = 2^k and j=q⋅2k+1+2kj = q \cdot 2^{k+1} + 2^k. For 0≤t<k0 \le t < k, the index j−2tj - 2^t has g(j−2t)=2tg(j - 2^t) = 2^t, since jj is a multiple of 2k>2t2^k > 2^t, so it is a child of jj. Conversely, let j′j' be a child of jj with g(j′)=2tg(j') = 2^t. Adding 2t2^t to j′j' clears bit tt with a carry, so j=j′+2tj = j' + 2^t is a multiple of 2t+12^{t+1}. Then 2k=g(j)≥2t+12^k = g(j) \ge 2^{t+1}, so t<kt < k and j′=j−2tj' = j - 2^t. So the children are j−2tj - 2^t for 0≤t<k0 \le t < k, with R(j−2t)=(j−2t+1,j−2t]R(j - 2^t) = (j - 2^{t+1}, j - 2^t]. These intervals for t=0,…,k−1t = 0, \ldots, k - 1 are adjacent and cover (j−2k,j−1](j - 2^k, j - 1], as required.

Column pass. Lines L4 and L5 do the same push over ii, with the whole row F[i][⋅]F[i][\cdot] as the value, and vectors add entry by entry. By the same argument, after the pass row ii is the sum of the rows i′i' of the row pass with i′∈R(i)i' \in R(i), entry by entry. With the row pass this gives (F). □\square

Lemma 4 (add). If (F) holds before ADD(r,c,δ)(r, c, \delta) with 0≤r<n0 \le r < n and 0≤c<m0 \le c < m, it holds after it for the matrix with M[r][c]M[r][c] increased by δ\delta.

Proof. The right side of (F) for (i,j)(i, j) grows by δ\delta exactly when r+1∈R(i)r + 1 \in R(i) and c+1∈R(j)c + 1 \in R(j), and stays the same otherwise. By Lemma 2 with N=nN = n and N=mN = m, these are exactly the pairs with ii on the up chain of r+1r + 1 and jj on the up chain of c+1c + 1. Lines L6 to L12 visit exactly these pairs, each once, since the outer loop runs over the up chain of r+1r + 1 while i≤ni \le n, and line L8 restarts the inner loop over the up chain of c+1c + 1 for each ii. □\square

Lemma 5 (prefix). If (F) holds, PREFIX(r,c)(r, c) with 0≤r≤n0 \le r \le n and 0≤c≤m0 \le c \le m returns ∑a=1r∑b=1cM′[a][b]\sum_{a = 1}^{r} \sum_{b = 1}^{c} M'[a][b].

Proof. The loops add F[i][j]F[i][j] for ii on the down chain of rr and jj on the down chain of cc, both without 0. By (F) this is the sum of M′M' over the union of the blocks R(i)×R(j)R(i) \times R(j). By Lemma 1, the sets R(i)R(i) split {1,…,r}\{1, \ldots, r\} and the sets R(j)R(j) split {1,…,c}\{1, \ldots, c\}, so their products split {1,…,r}×{1,…,c}\{1, \ldots, r\} \times \{1, \ldots, c\}, and each cell of the corner is counted once. If r=0r = 0 or c=0c = 0, a loop does not run and the result is the empty sum 0. □\square

Lemma 6 (query). If (F) holds, QUERY(r1,c1,r2,c2)(r_1, c_1, r_2, c_2) returns ∑r=r1r2∑c=c1c2M[r][c]\sum_{r = r_1}^{r_2} \sum_{c = c_1}^{c_2} M[r][c].

Proof. Let P(r,c)P(r, c) be the corner sum of Lemma 5. A cell (a,b)(a, b) of MM, 0-based, with a≤r2a \le r_2 and b≤c2b \le c_2 is counted in P(r2+1,c2+1)P(r_2 + 1, c_2 + 1), and is subtracted in P(r1,c2+1)P(r_1, c_2 + 1) when a<r1a < r_1 and in P(r2+1,c1)P(r_2 + 1, c_1) when b<c1b < c_1, and added back in P(r1,c1)P(r_1, c_1) when both hold. Its weight is (1−[a<r1])(1−[b<c1])(1 - [a < r_1])(1 - [b < c_1]), which is 1 inside the rectangle and 0 outside. Cells with a>r2a > r_2 or b>c2b > c_2 are in no term. The group is commutative, so the order of the terms does not matter. □\square

Termination. In BUILD all loops run over fixed finite ranges. In ADD, i≥1i \ge 1 and j≥1j \ge 1 at each step, so g(i),g(j)≥1g(i), g(j) \ge 1 and the indices grow strictly up to the bounds nn and mm. In PREFIX, the indices drop strictly to 0 by Lemma 1. So every operation ends.

Theorem. After BUILD on MM and any sequence of ADD calls, QUERY returns the sum of the current matrix over the given rectangle.

Proof. By Lemma 3, (F) holds after BUILD, and by Lemma 4 it holds after each ADD for the current matrix. By Lemma 6, QUERY then returns the rectangle sum, and every call ends by the termination argument. ■\blacksquare

Back to Two-dimensional Fenwick tree