Algorithm Search⌘K

Proofs: S2 cell hierarchy

These are the human proofs of S2 cell hierarchy, at full length.

S2 cell ids name the cells of the hierarchy, PARENT, CHILDREN and CONTAINS follow the hierarchy, and FROM-POINT returns the leaf that contains the point full

This proof shows: correctness, termination.

Let L=30L = 30. A cell of level kk on face ff is a pair of blocks I×JI \times J of leaf coordinates, I=[a2L−k,(a+1)2L−k)I = [a 2^{L-k}, (a+1) 2^{L-k}) and J=[b2L−k,(b+1)2L−k)J = [b 2^{L-k}, (b+1) 2^{L-k}), with 0≤a,b<2k0 \le a, b < 2^k. Its children are the four cells of level k+1k + 1 inside it. We treat the face projection and the quadratic map as given functions from points to (f,i,j)(f, i, j): they are computed by closed formulas, so they stop, and we prove the claims about ids.

Orientations. An orientation is o∈{0,1,2,3}o \in \{0, 1, 2, 3\}, with bit 1 for swap and bit 2 for invert. The table IJ-TO-POS[o][o] maps the four bit pairs (ib,jb)(i_b, j_b) to positions 0 to 3. For each oo the row is a permutation of {0,1,2,3}\{0, 1, 2, 3\}, as the four rows (0,1,3,2)(0, 1, 3, 2), (0,3,1,2)(0, 3, 1, 2), (2,3,1,0)(2, 3, 1, 0) and (2,1,3,0)(2, 1, 3, 0) show, and POS-TO-IJ[o][o] is its inverse.

Lemma 1 (the encoding names one cell per level). Let FROM-POINT reach (f,i,j)(f, i, j), and let q1,…,qLq_1, \ldots, q_L be the positions computed at line L6, in order. For each kk, the first kk positions and ff are determined by the cell of level kk that contains (i,j)(i, j), and distinct cells of level kk on face ff give distinct sequences q1,…,qkq_1, \ldots, q_k.

Proof. By induction on kk. The orientation o1o_1 before the first step is f mod 2f \bmod 2, fixed by ff. Suppose the orientation oko_k before step kk is a function of the cell Ck−1C_{k-1} of level k−1k - 1 that contains (i,j)(i, j). The bits at step kk (bit L−kL - k of ii and of jj) say which of the four children of Ck−1C_{k-1} contains (i,j)(i, j), so qk=IJ-TO-POS[ok][⋅]q_k = \text{IJ-TO-POS}[o_k][\cdot] is a function of CkC_k, and ok+1=ok⊕POS-TO-ORIENTATION[qk]o_{k+1} = o_k \oplus \text{POS-TO-ORIENTATION}[q_k] is a function of CkC_k too. For distinctness: two cells of level kk on face ff with the same q1,…,qkq_1, \ldots, q_{k} have, by induction, the same parent cell and so the same oko_k, and since row oko_k of the table is a permutation, the same qkq_k means the same bit pair, so the same child. □\square

So the map from cells of level kk to the numbers f⋅22k+∑lql4k−lf \cdot 2^{2k} + \sum_l q_l 4^{k-l} is injective, and since both sets have 6⋅4k6 \cdot 4^k elements, it is a bijection.

Definition. The id of a cell CC of level kk with face ff and positions q1,…,qkq_1, \ldots, q_k is id(C)=f261+∑l=1kql261−2l+260−2k\text{id}(C) = f 2^{61} + \sum_{l=1}^{k} q_l 2^{61 - 2l} + 2^{60 - 2k}. Its lowest set bit is 260−2k2^{60 - 2k}, since the sum has no bits below 261−2k2^{61 - 2k}; so the level is recovered from the id as (60−z)/2(60 - z)/2, where zz is the number of trailing zeros.

Lemma 2 (FROM-POINT). Line L8 returns the id of the leaf cell {i}×{j}\{i\} \times \{j\}.

Proof. After the loop, pos=∑l=1Lql4L−l\text{pos} = \sum_{l=1}^{L} q_l 4^{L-l}, so f261+2 pos+1=f261+∑lql261−2l+20f 2^{61} + 2\,\text{pos} + 1 = f 2^{61} + \sum_l q_l 2^{61 - 2l} + 2^{0}, which is the definition for k=Lk = L. The loop runs LL times, so it stops. □\square

Lemma 3 (ranges). For a cell CC of level kk, the ids of the leaf cells inside CC are exactly the odd numbers in [id(C)−260−2k+1, id(C)+260−2k−1][\text{id}(C) - 2^{60-2k} + 1, \ \text{id}(C) + 2^{60-2k} - 1].

Proof. A leaf inside CC has the face and the first kk positions of CC, by Lemma 1, since its ancestor of level kk is CC, and any positions qk+1,…,qLq_{k+1}, \ldots, q_L; and every such sequence gives a leaf inside CC, by the bijection. Let P=f261+∑l≤kql261−2lP = f 2^{61} + \sum_{l \le k} q_l 2^{61 - 2l}. The leaf ids are P+R+1P + R + 1, where R=∑l>kql261−2lR = \sum_{l > k} q_l 2^{61 - 2l} runs over all even numbers from 0 to 261−2k−22^{61 - 2k} - 2. So they are the odd numbers from P+1P + 1 to P+261−2k−1P + 2^{61 - 2k} - 1. Since id(C)=P+260−2k\text{id}(C) = P + 2^{60 - 2k}, these are the odd numbers from id(C)−260−2k+1\text{id}(C) - 2^{60-2k} + 1 to id(C)+260−2k−1\text{id}(C) + 2^{60-2k} - 1. □\square

Theorem. (a) PARENT(id(C),k′)(\text{id}(C), k') for k′≤kk' \le k returns the id of the ancestor of CC at level k′k'. (b) CHILDREN(id(C))(\text{id}(C)) for k<Lk < L returns the ids of the four children of CC, in the order of their positions. (c) For cells AA and BB, CONTAINS(id(A),id(B))(\text{id}(A), \text{id}(B)) is true if and only if B⊆AB \subseteq A. All three stop after a constant number of word operations.

Proof. (a) Line L9 sets b=260−2k′b = 2^{60 - 2k'}. Clearing the bits of id(C)\text{id}(C) below bb removes the terms ql261−2lq_l 2^{61 - 2l} with 61−2l<60−2k′61 - 2l < 60 - 2k', that is l>k′l > k', and the marker bit of CC, which is below bb when k′<kk' < k; when k′=kk' = k, it removes the marker bit only. What is left is f261+∑l≤k′ql261−2lf 2^{61} + \sum_{l \le k'} q_l 2^{61 - 2l}, and line L10 adds bb, which gives the id of the ancestor of level k′k', whose positions are q1,…,qk′q_1, \ldots, q_{k'} by Lemma 1.

(b) With b=260−2kb = 2^{60 - 2k} the lowest set bit and c=b/4=260−2(k+1)c = b/4 = 2^{60 - 2(k+1)}, line L12 gives id(C)−b+c+2cq=f261+∑l≤kql261−2l+q 261−2(k+1)+260−2(k+1)\text{id}(C) - b + c + 2cq = f 2^{61} + \sum_{l \le k} q_l 2^{61 - 2l} + q\,2^{61 - 2(k+1)} + 2^{60 - 2(k+1)}, the id of the child at position qq. By the bijection, the four values of qq give the four children.

(c) If B⊆AB \subseteq A, every leaf of BB is a leaf of AA; by Lemma 3 applied to BB, id(B)\text{id}(B) lies in the center of the range of the leaves of BB, which is inside the range of the leaves of AA, so range_min(A)≤id(B)≤range_max(A)\text{range\_min}(A) \le \text{id}(B) \le \text{range\_max}(A), with range_min(A)=id(A)−(lsb(A)−1)\text{range\_min}(A) = \text{id}(A) - (\text{lsb}(A) - 1) and range_max(A)=id(A)+(lsb(A)−1)\text{range\_max}(A) = \text{id}(A) + (\text{lsb}(A) - 1), which is the range of Lemma 3. Conversely, suppose id(B)\text{id}(B) is in the range of AA. If BB is a leaf, id(B)\text{id}(B) is odd and, by Lemma 3, a leaf of AA, so B⊆AB \subseteq A. If BB is not a leaf, take the leaf λ\lambda of BB with the first kBk_B positions of BB, then position 2 and then all positions 0; its id is id(B)+1\text{id}(B) + 1. Since id(B)\text{id}(B) is even and range_max(A)\text{range\_max}(A) is odd, id(B)+1≤range_max(A)\text{id}(B) + 1 \le \text{range\_max}(A), so λ\lambda is a leaf of AA. The cells of the hierarchy are nested or disjoint, and AA and BB share the leaf λ\lambda, so one contains the other. If A⊊BA \subsetneq B, then lsb(A)<lsb(B)\text{lsb}(A) < \text{lsb}(B), and the range of AA is a proper part of the range of BB that does not contain its center id(B)\text{id}(B): by Lemma 3, the range of AA is the block of leaves of AA, which lies entirely in the lower half or the upper half of the block of BB, since the four children of BB split its block into four quarters around its center, and AA lies inside one child. This contradicts id(B)\text{id}(B) in the range of AA. So B⊆AB \subseteq A.

Each of the three functions does a fixed number of additions, subtractions, ANDs and shifts, so it stops. ■\blacksquare

Back to S2 cell hierarchy