Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

The depth algorithm computes the number of the depth-d2d_2d2​ ancestor of vvv

Proved
HarelTarjan.SymOrder.depthAlg_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. Let vvv be a vertex of depth d1d_1d1​, let d2≤d1d_2 \le d_1d2​≤d1​, and let h=d−d2h = d - d_2h=d−d2​. Let uuu be the ancestor of vvv at depth d2d_2d2​ (the prefix of length d2d_2d2​ of the path to vvv). Then uuu is an ancestor of vvv, has depth d2d_2d2​, and it is the unique vertex with number 2h+1⌊sym(v)/2h+1⌋+2h2^{h+1} \lfloor \mathrm{sym}(v) / 2^{h+1} \rfloor + 2^h2h+1⌊sym(v)/2h+1⌋+2h: for every vertex xxx,

sym(x)=2h+1⌊sym(v)/2h+1⌋+2h  ⟺  x=u,\mathrm{sym}(x) = 2^{h+1} \left\lfloor \mathrm{sym}(v) / 2^{h+1} \right\rfloor + 2^h \iff x = u,sym(x)=2h+1⌊sym(v)/2h+1⌋+2h⟺x=u,

so the algorithm to solve the depth problem of §3 (p. 342), which returns sym−1\mathrm{sym}^{-1}sym−1 of this number, returns the ancestor of vvv whose depth is d2d_2d2​.

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

Formalization Note sym−1(N)=u\mathrm{sym}^{-1}(N) = usym−1(N)=u is stated as "a vertex has number NNN if and only if it is uuu", which asserts both that NNN is a number of the tree and that uuu is the vertex carrying it; no inverse function is defined. d−d2d - d_2d−d2​ is natural-number subtraction and does not truncate since d2≤d1≤dd_2 \le d_1 \le dd2​≤d1​≤d.

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 depth problem (§3, p. 342) is correct: given a vertex `v` and a
depth `d₂ ≤ depth(v)`, with `h = d − d₂`, the vertex `sym⁻¹(2^{h+1} ⌊sym(v) / 2^{h+1}⌋ + 2^h)`
is the ancestor of `v` whose depth is `d₂`: `ancestorAtDepth v d₂` is an ancestor of `v` of depth
`d₂`, and a vertex `u` has number `2^{h+1} ⌊sym(v) / 2^{h+1}⌋ + 2^h` if and only if it is that
ancestor. -/
theorem depthAlg_correct {d : ℕ} (v : Vertex d) (d₂ : ℕ) (hd₂ : d₂ ≤ depth v) :
    IsAncestor (ancestorAtDepth v d₂) v ∧ depth (ancestorAtDepth v d₂) = d₂ ∧
      ∀ u : Vertex d, sym u = depthAlgNum v d₂ ↔ u = ancestorAtDepth v d₂ := 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 depth problem (unnumbered)
Read-back

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

Undefined terms. This statement uses several terms that are defined in imported files, which I was not given: Vertexd\mathrm{Vertex}_dVertexd​, depth\mathrm{depth}depth, IsAncestor\mathrm{IsAncestor}IsAncestor, ancestorAtDepth\mathrm{ancestorAtDepth}ancestorAtDepth, sym\mathrm{sym}sym and depthAlgNum\mathrm{depthAlgNum}depthAlgNum. I cannot expand them, so this read-back treats them as opaque and uses only their names. The source has a documentation comment. It says the vertices form a tree of depth ddd and that sym\mathrm{sym}sym numbers them. It also gives a formula for depthAlgNum\mathrm{depthAlgNum}depthAlgNum:

2h+1⌊sym(v)2h+1⌋+2h,h=d−d2.2^{h+1}\left\lfloor \frac{\mathrm{sym}(v)}{2^{h+1}} \right\rfloor + 2^h, \qquad h = d - d_2.2h+1⌊2h+1sym(v)​⌋+2h,h=d−d2​.

That comment describes what the author intended. It is not part of the formal statement, and I could not check it against the hidden definitions.

Parameters.

  • ddd is a natural number, left implicit.
  • vvv is an element of the type Vertexd\mathrm{Vertex}_dVertexd​.
  • d2d_2d2​ is a natural number.
  • The hypothesis requires
d2≤depth(v).d_2 \le \mathrm{depth}(v).d2​≤depth(v).

Claim. Write a=ancestorAtDepth(v,d2)a = \mathrm{ancestorAtDepth}(v, d_2)a=ancestorAtDepth(v,d2​) for the vertex returned by that function. The theorem asserts all three of the following:

  1. aaa is an ancestor of vvv, in the sense of IsAncestor(a,v)\mathrm{IsAncestor}(a, v)IsAncestor(a,v). The statement does not say whether this relation is reflexive or strict.
  2. depth(a)=d2\mathrm{depth}(a) = d_2depth(a)=d2​.
  3. For every vertex u∈Vertexdu \in \mathrm{Vertex}_du∈Vertexd​:
sym(u)=depthAlgNum(v,d2)  ⟺  u=a.\mathrm{sym}(u) = \mathrm{depthAlgNum}(v, d_2) \iff u = a.sym(u)=depthAlgNum(v,d2​)⟺u=a.

Part 3 says two things. First, sym(a)\mathrm{sym}(a)sym(a) equals depthAlgNum(v,d2)\mathrm{depthAlgNum}(v, d_2)depthAlgNum(v,d2​). Second, no other vertex of Vertexd\mathrm{Vertex}_dVertexd​ has that number, so exactly one vertex has it. The statement does not say sym\mathrm{sym}sym is injective on all vertices; it only says that this one value has a single preimage. The theorem does not say that aaa is the only ancestor of vvv at depth d2d_2d2​.

Degenerate cases.

  • d2=depth(v)d_2 = \mathrm{depth}(v)d2​=depth(v). The hypothesis holds with equality. The theorem then says aaa is an ancestor of vvv at vvv's own depth. That only fits vvv if IsAncestor\mathrm{IsAncestor}IsAncestor lets a vertex be its own ancestor. If the relation is strict and no vertex is a strict ancestor at the same depth, then the theorem as stated is false in this case. It is not vacuous here, because the hypothesis d2≤depth(v)d_2 \le \mathrm{depth}(v)d2​≤depth(v) does allow it.
  • d2=0d_2 = 0d2​=0. The hypothesis always holds. The claim is that an ancestor of depth 000 exists and is the unique vertex numbered depthAlgNum(v,0)\mathrm{depthAlgNum}(v, 0)depthAlgNum(v,0).
  • d=0d = 0d=0, or Vertexd\mathrm{Vertex}_dVertexd​ has only one element. The only possible vvv presumably has depth 000, so only d2=0d_2 = 0d2​=0 is allowed. The claim then reduces to that vertex being its own ancestor, having depth 000, and being numbered depthAlgNum(v,0)\mathrm{depthAlgNum}(v, 0)depthAlgNum(v,0).
  • Vertexd\mathrm{Vertex}_dVertexd​ is empty. The theorem holds vacuously.
  • No link between depth(v)\mathrm{depth}(v)depth(v) and ddd. The statement itself does not require depth(v)≤d\mathrm{depth}(v) \le ddepth(v)≤d. If the documented h=d−d2h = d - d_2h=d−d2​ is used inside depthAlgNum\mathrm{depthAlgNum}depthAlgNum, it is subtraction on natural numbers. It would be cut off at 000 whenever d2>dd_2 > dd2​>d, which the statement does not rule out.
  • Division in the documented formula. 2h+12^{h+1}2h+1 is never zero, so the division by it never hits a divide-by-zero default. The floor is presumably integer division rounding down.

Whether any of these edge cases actually happens depends on the hidden definitions of Vertexd\mathrm{Vertex}_dVertexd​ and depth\mathrm{depth}depth, which I could not see.

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