AFTD Auto-Formalizing Theoretical Domains

simple_graph_isAcyclic_of_unique_lower_neighbor

theoremverified

A simple graph is acyclic if some height function h : V → ℕ separates adjacent vertices and every vertex has at most one neighbour of smaller height.

Statement

theorem simple_graph_isAcyclic_of_unique_lower_neighbor {V : Type*} (G : SimpleGraph V)
    (h : V → ℕ) (hne : ∀ u v, G.Adj u v → h u ≠ h v)
    (huniq : ∀ v u w, G.Adj v u → G.Adj v w → h u < h v → h w < h v → u = w) :
    G.IsAcyclic
Source
Helper for the all-k source-graph acyclicity in op309_girth9_acyclic_source_unbounded_switch_cost (OP-309): a forest certificate by a height function.
Verified
28 Nov 2026
Axioms
Classical.choiceQuot.soundpropext
Used by 1

The proof, explained

The proof proceeds by contradiction: suppose the graph contains a cycle $w$. Since a cycle has length at least 3, we can pick a vertex $v = w.getVert\ m$ along the cycle that maximizes the height function $h$. Because adjacent vertices have distinct heights, both the predecessor and the successor of $v$ on the cycle have strictly smaller height than $v$. By the uniqueness assumption, these two lower neighbors must coincide, which contradicts the injectivity of vertices along a cycle of length at least 3.

  1. Assume for contradiction that the graph contains a cycle, which must have length at least 3.

    By definition of acyclicity, the graph has no cycles. We assume for contradiction that there exists a cycle $w$ based at some vertex $a$ (`hw : w.IsCycle`). A cycle in a simple graph contains at least 3 edges, so $w.length \ge 3$ by `hw.three_le_length`.

    lines 5–6

    uses hw.three_le_length

      intro a w hw
      have hl := hw.three_le_length
  2. Vertices at distinct indices strictly less than the cycle length are distinct.

    If two indices $i, j < w.length$ have $w.getVert\ i = w.getVert\ j$, they must be equal. This follows from the injectivity of the vertex map on cycles (`hw.getVert_injOn'`), after rewriting set membership with `Set.mem_ofPred_eq` and verifying the index bounds via linear arithmetic.

    lines 7–9

    uses hw.getVert_injOn', Set.mem_ofPred_eq

      have hinj : ∀ i j, i < w.length → j < w.length → w.getVert i = w.getVert j → i = j := by
        intro i j hi hj he
        apply hw.getVert_injOn' _ _ he <;> simp only [Set.mem_ofPred_eq] <;> omega
  3. There is an index $m < w.length$ where the height function attains its maximum on the cycle.

    Since $w.length \ge 3 > 0$, the set of indices $\{0, \dots, w.length - 1\}$ is nonempty. By `Finset.exists_max_image`, there exists an index $m < w.length$ that maximizes $h(w.getVert\ m)$, meaning $h(w.getVert\ i) \le h(w.getVert\ m)$ for all $i < w.length$ (using `Finset.mem_range`).

    lines 10–14

    uses Finset.exists_max_image, Finset.mem_range

      obtain ⟨m, hm, hmax⟩ := Finset.exists_max_image (Finset.range w.length)
        (fun i => h (w.getVert i)) ⟨0, Finset.mem_range.2 (by omega)⟩
      rw [Finset.mem_range] at hm
      have hmax' : ∀ i, i < w.length → h (w.getVert i) ≤ h (w.getVert m) :=
        fun i hi => hmax i (Finset.mem_range.2 hi)
  4. The cycle's endpoint coincides with its starting vertex.

    Because $w$ is a cycle from $a$ to $a$, evaluating the vertex map at the end index $w.length$ (via `w.getVert_length`) and at $0$ (via `w.getVert_zero`) both yield $a$.

    line 15

    uses w.getVert_length, w.getVert_zero

      have hlast : w.getVert w.length = w.getVert 0 := by rw [w.getVert_length, w.getVert_zero]
  5. There is a successor neighbor $n' < w.length$ of vertex $m$ along the cycle, equal to $m+1$ (or $0$ if $m+1 = w.length$).

    We split into cases on whether $m + 1 = w.length$. If so, the next index wraps around to $0$, which is adjacent to $m$ by `w.adj_getVert_succ` together with the wrap-around identity. Otherwise, $m + 1 < w.length$, and vertex $m + 1$ is adjacent to $m$ directly by `w.adj_getVert_succ`.

    lines 16–23

    uses w.adj_getVert_succ

      -- the next vertex, with a normalised index
      obtain ⟨n', hn', hnadj, hneq⟩ : ∃ n', n' < w.length ∧ G.Adj (w.getVert m) (w.getVert n') ∧
          n' = (if m + 1 = w.length then 0 else m + 1) := by
        by_cases hc : m + 1 = w.length
        · refine ⟨0, by omega, ?_, by simp [hc]⟩
          have := w.adj_getVert_succ (i := m) hm
          rwa [hc, hlast] at this
        · exact ⟨m + 1, by omega, w.adj_getVert_succ hm, by simp [hc]⟩
  6. There is a predecessor neighbor $p' < w.length$ of vertex $m$ along the cycle, equal to $m-1$ (or $w.length - 1$ if $m = 0$).

    We split into cases on whether $m = 0$. If $m = 0$, the preceding index wraps around to $w.length - 1$; applying `w.adj_getVert_succ` at $w.length - 1$ and using symmetry of adjacency shows $0$ is adjacent to $w.length - 1$. If $m > 0$, applying `w.adj_getVert_succ` at $m - 1$ and symmetry shows $m$ is adjacent to $m - 1$.

    lines 24–34

    uses w.adj_getVert_succ

      obtain ⟨p', hp', hpadj, hpeq⟩ : ∃ p', p' < w.length ∧ G.Adj (w.getVert m) (w.getVert p') ∧
          p' = (if m = 0 then w.length - 1 else m - 1) := by
        by_cases hc : m = 0
        · refine ⟨w.length - 1, by omega, ?_, by simp [hc]⟩
          have := w.adj_getVert_succ (i := w.length - 1) (by omega)
          rw [show w.length - 1 + 1 = w.length by omega, hlast, ← hc] at this
          exact this.symm
        · refine ⟨m - 1, by omega, ?_, by simp [hc]⟩
          have := w.adj_getVert_succ (i := m - 1) (by omega)
          rw [show m - 1 + 1 = m by omega] at this
          exact this.symm
  7. Both neighbors $w.getVert\ n'$ and $w.getVert\ p'$ have strictly smaller height than $w.getVert\ m$.

    By the maximality of $m$, the heights of both neighbors are at most $h(w.getVert\ m)$. Since adjacent vertices cannot have equal heights by hypothesis `hne`, strict inequalities follow via `lt_of_le_of_ne`.

    lines 35–38

    uses lt_of_le_of_ne

      have hlt1 : h (w.getVert n') < h (w.getVert m) :=
        lt_of_le_of_ne (hmax' n' hn') (hne _ _ hnadj).symm
      have hlt2 : h (w.getVert p') < h (w.getVert m) :=
        lt_of_le_of_ne (hmax' p' hp') (hne _ _ hpadj).symm
  8. The two neighbors must be the same vertex, which leads to an index contradiction. automation

    By the uniqueness hypothesis `huniq`, vertex $w.getVert\ m$ cannot have two distinct neighbors of strictly smaller height, so $w.getVert\ n' = w.getVert\ p'$. By injectivity along the cycle (`hinj`), this implies $n' = p'$. Substituting the definitions of $n'$ and $p'$ and splitting the if-then-else conditions (`split_ifs`) leads to a contradiction in every branch via Presburger arithmetic (`omega`), since $w.length \ge 3$.

    lines 39–41

      have heq := hinj _ _ hn' hp' (huniq _ _ _ hnadj hpadj hlt1 hlt2)
      rw [hneq, hpeq] at heq
      split_ifs at heq <;> omega
hw.three_le_length
A cycle in a simple graph has length at least 3.
hw.getVert_injOn'
The vertex sequence of a cycle is injective on indices up to length - 1.
Set.mem_ofPred_eq
An element belongs to a set defined by a predicate if and only if the predicate holds for that element.
Finset.exists_max_image
Any function on a nonempty finite set attains its maximum value at some element.
Finset.mem_range
A natural number belongs to `Finset.range n` if and only if it is strictly less than n.
w.getVert_length
The vertex at index `w.length` along a walk `w` is its destination vertex.
w.getVert_zero
The vertex at index 0 along a walk `w` is its starting vertex.
w.adj_getVert_succ
Consecutive vertices at indices i and i + 1 along a walk are adjacent in the graph.
lt_of_le_of_ne
In a partial order, a ≤ b and a ≠ b together imply a < b.

Written by a model (gemini-3.8-flash) from the Lean proof and the goal states Lean reported at each step. Lean verified the proof; this explanation is a reading aid and is not itself machine-checked.

Lean source view module on GitHub

theorem simple_graph_isAcyclic_of_unique_lower_neighbor {V : Type*} (G : SimpleGraph V)    (h : V → ℕ) (hne : ∀ u v, G.Adj u v → h u ≠ h v)    (huniq : ∀ v u w, G.Adj v u → G.Adj v w → h u < h v → h w < h v → u = w) :    G.IsAcyclic := by  intro a w hw  have hl := hw.three_le_length  have hinj : ∀ i j, i < w.length → j < w.length → w.getVert i = w.getVert j → i = j := by    intro i j hi hj he    apply hw.getVert_injOn' _ _ he <;> simp only [Set.mem_ofPred_eq] <;> omega  obtain ⟨m, hm, hmax⟩ := Finset.exists_max_image (Finset.range w.length)    (fun i => h (w.getVert i)) ⟨0, Finset.mem_range.2 (by omega)⟩  rw [Finset.mem_range] at hm  have hmax' : ∀ i, i < w.length → h (w.getVert i) ≤ h (w.getVert m) :=    fun i hi => hmax i (Finset.mem_range.2 hi)  have hlast : w.getVert w.length = w.getVert 0 := by rw [w.getVert_length, w.getVert_zero]  -- the next vertex, with a normalised index  obtain ⟨n', hn', hnadj, hneq⟩ : ∃ n', n' < w.length ∧ G.Adj (w.getVert m) (w.getVert n') ∧      n' = (if m + 1 = w.length then 0 else m + 1) := by    by_cases hc : m + 1 = w.length    · refine ⟨0, by omega, ?_, by simp [hc]⟩      have := w.adj_getVert_succ (i := m) hm      rwa [hc, hlast] at this    · exact ⟨m + 1, by omega, w.adj_getVert_succ hm, by simp [hc]⟩  obtain ⟨p', hp', hpadj, hpeq⟩ : ∃ p', p' < w.length ∧ G.Adj (w.getVert m) (w.getVert p') ∧      p' = (if m = 0 then w.length - 1 else m - 1) := by    by_cases hc : m = 0    · refine ⟨w.length - 1, by omega, ?_, by simp [hc]⟩      have := w.adj_getVert_succ (i := w.length - 1) (by omega)      rw [show w.length - 1 + 1 = w.length by omega, hlast, ← hc] at this      exact this.symm    · refine ⟨m - 1, by omega, ?_, by simp [hc]⟩      have := w.adj_getVert_succ (i := m - 1) (by omega)      rw [show m - 1 + 1 = m by omega] at this      exact this.symm  have hlt1 : h (w.getVert n') < h (w.getVert m) :=    lt_of_le_of_ne (hmax' n' hn') (hne _ _ hnadj).symm  have hlt2 : h (w.getVert p') < h (w.getVert m) :=    lt_of_le_of_ne (hmax' p' hp') (hne _ _ hpadj).symm  have heq := hinj _ _ hn' hp' (huniq _ _ _ hnadj hpadj hlt1 hlt2)  rw [hneq, hpeq] at heq  split_ifs at heq <;> omega
See it in the graphDiscuss this resultChallenge it