Algorithm Search⌘K

Proofs: LCA by Euler tour and RMQ

These are the human proofs of LCA by Euler tour and RMQ, at full length.

LCA by Euler tour and RMQ returns the lowest common ancestor full

This proof shows: correctness, termination.

Let the tree have the vertices 0,…,n−10, \ldots, n - 1, n≥1n \ge 1, and the root rr. Write depth(x)depth(x) for the number of edges from rr to xx and TxT_x for the subtree of xx, the set of descendants of xx, including xx. For vertices u,vu, v, w=lca(u,v)w = \mathrm{lca}(u, v) is the deepest vertex with u,v∈Twu, v \in T_w. The lines L1 to L12 are those of the pseudocode on the page.

Lemma 1 (termination and the shape of the tour). The loop of lines L2 to L6 ends after 2n−12n - 1 iterations. Each vertex xx is pushed exactly once, and the entries appended to EE while xx is on the stack, from index first[x]first[x] up to the entry appended when xx is popped (exclusive), are exactly one entry for each time a vertex of TxT_x is entered or returned to. In particular EE has 2n−12n - 1 entries and every entry in E[first[x]..last(x)]E[first[x] .. last(x)] lies in TxT_x, where last(x)last(x) is the index of the last entry written while xx is on the stack.

Proof. A vertex is pushed only in line L5, and only if it has not been entered before, so it is pushed at most once. Each iteration either pushes a vertex (line L5) or pops one (line L6), and a popped vertex is never pushed again, so there are at most 2n2n iterations and the loop ends. Every vertex is pushed: by induction on depth, a vertex cc with parent vv is pushed while vv is on top of the stack, because vv is popped only when it has no child left to enter. So there are nn pushes and nn pops. Each push appends one entry; each pop except the last (the root) appends one entry. That gives n+(n−1)=2n−1n + (n - 1) = 2n - 1 entries.

The stack always holds the path from rr to its top, since line L5 pushes a child of the top. While xx is on the stack, the top is a descendant of xx, and every entry appended is either a pushed vertex, which is the new top, or the new top after a pop, which is still xx or a descendant of xx (the last pop with xx still on the stack leaves xx on top). So all entries from first[x]first[x] until xx is popped are in TxT_x, and the entry written when xx is popped is its parent. □\square

Lemma 2 (the range lies in TwT_w). Let u,vu, v be vertices with first[u]≤first[v]first[u] \le first[v] and w=lca(u,v)w = \mathrm{lca}(u, v). Every entry of E[first[u]..first[v]]E[first[u] .. first[v]] lies in TwT_w, so its depth is at least depth(w)depth(w).

Proof. Since u,v∈Twu, v \in T_w, they are pushed while ww is on the stack (each is pushed after its parent, which by induction is after ww, and before ww is popped, since ww is popped only after every vertex of TwT_w has been entered). So first[w]≤first[u]≤first[v]≤last(w)first[w] \le first[u] \le first[v] \le last(w), and by Lemma 1 every entry in that range lies in TwT_w. A vertex of TwT_w has depth at least depth(w)depth(w). □\square

Lemma 3 (the range contains ww). Under the assumptions of Lemma 2, ww occurs in E[first[u]..first[v]]E[first[u] .. first[v]].

Proof. If u=wu = w, then E[first[u]]=wE[first[u]] = w. If v=wv = w, then first[v]=first[w]≤first[u]≤first[v]first[v] = first[w] \le first[u] \le first[v], so u=v=wu = v = w. Otherwise u≠w≠vu \ne w \ne v, and uu and vv lie in the subtrees of children c1c_1 and c2c_2 of ww, with c1≠c2c_1 \ne c_2 because ww is the deepest common ancestor. Since first[u]≤first[v]first[u] \le first[v] and the blocks of Tc1T_{c_1} and Tc2T_{c_2} are disjoint (each is written while its root is on the stack, and two siblings are never on the stack together), c1c_1 is entered and popped before c2c_2 is entered. When c1c_1 is popped, line L6 appends its parent ww. That entry comes after first[u]first[u], which lies in the block of c1c_1, and before first[c2]≤first[v]first[c_2] \le first[v]. □\square

Lemma 4 (the minimum is unique). Under the same assumptions, ww is the only vertex of depth at most depth(w)depth(w) in E[first[u]..first[v]]E[first[u] .. first[v]].

Proof. By Lemma 2 every entry lies in TwT_w, and the only vertex of TwT_w of depth depth(w)depth(w) is ww; all others are deeper. □\square

Lemma 5 (the sparse table). After lines L7 to L9, for every kk and ii with i+2k≤∣E∣i + 2^k \le |E|, M[k][i]M[k][i] is a vertex of least depth among E[i],…,E[i+2k−1]E[i], \ldots, E[i + 2^k - 1].

Proof. By induction on kk. For k=0k = 0, line L7 sets M[0][i]=E[i]M[0][i] = E[i]. For k≥1k \ge 1, the range of length 2k2^k is the union of the ranges E[i..i+2k−1−1]E[i .. i + 2^{k-1} - 1] and E[i+2k−1..i+2k−1]E[i + 2^{k-1} .. i + 2^k - 1], whose shallowest vertices are M[k−1][i]M[k-1][i] and M[k−1][i+2k−1]M[k-1][i + 2^{k-1}] by induction; line L9 keeps the shallower one, which is a shallowest vertex of the union. □\square

Theorem. For all vertices uu and vv, LCA(u,v)(u, v) returns lca(u,v)\mathrm{lca}(u, v).

Proof. Line L10 sets l≤hl \le h to the two first occurrences, so by symmetry we may assume l=first[u]l = first[u] and h=first[v]h = first[v]. Let m=h−l+1≥1m = h - l + 1 \ge 1 and k=⌊lg⁡m⌋k = \lfloor \lg m \rfloor, so 2k≤m<2k+12^k \le m < 2^{k+1}. The blocks E[l..l+2k−1]E[l .. l + 2^k - 1] and E[h−2k+1..h]E[h - 2^k + 1 .. h] lie inside E[l..h]E[l .. h], and they cover it, because h−2k+1≤l+2kh - 2^k + 1 \le l + 2^k follows from m<2k+1m < 2^{k+1}. By Lemma 5, line L12 returns a vertex xx of least depth in the union, that is, of least depth in E[l..h]E[l .. h]. By Lemma 3, ww is in the range, so depth(x)≤depth(w)depth(x) \le depth(w), and by Lemma 4, x=wx = w. Each query does a fixed number of steps, so it ends. ■\blacksquare

Back to LCA by Euler tour and RMQ