Algorithm Search⌘K

Proofs: MX-CIF quadtree

These are the human proofs of MX-CIF quadtree, at full length.

The MX-CIF quadtree stores each rectangle at its smallest enclosing cell and reports exactly the rectangles that meet a window full

This proof shows: correctness, termination.

We use the names of the pseudocode. Cells are half-open squares, the root cell is [0,2D)2[0, 2^D)^2, and rectangles are half-open with integer corners. For a cell ZZ of side s≥2s \ge 2 and a rectangle r⊆Zr \subseteq Z, write quad(Z,r)\text{quad}(Z, r) for the quadrant of ZZ that holds rr, or none if no quadrant does; for s=1s = 1 it is none. The invariant (M) asks: (1) each stored rectangle is stored once, at a node uu whose cell holds it, with quad(u,r)\text{quad}(u, r) none; (2) the children of a node are its quadrants; (3) each node other than the root stores a rectangle or has a child.

Lemma 1 (the quadrant test). For r⊆Zr \subseteq Z, the test of the representation returns the quadrant that holds rr if there is one, and none otherwise. Proof. On the xx axis, [x1,x2)⊆[c,cx)[x_1, x_2) \subseteq [c, c_x) if and only if x2≤cxx_2 \le c_x, and [x1,x2)⊆[cx,c+s)[x_1, x_2) \subseteq [c_x, c + s) if and only if x1≥cxx_1 \ge c_x, since r⊆Zr \subseteq Z. The same holds on the yy axis, and a quadrant is the product of one half on each axis. □\square

Lemma 2 (the path of a rectangle). Let Z0Z_0 be the root cell and Zi+1=quad(Zi,r)Z_{i+1} = \text{quad}(Z_i, r) while this is not none, ending at ZtZ_t. Then ZtZ_t is the smallest cell of the quadtree that holds rr, and every cell of the quadtree that holds rr is one of Z0,…,ZtZ_0, \ldots, Z_t. Proof. Cells of the quadtree are nested or disjoint. A cell WW that holds rr meets Z0Z_0, so W⊆Z0W \subseteq Z_0; if W⊆ZiW \subseteq Z_i and W≠ZiW \ne Z_i, then WW lies in one quadrant of ZiZ_i, which holds rr, so it is Zi+1Z_{i+1} by Lemma 1 and W⊆Zi+1W \subseteq Z_{i+1}. Since the sides halve, W=ZiW = Z_i for some i≤ti \le t. Also no quadrant of ZtZ_t holds rr, so no cell smaller than ZtZ_t does. □\square

INSERT. Lines L1 to L4 follow the path of Lemma 2, making the missing nodes as quadrants of their parents, so part 2 holds. They stop at ZtZ_t, after at most DD steps. Line L5 stores rr at ZtZ_t, which holds rr and has no quadrant that holds it, so part 1 holds; the code first checks that rr is not already there, and by Lemma 2 this is the only node where it could be. Each node made on the path now has rr below it, so part 3 holds.

DELETE. By Lemma 2 and part 1, if rr is stored, it is at the node of ZtZ_t, and all nodes on the path to it exist; so line L11 returns false only if rr is not stored, and line L12 finds rr if it is. After removing rr, parts 1 and 2 still hold, and part 3 can fail only at nodes of the path, since only their sets of rectangles below them changed. Lines L13 and L14 go up the path and remove each node with no rectangle and no child. When the loop stops at a node uu that stores a rectangle or has a child, every node above uu has uu below it, so part 3 holds there. The loop goes up one level each pass, so it stops after at most DD passes.

Uniqueness. By part 1 and Lemma 2, each rectangle is at the node of its own ZtZ_t, which depends only on the rectangle. By part 3, a node exists if and only if its cell is on the path of a stored rectangle. So the tree depends only on the set of rectangles.

WINDOW. Each node is pushed once, by its parent, so the loop stops after visiting each node at most once, and by part 1 no rectangle is reported twice. Each reported rectangle is stored and meets qq, by line L9. Let rr be stored at uu and meet qq. For each node ww on the path from the root to uu, the cell of ww holds the cell of uu, which holds rr, so it meets qq, and line L7 does not skip ww. Line L10 pushes all children, so uu is visited, and line L9 reports rr.

STAB. If rr holds pp and is stored at uu, then the cell of uu holds pp, so it is one of the cells of the quadtree that hold pp, which form the single path from the root to the cell of side 1 at pp. The stab walk visits the existing nodes of that path, and each rectangle it reports holds pp by its test. ■\blacksquare

Back to MX-CIF quadtree