Proofs: Ladder decomposition
These are the human proofs of Ladder decomposition, at full length.
The ladder algorithm returns the level ancestor full
This proof shows: correctness, termination.
Let the tree have vertices , , and root . Write for the number of edges from to , for the subtree of , and for the number of vertices on a longest downward path from , so for a leaf and over the children otherwise. For , is the unique ancestor of at depth . Lines L1 to L11 are those of the pseudocode on the page.
Lemma 1 (heights). After line L2, and are as defined: is a child of with , for every non-leaf .
Proof. Reverse BFS order lists every vertex after all its children, so when is handled the values of its children are final.
Lemma 2 (partition). Every vertex lies on the long path of exactly one head, and the long path of a head has exactly vertices, with depths .
Proof. Walk up from while the current vertex is the long child of its parent; the walk stops at a head , and is on the long path of . Any head whose long path contains is found by this walk, because the long path of a head goes down through long children only, and a head is not the long child of its parent, so it is unique. Along the long path, decreases by exactly 1 per step (Lemma 1) and the path ends at a leaf, where , so it has vertices, one per depth.
Lemma 3 (ladders). For each head with long path , , the ladder built in lines L5 and L6 is the list of the ancestors of the bottom vertex of at the depths , where . Hence for every and every with , entry of the ladder is .
Proof. lists the nearest proper ancestors of , topmost first, which are the ancestors of at depths ; continues with one vertex per depth from down (Lemma 2), each the child of the one before. So the ladder lists one ancestor of the bottom vertex per depth, starting at depth , and entry has depth . For and , the ancestor of the bottom vertex at depth is also the ancestor of at depth , since is itself on that vertical path.
Lemma 4 (jump pointers). After line L7, is the -th ancestor of when .
Proof. By induction on : is the parent, and the -th ancestor is the -th ancestor of the -th ancestor. When both intermediate steps stay inside the tree, so the clamping of the root at line L1 is never used. The table has levels for every .
Theorem. LEVEL-ANCESTOR returns if and none otherwise, and all steps terminate.
Proof. Line L8 handles the invalid . Let ; if , line L9 returns . Otherwise let , so , and , the ancestor of at depth (Lemma 4). Then , and .
The path from down to has vertices, so . Let be the head of the long path containing . Since is on that path below , . Now
so . Also . By Lemma 3, entry of the ladder of is , which line L11 returns.
Termination: the BFS visits each vertex once, the walks of lines L4 and L5 have at most steps for a path of vertices, line L7 is a bounded double loop, and a query does a fixed number of steps.