The virtual tree build returns the vertices and the parents of the virtual tree full
This proof shows: correctness, termination.
Let T be a tree rooted at r, with the preorder positions tin, and let K be a non-empty set of vertices. Write u⪯v when u is an ancestor of v (each vertex is its own ancestor), and K={lca(x,y):x,y∈K}, which contains K since 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 u is the set of vertices whose preorder positions form the interval I(u)=[tin(u),tin(u)+s(u)−1]. So (a) u⪯v implies tin(u)≤tin(v), with equality only for u=v; and (b) if u⪯w and tin(u)≤tin(v)≤tin(w), then u⪯v, because I(u) contains tin(u) and tin(w), and so every position between them.
Lemma 1 (adjacent LCAs). Let x1,…,xb be distinct vertices sorted by tin, and 1≤i<j≤b. Let ℓt=lca(xt,xt+1). Then lca(xi,xj) is the shallowest of ℓi,…,ℓj−1, and it equals lca(xi,xi+1,…,xj).
Proof. Let c=lca(xi,xj). By Fact 1(b) with u=c and w=xj, c⪯xt for every i≤t≤j. So c⪯ℓt for every t, and c=lca(xi,…,xj), since a common ancestor of all of them is one of xi and xj. Each ℓt is at least as deep as c. Now suppose every ℓt is strictly deeper than c. Then each ℓt lies in the subtree of one child of c, and ℓt is an ancestor of both xt and xt+1, so xt and xt+1 lie below the same child of c. By induction on t, all of xi,…,xj lie below the same child of c, which is then a common ancestor of xi and xj deeper than c, a contradiction. So some ℓt has the depth of c, and being a descendant of c, it equals c. □
Lemma 2 (closure). The set A after line L4 is K, and K is closed under LCA: lca(a,a′)∈K for all a,a′∈K.
Proof. Line L1 lists the distinct keys x1,…,xb in preorder, and line L3 adds ℓ1,…,ℓb−1. Each ℓt is in K. Conversely, each lca(xi,xj) with i<j is some ℓt by Lemma 1, and lca(xi,xi)=xi is a key. So the set of line L4 is K.
For closure, let a=lca(xi,xj) and a′=lca(xi′,xj′). Then lca(a,a′) is the LCA of the set X={xi,xj,xi′,xj′}. 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. □
Lemma 3 (parents). Let A[0],…,A[m−1] be K sorted by tin. Then A[0]=lca(K), and for i≥1 the nearest proper ancestor of A[i] in K is c=lca(A[i−1],A[i]).
Proof. lca(K) is in K by Lemma 1 (with i=1, j=b) and is an ancestor of every vertex of K, so by Fact 1(a) its position is the smallest, and it is A[0].
Let i≥1, a=A[i], a−=A[i−1]. By Lemma 2, c∈K, and c⪯a. If c=a, then a⪯a−, and Fact 1(a) gives tin(a)<tin(a−), against the order. So c is a proper ancestor of a in K. Suppose x∈K is a proper ancestor of a and strictly deeper than c. Then tin(x)<tin(a) by Fact 1(a), and since a− is the element of K just before a, tin(x)≤tin(a−)<tin(a). By Fact 1(b) with u=x, w=a, x⪯a−. So x is a common ancestor of a− and a deeper than their LCA c, a contradiction. So c is the nearest proper ancestor of a in K. □
Theorem. BUILD-VIRTUAL-TREE(K) returns the vertex set K of the virtual tree V(K) in preorder, sets par[0]=NIL for its root lca(K), and gives each other vertex its parent in V(K). The virtual tree has at most 2k−1 vertices.
Proof. By Lemma 2 the list of line L4 is K in preorder. Line L5 marks A[0], which is lca(K) by Lemma 3, as the root. Line L7 sets the parent of A[i] to lca(A[i−1],A[i]), which by Lemma 3 is the nearest proper ancestor of A[i] in K, the parent in the definition of V(K). Line L2 starts with b≤k vertices and line L3 adds b−1, so m≤2b−1≤2k−1. ■