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 , , and the root . Write for the number of edges from to and for the subtree of , the set of descendants of , including . For vertices , is the deepest vertex with . 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 iterations. Each vertex is pushed exactly once, and the entries appended to while is on the stack, from index up to the entry appended when is popped (exclusive), are exactly one entry for each time a vertex of is entered or returned to. In particular has entries and every entry in lies in , where is the index of the last entry written while 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 iterations and the loop ends. Every vertex is pushed: by induction on depth, a vertex with parent is pushed while is on top of the stack, because is popped only when it has no child left to enter. So there are pushes and pops. Each push appends one entry; each pop except the last (the root) appends one entry. That gives entries.
The stack always holds the path from to its top, since line L5 pushes a child of the top. While is on the stack, the top is a descendant of , and every entry appended is either a pushed vertex, which is the new top, or the new top after a pop, which is still or a descendant of (the last pop with still on the stack leaves on top). So all entries from until is popped are in , and the entry written when is popped is its parent.
Lemma 2 (the range lies in ). Let be vertices with and . Every entry of lies in , so its depth is at least .
Proof. Since , they are pushed while is on the stack (each is pushed after its parent, which by induction is after , and before is popped, since is popped only after every vertex of has been entered). So , and by Lemma 1 every entry in that range lies in . A vertex of has depth at least .
Lemma 3 (the range contains ). Under the assumptions of Lemma 2, occurs in .
Proof. If , then . If , then , so . Otherwise , and and lie in the subtrees of children and of , with because is the deepest common ancestor. Since and the blocks of and are disjoint (each is written while its root is on the stack, and two siblings are never on the stack together), is entered and popped before is entered. When is popped, line L6 appends its parent . That entry comes after , which lies in the block of , and before .
Lemma 4 (the minimum is unique). Under the same assumptions, is the only vertex of depth at most in .
Proof. By Lemma 2 every entry lies in , and the only vertex of of depth is ; all others are deeper.
Lemma 5 (the sparse table). After lines L7 to L9, for every and with , is a vertex of least depth among .
Proof. By induction on . For , line L7 sets . For , the range of length is the union of the ranges and , whose shallowest vertices are and by induction; line L9 keeps the shallower one, which is a shallowest vertex of the union.
Theorem. For all vertices and , LCA returns .
Proof. Line L10 sets to the two first occurrences, so by symmetry we may assume and . Let and , so . The blocks and lie inside , and they cover it, because follows from . By Lemma 5, line L12 returns a vertex of least depth in the union, that is, of least depth in . By Lemma 3, is in the range, so , and by Lemma 4, . Each query does a fixed number of steps, so it ends.