Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

The nca depth algorithm returns the depth of nca⁡(v,w)\operatorname{nca}(v,w)nca(v,w)

Proved
HarelTarjan.SymOrder.ncaDepthAlg_correct

by mikedeng1 · Sep 27, 2026 · Mathlib 0df444a (Lean v4.33.1)

algorithmsnearest-common-ancestorp2o-batch-p100bp2o-gran-per-chapterp2o-plan-paperp2o-v1trees

Let TTT be the complete binary tree of depth ddd, numbered in symmetric order. For all vertices v,wv, wv,w of TTT, the algorithm to solve the nca depth problem of §3 (p. 342) returns the depth of their nearest common ancestor:

ncaDepth(v,w)=depth⁡(nca⁡(v,w)).\mathrm{ncaDepth}(v,w) = \operatorname{depth}(\operatorname{nca}(v,w)).ncaDepth(v,w)=depth(nca(v,w)).

Here ncaDepth(v,w)\mathrm{ncaDepth}(v,w)ncaDepth(v,w) is d−h(v)d - h(v)d−h(v) if sym(w)∈[sym(v)−2h(v)+1,sym(v)+2h(v)−1]\mathrm{sym}(w) \in [\mathrm{sym}(v) - 2^{h(v)} + 1, \mathrm{sym}(v) + 2^{h(v)} - 1]sym(w)∈[sym(v)−2h(v)+1,sym(v)+2h(v)−1]; otherwise d−h(w)d - h(w)d−h(w) if sym(v)∈[sym(w)−2h(w)+1,sym(w)+2h(w)−1]\mathrm{sym}(v) \in [\mathrm{sym}(w) - 2^{h(w)} + 1, \mathrm{sym}(w) + 2^{h(w)} - 1]sym(v)∈[sym(w)−2h(w)+1,sym(w)+2h(w)−1]; otherwise d−⌊lg⁡(sym(v)⊕sym(w))⌋d - \lfloor \lg(\mathrm{sym}(v) \oplus \mathrm{sym}(w)) \rfloord−⌊lg(sym(v)⊕sym(w))⌋.

This is Step 1 of the algorithm to compute nca⁡(v,w)\operatorname{nca}(v,w)nca(v,w); the paper derives it from Lemmas 2 and 4.

Preamble
import Mathlib
import Definitions.Def_HarelTarjan_SymOrder_Tree
import Definitions.Def_HarelTarjan_SymOrder_Sym
import Definitions.Def_HarelTarjan_SymOrder_Algorithms
Formal statement
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
Source
Harel, Tarjan, Fast Algorithms for Finding Nearest Common Ancestors, SIAM J. Comput. 13 (1984), p. 342, §3, Algorithm to solve the nca depth problem (unnumbered)
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 d∈Nd\in\mathbb Nd∈N and let TdT_dTd​ be the set of boolean sequences of length ≤d\le d≤d. The depth is the sequence length and the height is h(v)=d−∣sv∣h(v)=d-|s_v|h(v)=d−∣sv​∣. The vertex nca⁡(v,w)\operatorname{nca}(v,w)nca(v,w) has as its sequence the longest common prefix of svs_vsv​ and sws_wsw​. The number sym⁡(v)\operatorname{sym}(v)sym(v) counts the u∈Tdu\in T_du∈Td​ with κ(u)≤lexκ(v)\kappa(u)\le_{\mathrm{lex}}\kappa(v)κ(u)≤lex​κ(v). The key κ\kappaκ replaces false by 000 and true by 222 and appends 111, and the order is lexicographic with a proper prefix counting as smaller.

The algorithm value ncaDepthAlg⁡(v,w)\operatorname{ncaDepthAlg}(v,w)ncaDepthAlg(v,w) is computed in three cases:

  • If sym⁡(v)+1≤sym⁡(w)+2h(v)\operatorname{sym}(v)+1\le\operatorname{sym}(w)+2^{h(v)}sym(v)+1≤sym(w)+2h(v) and sym⁡(w)+1≤sym⁡(v)+2h(v)\operatorname{sym}(w)+1\le\operatorname{sym}(v)+2^{h(v)}sym(w)+1≤sym(v)+2h(v), it returns d−h(v)d-h(v)d−h(v).
  • Otherwise, if the same two conditions hold with h(w)h(w)h(w) in place of h(v)h(v)h(v), it returns d−h(w)d-h(w)d−h(w).
  • Otherwise it returns d−˙⌊log⁡2(sym⁡(v)⊕sym⁡(w))⌋d\mathbin{\dot-}\lfloor\log_2(\operatorname{sym}(v)\oplus\operatorname{sym}(w))\rfloord−˙​⌊log2​(sym(v)⊕sym(w))⌋, with truncated subtraction, bitwise exclusive or ⊕\oplus⊕, and ⌊log⁡20⌋=0\lfloor\log_2 0\rfloor=0⌊log2​0⌋=0.

For all v,w∈Tdv,w\in T_dv,w∈Td​, with no hypotheses, the theorem asserts

ncaDepthAlg⁡(v,w)=depth⁡(nca⁡(v,w))=∣lcp⁡(sv,sw)∣.\operatorname{ncaDepthAlg}(v,w)=\operatorname{depth}(\operatorname{nca}(v,w))=|\operatorname{lcp}(s_v,s_w)|.ncaDepthAlg(v,w)=depth(nca(v,w))=∣lcp(sv​,sw​)∣.

Degenerate cases:

  • When v=wv=wv=w, the claim reduces to ∣sv∣=∣sv∣|s_v|=|s_v|∣sv​∣=∣sv​∣.
  • When d=0d=0d=0, both sides are 000.
  • The statement also covers pairs where one vertex is an ancestor of the other.
  • It implicitly asserts that the truncated subtraction and the log⁡20\log_2 0log2​0 convention in the third branch never make the value differ from the true depth.
Human review
  • Endorsed by Shuze Chen · Sep 27, 2026

    Confirmed by the moderator at approval.

  • Endorsed by mikedeng1 · Sep 27, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me