Algorithm Search⌘K

Proofs: Region quadtree

These are the human proofs of Region quadtree, at full length.

The region quadtree operations keep a unique maximal tree of the image full

This proof shows: correctness, termination.

A block β\beta is uniform if all its pixels have one color. For a block β\beta and an image II, let T(β)T(\beta) be the tree defined by: if β\beta is uniform with color cc, a leaf of color cc; else an internal node whose children are T(β0),…,T(β3)T(\beta_0), \ldots, T(\beta_3) for the quadrants β0,…,β3\beta_0, \ldots, \beta_3 of β\beta. A block of side 1 is uniform, so T(β)T(\beta) is well defined by induction on the side.

Lemma 1 (uniqueness). A tree for a block β\beta satisfies (Q) for II exactly when it equals T(β)T(\beta). Proof. By induction on the side ss of β\beta. For s=1s = 1, by part 1 the only tree is a leaf with the color of the pixel. Let s≥2s \ge 2 and let RR satisfy (Q). If RR is a leaf, part 1 says that β\beta is uniform, and R=T(β)R = T(\beta). If RR is internal, each child satisfies (Q) for its quadrant, so by induction the children are T(β0),…,T(β3)T(\beta_0), \ldots, T(\beta_3). If β\beta were uniform with color cc, each quadrant would be uniform, each child a leaf of color cc, and part 2 would fail. So β\beta is not uniform, and R=T(β)R = T(\beta). Conversely, T(β)T(\beta) satisfies part 1 by its definition, and part 2 because, if all four children of an internal node are leaves of color cc, all four quadrants are uniform with color cc, so β\beta is uniform, and T(β)T(\beta) would be a leaf. □\square

Lemma 2 (merge). If R0,…,R3R_0, \ldots, R_3 equal T(β0),…,T(β3)T(\beta_0), \ldots, T(\beta_3), the merge step of lines L4 and L5 returns T(β)T(\beta). Proof. β\beta is uniform with color cc exactly when each βi\beta_i is uniform with color cc, that is, exactly when each RiR_i is a leaf of color cc. In that case the step returns a leaf of color cc; else it returns the internal node with the RiR_i, and both cases are T(β)T(\beta). □\square

BUILD. Claim: after the round with side ss (and at the start, for s=1s = 1, by line L1), the node kept for each block of side ss is TT of that block. The induction step is Lemma 2 applied in line L3 to each block of side 2s2s. The loop stops when s=Ns = N, after DD rounds, and the node of the whole image is TT of the image. By Lemma 1 it satisfies (Q).

GET. The block of the child chosen in line L7 holds (x,y)(x, y): the pixel is in the right half exactly when x≥x0+s/2x \ge x_0 + s/2, and in the bottom half exactly when y≥y0+s/2y \ge y_0 + s/2. So each node on the path holds the pixel, and the side halves each step, so the loop stops at a leaf after at most DD steps. By part 1 of (Q), its color is the color of (x,y)(x, y).

SET. If the pixel already has color cc, the image does not change and the tree is kept. Else, replacing a leaf of color c′c' and side s≥2s \ge 2 by an internal node with four leaves of color c′c' (line L11) keeps part 1, since each pixel keeps its color. Lines L10 and L11 go down to the leaf of side 1 at (x,y)(x, y), and line L12 sets it to cc, so after line L12 part 1 holds for the new image I′I'. Part 2 can now fail only at nodes on the recorded path, since only they changed. Let the path be the blocks γ1⊃γ2⊃⋯⊃γD\gamma_1 \supset \gamma_2 \supset \cdots \supset \gamma_D, from the quadrant of the root down to the pixel's block of side 1. Each child that is not on the path is TT of its block for I′I': it is a subtree that was not changed, or a leaf made by a split, which is uniform with its color in I′I' since the only changed pixel is on the path; and in both cases it satisfies (Q), so it is TT of its block by Lemma 1. The node for γD\gamma_D is a leaf of side 1, which is T(γD)T(\gamma_D). Line L13 goes up: if the node for γj+1\gamma_{j+1} is T(γj+1)T(\gamma_{j+1}), then the node for γj\gamma_j has four children equal to TT of its quadrants, and the merge of Lemma 2 makes it T(γj)T(\gamma_j). By induction up to the root, the tree is TT of the image I′I' and satisfies (Q). The path has DD blocks, so SET stops.

UNION. Claim: for nodes A=TI(β)A = T_I(\beta) and B=TJ(β)B = T_J(\beta) of two images, UNION returns TI∪J(β)T_{I \cup J}(\beta), where I∪JI \cup J is the pixelwise OR. By induction on the side of β\beta. If either is a black leaf, every pixel of β\beta is black in I∪JI \cup J, and a black leaf is TT (line L14). If AA is a white leaf, I∪JI \cup J equals JJ on β\beta, and B=TJ(β)B = T_J(\beta) is the answer (line L15); the same for BB. Else both are internal, the side is at least 2, and by induction the four recursive results are TI∪JT_{I \cup J} of the quadrants; Lemma 2 gives TI∪J(β)T_{I \cup J}(\beta) (line L16). INTERSECTION is the same with black and white swapped, since the AND with a white block is white and the AND with a black block is the other block (line L17). Each call goes down one level, and there are at most DD levels, so both stop. ■\blacksquare

Back to Region quadtree