Algorithm Search⌘K

Proofs: Linear quadtree

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

The linear quadtree keeps the list of maximal black blocks in Z-order full

This proof shows: correctness, termination.

We use the names of the pseudocode. For a block b=(c,ℓ)b = (c, \ell) let I(b)=[c,c+4ℓ)I(b) = [c, c + 4^\ell), the set of codes of its pixels. The invariant (L) asks: (1) 0≤ℓ≤D0 \le \ell \le D and 4ℓ∣c4^\ell \mid c; (2) the intervals I(b)I(b) are disjoint and the list is sorted by cc; (3) no four blocks (p+k4ℓ,ℓ)(p + k 4^\ell, \ell), k=0,…,3k = 0, \ldots, 3, with 4ℓ+1∣p4^{\ell+1} \mid p, are all in the list. The image of the list is the set of pixels whose codes lie in some I(b)I(b).

Lemma 1 (cells are intervals). For 4ℓ∣c4^\ell \mid c, the codes I(c,ℓ)I(c, \ell) are exactly the codes of the pixels of one aligned square of side 2ℓ2^\ell, and I(p,ℓ+1)I(p, \ell + 1) is the disjoint union of I(p+k4ℓ,ℓ)I(p + k 4^\ell, \ell), k=0,…,3k = 0, \ldots, 3. Two such intervals are either disjoint or nested. Proof. The code of (x,y)(x, y) has the bits of xx and yy at the positions 2i2i and 2i+12i + 1. Fixing all bits of position 2ℓ2\ell and above fixes the bits i≥ℓi \ge \ell of xx and yy, so it fixes an aligned square of side 2ℓ2^\ell, and leaves the lower 2ℓ2\ell bits free, which is the interval [c,c+4ℓ)[c, c + 4^\ell). The bits at positions 2ℓ2\ell and 2ℓ+12\ell + 1 give the four children in order. Two intervals of the form [c,c+4ℓ)[c, c + 4^\ell) with 4ℓ∣c4^\ell \mid c are dyadic in base 4, so they are disjoint or nested. □\square

Lemma 2 (uniqueness). For a set PP of black pixels, there is exactly one list satisfying (L) whose image is PP: the black leaves of the region quadtree of PP. Proof. Call a cell maximal black if it is all black and its parent is not (or it is the root). The maximal black cells are disjoint by Lemma 1, they cover PP, and they are the black leaves of the region quadtree. Let a list satisfy (L) with image PP. Each block is a black cell. Suppose some block bb is not maximal, so its parent QQ is all black. No block contains QQ, since it would overlap bb; so by Lemma 1 every block that meets QQ lies inside QQ, and since QQ is black these blocks tile QQ. Let b′b' be one of them of the least level mm; its parent Q′Q' lies inside QQ. A block that meets a child of Q′Q' is inside QQ, so it has level at least mm; if its level were more than mm it would contain Q′Q', by Lemma 1, and overlap b′b'. So each of the four children of Q′Q' is tiled by blocks of level exactly mm, that is, each child is a block, which part 3 forbids. Hence each block is maximal black. Each maximal black cell is tiled by blocks, which are maximal black cells and so cannot lie strictly inside it; so it is a block. □\square

LOCATE. By part 2, the intervals are sorted and disjoint. If z∈I(bi)z \in I(b_i), then ci≤zc_i \le z and every later block starts after zz, so ii is the last index with ci≤zc_i \le z, which line L7 finds, and line L8 accepts it. If line L8 accepts the last index with ci≤zc_i \le z, then z∈I(bi)z \in I(b_i). So LOCATE returns the block that holds zz, or none if zz is white.

BUILD. We claim that after each pass of lines L1 to L6, the stack, read from bottom to top, satisfies parts 1 and 2, has as image the pixels read so far, and satisfies part 3. Each block on the stack ends at or before the last code read, since a block is pushed as one pixel and a merge replaces four consecutive intervals by their union (Lemma 1). Line L2 pushes zz, which is larger than all earlier codes, so parts 1 and 2 hold. Line L4 merges the top four only when they are the four children of one cell, so the image does not change, and part 1 holds for the parent, and part 2 holds since the union of four consecutive intervals is an interval in the same place. For part 3, suppose four siblings (p+k4ℓ,ℓ)(p + k 4^\ell, \ell) are on the stack. They are consecutive in the sorted stack, since the blocks between them would lie in the parent's interval, and the last of them, by order of codes, was pushed or made last. At that moment the four were the top four, and lines L3 and L4 merged them. A merge makes one new block on top, and the loop goes on testing the new top four, so no complete group stays on the stack. Each code is pushed once and each merge removes three blocks, so the loop stops. By Lemma 2, the result is the list of black leaves.

INSERT. If line L10 finds pp black, nothing changes. Otherwise, {z}\{z\} is disjoint from all blocks, and line L11 adds (z,0)(z, 0) at its sorted place, so parts 1 and 2 hold and the image is P∪{p}P \cup \{p\}. Part 3 can now fail only for a group that contains a block that holds zz, since every other group was in the list before. Line L12 checks the group of the current block (c,ℓ)(c, \ell), which is the only block that holds zz. If the group is complete, line L13 replaces the four blocks by their parent, which does not change the image and keeps parts 1 and 2 by Lemma 1, and the new block is the only one that holds zz. If the group is not complete, or ℓ=D\ell = D, part 3 holds for all groups. Each pass raises ℓ\ell, so the loop stops after at most DD passes.

DELETE. If pp is white, nothing changes. Otherwise let (c,ℓ)(c, \ell) be the block that holds zz. For j<ℓj < \ell, let CjC_j be the cell of level jj that holds zz, so C0={z}C_0 = \{z\} and Cℓ=I(c,ℓ)C_\ell = I(c, \ell). By Lemma 1, Cj+1∖CjC_{j+1} \setminus C_j is the union of the three other children of Cj+1C_{j+1}, and so

I(c,ℓ)∖{z}=⋃j=0ℓ−1(Cj+1∖Cj),I(c, \ell) \setminus \{z\} = \bigcup_{j=0}^{\ell-1} (C_{j+1} \setminus C_j),

a disjoint union of the 3ℓ3\ell blocks that lines L16 and L17 add. They lie inside the removed block, so they are disjoint from the other blocks and from each other, parts 1 and 2 hold, and the image is P∖{p}P \setminus \{p\}. For part 3, a group made only of old blocks was there before. A group that contains a new block of level jj is the four children of Cj+1C_{j+1}, and one of them, CjC_j, holds the white pixel zz, so it is not a block. Any other group that changed would contain a part of the removed block, so it is one of these. Hence part 3 holds. The loop runs ℓ≤D\ell \le D times.

Blocks in a cell. For an aligned cell CC with codes [lo,hi)[lo, hi), Lemma 1 says each block is inside CC, contains CC, or is disjoint from it. If a block contains CC, it holds lolo, and LOCATE finds it. Otherwise the blocks inside CC are exactly those with lo≤c<hilo \le c < hi, a run of the sorted list. ■\blacksquare

Back to Linear quadtree