Algorithm Search⌘K

Proofs: Virtual tree

These are the human proofs of Virtual tree, at full length.

The virtual tree build returns the vertices and the parents of the virtual tree full

This proof shows: correctness, termination.

Let TT be a tree rooted at rr, with the preorder positions tin\mathrm{tin}, and let KK be a non-empty set of vertices. Write u⪯vu \preceq v when uu is an ancestor of vv (each vertex is its own ancestor), and K‾={lca(x,y):x,y∈K}\overline{K} = \{\mathrm{lca}(x, y) : x, y \in K\}, which contains KK since lca(x,x)=x\mathrm{lca}(x, x) = x. The build is BUILD-VIRTUAL-TREE with the lines L1 to L8.

Termination. Every line is a sort, a loop with a fixed bound, or a single step, so the build stops.

Fact 1 (intervals). The subtree of uu is the set of vertices whose preorder positions form the interval I(u)=[tin(u),tin(u)+s(u)−1]I(u) = [\mathrm{tin}(u), \mathrm{tin}(u) + s(u) - 1]. So (a) u⪯vu \preceq v implies tin(u)≤tin(v)\mathrm{tin}(u) \le \mathrm{tin}(v), with equality only for u=vu = v; and (b) if u⪯wu \preceq w and tin(u)≤tin(v)≤tin(w)\mathrm{tin}(u) \le \mathrm{tin}(v) \le \mathrm{tin}(w), then u⪯vu \preceq v, because I(u)I(u) contains tin(u)\mathrm{tin}(u) and tin(w)\mathrm{tin}(w), and so every position between them.

Lemma 1 (adjacent LCAs). Let x1,…,xbx_1, \ldots, x_b be distinct vertices sorted by tin\mathrm{tin}, and 1≤i<j≤b1 \le i < j \le b. Let ℓt=lca(xt,xt+1)\ell_t = \mathrm{lca}(x_t, x_{t+1}). Then lca(xi,xj)\mathrm{lca}(x_i, x_j) is the shallowest of ℓi,…,ℓj−1\ell_i, \ldots, \ell_{j-1}, and it equals lca(xi,xi+1,…,xj)\mathrm{lca}(x_i, x_{i+1}, \ldots, x_j).

Proof. Let c=lca(xi,xj)c = \mathrm{lca}(x_i, x_j). By Fact 1(b) with u=cu = c and w=xjw = x_j, c⪯xtc \preceq x_t for every i≤t≤ji \le t \le j. So c⪯ℓtc \preceq \ell_t for every tt, and c=lca(xi,…,xj)c = \mathrm{lca}(x_i, \ldots, x_j), since a common ancestor of all of them is one of xix_i and xjx_j. Each ℓt\ell_t is at least as deep as cc. Now suppose every ℓt\ell_t is strictly deeper than cc. Then each ℓt\ell_t lies in the subtree of one child of cc, and ℓt\ell_t is an ancestor of both xtx_t and xt+1x_{t+1}, so xtx_t and xt+1x_{t+1} lie below the same child of cc. By induction on tt, all of xi,…,xjx_i, \ldots, x_j lie below the same child of cc, which is then a common ancestor of xix_i and xjx_j deeper than cc, a contradiction. So some ℓt\ell_t has the depth of cc, and being a descendant of cc, it equals cc. □\square

Lemma 2 (closure). The set AA after line L4 is K‾\overline{K}, and K‾\overline{K} is closed under LCA: lca(a,a′)∈K‾\mathrm{lca}(a, a') \in \overline{K} for all a,a′∈K‾a, a' \in \overline{K}.

Proof. Line L1 lists the distinct keys x1,…,xbx_1, \ldots, x_b in preorder, and line L3 adds ℓ1,…,ℓb−1\ell_1, \ldots, \ell_{b-1}. Each ℓt\ell_t is in K‾\overline{K}. Conversely, each lca(xi,xj)\mathrm{lca}(x_i, x_j) with i<ji < j is some ℓt\ell_t by Lemma 1, and lca(xi,xi)=xi\mathrm{lca}(x_i, x_i) = x_i is a key. So the set of line L4 is K‾\overline{K}.

For closure, let a=lca(xi,xj)a = \mathrm{lca}(x_i, x_j) and a′=lca(xi′,xj′)a' = \mathrm{lca}(x_{i'}, x_{j'}). Then lca(a,a′)\mathrm{lca}(a, a') is the LCA of the set X={xi,xj,xi′,xj′}X = \{x_i, x_j, x_{i'}, x_{j'}\}. The LCA of a set of keys is a common ancestor of all of them, and by the argument of Lemma 1 it equals the LCA of the first and the last of them in preorder, which is in K‾\overline{K}. □\square

Lemma 3 (parents). Let A[0],…,A[m−1]A[0], \ldots, A[m-1] be K‾\overline{K} sorted by tin\mathrm{tin}. Then A[0]=lca(K)A[0] = \mathrm{lca}(K), and for i≥1i \ge 1 the nearest proper ancestor of A[i]A[i] in K‾\overline{K} is c=lca(A[i−1],A[i])c = \mathrm{lca}(A[i-1], A[i]).

Proof. lca(K)\mathrm{lca}(K) is in K‾\overline{K} by Lemma 1 (with i=1i = 1, j=bj = b) and is an ancestor of every vertex of K‾\overline{K}, so by Fact 1(a) its position is the smallest, and it is A[0]A[0].

Let i≥1i \ge 1, a=A[i]a = A[i], a−=A[i−1]a^- = A[i-1]. By Lemma 2, c∈K‾c \in \overline{K}, and c⪯ac \preceq a. If c=ac = a, then a⪯a−a \preceq a^-, and Fact 1(a) gives tin(a)<tin(a−)\mathrm{tin}(a) < \mathrm{tin}(a^-), against the order. So cc is a proper ancestor of aa in K‾\overline{K}. Suppose x∈K‾x \in \overline{K} is a proper ancestor of aa and strictly deeper than cc. Then tin(x)<tin(a)\mathrm{tin}(x) < \mathrm{tin}(a) by Fact 1(a), and since a−a^- is the element of K‾\overline{K} just before aa, tin(x)≤tin(a−)<tin(a)\mathrm{tin}(x) \le \mathrm{tin}(a^-) < \mathrm{tin}(a). By Fact 1(b) with u=xu = x, w=aw = a, x⪯a−x \preceq a^-. So xx is a common ancestor of a−a^- and aa deeper than their LCA cc, a contradiction. So cc is the nearest proper ancestor of aa in K‾\overline{K}. □\square

Theorem. BUILD-VIRTUAL-TREE(K)(K) returns the vertex set K‾\overline{K} of the virtual tree V(K)V(K) in preorder, sets par[0]=NIL\mathrm{par}[0] = \mathrm{NIL} for its root lca(K)\mathrm{lca}(K), and gives each other vertex its parent in V(K)V(K). The virtual tree has at most 2k−12k - 1 vertices.

Proof. By Lemma 2 the list of line L4 is K‾\overline{K} in preorder. Line L5 marks A[0]A[0], which is lca(K)\mathrm{lca}(K) by Lemma 3, as the root. Line L7 sets the parent of A[i]A[i] to lca(A[i−1],A[i])\mathrm{lca}(A[i-1], A[i]), which by Lemma 3 is the nearest proper ancestor of A[i]A[i] in K‾\overline{K}, the parent in the definition of V(K)V(K). Line L2 starts with b≤kb \le k vertices and line L3 adds b−1b - 1, so m≤2b−1≤2k−1m \le 2b - 1 \le 2k - 1. ■\blacksquare

Back to Virtual tree