Algorithm Search⌘K

Proofs: Two-dimensional segment tree

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

The two-dimensional segment tree keeps the rectangle products and returns rectangle queries full

This proof shows: correctness, termination.

Let (S,⊕,e)(S, \oplus, e) be a commutative monoid. For a finite set ZZ of cells of MM write Π(Z)=⨁(i,j)∈ZM[i][j]\Pi(Z) = \bigoplus_{(i, j) \in Z} M[i][j], which is well defined by associativity and commutativity, with Π(∅)=e\Pi(\emptyset) = e, and Π(Z1∪Z2)=Π(Z1)⊕Π(Z2)\Pi(Z_1 \cup Z_2) = \Pi(Z_1) \oplus \Pi(Z_2) for disjoint Z1,Z2Z_1, Z_2. For an outer node vxv_x with rows X(vx)=[lx,rx]X(v_x) = [l_x, r_x] and an inner node vyv_y with columns Y(vy)=[ly,ry]Y(v_y) = [l_y, r_y], the invariant is

(T)t[vx][vy]=Π(X(vx)×Y(vy)).\text{(T)} \qquad t[v_x][v_y] = \Pi\bigl(X(v_x) \times Y(v_y)\bigr).

We use the facts about the shape of a one-dimensional segment tree from the segment tree page: the children of a node with range [lo,hi][lo, hi], lo<hilo < hi, have the ranges [lo,mid][lo, mid] and [mid+1,hi][mid + 1, hi], which split it; a node with lo=hilo = hi is a leaf; and the recursion depth is at most ⌈lg⁡n⌉+1\lceil \lg n \rceil + 1.

Termination. Every recursive call of BUILD-X, BUILD-Y, QUERY-X, QUERY-Y, UPDATE-X and UPDATE-Y is on a child range, which is strictly shorter than the range of the caller, and no call recurses on a range of length 1. So each call tree is finite.

Lemma 1 (build). After BUILD-X(1,0,n−1)(1, 0, n - 1), (T) holds for every outer node vxv_x and every inner node vyv_y.

Proof. By induction on the outer recursion: assume that (T) holds for all cells of the outer children of vxv_x when line L7 runs, which line L6 ensures by the hypothesis, since it builds them first; outer leaves have no children. We show that BUILD-Y on vxv_x makes (T) true for each vyv_y, by induction on the inner recursion. At an inner leaf, Y(vy)={ly}Y(v_y) = \{l_y\}. If vxv_x is an outer leaf, X(vx)={lx}X(v_x) = \{l_x\} and line L2 stores M[lx][ly]=Π({(lx,ly)})M[l_x][l_y] = \Pi(\{(l_x, l_y)\}). Otherwise, X(vx)X(v_x) is the disjoint union of X(2vx)X(2v_x) and X(2vx+1)X(2v_x + 1), and by the outer hypothesis line L2 stores Π(X(2vx)×{ly})⊕Π(X(2vx+1)×{ly})=Π(X(vx)×{ly})\Pi(X(2v_x) \times \{l_y\}) \oplus \Pi(X(2v_x + 1) \times \{l_y\}) = \Pi(X(v_x) \times \{l_y\}). At an inner node that is not a leaf, line L4 builds both inner children first, so by the inner hypothesis line L5 stores Π(X(vx)×Y(2vy))⊕Π(X(vx)×Y(2vy+1))=Π(X(vx)×Y(vy))\Pi(X(v_x) \times Y(2v_y)) \oplus \Pi(X(v_x) \times Y(2v_y + 1)) = \Pi(X(v_x) \times Y(v_y)), since Y(2vy)Y(2v_y) and Y(2vy+1)Y(2v_y + 1) split Y(vy)Y(v_y). □\square

Lemma 2 (inner query). If (T) holds for vxv_x, then QUERY-Y(vx,vy,ly,ry,c1,c2)(v_x, v_y, l_y, r_y, c_1, c_2), with [ly,ry]=Y(vy)[l_y, r_y] = Y(v_y), returns Π(X(vx)×([ly,ry]∩[c1,c2]))\Pi\bigl(X(v_x) \times ([l_y, r_y] \cap [c_1, c_2])\bigr).

Proof. By induction on ry−lyr_y - l_y. If the two ranges are disjoint, line L8 returns ee, the product of the empty set. If [ly,ry]⊆[c1,c2][l_y, r_y] \subseteq [c_1, c_2], line L9 returns t[vx][vy]t[v_x][v_y], which is right by (T). Otherwise ly<ryl_y < r_y, since a range of length 1 that meets [c1,c2][c_1, c_2] lies inside it. Then [ly,ry]∩[c1,c2][l_y, r_y] \cap [c_1, c_2] is the disjoint union of its intersections with [ly,my][l_y, m_y] and [my+1,ry][m_y + 1, r_y], and line L10 returns the product of the two results, which are right by the hypothesis. □\square

Lemma 3 (outer query). If (T) holds everywhere, then QUERY-X(vx,lx,rx,r1,r2,c1,c2)(v_x, l_x, r_x, r_1, r_2, c_1, c_2), with [lx,rx]=X(vx)[l_x, r_x] = X(v_x), returns Π(([lx,rx]∩[r1,r2])×[c1,c2])\Pi\bigl(([l_x, r_x] \cap [r_1, r_2]) \times [c_1, c_2]\bigr) for 0≤c1≤c2≤m−10 \le c_1 \le c_2 \le m - 1.

Proof. By induction on rx−lxr_x - l_x, as in Lemma 2. Line L11 returns ee for disjoint rows. Line L12 returns, by Lemma 2 at the inner root with Y(1)=[0,m−1]Y(1) = [0, m - 1], the value Π(X(vx)×[c1,c2])\Pi(X(v_x) \times [c_1, c_2]), which is right when X(vx)⊆[r1,r2]X(v_x) \subseteq [r_1, r_2]. Otherwise the rows split between the two children, and line L13 combines the two correct parts, which are products over disjoint sets of cells. □\square

Lemma 4 (update). Let (T) hold for a matrix MM, let M′M' differ from MM only at (r,c)(r, c), and let the stored matrix be M′M' when UPDATE-X(1,0,n−1,r,c)(1, 0, n - 1, r, c) is called. After the call, (T) holds for M′M'.

Proof. A cell (vx,vy)(v_x, v_y) has a different right side in (T) for MM and for M′M' only if r∈X(vx)r \in X(v_x) and c∈Y(vy)c \in Y(v_y). The outer nodes with r∈X(vx)r \in X(v_x) are the nodes on the root-to-leaf path PxP_x of row rr, and the inner nodes with c∈Y(vy)c \in Y(v_y) are the nodes on the path PyP_y of column cc. So only the cells of Px×PyP_x \times P_y need a new value.

The recursion of UPDATE-X visits exactly the nodes of PxP_x, and for each of them it calls UPDATE-Y only after the call on its child in PxP_x has returned (line L19 before line L20). We show by induction from the outer leaf upward that, when UPDATE-Y on vx∈Pxv_x \in P_x returns, all cells (vx,vy)(v_x, v_y) satisfy (T) for M′M'. Cells with vy∉Pyv_y \notin P_y did not change and were right. UPDATE-Y visits the nodes of PyP_y from the root down to the leaf and recomputes them on the way back. At the inner leaf, line L15 reads M′M' if vxv_x is an outer leaf, or the cells (2vx,vy)(2v_x, v_y) and (2vx+1,vy)(2v_x + 1, v_y) otherwise: one child is in PxP_x and right for M′M' by the outer hypothesis, and the other child's rows do not hold rr, so its cells were right and unchanged. As in Lemma 1, the new value is right. Each inner node of PyP_y above the leaf is recomputed at line L18 from its two inner children, of which the one in PyP_y was just made right and the other one did not change. So all cells of vxv_x are right for M′M'. At the outer root this gives (T) everywhere. □\square

Theorem. After BUILD-X on MM and any sequence of updates, each done by setting one entry and calling UPDATE-X, a call QUERY-X(1,0,n−1,r1,r2,c1,c2)(1, 0, n - 1, r_1, r_2, c_1, c_2) with 0≤r1≤r2≤n−10 \le r_1 \le r_2 \le n - 1 and 0≤c1≤c2≤m−10 \le c_1 \le c_2 \le m - 1 returns ⨁r1≤i≤r2⨁c1≤j≤c2M[i][j]\bigoplus_{r_1 \le i \le r_2} \bigoplus_{c_1 \le j \le c_2} M[i][j] for the current matrix.

Proof. By Lemma 1 and Lemma 4, (T) holds for the current matrix. By Lemma 3 at the outer root, with X(1)=[0,n−1]X(1) = [0, n - 1], the query returns Π([r1,r2]×[c1,c2])\Pi([r_1, r_2] \times [c_1, c_2]). All calls end by the termination argument. ■\blacksquare

Back to Two-dimensional segment tree