The nca depth algorithm returns the depth of
ProvedHarelTarjan.SymOrder.ncaDepthAlg_correctLet be the complete binary tree of depth , numbered in symmetric order. For all vertices of , the algorithm to solve the nca depth problem of §3 (p. 342) returns the depth of their nearest common ancestor:
Here is if ; otherwise if ; otherwise .
This is Step 1 of the algorithm to compute ; the paper derives it from Lemmas 2 and 4.
import Mathlib import Definitions.Def_HarelTarjan_SymOrder_Tree import Definitions.Def_HarelTarjan_SymOrder_Sym import Definitions.Def_HarelTarjan_SymOrder_Algorithms
namespace HarelTarjan.SymOrder
/-- The algorithm to solve the nca depth problem (§3, p. 342) is correct: for all vertices `v`,
`w`, it returns the depth of the nearest common ancestor of `v` and `w`. -/
theorem ncaDepthAlg_correct {d : ℕ} (v w : Vertex d) :
ncaDepthAlg v w = depth (nca v w) := by sorry
end HarelTarjan.SymOrder
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
This theorem says the first algorithm computes the depth of the nearest common ancestor. Let and let be the set of boolean sequences of length . The depth is the sequence length and the height is . The vertex has as its sequence the longest common prefix of and . The number counts the with . The key replaces false by and true by and appends , and the order is lexicographic with a proper prefix counting as smaller.
The algorithm value is computed in three cases:
- If and , it returns .
- Otherwise, if the same two conditions hold with in place of , it returns .
- Otherwise it returns , with truncated subtraction, bitwise exclusive or , and .
For all , with no hypotheses, the theorem asserts
Degenerate cases:
- When , the claim reduces to .
- When , both sides are .
- The statement also covers pairs where one vertex is an ancestor of the other.
- It implicitly asserts that the truncated subtraction and the convention in the third branch never make the value differ from the true depth.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.