LCA by binary lifting returns the lowest common ancestor full
This proof shows: correctness, termination.
Let the tree have the vertices 0,…,n−1 and the root r. Write depth(x) for the number of edges from r to x, p(x) for the parent of x with p(r)=r, and pj(x) for p applied j times. For 0≤j≤depth(x), pj(x) is the ancestor of x at distance j; for j>depth(x) it is r. A vertex is an ancestor of itself. For vertices u,v, lca(u,v) is the deepest common ancestor; it exists because r is a common ancestor, and it is unique because the common ancestors of u and v all lie on the path from u to r, which has at most one vertex at each depth.
Lemma 1 (the table). After PREPROCESS, up[k][x]=p2k(x) for each 0≤k<K and each vertex x.
Proof. Line L1 computes p and depth by BFS, which visits the vertices in order of distance from r and sets the parent of each vertex to the vertex from which it is first reached, a vertex one level closer to r. Line L3 gives up[0][x]=p(x)=p1(x). For k≥1, the loop of line L4 runs after level k−1 is complete, and by the induction hypothesis on k, up[k][x]=p2k−1(p2k−1(x))=p2k(x). □
Lemma 2 (lifting and the LCA). (a) If depth(u)≥depth(v) and d=depth(u)−depth(v), then lca(pd(u),v)=lca(u,v). (b) If depth(u)=depth(v), then for each j≥0, pj(u)=pj(v) if and only if j≥depth(u)−depth(lca(u,v)).
Proof. (a) The common ancestors of u and v have depth at most depth(v), since each is an ancestor of v. The ancestors of u with depth at most depth(v) are exactly the ancestors of pd(u). So u and pd(u) have the same common ancestors with v, and the same deepest one. (b) Let ℓ=lca(u,v) and t=depth(u)−depth(ℓ). If j≥t, both pj(u) and pj(v) are the ancestor of ℓ at distance j−t (or r), so they are equal. If j<t and pj(u)=pj(v)=x, then x is a common ancestor of u and v with depth(x)=depth(u)−j>depth(ℓ), which contradicts the choice of ℓ. □
Theorem. LCA(u,v) returns lca(u,v), after O(K) steps.
Proof. Write ℓ=lca(u,v) for the original pair. After line L5, depth(u)≥depth(v), and the pair is the same set, so ℓ is unchanged.
Line L7. Let d=∑k∈B2k, where B is the set of one bits of d. Since d≤n−1<2K, B⊆{0,…,K−1}. By Lemma 1 each jump applies p2k, and pa(pb(x))=pa+b(x), so after line L7, u is pd of the original u. By Lemma 2(a), lca(u,v)=ℓ still, and now depth(u)=depth(v).
Line L8. If u=v, then u is an ancestor of both original vertices at the depth of v, and ℓ=lca(u,u)=u. The return is correct.
Lines L9 and L10. Now u=v and depth(u)=depth(v). Let t=depth(u)−depth(ℓ)≥1, the distance from u (and from v) up to ℓ, and let s=t−1≥0. We claim this invariant before the step for k (for k=K−1,…,0), and after the step for k=0 with k=−1:
(I) u=v, depth(u)=depth(v), lca(u,v)=ℓ, and s<2k+1.
Initialization. Before the step for k=K−1: the first three parts hold as shown. Also s<t≤depth(u)≤n−1<2K.
Maintenance. Consider the step for k. By Lemma 1, up[k][u]=p2k(u) and up[k][v]=p2k(v). By Lemma 2(b), these differ if and only if 2k<t, that is 2k≤s.
If 2k≤s, line L10 sets u←p2k(u) and v←p2k(v). The new vertices differ, have equal depth, and are ancestors of the old ones below ℓ (their distance to ℓ is t−2k≥1), so ℓ is still their LCA by Lemma 2(b) applied to the old pair. The new s is s−2k<2k+1−2k=2k.
If 2k>s, nothing changes, and s<2k already.
In both cases (I) holds with k−1 in place of k.
Termination. The loop has exactly K steps. After the step for k=0, (I) gives s<20=1, so s=0 and t=1: ℓ is the parent of u. Line L11 returns up[0][u]=p(u)=ℓ.
The number of steps is at most K for line L7 and exactly K for line L9, each of O(1) work. ■