Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Lemma 4 — the nca of unrelated v,wv, wv,w has height ⌊lg⁡(sym(v)⊕sym(w))⌋\lfloor\lg(\mathrm{sym}(v)\oplus\mathrm{sym}(w))\rfloor⌊lg(sym(v)⊕sym(w))⌋

Proved
HarelTarjan.SymOrder.lemma4_nca_height_xor

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

binary-treenumber-theoryp2o-batch-p100bp2o-gran-per-chapterp2o-plan-paperp2o-v1trees

Let TTT be the complete binary tree of depth ddd, numbered in symmetric order. If vvv and www are two unrelated vertices (neither is an ancestor of the other), the height of the nearest common ancestor of vvv and www is

h(nca⁡(v,w))=⌊lg⁡(sym(v)⊕sym(w))⌋,h(\operatorname{nca}(v,w)) = \left\lfloor \lg\left(\mathrm{sym}(v) \oplus \mathrm{sym}(w)\right) \right\rfloor,h(nca(v,w))=⌊lg(sym(v)⊕sym(w))⌋,

where i⊕ji \oplus ji⊕j is the integer whose binary representation is the bitwise exclusive or of the binary representations of iii and jjj.

Lemma 4 is the third case of the nca depth algorithm. The hypothesis that vvv and www are unrelated cannot be dropped: when vvv is a proper ancestor of www, the formula can give a height smaller than h(v)h(v)h(v).

Formalization Note ⊕\oplus⊕ is ^^^ on N\mathbb NN and ⌊lg⁡x⌋\lfloor \lg x \rfloor⌊lgx⌋ is Nat.log 2 x; they agree because unrelated vertices have distinct numbers, so the exclusive or is at least 111.

Preamble
import Mathlib
import Definitions.Def_HarelTarjan_SymOrder_Tree
import Definitions.Def_HarelTarjan_SymOrder_Sym
Formal statement
namespace HarelTarjan.SymOrder

/-- Lemma 4 (p. 342): if `v` and `w` are two unrelated vertices, the height of the nearest common
ancestor of `v` and `w` is `⌊lg (sym(v) ⊕ sym(w))⌋`, where `⊕` is bitwise exclusive or
(`^^^` on `ℕ`) and `⌊lg x⌋ = Nat.log 2 x` for `x ≥ 1`. -/
theorem lemma4_nca_height_xor {d : ℕ} (v w : Vertex d) (hvw : Unrelated v w) :
    height (nca v w) = Nat.log 2 (sym v ^^^ sym w) := by sorry

end HarelTarjan.SymOrder
Source
Harel, Tarjan, Fast Algorithms for Finding Nearest Common Ancestors, SIAM J. Comput. 13 (1984), p. 342, Lemma 4
Read-back

What the Lean code literally says, in plain math · claude-opus-5-5

This theorem gives the height of a nearest common ancestor in terms of sym⁡\operatorname{sym}sym. Let d∈Nd\in\mathbb Nd∈N and let TdT_dTd​ be the set of boolean sequences of length ≤d\le d≤d. The height is h(v)=d−∣sv∣h(v)=d-|s_v|h(v)=d−∣sv​∣. Two vertices are unrelated when neither sequence is a prefix of the other; in particular they are distinct. 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 theorem assumes that v,w∈Tdv,w\in T_dv,w∈Td​ are unrelated. It then asserts

h(nca⁡(v,w))=⌊log⁡2(sym⁡(v)⊕sym⁡(w))⌋.h(\operatorname{nca}(v,w))=\lfloor\log_2(\operatorname{sym}(v)\oplus\operatorname{sym}(w))\rfloor.h(nca(v,w))=⌊log2​(sym(v)⊕sym(w))⌋.

Here ⊕\oplus⊕ is bitwise exclusive or on binary expansions, and ⌊log⁡20⌋\lfloor\log_2 0\rfloor⌊log2​0⌋ is taken to be 000.

Degenerate cases:

  • For d=0d=0d=0, no two vertices are unrelated, so the statement is vacuous.
  • Pairs where one vertex is an ancestor of the other, including v=wv=wv=w, are not covered.
  • If the exclusive or were 000, the right side would be 000 by convention. The statement itself does not rule this case out.
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