← 全部题目

WOWII #199:一个部分结果

部分结果:部分形式化 图论,Written on the Wall II

完整猜想没有解决。下面只证明了几个特殊情形:图里没有长度为 3 和 5 的圈;或者没有三角形且连通度不超过 3;或者没有三角形且没有带悬挂邻点的 5 圈。第一个情形(κ ≥ 3 时)在我们之前已有人公开宣布过(lefttosolve 上 @johnnyferguson-cpu 的笔记,2026-11-27),κ ≤ 2 的情形也已有别人的公开 Lean 证明。我们没找到前人工作的部分只有:无三角形、κ = 3 且含 5 圈的情形,以及第三个情形。没有人工审稿。

原猜想(WOWII #199,未解决): G 是至少有 2 个点的有限简单连通图,κ 是它的点连通度,t 是最大导出树的点数。如果 t ≤ κ+2,那么 G 有哈密顿路。

部分定理: 如果 G 还没有长度为 3 或 5 的圈,那么结论成立。

证明。 记 k=κ(G),N(x) 是 x 的邻点集,d(x)=|N(x)|,Δ 表示对称差。

度数。 每个点的度至少是 k:如果 x 不和其他所有点相邻,删掉 N(x) 就能把 x 分开,所以 d(x) ≥ k;如果 x 和其他所有点都相邻,d(x)=n−1 ≥ k。又因为没有三角形,x 和它的邻点导出一颗星,所以 d(x)+1 ≤ t ≤ k+2,即 d(x) ≤ k+1。

如果 k=1,最大度不超过 2,G 是路或圈,结论成立(包括 K₂)。以下设 k ≥ 2。

引理 1。 如果不同的点 x、z 有公共邻点,则 2|N(x)∩N(z)| ≥ d(x)+1。交换 x、z,同样有 2|N(x)∩N(z)| ≥ d(z)+1。

证明。 取公共邻点 y,考虑 {x,y,z} ∪ (N(x)ΔN(z))。没有三角形,所以 x、z 不相邻。每个独有邻点在 x、z 中只和一个相邻;它也不和 y 相邻,否则会有三角形。同一侧的两个独有邻点不相邻,否则也会有三角形。若 u∈N(x)∖N(z) 和 v∈N(z)∖N(x) 相邻,则 x,u,v,z,y 构成 5 圈,矛盾。所以这些点导出一棵树,3+|N(x)ΔN(z)| ≤ k+2,即 d(x)+d(z)−2|N(x)∩N(z)| ≤ k−1。再用 d(z) ≥ k,就得到第一个结论;第二个同理。∎

引理 2。 G 是二部图,每一部分最多有 2k 个点。如果某一部分正好有 2k 个点,那里每个点的度都是 k+1。

证明。 先说明“N(x)∩N(z) 非空”这个关系有传递性。如果 x、w 都和 z 有这个关系,那么 N(x)∩N(z) 和 N(w)∩N(z) 都包含 N(z) 的一半以上(由引理 1 的第二个不等式;某个点就是 z 时显然成立),所以两者相交。因此,长度为偶数的途径,两端的邻点集一定相交。而相邻的两点没有公共邻点,否则会有三角形。

取一个根,按到根的距离是奇数还是偶数把点分成两部分。如果某条边的两端在同一部分,那么从一端经过根到另一端,有一条长度为偶数的途径,于是这两端有公共邻点,矛盾。所以 G 是二部图。

设某一部分有 s 个点,x 是其中一点,d=d(x)。这一部分的其他点都能由长度为偶数的途径连到 x,所以都和 x 有公共邻点。用引理 1,并数出从 x 到这些点、长度为 2 的路,得到

(s−1)(d+1)/2 ≤ Σ_{y∈N(x)}(d(y)−1) ≤ dk。

所以 s−1 < 2k,即 s ≤ 2k。如果 s=2k 且 d=k,就有 (2k−1)(k+1) ≤ 2k²,这在 k ≥ 2 时不成立。再用 k ≤ d ≤ k+1,得到 d=k+1。∎

两部分的大小。 设两部分的大小是 a ≤ b。数边得到 kb ≤ (k+1)a。如果 b ≥ a+2,就有 a ≥ 2k,所以 b ≥ 2k+2,和 b ≤ 2k 矛盾。因此 b−a ≤ 1。又因为每个点在另一部分至少有 k ≥ 2 个邻点,所以 a、b ≥ k ≥ 2。

引理 3(度和条件)。 二部图两部分都有 m ≥ 2 个点。如果每一对分属两部分、又不相邻的点,度数之和都大于 m,那么图有哈密顿圈。

证明。 逐条添加两部分之间缺少的边,直到得到完全二部图,它有哈密顿圈。加边不会减少度数,也不会增加不相邻的点对,所以每一步都满足条件。再按相反的顺序逐条删边。

如果当前的圈没有用到要删的边,就保留它。否则,删掉这条边后得到一条经过所有点的路 x₁,y₁,…,x_m,y_m,其中 x₁ 和 y_m 是这条边的两端,现在不相邻。令 I={i : x₁~y_i},J={i : x_i~y_m}。因为 x₁ 和 y_m 不相邻,所以 m∉I、1∉J。删边后 |I|+|J| > m,所以 I∩J 非空,而且其中的 i 满足 2 ≤ i ≤ m−1。把路上的边 x_i y_i 换成 x₁y_i 和 x_i y_m,得到新的哈密顿圈:x₁,…,x_i,y_m,x_m,…,x_{i+1},y_i,x₁。∎

完成证明。
- a=b<2k:不相邻的跨部分点对,度数之和至少是 2k,大于 a。由引理 3,有哈密顿圈,因此有哈密顿路。
- a=b=2k:由引理 2,度数之和至少是 2k+2,大于 2k。同样由引理 3 得到结论。
- a=b−1:在较小的部分加一个新点,让它和较大部分的所有点相邻。此时两部分都有 b 个点。剩下不相邻的跨部分点对里,较大部分的那个点多了一个邻点,所以度数之和至少是 2k+1,大于 b(因为 b ≤ 2k)。由引理 3 有哈密顿圈,删掉新点就得到 G 的哈密顿路。∎

论文 PDF(英文,最终版:独立 Claude agent 和 GPT-6 Pro 都审过) · Zenodo 存档(论文、TeX 源文件、Lean 文件)

第二批部分结果

第二批部分结果(2026-11-30): 下面两种情形猜想也成立。两者都只要求图里没有三角形,允许有 5 圈。

证明思路。 没有 5 圈的情形由第一个部分定理处理。有 5 圈 C 时,圈外的点在 C 上最多有两个邻点,且不相邻。删掉圈上一个点,再加一个圈外点,就得到 5 个点的导出树。κ = 2 时,这直接说明 G 就是 5 圈。κ = 3 时,再加任何一个只和它连一条边的点都会得到 6 个点的导出树,用这一点逐步排除各种情形,最后只剩一种 8 个点的图,可以直接写出哈密顿路。情形 2 把圈上每个点和“可以替换它的点”归成一类,证明相邻两类之间全部相连、五类一样大,再轮流走五类。

来源和审查: Astra(gpt-6.2-astra)提出,独立的 Claude agent 逐步审查:两部分都判为正确,有 4 处措辞上的小问题,下面的原文没有改。同一份回答里还有一个关于 5 圈个数的计数引理,数学上正确,但它不证明任何新的情形,所以不算部分结果。

Astra 的英文原文(含证明)
RESULT: PARTIAL

**Theorem.** In any triangle-free counterexample, all three of the following must hold:

1. \(\kappa\ge 4\).
2. There is an induced 5-cycle with a pendant neighbour (a vertex with exactly one neighbour on the cycle).
3. Every 5-cycle contains an edge belonging to at least \(\left\lceil(3\kappa-2)/4\right\rceil\) distinct 5-cycles.

In particular, this proves the conjecture for triangle-free graphs with \(\kappa\le 3\), with no restriction on 5-cycles. The other two conditions give partial results for unbounded connectivity.

Write \(k=\kappa\), assume \(t\le k+2\), and assume \(G\) is triangle-free. By the supplied partial theorem we need only consider graphs containing a 5-cycle \(C=v_0v_1v_2v_3v_4v_0\). Indices are modulo 5. The cycle is induced, and every outside vertex has at most two neighbours on it, necessarily nonconsecutive.

**1. Small connectivity.** Call an outside vertex with one or two neighbours on \(C\) a *single* or a *double*, respectively. For either type of vertex \(y\), one can choose \(j\) so that
\[
T(y,j)=(C-v_j)\cup\{y\}
\]
induces a tree: for a single, delete any vertex other than its neighbour; for a double, delete either neighbour. The four cycle vertices induce exactly a path, and \(y\) has exactly one edge to that path.

For \(k=1\), already \(C-v_0\) is too large. For \(k=2\), the five-vertex tree \(T(y,j)\) is too large, so connectedness forces \(G=C\).

Now let \(k=3\). Every \(T(y,j)\) is **saturated**: no outside vertex can have exactly one neighbour in it, since adding that vertex would give an induced six-vertex tree with just one new edge. Consequently a vertex with no neighbour on \(C\) cannot be adjacent to a single or double \(y\): it would see only \(y\) in \(T(y,j)\). By connectedness, every vertex outside \(C\) is therefore a single or double.

Two doubles cannot share a cycle neighbour \(v_j\). Indeed, they would be nonadjacent by triangle-freeness, so after forming \(T(y,j)\) with one double, the other would see only its surviving cycle neighbour. Thus there are at most two doubles.

If \(x\) is a single, every other outside vertex \(y\) must be adjacent to \(x\). To see this, choose \(j\) different from the neighbour of \(x\) so that \(y\) retains exactly one neighbour on \(C-v_j\). This is possible for a single by avoiding both singles’ neighbours, and for a double by deleting one of its two neighbours. Saturation of \(T(x,j)\) forces \(xy\). Moreover \(y\) cannot itself be a single: its neighbour would differ from that of \(x\) by triangle-freeness, and deleting that neighbour would leave \(y\) seeing only \(x\) in \(T(x,j)\).

Since \(\delta(G)\ge3\), all five cycle vertices need an outside neighbour. At most two doubles and at most one single can cover them, so there are exactly two doubles \(a,b\) and one single \(x\). A Hamiltonian path starts \(a,x,b\), enters \(C\) at a neighbour of \(b\), and traverses its five vertices in cyclic order. This proves condition 1.

**2. The case with no pendant neighbour on any 5-cycle.** Under this assumption every vertex touching \(C\) from outside is a double. Put in \(V_i\) the vertex \(v_i\) and all doubles with neighbours \(v_{i-1},v_{i+1}\). Replacing \(v_i\) by any such double gives another induced 5-cycle: its two cycle edges are exactly the two required ones. Thus a vertex missing \(C\) cannot be adjacent to a double, since it would be pendant on the replacement cycle. Connectedness now gives \(V(G)=\bigcup_i V_i\).

Vertices within one class or in classes two apart share a cycle neighbour, so distinct such vertices are nonadjacent. Consecutive classes are completely joined: if \(u\in V_i\) and \(w\in V_{i+1}\) were nonadjacent, both would have to be doubles by definition of the classes. Replacing \(v_i\) by \(u\) would leave \(w\) seeing only \(v_{i+2}\) on the replacement cycle.

Write \(a_i=|V_i|\). To the induced path \(C-v_j\) add all vertices of \(V_{j-1}\setminus\{v_{j-1}\}\) and \(V_{j+1}\setminus\{v_{j+1}\}\). The added vertices are pairwise nonadjacent, and each sees only \(v_{j-2}\) or only \(v_{j+2}\) on the path, respectively. Hence this is an induced tree of order \(2+a_{j-1}+a_{j+1}\). It follows that
\[
k\le d(v_j)=a_{j-1}+a_{j+1}\le k.
\]
Comparing the equalities for \(j\) and \(j+2\) gives \(a_{j-1}=a_{j+3}\), so all five classes have equal size. Visiting classes \(0,1,2,3,4\) repeatedly, taking a fresh vertex each time, gives a Hamiltonian path. This proves condition 2.

**3. A local 5-cycle multiplicity bound.** Fix any \(C\) as above. Let \(1+h_i\) be the number of common neighbours of \(x=v_{i-1}\) and \(z=v_{i+1}\), counting \(v_i\). Let \(f_i\) count the 5-cycles containing the path \(xv_i z\). Put \(A=N(x)\setminus N(z)\) and \(B=N(z)\setminus N(x)\). The edges between \(A\) and \(B\) are in bijection with these \(f_i\) cycles: an edge \(ab\) gives \(x a b z v_i x\), and triangle-freeness forces the two remaining vertices of any such cycle to be private neighbours in this way.

Delete one endpoint of each cross edge, using at most \(f_i\) deletions. The surviving private neighbours together with \(x,v_i,z\) induce a tree: \(xz\) is absent, each private set is independent, neither set sees \(v_i\) by triangle-freeness, and no cross edge remains. Each surviving vertex thus attaches only to its designated endpoint. Consequently
\[
k+2\ \ge\ 3+d(x)+d(z)-2(1+h_i)-f_i,
\qquad f_i\ge k-1-2h_i.
\]

Let \(L\) be the largest number of 5-cycles through any one edge of \(C\), and put \(H=\frac15\sum_i h_i\). For edge \(v_i v_{i+1}\), replacing any one of its three non-end vertices on \(C\) by an alternative common neighbour of that vertex’s two cycle neighbours gives a distinct 5-cycle through the edge. The alternative vertex is outside \(C\) since \(C\) is chordless. Counting also \(C\) and averaging over edges gives \(L\ge 1+3H\).

On the other hand, the cycles counted by \(f_i\) and \(f_{i+1}\) all contain \(v_i v_{i+1}\). Their intersection consists of cycles completing the four-vertex path \(v_{i-1}v_iv_{i+1}v_{i+2}\), so has size \(1+h_{i+3}\): the fifth vertex is precisely a common neighbour of its endpoints. Averaging the union bound \(L\ge f_i+f_{i+1}-(1+h_{i+3})\) gives \(L\ge 2k-3-5H\). Taking weights \(5/8\) and \(3/8\) on the two lower bounds eliminates \(H\), giving \(L\ge(3k-2)/4\). This proves condition 3.

**Where the argument stops.** For \(k\ge4\), \(T(y,j)\) is no longer saturated: adding a vertex that sees it exactly once gives only six vertices, which is allowed. Thus a pendant neighbour on a 5-cycle can neither be excluded nor forced adjacent to all other vertices touching the cycle, and the five-class replacement argument does not apply. The multiplicity bound measures the resulting obstruction but gives no contradiction when 5-cycles overlap heavily; with triangles, even the independence of each private-neighbour set used in that bound fails.

原题来源:formal-conjectures。

状态

证明第一个情形(无 3 圈和 5 圈)的完整 Lean 文件通过全部检查,目标陈述已独立核对;另外两个情形只有书面证明
审查两次审查都由 Claude 做:主会话逐步检查,另一个独立的 Claude agent 从头审查,没有发现实质错误。之后论文又交 GPT-6 Pro 审查(见下)。没有人工审稿。
Lean 陈述核对14 条引理陈述由独立 Claude agent 只读核对,判为真;目标陈述判为忠实于上面的部分定理。没有人工核对。
第二批只有书面证明。由独立 Claude agent 审查一次,没有 Lean,没有人工审稿。
优先权检索过(网页、arXiv、formal-conjectures、lefttosolve)。第一个情形的 κ ≥ 3 部分已由 @johnnyferguson-cpu 在 2026-11-27 公开宣布,用的是同样的主要步骤;我们的证明是 2026-11-29 由 AI 给出的,不能排除它参考了那篇笔记,应归功于先发表的人。κ = 1、2 已被别人的公开 Lean 工作覆盖。其余部分没找到,但没找到不等于没人做过。
GPT-6 Pro 审查三个证明都判为正确,还穷举检查了 2–6 个点的 3,801 个连通无三角形图,没有反例;指出了上面的优先权问题和几处措辞,改完后复核通过(MINOR,改完措辞即接受,已改)。
用量DeepSeek 107 次调用,2,141,853 token(含没有结算的预留)

Lean 目标陈述

∀ (V : Type) [Fintype V] [DecidableEq V] (G : SimpleGraph V) (k : ℕ), 1 ≤ k → k < Fintype.card V →
  (∀ S : Finset V, S.card < k → (G.induce ((S : Set V)ᶜ)).Connected) →
  (∀ s : Finset V, (G.induce (s : Set V)).IsTree → s.card ≤ k + 2) →
  (∀ a b c : V, G.Adj a b → G.Adj b c → ¬ G.Adj a c) →
  (∀ a b c d e : V, G.Adj a b → G.Adj b c → G.Adj c d → G.Adj d e → ¬ G.Adj e a) →
  ∃ (a b : V) (p : G.Walk a b), p.IsHamiltonian
完整 Lean 文件(2224 行)
set_option maxHeartbeats 400000
namespace P_leaf
theorem leaf : ∀ (V : Type) [DecidableEq V] (G : SimpleGraph V) (s : Finset V) (v w : V), (G.induce (s : Set V)).IsTree → v ∉ s → w ∈ s → G.Adj v w →
  (∀ u ∈ s, G.Adj v u → u = w) → (G.induce ((insert v s : Finset V) : Set V)).IsTree := by
  intro V _ G s v w ht hvs hws hvw huniq
  have hsW : ((s : Finset V) : Set V) ⊆ ((insert v s : Finset V) : Set V) := by
    intro a ha
    simp only [Finset.coe_insert, Set.mem_insert_iff]
    exact Or.inr ha
  have hvW : v ∈ ((insert v s : Finset V) : Set V) := by simp
  have hwW : w ∈ ((insert v s : Finset V) : Set V) := hsW hws
  have mem : ∀ x : ((insert v s : Finset V) : Set V), x.1 = v ∨ x.1 ∈ s := by
    intro x
    have hx := x.2
    simp only [Finset.coe_insert, Set.mem_insert_iff, Finset.mem_coe] at hx
    exact hx
  have nbr : ∀ x : ((insert v s : Finset V) : Set V), G.Adj v x.1 → x.1 = w := by
    intro x hx
    rcases mem x with h | h
    · rw [h] at hx
      exact (G.irrefl hx).elim
    · exact huniq _ h hx
  have hreach : ∀ a : ((insert v s : Finset V) : Set V),
      (G.induce ((insert v s : Finset V) : Set V)).Reachable a ⟨w, hwW⟩ := by
    intro a
    rcases mem a with h | h
    · apply SimpleGraph.Adj.reachable
      simp only [SimpleGraph.induce_adj]
      rw [h]
      exact hvw
    · have r : (G.induce (s : Set V)).Reachable ⟨a.1, h⟩ ⟨w, hws⟩ := ht.connected.preconnected _ _
      exact r.map (G.induceHomOfLE hsW).toHom
  refine ⟨?_, ?_⟩
  · haveI : Nonempty ((insert v s : Finset V) : Set V) := ⟨⟨v, hvW⟩⟩
    exact SimpleGraph.Connected.mk (fun a b => (hreach a).trans (hreach b).symm)
  · intro u c hc
    by_cases hv : (⟨v, hvW⟩ : ((insert v s : Finset V) : Set V)) ∈ c.support
    · have hc' := hc.rotate hv
      have hnil : ¬ (c.rotate _ hv).Nil := hc'.not_nil
      have h1 := (c.rotate _ hv).adj_snd hnil
      have h2 := (c.rotate _ hv).adj_penultimate hnil
      have e1 := nbr _ (SimpleGraph.induce_adj.1 h1)
      have e2 := nbr _ (SimpleGraph.induce_adj.1 h2.symm)
      exact hc'.snd_ne_penultimate (Subtype.ext (e1.trans e2.symm))
    · have hinj : Function.Injective (SimpleGraph.Embedding.induce ((insert v s : Finset V) : Set V) (G := G)).toHom :=
        (SimpleGraph.Embedding.induce ((insert v s : Finset V) : Set V) (G := G)).injective
      have hcm := (SimpleGraph.Walk.isCycle_map_iff_of_injective hinj).2 hc
      have hsupp : ∀ x ∈ (c.map (SimpleGraph.Embedding.induce ((insert v s : Finset V) : Set V) (G := G)).toHom).support,
          x ∈ ((s : Finset V) : Set V) := by
        intro x hx
        rw [SimpleGraph.Walk.support_map, List.mem_map] at hx
        obtain ⟨y, hy, rfl⟩ := hx
        rcases mem y with h | h
        · exact absurd (show y = ⟨v, hvW⟩ from Subtype.ext (by exact h)) (fun e => hv (e ▸ hy))
        · exact h
      exact ht.isAcyclic _ ((SimpleGraph.Walk.isCycle_induce (hc := hsupp)).2 hcm)

end P_leaf
theorem leaf : ∀ (V : Type) [DecidableEq V] (G : SimpleGraph V) (s : Finset V) (v w : V), (G.induce (s : Set V)).IsTree → v ∉ s → w ∈ s → G.Adj v w →
  (∀ u ∈ s, G.Adj v u → u = w) → (G.induce ((insert v s : Finset V) : Set V)).IsTree := P_leaf.leaf
namespace P_leaves
theorem leaves : ∀ (V : Type) [DecidableEq V] (G : SimpleGraph V) (s T : Finset V), (G.induce (s : Set V)).IsTree → Disjoint T s →
  (∀ t ∈ T, ∃ w ∈ s, G.Adj t w ∧ ∀ u ∈ s, G.Adj t u → u = w) → (∀ t ∈ T, ∀ t' ∈ T, ¬ G.Adj t t') →
  (G.induce ((s ∪ T : Finset V) : Set V)).IsTree := by
  intro V inst G s T
  revert s
  induction T using Finset.induction_on with
  | empty =>
      intro s htree hdisj hleaf hno
      rw [Finset.union_empty]
      exact htree
  | insert t T ht ih =>
      intro s htree hdisj hleaf hno
      have hdisjT : Disjoint T s := by
        rw [Finset.disjoint_left] at hdisj ⊢
        intro x hxT
        exact hdisj (Finset.mem_insert_of_mem hxT)
      have hleafT : ∀ x ∈ T, ∃ w ∈ s, G.Adj x w ∧ ∀ u ∈ s, G.Adj x u → u = w := by
        intro x hxT
        exact hleaf x (Finset.mem_insert_of_mem hxT)
      have hnoT : ∀ x ∈ T, ∀ y ∈ T, ¬ G.Adj x y := by
        intro x hxT y hyT
        exact hno x (Finset.mem_insert_of_mem hxT) y (Finset.mem_insert_of_mem hyT)
      have htreeT : (G.induce ((s ∪ T : Finset V) : Set V)).IsTree :=
        ih s htree hdisjT hleafT hnoT
      rcases hleaf t (Finset.mem_insert_self t T) with ⟨w, hw_s, hadj_tw, huniq_s⟩
      have ht_not_s : t ∉ s := by
        have := Finset.disjoint_left.mp hdisj (Finset.mem_insert_self t T)
        exact this
      have ht_not_union : t ∉ s ∪ T := by
        rw [Finset.mem_union]
        push_neg
        exact ⟨ht_not_s, ht⟩
      have hw_union : w ∈ s ∪ T := Finset.mem_union_left T hw_s
      have huniq_union : ∀ u ∈ s ∪ T, G.Adj t u → u = w := by
        intro u hu hadj
        rw [Finset.mem_union] at hu
        rcases hu with hus | huT
        · exact huniq_s u hus hadj
        · exact absurd hadj (hno t (Finset.mem_insert_self t T) u (Finset.mem_insert_of_mem huT))
      have htree_step : (G.induce ((insert t (s ∪ T) : Finset V) : Set V)).IsTree :=
        leaf V G (s ∪ T) t w htreeT ht_not_union hw_union hadj_tw huniq_union
      rw [Finset.union_insert]
      exact htree_step

end P_leaves
theorem leaves : ∀ (V : Type) [DecidableEq V] (G : SimpleGraph V) (s T : Finset V), (G.induce (s : Set V)).IsTree → Disjoint T s →
  (∀ t ∈ T, ∃ w ∈ s, G.Adj t w ∧ ∀ u ∈ s, G.Adj t u → u = w) → (∀ t ∈ T, ∀ t' ∈ T, ¬ G.Adj t t') →
  (G.induce ((s ∪ T : Finset V) : Set V)).IsTree := P_leaves.leaves
namespace P_mindeg
theorem mindeg : ∀ (V : Type) [Fintype V] [DecidableEq V] (G : SimpleGraph V) (N : V → Finset V), (∀ x y, y ∈ N x ↔ G.Adj x y) → ∀ k : ℕ, k < Fintype.card V →
  (∀ S : Finset V, S.card < k → (G.induce ((S : Set V)ᶜ)).Connected) → ∀ x, k ≤ (N x).card := by
  intro V instF instD G N hN k hk hconn x
  by_contra hnot
  have hlt : (N x).card < k := Nat.lt_of_not_ge hnot
  let S : Finset V := N x
  have hScard : S.card < k := by
    simpa [S] using hlt
  have hxS : x ∉ S := by
    dsimp [S]
    intro hx
    exact G.irrefl ((hN x x).mp hx)
  have hins_lt : (insert x S).card < Fintype.card V := by
    calc
      (insert x S).card ≤ S.card + 1 := Finset.card_insert_le x S
      _ ≤ k := Nat.succ_le_of_lt hScard
      _ < Fintype.card V := hk
  obtain ⟨y, hy⟩ := Finset.exists_mem_notMem_of_card_lt_card hins_lt
  have hyS : y ∉ S := by
    intro h
    exact hy.2 (by simp [h])
  have hyx : y ≠ x := by
    intro hyx
    apply hy.2
    simp [hyx]
  let C : Set V := (S : Set V)ᶜ
  have hxC : x ∈ C := by
    simpa [C] using hxS
  have hyC : y ∈ C := by
    simpa [C] using hyS
  have hpre : (G.induce C).Preconnected := by
    simpa [C] using (hconn S hScard).1
  let x' : C := ⟨x, hxC⟩
  let y' : C := ⟨y, hyC⟩
  have hxy : x' ≠ y' := by
    intro h
    have hval : x = y := by
      simpa [x', y'] using congrArg Subtype.val h
    exact hyx hval.symm
  have no_adj : ∀ z : C, ¬ (G.induce C).Adj x' z := by
    intro z hz
    change G.Adj x z.1 at hz
    have hzS : z.1 ∈ S := by
      simpa [S] using (hN x z.1).mpr hz
    have hzC : z.1 ∉ S := by
      exact z.2
    exact hzC hzS
  have no_walk : ∀ {u v : C}, (G.induce C).Walk u v → u = x' → v = x' := by
    intro u v p
    induction p with
    | nil =>
        intro hu
        exact hu
    | cons h rest ih =>
        intro hu
        exfalso
        rw [hu] at h
        exact no_adj _ h
  have hreach : (G.induce C).Reachable x' y' := hpre x' y'
  obtain ⟨p⟩ := hreach
  exact hxy ((no_walk p rfl).symm)
end P_mindeg
theorem mindeg : ∀ (V : Type) [Fintype V] [DecidableEq V] (G : SimpleGraph V) (N : V → Finset V), (∀ x y, y ∈ N x ↔ G.Adj x y) → ∀ k : ℕ, k < Fintype.card V →
  (∀ S : Finset V, S.card < k → (G.induce ((S : Set V)ᶜ)).Connected) → ∀ x, k ≤ (N x).card := P_mindeg.mindeg
namespace P_maxdeg
theorem maxdeg : ∀ (V : Type) [DecidableEq V] (G : SimpleGraph V) (N : V → Finset V), (∀ x y, y ∈ N x ↔ G.Adj x y) → ∀ k : ℕ,
  (∀ s : Finset V, (G.induce (s : Set V)).IsTree → s.card ≤ k + 2) → (∀ a b c : V, G.Adj a b → G.Adj b c → ¬ G.Adj a c) → ∀ x, (N x).card ≤ k + 1 := by
  intro V _ G N hN k htree htri x
  let s : Finset V := {x}
  let T : Finset V := N x
  have hno_self : x ∉ N x := by
    intro hx
    exact G.irrefl ((hN x x).mp hx)
  have htree_s : (G.induce (s : Set V)).IsTree := by
    haveI : Nonempty (s : Set V) := ⟨⟨x, by simp [s]⟩⟩
    haveI : Subsingleton (s : Set V) := ⟨by
      intro a b
      have ha : a.1 = x := by simpa [s] using a.2
      have hb : b.1 = x := by simpa [s] using b.2
      exact Subtype.ext (ha.trans hb.symm)
    ⟩
    apply SimpleGraph.IsTree.of_subsingleton
  have hdisj : Disjoint T s := by
    rw [Finset.disjoint_left]
    intro t ht hts
    have htx : t = x := by simpa [s] using hts
    subst t
    have hxN : x ∈ N x := by simpa [T] using ht
    exact hno_self hxN
  have hleaf : ∀ t ∈ T, ∃ w ∈ s, G.Adj t w ∧ ∀ u ∈ s, G.Adj t u → u = w := by
    intro t ht
    refine ⟨x, ?_⟩
    constructor
    · simp [s]
    · constructor
      · have hxt : G.Adj x t := (hN x t).mp (by simpa [T] using ht)
        exact hxt.symm
      · intro u hu htu
        simpa [s] using hu
  have hind : ∀ t ∈ T, ∀ t' ∈ T, ¬ G.Adj t t' := by
    intro t ht t' ht' htt'
    have hxt : G.Adj x t := (hN x t).mp (by simpa [T] using ht)
    have hxt' : G.Adj x t' := (hN x t').mp (by simpa [T] using ht')
    exact htri x t t' hxt htt' hxt'
  have hU_tree : (G.induce ((s ∪ T : Finset V) : Set V)).IsTree :=
    leaves (V := V) (G := G) s T htree_s hdisj hleaf hind
  have hcardU : (s ∪ T).card ≤ k + 2 := htree (s ∪ T) hU_tree
  have hcard_union : (s ∪ T).card = s.card + T.card :=
    Finset.card_union_of_disjoint hdisj.symm
  have hcardU_eq : (s ∪ T).card = 1 + (N x).card := by
    rw [hcard_union]
    simp [s, T]
  rw [hcardU_eq] at hcardU
  omega
end P_maxdeg
theorem maxdeg : ∀ (V : Type) [DecidableEq V] (G : SimpleGraph V) (N : V → Finset V), (∀ x y, y ∈ N x ↔ G.Adj x y) → ∀ k : ℕ,
  (∀ s : Finset V, (G.induce (s : Set V)).IsTree → s.card ≤ k + 2) → (∀ a b c : V, G.Adj a b → G.Adj b c → ¬ G.Adj a c) → ∀ x, (N x).card ≤ k + 1 := P_maxdeg.maxdeg
namespace P_overlap
theorem overlap : ∀ (V : Type) [DecidableEq V] (G : SimpleGraph V) (N : V → Finset V), (∀ x y, y ∈ N x ↔ G.Adj x y) → ∀ k : ℕ,
  (∀ s : Finset V, (G.induce (s : Set V)).IsTree → s.card ≤ k + 2) → (∀ a b c : V, G.Adj a b → G.Adj b c → ¬ G.Adj a c) →
  (∀ a b c d e : V, G.Adj a b → G.Adj b c → G.Adj c d → G.Adj d e → ¬ G.Adj e a) → (∀ x, k ≤ (N x).card) →
  ∀ x z y : V, x ≠ z → y ∈ N x → y ∈ N z → (N x).card + 1 ≤ 2 * (N x ∩ N z).card := by
  intro V _ G N hN k htree htri hfive hdeg x z y hxz hyx hyz
  have hxy : G.Adj x y := (hN x y).mp hyx
  have hzy : G.Adj z y := (hN z y).mp hyz
  have hxy_ne : x ≠ y := G.ne_of_adj hxy
  have hzy_ne : z ≠ y := G.ne_of_adj hzy
  have hnxz : ¬ G.Adj x z := htri x y z hxy hzy.symm
  have t0 : (G.induce (({y} : Finset V) : Set V)).IsTree := by
    haveI : Nonempty (({y} : Finset V) : Set V) := ⟨⟨y, by simp⟩⟩
    haveI : Subsingleton (({y} : Finset V) : Set V) := ⟨by
      intro a b
      have ha : a.1 = y := by simpa using a.2
      have hb : b.1 = y := by simpa using b.2
      exact Subtype.ext (ha.trans hb.symm)⟩
    apply SimpleGraph.IsTree.of_subsingleton
  have hd0 : Disjoint ({x, z} : Finset V) {y} := by
    rw [Finset.disjoint_left]
    intro t ht hty
    simp only [Finset.mem_insert, Finset.mem_singleton] at ht hty
    rcases ht with h | h
    · exact hxy_ne (h.symm.trans hty)
    · exact hzy_ne (h.symm.trans hty)
  have hl0 : ∀ t ∈ ({x, z} : Finset V), ∃ w ∈ ({y} : Finset V), G.Adj t w ∧ ∀ u ∈ ({y} : Finset V), G.Adj t u → u = w := by
    intro t ht
    refine ⟨y, Finset.mem_singleton_self y, ?_, fun u hu _ => Finset.mem_singleton.mp hu⟩
    simp only [Finset.mem_insert, Finset.mem_singleton] at ht
    rcases ht with h | h
    · rw [h]; exact hxy
    · rw [h]; exact hzy
  have hi0 : ∀ t ∈ ({x, z} : Finset V), ∀ t' ∈ ({x, z} : Finset V), ¬ G.Adj t t' := by
    intro t ht t' ht' hadj
    simp only [Finset.mem_insert, Finset.mem_singleton] at ht ht'
    rcases ht with h | h <;> rcases ht' with h' | h' <;> rw [h, h'] at hadj
    · exact G.irrefl hadj
    · exact hnxz hadj
    · exact hnxz hadj.symm
    · exact G.irrefl hadj
  have t1 := leaves V G {y} {x, z} t0 hd0 hl0 hi0
  have hd2 : Disjoint ((N x \ N z) ∪ (N z \ N x)) ({y} ∪ {x, z}) := by
    rw [Finset.disjoint_left]
    intro t ht hts
    simp only [Finset.mem_union, Finset.mem_sdiff, Finset.mem_insert, Finset.mem_singleton] at ht hts
    rcases hts with h | h | h <;> rw [h] at ht
    · rcases ht with ⟨_, h2⟩ | ⟨_, h2⟩
      · exact h2 hyz
      · exact h2 hyx
    · rcases ht with ⟨h1, _⟩ | ⟨h1, _⟩
      · exact G.irrefl ((hN x x).mp h1)
      · exact hnxz ((hN z x).mp h1).symm
    · rcases ht with ⟨h1, _⟩ | ⟨h1, _⟩
      · exact hnxz ((hN x z).mp h1)
      · exact G.irrefl ((hN z z).mp h1)
  have hl2 : ∀ t ∈ (N x \ N z) ∪ (N z \ N x), ∃ w ∈ ({y} ∪ {x, z} : Finset V), G.Adj t w ∧
      ∀ u ∈ ({y} ∪ {x, z} : Finset V), G.Adj t u → u = w := by
    intro t ht
    simp only [Finset.mem_union, Finset.mem_sdiff] at ht
    rcases ht with ⟨hA, hnB⟩ | ⟨hB, hnA⟩
    · have hxt : G.Adj x t := (hN x t).mp hA
      refine ⟨x, by simp, hxt.symm, ?_⟩
      intro u hu htu
      simp only [Finset.mem_union, Finset.mem_insert, Finset.mem_singleton] at hu
      rcases hu with h | h | h
      · rw [h] at htu; exact absurd hxy (htri x t y hxt htu)
      · exact h
      · rw [h] at htu; exact absurd ((hN z t).mpr htu.symm) hnB
    · have hzt : G.Adj z t := (hN z t).mp hB
      refine ⟨z, by simp, hzt.symm, ?_⟩
      intro u hu htu
      simp only [Finset.mem_union, Finset.mem_insert, Finset.mem_singleton] at hu
      rcases hu with h | h | h
      · rw [h] at htu; exact absurd hzy (htri z t y hzt htu)
      · rw [h] at htu; exact absurd ((hN x t).mpr htu.symm) hnA
      · exact h
  have hi2 : ∀ t ∈ (N x \ N z) ∪ (N z \ N x), ∀ t' ∈ (N x \ N z) ∪ (N z \ N x), ¬ G.Adj t t' := by
    intro t ht t' ht' hadj
    simp only [Finset.mem_union, Finset.mem_sdiff] at ht ht'
    rcases ht with ⟨hA, hnB⟩ | ⟨hB, hnA⟩ <;> rcases ht' with ⟨hA', hnB'⟩ | ⟨hB', hnA'⟩
    · exact htri x t t' ((hN x t).mp hA) hadj ((hN x t').mp hA')
    · exact hfive x t t' z y ((hN x t).mp hA) hadj ((hN z t').mp hB').symm hzy hxy.symm
    · exact hfive x t' t z y ((hN x t').mp hA') hadj.symm ((hN z t).mp hB).symm hzy hxy.symm
    · exact htri z t t' ((hN z t).mp hB) hadj ((hN z t').mp hB')
  have htr := leaves V G _ _ t1 hd2 hl2 hi2
  have hc := htree _ htr
  have hs1 : ({y} ∪ {x, z} : Finset V).card = 3 :=
    Finset.card_eq_three.mpr ⟨y, x, z, hxy_ne.symm, hzy_ne.symm, hxz, by ext a; simp only [Finset.mem_union, Finset.mem_insert, Finset.mem_singleton]⟩
  have hdAB : Disjoint (N x \ N z) (N z \ N x) := by
    rw [Finset.disjoint_left]
    intro a h1 h2
    simp only [Finset.mem_sdiff] at h1 h2
    exact h1.2 h2.1
  rw [Finset.card_union_of_disjoint hd2.symm, hs1, Finset.card_union_of_disjoint hdAB] at hc
  have e1 := Finset.card_sdiff_add_card_inter (N x) (N z)
  have e2 := Finset.card_sdiff_add_card_inter (N z) (N x)
  rw [Finset.inter_comm] at e2
  have hk := hdeg z
  omega

end P_overlap
theorem overlap : ∀ (V : Type) [DecidableEq V] (G : SimpleGraph V) (N : V → Finset V), (∀ x y, y ∈ N x ↔ G.Adj x y) → ∀ k : ℕ,
  (∀ s : Finset V, (G.induce (s : Set V)).IsTree → s.card ≤ k + 2) → (∀ a b c : V, G.Adj a b → G.Adj b c → ¬ G.Adj a c) →
  (∀ a b c d e : V, G.Adj a b → G.Adj b c → G.Adj c d → G.Adj d e → ¬ G.Adj e a) → (∀ x, k ≤ (N x).card) →
  ∀ x z y : V, x ≠ z → y ∈ N x → y ∈ N z → (N x).card + 1 ≤ 2 * (N x ∩ N z).card := P_overlap.overlap
namespace P_parts
theorem parts : ∀ (V : Type) [Fintype V] [DecidableEq V] (G : SimpleGraph V) (N : V → Finset V), (∀ x y, y ∈ N x ↔ G.Adj x y) → G.Connected → 1 < Fintype.card V →
  (∀ a b c : V, G.Adj a b → G.Adj b c → ¬ G.Adj a c) → (∀ x z y : V, x ≠ z → y ∈ N x → y ∈ N z → (N x).card + 1 ≤ 2 * (N x ∩ N z).card) →
  ∃ X Y : Finset V, Disjoint X Y ∧ X ∪ Y = Finset.univ ∧ (∀ x ∈ X, N x ⊆ Y) ∧ (∀ y ∈ Y, N y ⊆ X) ∧
    (∀ x ∈ X, ∀ x' ∈ X, (N x ∩ N x').Nonempty) ∧ (∀ y ∈ Y, ∀ y' ∈ Y, (N y ∩ N y').Nonempty) := by
  intro V _ _ G N hN hconn hcard htri hcond
  classical
  let R : V → V → Prop := fun x z => (N x ∩ N z).Nonempty
  have hneigh : ∀ x : V, (N x).Nonempty := by
    intro x
    obtain ⟨w, hwx⟩ := Fintype.exists_ne_of_one_lt_card hcard x
    have hreach : G.Reachable x w := hconn.preconnected x w
    rcases hreach with ⟨p⟩
    cases p with
    | nil =>
        exact (hwx rfl).elim
    | cons h p =>
        exact ⟨_, (hN x _).mpr h⟩
  have hR_refl : ∀ x : V, R x x := by
    intro x
    have hx : (N x).Nonempty := hneigh x
    simpa [R] using hx
  have hR_symm : ∀ {x z : V}, R x z → R z x := by
    intro x z h
    simpa [R, Finset.inter_comm] using h
  have hR_trans : ∀ {x z w : V}, R x z → R z w → R x w := by
    intro x z w hxz hzw
    by_cases hxz_eq : x = z
    · subst z
      exact hzw
    by_cases hzw_eq : w = z
    · subst w
      exact hxz
    rcases hxz with ⟨c, hc⟩
    rcases hzw with ⟨d, hd⟩
    have hcz : c ∈ N z := (Finset.mem_inter.mp hc).2
    have hcx : c ∈ N x := (Finset.mem_inter.mp hc).1
    have hdz : d ∈ N z := (Finset.mem_inter.mp hd).1
    have hdw : d ∈ N w := (Finset.mem_inter.mp hd).2
    have hzx : z ≠ x := by
      intro h
      exact hxz_eq h.symm
    have hzw_ne : z ≠ w := by
      intro h
      exact hzw_eq h.symm
    let A : Finset V := N z ∩ N x
    let B : Finset V := N z ∩ N w
    have hAcard : (N z).card + 1 ≤ 2 * A.card := by
      simpa [A] using hcond z x c hzx hcz hcx
    have hBcard : (N z).card + 1 ≤ 2 * B.card := by
      simpa [B] using hcond z w d hzw_ne hdz hdw
    have hsum : (N z).card + 1 ≤ A.card + B.card := by
      omega
    have hsubset : A ∪ B ⊆ N z := by
      intro v hv
      rcases Finset.mem_union.mp hv with hv | hv
      · have hv' : v ∈ N z ∩ N x := by simpa [A] using hv
        exact (Finset.mem_inter.mp hv').1
      · have hv' : v ∈ N z ∩ N w := by simpa [B] using hv
        exact (Finset.mem_inter.mp hv').1
    have hle_union : (A ∪ B).card ≤ (N z).card := Finset.card_le_card hsubset
    have hpos : 0 < (A ∩ B).card := by
      by_contra hnot
      have hzero : (A ∩ B).card = 0 := Nat.eq_zero_of_not_pos hnot
      have hform := Finset.card_union_add_card_inter A B
      omega
    have hinter : (A ∩ B).Nonempty := Finset.card_pos.mp hpos
    rcases hinter with ⟨e, heAB⟩
    have heA : e ∈ A := (Finset.mem_inter.mp heAB).1
    have heB : e ∈ B := (Finset.mem_inter.mp heAB).2
    have heA' : e ∈ N z ∩ N x := by simpa [A] using heA
    have heB' : e ∈ N z ∩ N w := by simpa [B] using heB
    have hex : e ∈ N x := (Finset.mem_inter.mp heA').2
    have hew : e ∈ N w := (Finset.mem_inter.mp heB').2
    refine ⟨e, ?_⟩
    rw [Finset.mem_inter]
    exact ⟨hex, hew⟩
  have hAdj_not_R : ∀ {a b : V}, G.Adj a b → ¬ R a b := by
    intro a b hab hr
    rcases hr with ⟨c, hc⟩
    have hca : c ∈ N a := (Finset.mem_inter.mp hc).1
    have hcb : c ∈ N b := (Finset.mem_inter.mp hc).2
    have hac : G.Adj a c := (hN a c).mp hca
    have hbc : G.Adj b c := (hN b c).mp hcb
    exact (htri a b c hab hbc) hac
  have hVne : Nonempty V := Fintype.card_pos_iff.mp (by omega : 0 < Fintype.card V)
  let r : V := Classical.choice hVne
  obtain ⟨r', hr'⟩ := hneigh r
  have hrr' : G.Adj r r' := (hN r r').mp hr'
  have hkey1 : ∀ {a b : V}, G.Adj a b → R a r → R b r' := by
    intro a b hab har
    rcases har with ⟨c, hc⟩
    have hac : G.Adj a c := (hN a c).mp (Finset.mem_inter.mp hc).1
    have hrc : G.Adj r c := (hN r c).mp (Finset.mem_inter.mp hc).2
    have hRbc : R b c := by
      refine ⟨a, ?_⟩
      rw [Finset.mem_inter]
      exact ⟨(hN b a).mpr hab.symm, (hN c a).mpr hac.symm⟩
    have hRcr' : R c r' := by
      refine ⟨r, ?_⟩
      rw [Finset.mem_inter]
      exact ⟨(hN c r).mpr hrc.symm, (hN r' r).mpr hrr'.symm⟩
    exact hR_trans hRbc hRcr'
  have hkey2 : ∀ {a b : V}, G.Adj a b → R a r' → R b r := by
    intro a b hab har'
    rcases har' with ⟨c, hc⟩
    have hac : G.Adj a c := (hN a c).mp (Finset.mem_inter.mp hc).1
    have hr'c : G.Adj r' c := (hN r' c).mp (Finset.mem_inter.mp hc).2
    have hRbc : R b c := by
      refine ⟨a, ?_⟩
      rw [Finset.mem_inter]
      exact ⟨(hN b a).mpr hab.symm, (hN c a).mpr hac.symm⟩
    have hRcr : R c r := by
      refine ⟨r', ?_⟩
      rw [Finset.mem_inter]
      exact ⟨(hN c r').mpr hr'c.symm, (hN r r').mpr hrr'⟩
    exact hR_trans hRbc hRcr
  have hP_all : ∀ v : V, R v r ∨ R v r' := by
    intro v
    have hreach : G.Reachable r v := hconn.preconnected r v
    rcases hreach with ⟨p⟩
    have hwalk : ∀ {a b : V}, G.Walk a b → (R a r ∨ R a r') → (R b r ∨ R b r') := by
      intro a b p
      induction p with
      | nil =>
          intro hp
          exact hp
      | cons h p ih =>
          intro hp
          apply ih
          rcases hp with hpr | hpr'
          · exact Or.inr (hkey1 h hpr)
          · exact Or.inl (hkey2 h hpr')
    exact hwalk p (Or.inl (hR_refl r))
  let X : Finset V := Finset.univ.filter (fun x => R x r)
  let Y : Finset V := Finset.univ.filter (fun x => R x r')
  have hdisj : Disjoint X Y := by
    rw [Finset.disjoint_left]
    intro v hvX hvY
    have hvR : R v r := by simpa [X] using hvX
    have hvR' : R v r' := by simpa [Y] using hvY
    have hrR : R r v := hR_symm hvR
    have hrr : R r r' := hR_trans hrR hvR'
    exact hAdj_not_R hrr' hrr
  have hcover : X ∪ Y = Finset.univ := by
    ext v
    simp only [X, Y, Finset.mem_union, Finset.mem_filter, Finset.mem_univ, true_and]
    constructor
    · intro _
      trivial
    · intro _
      exact hP_all v
  have hXY : ∀ x ∈ X, N x ⊆ Y := by
    intro x hx y hy
    have hxr : R x r := by simpa [X] using hx
    have hxy : G.Adj x y := (hN x y).mp hy
    have hyR : R y r' := hkey1 hxy hxr
    simpa [Y] using hyR
  have hYX : ∀ y ∈ Y, N y ⊆ X := by
    intro y hy x hx
    have hyr' : R y r' := by simpa [Y] using hy
    have hyx : G.Adj y x := (hN y x).mp hx
    have hxR : R x r := hkey2 hyx hyr'
    simpa [X] using hxR
  have hXint : ∀ x ∈ X, ∀ x' ∈ X, (N x ∩ N x').Nonempty := by
    intro x hx x' hx'
    have hxr : R x r := by simpa [X] using hx
    have hx'r : R x' r := by simpa [X] using hx'
    have hrx' : R r x' := hR_symm hx'r
    exact hR_trans hxr hrx'
  have hYint : ∀ y ∈ Y, ∀ y' ∈ Y, (N y ∩ N y').Nonempty := by
    intro y hy y' hy'
    have hyr' : R y r' := by simpa [Y] using hy
    have hy'r' : R y' r' := by simpa [Y] using hy'
    have hr'y' : R r' y' := hR_symm hy'r'
    exact hR_trans hyr' hr'y'
  refine ⟨X, Y, hdisj, hcover, hXY, hYX, hXint, hYint⟩
end P_parts
theorem parts : ∀ (V : Type) [Fintype V] [DecidableEq V] (G : SimpleGraph V) (N : V → Finset V), (∀ x y, y ∈ N x ↔ G.Adj x y) → G.Connected → 1 < Fintype.card V →
  (∀ a b c : V, G.Adj a b → G.Adj b c → ¬ G.Adj a c) → (∀ x z y : V, x ≠ z → y ∈ N x → y ∈ N z → (N x).card + 1 ≤ 2 * (N x ∩ N z).card) →
  ∃ X Y : Finset V, Disjoint X Y ∧ X ∪ Y = Finset.univ ∧ (∀ x ∈ X, N x ⊆ Y) ∧ (∀ y ∈ Y, N y ⊆ X) ∧
    (∀ x ∈ X, ∀ x' ∈ X, (N x ∩ N x').Nonempty) ∧ (∀ y ∈ Y, ∀ y' ∈ Y, (N y ∩ N y').Nonempty) := P_parts.parts
namespace P_size
lemma aux_card_inter_eq_sum {V : Type} [DecidableEq V] (A B : Finset V) :
    (A ∩ B).card = ∑ y ∈ A, if y ∈ B then 1 else 0 := by
  classical
  rw [show A ∩ B = A.filter (fun y => y ∈ B) by
    ext y
    simp]
  rw [Finset.card_eq_sum_ones, Finset.sum_filter]

lemma aux_sum_inter_le {V : Type} [DecidableEq V] (k : ℕ) (N : V → Finset V)
    (X : Finset V) (x : V)
    (hsymm : ∀ a b, b ∈ N a ↔ a ∈ N b)
    (hupper : ∀ a, (N a).card ≤ k + 1) :
    (∑ x' ∈ X.erase x, (N x ∩ N x').card) ≤ (N x).card * k := by
  classical
  calc
    (∑ x' ∈ X.erase x, (N x ∩ N x').card)
        = ∑ x' ∈ X.erase x, ∑ y ∈ N x, (if y ∈ N x' then 1 else 0) := by
            apply Finset.sum_congr rfl
            intro x' hx'
            exact aux_card_inter_eq_sum (N x) (N x')
    _ = ∑ y ∈ N x, ∑ x' ∈ X.erase x, (if y ∈ N x' then 1 else 0) := by
            rw [Finset.sum_comm]
    _ = ∑ y ∈ N x, ∑ x' ∈ X.erase x, (if x' ∈ N y then 1 else 0) := by
            apply Finset.sum_congr rfl
            intro y hy
            apply Finset.sum_congr rfl
            intro x' hx'
            have hiff : (x' ∈ N y) ↔ (y ∈ N x') := hsymm y x'
            by_cases h : y ∈ N x'
            · have h' : x' ∈ N y := hiff.mpr h
              simp [h, h']
            · have h' : x' ∉ N y := fun hc => h (hiff.mp hc)
              simp [h, h']
    _ ≤ ∑ y ∈ N x, ((N y).erase x).card := by
            apply Finset.sum_le_sum
            intro y hy
            have hinner : (∑ x' ∈ X.erase x, (if x' ∈ N y then 1 else 0)) =
                ((X.erase x).filter (fun x' => x' ∈ N y)).card := by
              rw [Finset.card_eq_sum_ones, Finset.sum_filter]
            rw [hinner]
            apply Finset.card_le_card
            intro x' hx'
            have hxe : x' ∈ X.erase x := (Finset.mem_filter.mp hx').1
            have hxy : x' ∈ N y := (Finset.mem_filter.mp hx').2
            have hne : x' ≠ x := (Finset.mem_erase.mp hxe).1
            exact Finset.mem_erase.mpr ⟨hne, hxy⟩
    _ = ∑ y ∈ N x, ((N y).card - 1) := by
            apply Finset.sum_congr rfl
            intro y hy
            exact Finset.card_erase_of_mem ((hsymm y x).mpr hy)
    _ ≤ ∑ y ∈ N x, k := by
            apply Finset.sum_le_sum
            intro y hy
            have hcard := hupper y
            omega
    _ = (N x).card * k := by
            rw [Finset.sum_const]
            simp

lemma aux_ineq_for_mem {V : Type} [DecidableEq V] (k : ℕ) (N : V → Finset V)
    (X : Finset V) (x : V)
    (hxX : x ∈ X)
    (hsymm : ∀ a b, b ∈ N a ↔ a ∈ N b)
    (hupper : ∀ a, (N a).card ≤ k + 1)
    (hcond : ∀ a ∈ X, ∀ b ∈ X, a ≠ b → (N a).card + 1 ≤ 2 * (N a ∩ N b).card) :
    (X.erase x).card * ((N x).card + 1) ≤ 2 * ((N x).card * k) := by
  classical
  have hconst : (X.erase x).card * ((N x).card + 1) ≤
      ∑ x' ∈ X.erase x, 2 * (N x ∩ N x').card := by
    calc
      (X.erase x).card * ((N x).card + 1)
          = ∑ x' ∈ X.erase x, ((N x).card + 1) := by
              rw [Finset.sum_const]
              simp
      _ ≤ ∑ x' ∈ X.erase x, 2 * (N x ∩ N x').card := by
              apply Finset.sum_le_sum
              intro x' hx'
              have hx'X : x' ∈ X := (Finset.mem_erase.mp hx').2
              have hne : x' ≠ x := (Finset.mem_erase.mp hx').1
              exact hcond x hxX x' hx'X (Ne.symm hne)
  have hsum := aux_sum_inter_le (V := V) k N X x hsymm hupper
  have hsum2 : (∑ x' ∈ X.erase x, 2 * (N x ∩ N x').card) ≤
      2 * ((N x).card * k) := by
    calc
      (∑ x' ∈ X.erase x, 2 * (N x ∩ N x').card)
          = 2 * (∑ x' ∈ X.erase x, (N x ∩ N x').card) := by
              rw [Finset.mul_sum]
      _ ≤ 2 * ((N x).card * k) := Nat.mul_le_mul_left 2 hsum
  exact le_trans hconst hsum2

theorem size : ∀ (V : Type) [DecidableEq V] (N : V → Finset V) (X : Finset V) (k : ℕ), 2 ≤ k → (∀ x y, y ∈ N x ↔ x ∈ N y) → (∀ x, k ≤ (N x).card) → (∀ x, (N x).card ≤ k + 1) →
  (∀ x ∈ X, ∀ x' ∈ X, x ≠ x' → (N x).card + 1 ≤ 2 * (N x ∩ N x').card) → X.card ≤ 2 * k ∧ (X.card = 2 * k → ∀ x ∈ X, k + 1 ≤ (N x).card) := by
  intro V hV N X k hk hsymm hlower hupper hcond
  classical
  constructor
  · by_cases hX : X = ∅
    · rw [hX]
      simp
    · have hne : X.Nonempty := Finset.nonempty_iff_ne_empty.mpr hX
      obtain ⟨x, hxX⟩ := hne
      have hineq := aux_ineq_for_mem (V := V) k N X x hxX hsymm hupper hcond
      by_contra hnot
      have hm : 2 * k + 1 ≤ X.card := by omega
      have he_add : (X.erase x).card + 1 = X.card := Finset.card_erase_add_one hxX
      have he_ge : 2 * k ≤ (X.erase x).card := by omega
      have hmul : 2 * k * ((N x).card + 1) ≤
          (X.erase x).card * ((N x).card + 1) :=
        Nat.mul_le_mul_right ((N x).card + 1) he_ge
      have hcontr : 2 * k * ((N x).card + 1) ≤
          2 * ((N x).card * k) := le_trans hmul hineq
      have hEq : 2 * k * ((N x).card + 1) =
          2 * ((N x).card * k) + 2 * k := by ring
      have hkpos : 0 < 2 * k := by omega
      have hlt : 2 * ((N x).card * k) < 2 * k * ((N x).card + 1) := by
        rw [hEq]
        omega
      exact (not_lt_of_ge hcontr) hlt
  · intro hXcard x hxX
    have hineq := aux_ineq_for_mem (V := V) k N X x hxX hsymm hupper hcond
    have he_add : (X.erase x).card + 1 = X.card := Finset.card_erase_add_one hxX
    have he : (X.erase x).card + 1 = 2 * k := by omega
    have hlo : k ≤ (N x).card := hlower x
    by_contra hnot
    have hd_le : (N x).card ≤ k := by omega
    have hd_eq : (N x).card = k := le_antisymm hd_le hlo
    have hcontr : (X.erase x).card * (k + 1) ≤ 2 * (k * k) := by
      simpa [hd_eq] using hineq
    have he_mul : 2 * k * (k + 1) = (X.erase x).card * (k + 1) + (k + 1) := by
      calc
        2 * k * (k + 1) = ((X.erase x).card + 1) * (k + 1) := by rw [he]
        _ = (X.erase x).card * (k + 1) + (k + 1) := by ring
    have hle' : 2 * k * (k + 1) ≤ 2 * (k * k) + (k + 1) := by
      calc
        2 * k * (k + 1) = (X.erase x).card * (k + 1) + (k + 1) := he_mul
        _ ≤ 2 * (k * k) + (k + 1) := Nat.add_le_add_right hcontr (k + 1)
    have hk_le : k ≤ 1 := by nlinarith [hk, hle']
    omega
end P_size
theorem size : ∀ (V : Type) [DecidableEq V] (N : V → Finset V) (X : Finset V) (k : ℕ), 2 ≤ k → (∀ x y, y ∈ N x ↔ x ∈ N y) → (∀ x, k ≤ (N x).card) → (∀ x, (N x).card ≤ k + 1) →
  (∀ x ∈ X, ∀ x' ∈ X, x ≠ x' → (N x).card + 1 ≤ 2 * (N x ∩ N x').card) → X.card ≤ 2 * k ∧ (X.card = 2 * k → ∀ x ∈ X, k + 1 ≤ (N x).card) := P_size.size
namespace P_balance
theorem balance : ∀ (V : Type) [DecidableEq V] (N : V → Finset V) (X Y : Finset V) (k : ℕ), (∀ x y, y ∈ N x ↔ x ∈ N y) → (∀ x, k ≤ (N x).card) → (∀ x, (N x).card ≤ k + 1) →
  (∀ x ∈ X, N x ⊆ Y) → (∀ y ∈ Y, N y ⊆ X) → Y.card ≤ 2 * k → Y.card ≤ X.card + 1 := by
  intro V hV N X Y k hsym hdegLow hdegHigh hXsub hYsub hYcard
  classical
  have hsum : (∑ y ∈ Y, (N y).card) = ∑ x ∈ X, (N x).card := by
    calc
      (∑ y ∈ Y, (N y).card)
          = ∑ y ∈ Y, ∑ x ∈ X, if x ∈ N y then (1 : ℕ) else 0 := by
              apply Finset.sum_congr rfl
              intro y hy
              have hsub : N y ⊆ X := hYsub y hy
              have hfilter : X.filter (fun x => x ∈ N y) = N y := by
                ext x
                constructor
                · intro hx
                  exact (Finset.mem_filter.mp hx).2
                · intro hx
                  exact Finset.mem_filter.mpr ⟨hsub hx, hx⟩
              rw [← Finset.card_filter (fun x => x ∈ N y) X, hfilter]
      _ = ∑ x ∈ X, ∑ y ∈ Y, if x ∈ N y then (1 : ℕ) else 0 := by
              rw [Finset.sum_comm]
      _ = ∑ x ∈ X, ∑ y ∈ Y, if y ∈ N x then (1 : ℕ) else 0 := by
              apply Finset.sum_congr rfl
              intro x hx
              apply Finset.sum_congr rfl
              intro y hy
              simp only [hsym y x]
      _ = ∑ x ∈ X, (N x).card := by
              apply Finset.sum_congr rfl
              intro x hx
              have hsub : N x ⊆ Y := hXsub x hx
              have hfilter : Y.filter (fun y => y ∈ N x) = N x := by
                ext y
                constructor
                · intro hy
                  exact (Finset.mem_filter.mp hy).2
                · intro hy
                  exact Finset.mem_filter.mpr ⟨hsub hy, hy⟩
              rw [← Finset.card_filter (fun y => y ∈ N x) Y, hfilter]
  have hlower : Y.card * k ≤ ∑ y ∈ Y, (N y).card := by
    calc
      Y.card * k = ∑ y ∈ Y, k := by
        simp [Finset.sum_const]
      _ ≤ ∑ y ∈ Y, (N y).card := by
        exact Finset.sum_le_sum (fun y hy => hdegLow y)
  have hupper : (∑ x ∈ X, (N x).card) ≤ X.card * (k + 1) := by
    calc
      (∑ x ∈ X, (N x).card) ≤ ∑ x ∈ X, (k + 1) := by
        exact Finset.sum_le_sum (fun x hx => hdegHigh x)
      _ = X.card * (k + 1) := by
        simp [Finset.sum_const]
  have hmain : Y.card * k ≤ X.card * (k + 1) := by
    calc
      Y.card * k ≤ ∑ y ∈ Y, (N y).card := hlower
      _ = ∑ x ∈ X, (N x).card := hsum
      _ ≤ X.card * (k + 1) := hupper
  by_contra hnot
  have hYge : X.card + 2 ≤ Y.card := by omega
  have h1 : k * (X.card + 2) ≤ k * Y.card := Nat.mul_le_mul_left k hYge
  have h2 : k * Y.card ≤ X.card * (k + 1) := by
    rw [Nat.mul_comm k Y.card]
    exact hmain
  have h3 : k * (X.card + 2) ≤ X.card * (k + 1) := le_trans h1 h2
  have h3' : X.card * k + 2 * k ≤ X.card * k + X.card := by
    calc
      X.card * k + 2 * k = k * (X.card + 2) := by ring
      _ ≤ X.card * (k + 1) := h3
      _ = X.card * k + X.card := by ring
  have hX2k : 2 * k ≤ X.card := (Nat.add_le_add_iff_left).mp h3'
  have hYX : Y.card ≤ X.card := le_trans hYcard hX2k
  omega
end P_balance
theorem balance : ∀ (V : Type) [DecidableEq V] (N : V → Finset V) (X Y : Finset V) (k : ℕ), (∀ x y, y ∈ N x ↔ x ∈ N y) → (∀ x, k ≤ (N x).card) → (∀ x, (N x).card ≤ k + 1) →
  (∀ x ∈ X, N x ⊆ Y) → (∀ y ∈ Y, N y ⊆ X) → Y.card ≤ 2 * k → Y.card ≤ X.card + 1 := P_balance.balance
namespace P_cross
theorem cross : ∀ (m : ℕ) (E : ℕ → ℕ → Prop) (x y : ℕ → ℕ), 2 ≤ m → (∀ i < m, x i < m) → (∀ i < m, y i < m) → (∀ i < m, ∀ j < m, x i = x j → i = j) → (∀ i < m, ∀ j < m, y i = y j → i = j) →
  (∀ i < m, E (x i) (y i)) → (∀ i, i + 1 < m → E (x (i + 1)) (y i)) → (∃ i, i + 1 < m ∧ E (x 0) (y i) ∧ E (x i) (y (m - 1))) →
  ∃ x' y' : ℕ → ℕ, (∀ i < m, x' i < m) ∧ (∀ i < m, y' i < m) ∧ (∀ i < m, ∀ j < m, x' i = x' j → i = j) ∧ (∀ i < m, ∀ j < m, y' i = y' j → i = j) ∧
    (∀ i < m, E (x' i) (y' i)) ∧ (∀ i, i + 1 < m → E (x' (i + 1)) (y' i)) ∧ E (x' 0) (y' (m - 1)) := by
  intro m E x y hm hxlt hylt hxinj hyinj hdiag hsub hIndex
  rcases hIndex with ⟨i0, hi0m, hx0yi0, hxiyend⟩
  let x' : ℕ → ℕ := fun j => if j ≤ i0 then x j else x (m + i0 - j)
  let y' : ℕ → ℕ := fun j => if j < i0 then y j else y (m - 1 + i0 - j)
  have hx'lt : ∀ i < m, x' i < m := by
    intro i hi
    dsimp [x']
    by_cases hle : i ≤ i0
    · rw [if_pos hle]
      exact hxlt i hi
    · rw [if_neg hle]
      apply hxlt
      omega
  have hy'lt : ∀ i < m, y' i < m := by
    intro i hi
    dsimp [y']
    by_cases hlt : i < i0
    · rw [if_pos hlt]
      exact hylt i hi
    · rw [if_neg hlt]
      apply hylt
      omega
  have hx'inj : ∀ i < m, ∀ j < m, x' i = x' j → i = j := by
    intro i hi j hj heq
    dsimp [x'] at heq
    by_cases hi_le : i ≤ i0
    · by_cases hj_le : j ≤ i0
      · rw [if_pos hi_le] at heq
        rw [if_pos hj_le] at heq
        exact hxinj i hi j hj heq
      · rw [if_pos hi_le] at heq
        rw [if_neg hj_le] at heq
        have hjidx : m + i0 - j < m := by omega
        have hEqIdx : i = m + i0 - j := hxinj i hi (m + i0 - j) hjidx heq
        have hj_gt : i0 < j := by omega
        have hi_gt : i0 < i := by
          rw [hEqIdx]
          omega
        omega
    · by_cases hj_le : j ≤ i0
      · rw [if_neg hi_le] at heq
        rw [if_pos hj_le] at heq
        have hiidx : m + i0 - i < m := by omega
        have hEqIdx : m + i0 - i = j := hxinj (m + i0 - i) hiidx j hj heq
        have hi_gt : i0 < i := by omega
        have hj_gt : i0 < j := by
          rw [← hEqIdx]
          omega
        omega
      · rw [if_neg hi_le] at heq
        rw [if_neg hj_le] at heq
        have hiidx : m + i0 - i < m := by omega
        have hjidx : m + i0 - j < m := by omega
        have hEqIdx : m + i0 - i = m + i0 - j :=
          hxinj (m + i0 - i) hiidx (m + i0 - j) hjidx heq
        have hi_base : i ≤ m + i0 := by omega
        have hj_base : j ≤ m + i0 := by omega
        omega
  have hy'inj : ∀ i < m, ∀ j < m, y' i = y' j → i = j := by
    intro i hi j hj heq
    dsimp [y'] at heq
    by_cases hi_lt : i < i0
    · by_cases hj_lt : j < i0
      · rw [if_pos hi_lt] at heq
        rw [if_pos hj_lt] at heq
        exact hyinj i hi j hj heq
      · rw [if_pos hi_lt] at heq
        rw [if_neg hj_lt] at heq
        have hjidx : m - 1 + i0 - j < m := by omega
        have hEqIdx : i = m - 1 + i0 - j := hyinj i hi (m - 1 + i0 - j) hjidx heq
        have hj_ge : i0 ≤ j := by omega
        have hi_ge : i0 ≤ i := by
          rw [hEqIdx]
          omega
        omega
    · by_cases hj_lt : j < i0
      · rw [if_neg hi_lt] at heq
        rw [if_pos hj_lt] at heq
        have hiidx : m - 1 + i0 - i < m := by omega
        have hEqIdx : m - 1 + i0 - i = j := hyinj (m - 1 + i0 - i) hiidx j hj heq
        have hj_ge : i0 ≤ j := by
          rw [← hEqIdx]
          omega
        omega
      · rw [if_neg hi_lt] at heq
        rw [if_neg hj_lt] at heq
        have hiidx : m - 1 + i0 - i < m := by omega
        have hjidx : m - 1 + i0 - j < m := by omega
        have hEqIdx : m - 1 + i0 - i = m - 1 + i0 - j :=
          hyinj (m - 1 + i0 - i) hiidx (m - 1 + i0 - j) hjidx heq
        have hi_base : i ≤ m - 1 + i0 := by omega
        have hj_base : j ≤ m - 1 + i0 := by omega
        omega
  have hdiag' : ∀ i < m, E (x' i) (y' i) := by
    intro i hi
    dsimp [x', y']
    by_cases hlt : i < i0
    · have hle : i ≤ i0 := by omega
      rw [if_pos hle]
      rw [if_pos hlt]
      exact hdiag i hi
    · by_cases heq : i = i0
      · subst i
        rw [if_pos (by omega : i0 ≤ i0)]
        rw [if_neg (by omega : ¬ i0 < i0)]
        have hidx : m - 1 + i0 - i0 = m - 1 := by omega
        rw [hidx]
        exact hxiyend
      · have hgt : i0 < i := by omega
        rw [if_neg (by omega : ¬ i ≤ i0)]
        rw [if_neg (by omega : ¬ i < i0)]
        have hklt : (m - 1 + i0 - i) + 1 < m := by omega
        have h := hsub (m - 1 + i0 - i) hklt
        have hxidx : m + i0 - i = (m - 1 + i0 - i) + 1 := by omega
        simpa [hxidx] using h
  have hsub' : ∀ i, i + 1 < m → E (x' (i + 1)) (y' i) := by
    intro i hi1
    dsimp [x', y']
    by_cases hlt : i < i0
    · have hle : i + 1 ≤ i0 := by omega
      rw [if_pos hle]
      rw [if_pos hlt]
      exact hsub i hi1
    · have hi_ge : i0 ≤ i := by omega
      have hnot : ¬ (i + 1 ≤ i0) := by omega
      rw [if_neg hnot]
      rw [if_neg hlt]
      let a := m - 1 + i0 - i
      have ham : a < m := by
        dsimp [a]
        omega
      have h := hdiag a ham
      have hxidx : m + i0 - (i + 1) = a := by
        dsimp [a]
        omega
      have hyidx : m - 1 + i0 - i = a := by
        dsimp [a]
      simpa [hxidx, hyidx] using h
  have hclose' : E (x' 0) (y' (m - 1)) := by
    dsimp [x', y']
    have h0 : (0 : ℕ) ≤ i0 := by omega
    have hnlt : ¬ (m - 1 < i0) := by omega
    have hidx : m - 1 + i0 - (m - 1) = i0 := by omega
    simp only [h0, if_true, hnlt, if_false, hidx]
    exact hx0yi0
  refine ⟨x', y', ?_⟩
  exact ⟨hx'lt, hy'lt, hx'inj, hy'inj, hdiag', hsub', hclose'⟩
end P_cross
theorem cross : ∀ (m : ℕ) (E : ℕ → ℕ → Prop) (x y : ℕ → ℕ), 2 ≤ m → (∀ i < m, x i < m) → (∀ i < m, y i < m) → (∀ i < m, ∀ j < m, x i = x j → i = j) → (∀ i < m, ∀ j < m, y i = y j → i = j) →
  (∀ i < m, E (x i) (y i)) → (∀ i, i + 1 < m → E (x (i + 1)) (y i)) → (∃ i, i + 1 < m ∧ E (x 0) (y i) ∧ E (x i) (y (m - 1))) →
  ∃ x' y' : ℕ → ℕ, (∀ i < m, x' i < m) ∧ (∀ i < m, y' i < m) ∧ (∀ i < m, ∀ j < m, x' i = x' j → i = j) ∧ (∀ i < m, ∀ j < m, y' i = y' j → i = j) ∧
    (∀ i < m, E (x' i) (y' i)) ∧ (∀ i, i + 1 < m → E (x' (i + 1)) (y' i)) ∧ E (x' 0) (y' (m - 1)) := P_cross.cross
namespace P_rotA
lemma rotA_mod_add_right_eq_self {m N d : ℕ} (hm : 0 < m) (hd : d < m)
    (h : N % m = (N + d) % m) : d = 0 := by
  let r := N % m
  have hr : r < m := by
    dsimp [r]
    exact Nat.mod_lt N hm
  have h' : r = (r + d) % m := by
    calc
      r = N % m := rfl
      _ = (N + d) % m := h
      _ = (r + d) % m := by
        rw [Nat.add_mod]
        have hdmod : d % m = d := Nat.mod_eq_of_lt hd
        have hrmod : r % m = r := Nat.mod_eq_of_lt hr
        simp [r, hdmod, hrmod]
  by_cases hrd : r + d < m
  · rw [Nat.mod_eq_of_lt hrd] at h'
    omega
  · have hrd_ge : m ≤ r + d := by omega
    have hmod : (r + d) % m = r + d - m := by
      have hslt : r + d - m < m := by omega
      have hs : r + d = m + (r + d - m) := by omega
      rw [hs]
      rw [Nat.add_mod]
      have hsmod : (r + d - m) % m = r + d - m := Nat.mod_eq_of_lt hslt
      simp [hsmod]
    rw [hmod] at h'
    have hdm : d = m := by omega
    omega

lemma rotA_mod_add_right_inj_lt {m a t u : ℕ} (hm : 0 < m)
    (ht : t < m) (hu : u < m)
    (h : (t + a) % m = (u + a) % m) : t = u := by
  by_cases htu : t ≤ u
  · have hdlt : u - t < m := by omega
    have hN : (t + a) % m = (t + a + (u - t)) % m := by
      have hu' : u + a = t + a + (u - t) := by omega
      rw [hu'] at h
      exact h
    have hd0 : u - t = 0 := rotA_mod_add_right_eq_self hm hdlt hN
    omega
  · have hut : u ≤ t := by omega
    have hdlt : t - u < m := by omega
    have hN : (u + a) % m = (u + a + (t - u)) % m := by
      have ht' : t + a = u + a + (t - u) := by omega
      rw [ht'] at h
      exact h.symm
    have hd0 : t - u = 0 := rotA_mod_add_right_eq_self hm hdlt hN
    omega

lemma rotA_mod_shift_inj (m j t u : ℕ) (hm : 2 ≤ m) (ht : t < m) (hu : u < m)
    (h : (t + j + 1) % m = (u + j + 1) % m) : t = u := by
  have hm0 : 0 < m := by omega
  have h' : (t + (j + 1)) % m = (u + (j + 1)) % m := by
    simpa [Nat.add_assoc] using h
  exact rotA_mod_add_right_inj_lt (m := m) (a := j + 1) hm0 ht hu h'

theorem rotA : ∀ (m : ℕ) (P : ℕ → ℕ → Prop) (x y : ℕ → ℕ) (j : ℕ), 2 ≤ m → j + 1 < m → ((∀ i < m, x i < m) ∧ (∀ i < m, y i < m) ∧ (∀ i < m, ∀ j < m, x i = x j → i = j) ∧ (∀ i < m, ∀ j < m, y i = y j → i = j) ∧ (∀ i < m, P (x i) (y i)) ∧ (∀ i, i + 1 < m → P (x (i + 1)) (y i)) ∧ P (x 0) (y (m - 1))) →
  ∃ x' y' : ℕ → ℕ, ((∀ i < m, x' i < m) ∧ (∀ i < m, y' i < m) ∧ (∀ i < m, ∀ j < m, x' i = x' j → i = j) ∧ (∀ i < m, ∀ j < m, y' i = y' j → i = j) ∧ (∀ i < m, P (x' i) (y' i)) ∧ (∀ i, i + 1 < m → P (x' (i + 1)) (y' i)) ∧ P (x' 0) (y' (m - 1))) ∧ x' 0 = x (j + 1) ∧ y' (m - 1) = y j := by
  intro m P x y j hm hj h
  rcases h with ⟨hxlt, hylt, hxinj, hyinj, hPdiag, hPshift, hPclose⟩
  let x' : ℕ → ℕ := fun t => x ((t + j + 1) % m)
  let y' : ℕ → ℕ := fun t => y ((t + j + 1) % m)
  have hxstep : ∀ i : ℕ, x' (i + 1) = x (((i + j + 1) % m + 1) % m) := by
    intro i
    dsimp [x']
    congr 1
    calc
      ((i + 1) + j + 1) % m = ((i + j + 1) + 1) % m := by
        congr 1
        omega
      _ = (((i + j + 1) % m) + 1) % m := by
        rw [Nat.add_mod]
        have h1 : (1 : ℕ) % m = 1 := Nat.mod_eq_of_lt (by omega)
        have hmod : ((i + j + 1) % m) % m = (i + j + 1) % m :=
          Nat.mod_eq_of_lt (Nat.mod_lt _ (by omega))
        simp [h1, hmod]
  have hx0 : x' 0 = x (j + 1) := by
    dsimp [x']
    rw [Nat.zero_add, Nat.mod_eq_of_lt hj]
  have hylast : y' (m - 1) = y j := by
    dsimp [y']
    congr 1
    have harg : m - 1 + j + 1 = m + j := by omega
    rw [harg, Nat.add_mod]
    have hjlt : j < m := by omega
    simp [Nat.mod_eq_of_lt hjlt]
  have hx'lt : ∀ i < m, x' i < m := by
    intro i hi
    dsimp [x']
    exact hxlt _ (Nat.mod_lt _ (by omega))
  have hy'lt : ∀ i < m, y' i < m := by
    intro i hi
    dsimp [y']
    exact hylt _ (Nat.mod_lt _ (by omega))
  have hx'inj : ∀ i < m, ∀ k < m, x' i = x' k → i = k := by
    intro i hi k hk heq
    have hiidx : (i + j + 1) % m < m := Nat.mod_lt _ (by omega)
    have hkidx : (k + j + 1) % m < m := Nat.mod_lt _ (by omega)
    have hidx : (i + j + 1) % m = (k + j + 1) % m := by
      exact hxinj _ hiidx _ hkidx (by simpa [x'] using heq)
    exact rotA_mod_shift_inj m j i k hm hi hk hidx
  have hy'inj : ∀ i < m, ∀ k < m, y' i = y' k → i = k := by
    intro i hi k hk heq
    have hiidx : (i + j + 1) % m < m := Nat.mod_lt _ (by omega)
    have hkidx : (k + j + 1) % m < m := Nat.mod_lt _ (by omega)
    have hidx : (i + j + 1) % m = (k + j + 1) % m := by
      exact hyinj _ hiidx _ hkidx (by simpa [y'] using heq)
    exact rotA_mod_shift_inj m j i k hm hi hk hidx
  have hdiag' : ∀ i < m, P (x' i) (y' i) := by
    intro i hi
    have hiidx : (i + j + 1) % m < m := Nat.mod_lt _ (by omega)
    simpa [x', y'] using hPdiag _ hiidx
  have hshift' : ∀ i, i + 1 < m → P (x' (i + 1)) (y' i) := by
    intro i hi
    let idx := (i + j + 1) % m
    have hidx_lt : idx < m := by
      dsimp [idx]
      exact Nat.mod_lt _ (by omega)
    have hxstep_i : x' (i + 1) = x ((idx + 1) % m) := by
      simpa [idx] using hxstep i
    have hyi : y' i = y idx := by rfl
    by_cases hnext : idx + 1 < m
    · have hx : x' (i + 1) = x (idx + 1) := by
        rw [hxstep_i, Nat.mod_eq_of_lt hnext]
      have hP : P (x (idx + 1)) (y idx) := hPshift idx hnext
      simpa [hx, hyi] using hP
    · have hidx_eq : idx + 1 = m := by omega
      have hx : x' (i + 1) = x 0 := by
        rw [hxstep_i, hidx_eq, Nat.mod_self]
      have hidx_m1 : idx = m - 1 := by omega
      have hy : y' i = y (m - 1) := by
        rw [hyi, hidx_m1]
      simpa [hx, hy] using hPclose
  have hclose' : P (x' 0) (y' (m - 1)) := by
    rw [hx0, hylast]
    exact hPshift j hj
  exact ⟨x', y', ⟨hx'lt, hy'lt, hx'inj, hy'inj, hdiag', hshift', hclose'⟩, hx0, hylast⟩

end P_rotA
theorem rotA : ∀ (m : ℕ) (P : ℕ → ℕ → Prop) (x y : ℕ → ℕ) (j : ℕ), 2 ≤ m → j + 1 < m → ((∀ i < m, x i < m) ∧ (∀ i < m, y i < m) ∧ (∀ i < m, ∀ j < m, x i = x j → i = j) ∧ (∀ i < m, ∀ j < m, y i = y j → i = j) ∧ (∀ i < m, P (x i) (y i)) ∧ (∀ i, i + 1 < m → P (x (i + 1)) (y i)) ∧ P (x 0) (y (m - 1))) →
  ∃ x' y' : ℕ → ℕ, ((∀ i < m, x' i < m) ∧ (∀ i < m, y' i < m) ∧ (∀ i < m, ∀ j < m, x' i = x' j → i = j) ∧ (∀ i < m, ∀ j < m, y' i = y' j → i = j) ∧ (∀ i < m, P (x' i) (y' i)) ∧ (∀ i, i + 1 < m → P (x' (i + 1)) (y' i)) ∧ P (x' 0) (y' (m - 1))) ∧ x' 0 = x (j + 1) ∧ y' (m - 1) = y j := P_rotA.rotA
namespace P_rotB
lemma rot_mod_formula (m j i : ℕ) (hm : 0 < m) (hj : j < m) (hi : i < m) :
    (j + m - i) % m = if i ≤ j then j - i else j + m - i := by
  by_cases h : i ≤ j
  · rw [if_pos h]
    have h1 : j + m - i = (j - i) + m := by omega
    rw [h1, Nat.add_mod_right]
    rw [Nat.mod_eq_of_lt (by omega)]
  · rw [if_neg h]
    rw [Nat.mod_eq_of_lt (by omega)]

lemma rot_mod_inj (m j i k : ℕ) (hm : 0 < m) (hj : j < m) (hi : i < m) (hk : k < m)
    (h : (j + m - i) % m = (j + m - k) % m) : i = k := by
  rw [rot_mod_formula m j i hm hj hi, rot_mod_formula m j k hm hj hk] at h
  by_cases h1 : i ≤ j
  · by_cases h2 : k ≤ j
    · rw [if_pos h1, if_pos h2] at h
      omega
    · rw [if_pos h1, if_neg h2] at h
      omega
  · by_cases h2 : k ≤ j
    · rw [if_neg h1, if_pos h2] at h
      omega
    · rw [if_neg h1, if_neg h2] at h
      omega

lemma rev_mod_formula (m j i : ℕ) (hm : 0 < m) (hj : j < m) (hi : i < m) :
    (j + 2 * m - 1 - i) % m =
      if i < j then j - 1 - i else j + m - 1 - i := by
  have hEq : j + 2 * m - 1 - i = (j + m - 1 - i) + m := by omega
  rw [hEq, Nat.add_mod_right]
  by_cases h : i < j
  · rw [if_pos h]
    have hEq2 : j + m - 1 - i = (j - 1 - i) + m := by omega
    rw [hEq2, Nat.add_mod_right]
    rw [Nat.mod_eq_of_lt (by omega)]
  · rw [if_neg h]
    rw [Nat.mod_eq_of_lt (by omega)]

lemma rev_mod_inj (m j i k : ℕ) (hm : 0 < m) (hj : j < m) (hi : i < m) (hk : k < m)
    (h : (j + 2 * m - 1 - i) % m = (j + 2 * m - 1 - k) % m) : i = k := by
  rw [rev_mod_formula m j i hm hj hi, rev_mod_formula m j k hm hj hk] at h
  by_cases h1 : i < j
  · by_cases h2 : k < j
    · rw [if_pos h1, if_pos h2] at h
      omega
    · rw [if_pos h1, if_neg h2] at h
      omega
  · by_cases h2 : k < j
    · rw [if_neg h1, if_pos h2] at h
      omega
    · rw [if_neg h1, if_neg h2] at h
      omega

lemma rot_diag_aux (m j t : ℕ) (P : ℕ → ℕ → Prop) (x y : ℕ → ℕ)
    (hm : 0 < m) (hj : j < m) (ht : t < m)
    (hPnext : ∀ i, i + 1 < m → P (x (i + 1)) (y i))
    (hPclose : P (x 0) (y (m - 1))) :
    P (x ((j + m - t) % m)) (y ((j + 2 * m - 1 - t) % m)) := by
  by_cases htj : t < j
  · have ha : (j + m - t) % m = j - t := by
      rw [rot_mod_formula m j t hm hj ht]
      rw [if_pos (by omega)]
    have hb : (j + 2 * m - 1 - t) % m = j - 1 - t := by
      rw [rev_mod_formula m j t hm hj ht]
      rw [if_pos htj]
    rw [ha, hb]
    have hi : (j - 1 - t) + 1 < m := by omega
    have h := hPnext (j - 1 - t) hi
    simpa [show (j - 1 - t) + 1 = j - t by omega] using h
  · by_cases htj' : t = j
    · subst t
      have ha : (j + m - j) % m = 0 := by
        have hEq : j + m - j = m := by omega
        rw [hEq, Nat.mod_self]
      have hb : (j + 2 * m - 1 - j) % m = m - 1 := by
        have hEq : j + 2 * m - 1 - j = (m - 1) + m := by omega
        rw [hEq, Nat.add_mod_right]
        rw [Nat.mod_eq_of_lt (by omega)]
      rw [ha, hb]
      exact hPclose
    · have hjt : j < t := by omega
      have ha : (j + m - t) % m = j + m - t := by
        rw [rot_mod_formula m j t hm hj ht]
        rw [if_neg (by omega)]
      have hb : (j + 2 * m - 1 - t) % m = j + m - 1 - t := by
        rw [rev_mod_formula m j t hm hj ht]
        rw [if_neg (by omega)]
      rw [ha, hb]
      have hi : (j + m - 1 - t) + 1 < m := by omega
      have h := hPnext (j + m - 1 - t) hi
      simpa [show (j + m - 1 - t) + 1 = j + m - t by omega] using h

theorem rotB : ∀ (m : ℕ) (P : ℕ → ℕ → Prop) (x y : ℕ → ℕ) (j : ℕ), 2 ≤ m → j < m → ((∀ i < m, x i < m) ∧ (∀ i < m, y i < m) ∧ (∀ i < m, ∀ j < m, x i = x j → i = j) ∧ (∀ i < m, ∀ j < m, y i = y j → i = j) ∧ (∀ i < m, P (x i) (y i)) ∧ (∀ i, i + 1 < m → P (x (i + 1)) (y i)) ∧ P (x 0) (y (m - 1))) →
  ∃ x' y' : ℕ → ℕ, ((∀ i < m, x' i < m) ∧ (∀ i < m, y' i < m) ∧ (∀ i < m, ∀ j < m, x' i = x' j → i = j) ∧ (∀ i < m, ∀ j < m, y' i = y' j → i = j) ∧ (∀ i < m, P (x' i) (y' i)) ∧ (∀ i, i + 1 < m → P (x' (i + 1)) (y' i)) ∧ P (x' 0) (y' (m - 1))) ∧ x' 0 = x j ∧ y' (m - 1) = y j := by
  intro m P x y j hm hj h
  rcases h with ⟨hxlt, hylt, hinjx, hinjy, hPdiag, hPnext, hPclose⟩
  have hm0 : 0 < m := by omega
  let x' : ℕ → ℕ := fun t => x ((j + m - t) % m)
  let y' : ℕ → ℕ := fun t => y ((j + 2 * m - 1 - t) % m)
  have hx'lt : ∀ i < m, x' i < m := by
    intro i hi
    dsimp [x']
    exact hxlt ((j + m - i) % m) (Nat.mod_lt _ hm0)
  have hy'lt : ∀ i < m, y' i < m := by
    intro i hi
    dsimp [y']
    exact hylt ((j + 2 * m - 1 - i) % m) (Nat.mod_lt _ hm0)
  have hx'inj : ∀ i < m, ∀ k < m, x' i = x' k → i = k := by
    intro i hi k hk hik
    dsimp [x'] at hik
    have hai : (j + m - i) % m < m := Nat.mod_lt _ hm0
    have hak : (j + m - k) % m < m := Nat.mod_lt _ hm0
    have hmod : (j + m - i) % m = (j + m - k) % m :=
      hinjx ((j + m - i) % m) hai ((j + m - k) % m) hak hik
    exact rot_mod_inj m j i k hm0 hj hi hk hmod
  have hy'inj : ∀ i < m, ∀ k < m, y' i = y' k → i = k := by
    intro i hi k hk hik
    dsimp [y'] at hik
    have hai : (j + 2 * m - 1 - i) % m < m := Nat.mod_lt _ hm0
    have hak : (j + 2 * m - 1 - k) % m < m := Nat.mod_lt _ hm0
    have hmod : (j + 2 * m - 1 - i) % m = (j + 2 * m - 1 - k) % m :=
      hinjy ((j + 2 * m - 1 - i) % m) hai ((j + 2 * m - 1 - k) % m) hak hik
    exact rev_mod_inj m j i k hm0 hj hi hk hmod
  have hdiag : ∀ i < m, P (x' i) (y' i) := by
    intro i hi
    dsimp [x', y']
    exact rot_diag_aux m j i P x y hm0 hj hi hPnext hPclose
  have hnext' : ∀ i, i + 1 < m → P (x' (i + 1)) (y' i) := by
    intro i hi
    dsimp [x', y']
    let c : ℕ := (j + m - 1 - i) % m
    have hc : c < m := by
      dsimp [c]
      exact Nat.mod_lt _ hm0
    have hxidx : (j + m - (i + 1)) % m = c := by
      dsimp [c]
      have hEq : j + m - (i + 1) = j + m - 1 - i := by omega
      rw [hEq]
    have hyidx : (j + 2 * m - 1 - i) % m = c := by
      dsimp [c]
      have hEq : j + 2 * m - 1 - i = (j + m - 1 - i) + m := by omega
      rw [hEq, Nat.add_mod_right]
    rw [hxidx, hyidx]
    exact hPdiag c hc
  have hx0mod : (j + m - 0) % m = j := by
    have hEq : j + m - 0 = j + m := by omega
    rw [hEq, Nat.add_mod_right]
    exact Nat.mod_eq_of_lt hj
  have hx0mod' : (j + m) % m = j := by
    have hEq : j + m = j + m := rfl
    rw [Nat.add_mod_right]
    exact Nat.mod_eq_of_lt hj
  have hymod : (j + 2 * m - 1 - (m - 1)) % m = j := by
    have hEq : j + 2 * m - 1 - (m - 1) = j + m := by omega
    rw [hEq, Nat.add_mod_right]
    exact Nat.mod_eq_of_lt hj
  have hclose' : P (x' 0) (y' (m - 1)) := by
    dsimp [x', y']
    rw [hx0mod', hymod]
    exact hPdiag j hj
  have hx0eq : x' 0 = x j := by
    dsimp [x']
    rw [hx0mod']
  have hylasteq : y' (m - 1) = y j := by
    dsimp [y']
    rw [hymod]
  refine ⟨x', y', ?_, ?_, ?_⟩
  · exact ⟨hx'lt, hy'lt, hx'inj, hy'inj, hdiag, hnext', hclose'⟩
  · exact hx0eq
  · exact hylasteq
end P_rotB
theorem rotB : ∀ (m : ℕ) (P : ℕ → ℕ → Prop) (x y : ℕ → ℕ) (j : ℕ), 2 ≤ m → j < m → ((∀ i < m, x i < m) ∧ (∀ i < m, y i < m) ∧ (∀ i < m, ∀ j < m, x i = x j → i = j) ∧ (∀ i < m, ∀ j < m, y i = y j → i = j) ∧ (∀ i < m, P (x i) (y i)) ∧ (∀ i, i + 1 < m → P (x (i + 1)) (y i)) ∧ P (x 0) (y (m - 1))) →
  ∃ x' y' : ℕ → ℕ, ((∀ i < m, x' i < m) ∧ (∀ i < m, y' i < m) ∧ (∀ i < m, ∀ j < m, x' i = x' j → i = j) ∧ (∀ i < m, ∀ j < m, y' i = y' j → i = j) ∧ (∀ i < m, P (x' i) (y' i)) ∧ (∀ i, i + 1 < m → P (x' (i + 1)) (y' i)) ∧ P (x' 0) (y' (m - 1))) ∧ x' 0 = x j ∧ y' (m - 1) = y j := P_rotB.rotB
namespace P_pigeon
open Classical

lemma natCard_subtype_lt (m : ℕ) (Q : ℕ → Prop) :
    Nat.card {b : ℕ // b < m ∧ Q b} =
      (Finset.univ.filter (fun i : Fin m => Q i.val)).card := by
  classical
  let e : {b : ℕ // b < m ∧ Q b} ≃ {i : Fin m // Q i.val} :=
  { toFun := fun b => ⟨⟨b.1, b.2.1⟩, b.2.2⟩
    invFun := fun i => ⟨i.1.1, ⟨i.1.2, i.2⟩⟩
    left_inv := by
      intro b
      apply Subtype.ext
      rfl
    right_inv := by
      intro i
      apply Subtype.ext
      apply Fin.ext
      rfl }
  calc
    Nat.card {b : ℕ // b < m ∧ Q b} = Nat.card {i : Fin m // Q i.val} :=
      Nat.card_congr e
    _ = (Finset.univ.filter (fun i : Fin m => Q i.val)).card := by
      rw [Nat.card_eq_fintype_card]
      simp [Fintype.card_subtype]

lemma surj_of_inj_fin {m : ℕ} {f : Fin m → Fin m}
    (hf : Function.Injective f) : Function.Surjective f := by
  classical
  have hcard : ((Finset.univ : Finset (Fin m)).image f).card =
      (Finset.univ : Finset (Fin m)).card := by
    apply Finset.card_image_of_injOn
    intro a _ b _ hab
    exact hf hab
  have hEq : ((Finset.univ : Finset (Fin m)).image f) =
      (Finset.univ : Finset (Fin m)) := by
    apply Finset.eq_of_subset_of_card_le
    · intro z _
      simp
    · rw [hcard]
  intro b
  have hb : b ∈ ((Finset.univ : Finset (Fin m)).image f) := by
    rw [hEq]
    simp
  rcases Finset.mem_image.mp hb with ⟨a, _, hfa⟩
  exact ⟨a, hfa⟩

lemma card_filter_comp_of_inj_fin {m : ℕ} (f : Fin m → Fin m)
    (hf : Function.Injective f) (Q : ℕ → Prop) :
    (Finset.univ.filter (fun i : Fin m => Q (f i).val)).card =
      (Finset.univ.filter (fun b : Fin m => Q b.val)).card := by
  classical
  have hsurj : Function.Surjective f := surj_of_inj_fin hf
  refine Finset.card_bij (fun i _ => f i) ?_ ?_ ?_
  · intro i hi
    rw [Finset.mem_filter] at hi ⊢
    exact ⟨by simp, hi.2⟩
  · intro i _ j _ hij
    exact hf hij
  · intro b hb
    rw [Finset.mem_filter] at hb
    rcases hsurj b with ⟨i, rfl⟩
    refine ⟨i, ?_, rfl⟩
    rw [Finset.mem_filter]
    exact ⟨by simp, hb.2⟩

theorem pigeon : ∀ (m : ℕ) (P : ℕ → ℕ → Prop) (x y : ℕ → ℕ), 2 ≤ m → (∀ i < m, x i < m) → (∀ i < m, y i < m) → (∀ i < m, ∀ j < m, x i = x j → i = j) → (∀ i < m, ∀ j < m, y i = y j → i = j) →
  ¬ P (x 0) (y (m - 1)) → m < Nat.card {b : ℕ // b < m ∧ P (x 0) b} + Nat.card {a : ℕ // a < m ∧ P a (y (m - 1))} →
  ∃ i, i + 1 < m ∧ P (x 0) (y i) ∧ P (x i) (y (m - 1)) := by
  intro m P x y h2 hxlt hylt hxinj hyinj hnot hlarge
  classical
  let gy : Fin m → Fin m := fun i => ⟨y i.val, hylt i.val i.isLt⟩
  have hgy_inj : Function.Injective gy := by
    intro i j h
    apply Fin.ext
    apply hyinj i.val i.isLt j.val j.isLt
    have := congrArg (fun z : Fin m => z.val) h
    simpa [gy] using this
  let gx : Fin m → Fin m := fun i => ⟨x i.val, hxlt i.val i.isLt⟩
  have hgx_inj : Function.Injective gx := by
    intro i j h
    apply Fin.ext
    apply hxinj i.val i.isLt j.val j.isLt
    have := congrArg (fun z : Fin m => z.val) h
    simpa [gx] using this
  let I : Finset (Fin m) :=
    Finset.univ.filter (fun i : Fin m => P (x 0) (y i.val))
  let J : Finset (Fin m) :=
    Finset.univ.filter (fun i : Fin m => P (x i.val) (y (m - 1)))
  let B : Finset (Fin m) :=
    Finset.univ.filter (fun b : Fin m => P (x 0) b.val)
  let C : Finset (Fin m) :=
    Finset.univ.filter (fun a : Fin m => P a.val (y (m - 1)))
  have hI_card : I.card = B.card := by
    have h := card_filter_comp_of_inj_fin gy hgy_inj (fun b => P (x 0) b)
    simpa [I, B, gy] using h
  have hJ_card : J.card = C.card := by
    have h := card_filter_comp_of_inj_fin gx hgx_inj (fun a => P a (y (m - 1)))
    simpa [J, C, gx] using h
  have hNatB : Nat.card {b : ℕ // b < m ∧ P (x 0) b} = B.card := by
    simpa [B] using natCard_subtype_lt m (fun b => P (x 0) b)
  have hNatC : Nat.card {a : ℕ // a < m ∧ P a (y (m - 1))} = C.card := by
    simpa [C] using natCard_subtype_lt m (fun a => P a (y (m - 1)))
  have hNatI : Nat.card {b : ℕ // b < m ∧ P (x 0) b} = I.card := by
    rw [hNatB]
    exact hI_card.symm
  have hNatJ : Nat.card {a : ℕ // a < m ∧ P a (y (m - 1))} = J.card := by
    rw [hNatC]
    exact hJ_card.symm
  have hlarge' : m < I.card + J.card := by
    simpa [hNatI, hNatJ] using hlarge
  have hinter_nonempty : (I ∩ J).Nonempty := by
    apply Finset.card_pos.mp
    by_contra hnotpos
    have hzero : (I ∩ J).card = 0 := by omega
    have hempty : I ∩ J = ∅ := Finset.card_eq_zero.mp hzero
    have hdisj : Disjoint I J := by
      rw [Finset.disjoint_iff_inter_eq_empty]
      exact hempty
    have hcard_union : (I ∪ J).card = I.card + J.card :=
      Finset.card_union_of_disjoint hdisj
    have hUnionSub : I ∪ J ⊆ (Finset.univ : Finset (Fin m)) := by
      intro z _
      simp
    have hcard_union_le : (I ∪ J).card ≤ (Finset.univ : Finset (Fin m)).card :=
      Finset.card_le_card hUnionSub
    have hcard_univ : (Finset.univ : Finset (Fin m)).card = m := by
      simp
    have hsum_le : I.card + J.card ≤ m := by
      calc
        I.card + J.card = (I ∪ J).card := hcard_union.symm
        _ ≤ (Finset.univ : Finset (Fin m)).card := hcard_union_le
        _ = m := hcard_univ
    exact (not_lt_of_ge hsum_le) hlarge'
  rcases hinter_nonempty with ⟨i, hi_mem⟩
  have hiI : i ∈ I := (Finset.mem_inter.mp hi_mem).1
  have hiJ : i ∈ J := (Finset.mem_inter.mp hi_mem).2
  have hi_pred1 : P (x 0) (y i.val) := by
    have hmem : i ∈ Finset.univ.filter (fun i : Fin m => P (x 0) (y i.val)) := by
      simpa [I] using hiI
    exact (Finset.mem_filter.mp hmem).2
  have hi_pred2 : P (x i.val) (y (m - 1)) := by
    have hmem : i ∈ Finset.univ.filter (fun i : Fin m => P (x i.val) (y (m - 1))) := by
      simpa [J] using hiJ
    exact (Finset.mem_filter.mp hmem).2
  have hne : i.val ≠ m - 1 := by
    intro heq
    apply hnot
    have hPeq : P (x 0) (y (m - 1)) := by
      simpa [heq] using hi_pred1
    exact hPeq
  have hlt_i : i.val + 1 < m := by
    have hi_lt : i.val < m := i.isLt
    omega
  exact ⟨i.val, hlt_i, hi_pred1, hi_pred2⟩
end P_pigeon
theorem pigeon : ∀ (m : ℕ) (P : ℕ → ℕ → Prop) (x y : ℕ → ℕ), 2 ≤ m → (∀ i < m, x i < m) → (∀ i < m, y i < m) → (∀ i < m, ∀ j < m, x i = x j → i = j) → (∀ i < m, ∀ j < m, y i = y j → i = j) →
  ¬ P (x 0) (y (m - 1)) → m < Nat.card {b : ℕ // b < m ∧ P (x 0) b} + Nat.card {a : ℕ // a < m ∧ P a (y (m - 1))} →
  ∃ i, i + 1 < m ∧ P (x 0) (y i) ∧ P (x i) (y (m - 1)) := P_pigeon.pigeon
namespace P_cycle
noncomputable def cycleMissingPairs (m : ℕ) (E : ℕ → ℕ → Prop) : Finset (ℕ × ℕ) := by
  classical
  exact (Finset.product (Finset.range m) (Finset.range m)).filter (fun p => ¬ E p.1 p.2)

noncomputable def cycleMissingCard (m : ℕ) (E : ℕ → ℕ → Prop) : ℕ :=
  (cycleMissingPairs m E).card

lemma cycle_mem_missingPairs {m : ℕ} {E : ℕ → ℕ → Prop} {p : ℕ × ℕ} :
    p ∈ cycleMissingPairs m E ↔ p.1 < m ∧ p.2 < m ∧ ¬ E p.1 p.2 := by
  classical
  simp [cycleMissingPairs, Finset.mem_product, and_assoc]

lemma cycle_close_added
    (m : ℕ) (F : ℕ → ℕ → Prop) (a b : ℕ) (x y : ℕ → ℕ)
    (hm : 2 ≤ m) (ha : a < m) (hb : b < m) (hnotFab : ¬ F a b)
    (hF : ∀ i < m, ∀ j < m, ¬ F i j →
      m < Nat.card {c : ℕ // c < m ∧ F i c} +
          Nat.card {c : ℕ // c < m ∧ F c j})
    (hxlt : ∀ i < m, x i < m)
    (hylt : ∀ i < m, y i < m)
    (hxinj : ∀ i < m, ∀ j < m, x i = x j → i = j)
    (hyinj : ∀ i < m, ∀ j < m, y i = y j → i = j)
    (hdiag : ∀ i < m, F (x i) (y i) ∨ (x i = a ∧ y i = b))
    (hsucc : ∀ i, i + 1 < m → F (x (i + 1)) (y i) ∨ (x (i + 1) = a ∧ y i = b))
    (hx0 : x 0 = a) (hyclose : y (m - 1) = b) :
    ∃ x' y' : ℕ → ℕ,
      (∀ i < m, x' i < m) ∧
      (∀ i < m, y' i < m) ∧
      (∀ i < m, ∀ j < m, x' i = x' j → i = j) ∧
      (∀ i < m, ∀ j < m, y' i = y' j → i = j) ∧
      (∀ i < m, F (x' i) (y' i)) ∧
      (∀ i, i + 1 < m → F (x' (i + 1)) (y' i)) ∧
      F (x' 0) (y' (m - 1)) := by
  have h0 : 0 < m := by omega
  have hmlt : m - 1 < m := by omega
  have hdiagF : ∀ i < m, F (x i) (y i) := by
    intro i hi
    rcases hdiag i hi with h | ⟨hxa, hyb⟩
    · exact h
    · exfalso
      have hi0 : i = 0 := by
        apply hxinj i hi 0 h0
        exact hxa.trans hx0.symm
      have him : i = m - 1 := by
        apply hyinj i hi (m - 1) hmlt
        exact hyb.trans hyclose.symm
      omega
  have hsuccF : ∀ i, i + 1 < m → F (x (i + 1)) (y i) := by
    intro i hi1
    rcases hsucc i hi1 with h | ⟨hxa, hyb⟩
    · exact h
    · exfalso
      have hix : i + 1 = 0 := by
        apply hxinj (i + 1) hi1 0 h0
        exact hxa.trans hx0.symm
      omega
  have hnotClose : ¬ F (x 0) (y (m - 1)) := by
    simpa [hx0, hyclose] using hnotFab
  have hcard : m < Nat.card {c : ℕ // c < m ∧ F (x 0) c} +
      Nat.card {c : ℕ // c < m ∧ F c (y (m - 1))} := by
    simpa [hx0, hyclose] using hF a ha b hb hnotFab
  obtain ⟨i, hi1, hleft, hright⟩ :=
    pigeon m F x y hm hxlt hylt hxinj hyinj hnotClose hcard
  exact cross m F x y hm hxlt hylt hxinj hyinj hdiagF hsuccF
    ⟨i, hi1, hleft, hright⟩

theorem cycle : ∀ (m : ℕ), 2 ≤ m → ∀ (E : ℕ → ℕ → Prop), (∀ i < m, ∀ j < m, ¬ E i j → m < Nat.card {b : ℕ // b < m ∧ E i b} + Nat.card {a : ℕ // a < m ∧ E a j}) →
  ∃ x y : ℕ → ℕ, (∀ i < m, x i < m) ∧ (∀ i < m, y i < m) ∧ (∀ i < m, ∀ j < m, x i = x j → i = j) ∧ (∀ i < m, ∀ j < m, y i = y j → i = j) ∧
    (∀ i < m, E (x i) (y i)) ∧ (∀ i, i + 1 < m → E (x (i + 1)) (y i)) ∧ E (x 0) (y (m - 1)) := by
  intro m hm E hE
  classical
  have hmain : ∀ n : ℕ, ∀ F : ℕ → ℕ → Prop,
      cycleMissingCard m F = n →
      (∀ i < m, ∀ j < m, ¬ F i j →
        m < Nat.card {b : ℕ // b < m ∧ F i b} +
            Nat.card {a : ℕ // a < m ∧ F a j}) →
      ∃ x y : ℕ → ℕ,
        (∀ i < m, x i < m) ∧
        (∀ i < m, y i < m) ∧
        (∀ i < m, ∀ j < m, x i = x j → i = j) ∧
        (∀ i < m, ∀ j < m, y i = y j → i = j) ∧
        (∀ i < m, F (x i) (y i)) ∧
        (∀ i, i + 1 < m → F (x (i + 1)) (y i)) ∧
        F (x 0) (y (m - 1)) := by
    intro n
    induction n using Nat.strong_induction_on with
    | ind n ih =>
      intro F hn hF
      by_cases hzero : n = 0
      · have hcard0 : cycleMissingCard m F = 0 := by
          simpa [hzero] using hn
        have hnone : ∀ i < m, ∀ j < m, F i j := by
          intro i hi j hj
          by_contra hnot
          have hpair : (i, j) ∈ cycleMissingPairs m F := by
            rw [cycle_mem_missingPairs]
            exact ⟨hi, hj, hnot⟩
          have hpos : 0 < (cycleMissingPairs m F).card :=
            Finset.card_pos.mpr ⟨(i, j), hpair⟩
          have hcard0' : (cycleMissingPairs m F).card = 0 := by
            simpa [cycleMissingCard] using hcard0
          rw [hcard0'] at hpos
          omega
        refine ⟨fun i => i, fun i => i, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
        · intro i hi
          exact hi
        · intro i hi
          exact hi
        · intro i hi j hj h
          exact h
        · intro i hi j hj h
          exact h
        · intro i hi
          exact hnone i hi i hi
        · intro i hi1
          have hi : i < m := by omega
          exact hnone (i + 1) hi1 i hi
        · have h0 : 0 < m := by omega
          have hmlt : m - 1 < m := by omega
          exact hnone 0 h0 (m - 1) hmlt
      · have hnpos : 0 < n := Nat.pos_of_ne_zero hzero
        have hposCard : 0 < (cycleMissingPairs m F).card := by
          have : 0 < cycleMissingCard m F := by
            rw [hn]
            exact hnpos
          simpa [cycleMissingCard] using this
        obtain ⟨p, hp⟩ := Finset.card_pos.mp hposCard
        rcases p with ⟨a, b⟩
        have hp_prop := cycle_mem_missingPairs.mp hp
        have ha : a < m := hp_prop.1
        have hb : b < m := hp_prop.2.1
        have hnotFab : ¬ F a b := hp_prop.2.2
        let F' : ℕ → ℕ → Prop := fun i j => F i j ∨ (i = a ∧ j = b)
        have hsub : cycleMissingPairs m F' ⊆ cycleMissingPairs m F := by
          intro q hq
          rw [cycle_mem_missingPairs] at hq ⊢
          exact ⟨hq.1, hq.2.1, by
            intro hFq
            exact hq.2.2 (Or.inl hFq)⟩
        have hpairmem : (a, b) ∈ cycleMissingPairs m F := by
          rw [cycle_mem_missingPairs]
          exact ⟨ha, hb, hnotFab⟩
        have hnotmem : (a, b) ∉ cycleMissingPairs m F' := by
          rw [cycle_mem_missingPairs]
          intro hq
          exact hq.2.2 (Or.inr ⟨rfl, rfl⟩)
        have hssub : cycleMissingPairs m F' ⊂ cycleMissingPairs m F := by
          rw [Finset.ssubset_iff_subset_ne]
          refine ⟨hsub, ?_⟩
          intro heq
          have : (a, b) ∈ cycleMissingPairs m F' := by
            rw [heq]
            exact hpairmem
          exact hnotmem this
        have hlt : cycleMissingCard m F' < n := by
          have hcardlt : (cycleMissingPairs m F').card < (cycleMissingPairs m F).card :=
            Finset.card_lt_card hssub
          have hcardF : (cycleMissingPairs m F).card = n := by
            simpa [cycleMissingCard] using hn
          have hlt' : (cycleMissingPairs m F').card < n := by
            simpa [hcardF] using hcardlt
          simpa [cycleMissingCard] using hlt'
        have hF' : ∀ i < m, ∀ j < m, ¬ F' i j →
            m < Nat.card {c : ℕ // c < m ∧ F' i c} +
                Nat.card {c : ℕ // c < m ∧ F' c j} := by
          intro i hi j hj hnotF'
          have hnotF : ¬ F i j := by
            intro h
            exact hnotF' (Or.inl h)
          have hbase := hF i hi j hj hnotF
          have hle1 : Nat.card {c : ℕ // c < m ∧ F i c} ≤
              Nat.card {c : ℕ // c < m ∧ F' i c} := by
            haveI hfin1 : Finite {c : ℕ // c < m ∧ F i c} := by
              exact Finite.of_injective
                (fun c : {c : ℕ // c < m ∧ F i c} => (⟨c.1, c.2.1⟩ : Fin m))
                (by
                  intro u v huv
                  apply Subtype.ext
                  exact congrArg Fin.val huv)
            haveI hfin2 : Finite {c : ℕ // c < m ∧ F' i c} := by
              exact Finite.of_injective
                (fun c : {c : ℕ // c < m ∧ F' i c} => (⟨c.1, c.2.1⟩ : Fin m))
                (by
                  intro u v huv
                  apply Subtype.ext
                  exact congrArg Fin.val huv)
            let f : {c : ℕ // c < m ∧ F i c} → {c : ℕ // c < m ∧ F' i c} :=
              fun c => ⟨c.1, by
                refine ⟨c.2.1, ?_⟩
                dsimp [F']
                exact Or.inl c.2.2⟩
            exact Nat.card_le_card_of_injective f (by
              intro u v huv
              have h := congrArg Subtype.val huv
              exact Subtype.ext h)
          have hle2 : Nat.card {c : ℕ // c < m ∧ F c j} ≤
              Nat.card {c : ℕ // c < m ∧ F' c j} := by
            haveI hfin3 : Finite {c : ℕ // c < m ∧ F c j} := by
              exact Finite.of_injective
                (fun c : {c : ℕ // c < m ∧ F c j} => (⟨c.1, c.2.1⟩ : Fin m))
                (by
                  intro u v huv
                  apply Subtype.ext
                  exact congrArg Fin.val huv)
            haveI hfin4 : Finite {c : ℕ // c < m ∧ F' c j} := by
              exact Finite.of_injective
                (fun c : {c : ℕ // c < m ∧ F' c j} => (⟨c.1, c.2.1⟩ : Fin m))
                (by
                  intro u v huv
                  apply Subtype.ext
                  exact congrArg Fin.val huv)
            let f : {c : ℕ // c < m ∧ F c j} → {c : ℕ // c < m ∧ F' c j} :=
              fun c => ⟨c.1, by
                refine ⟨c.2.1, ?_⟩
                dsimp [F']
                exact Or.inl c.2.2⟩
            exact Nat.card_le_card_of_injective f (by
              intro u v huv
              have h := congrArg Subtype.val huv
              exact Subtype.ext h)
          exact lt_of_lt_of_le hbase (Nat.add_le_add hle1 hle2)
        obtain ⟨x, y, hxlt, hylt, hxinj, hyinj, hdiag', hsucc', hclose'⟩ :=
          ih (cycleMissingCard m F') hlt F' rfl hF'
        by_cases hOcc :
            (∃ i, i < m ∧ x i = a ∧ y i = b) ∨
            (∃ i, i + 1 < m ∧ x (i + 1) = a ∧ y i = b) ∨
            (x 0 = a ∧ y (m - 1) = b)
        · rcases hOcc with hdiagOcc | hrest
          · obtain ⟨j, hj, hja, hjb⟩ := hdiagOcc
            obtain ⟨x', y', hcyc, hx0', hyclose'⟩ :=
              rotB m F' x y j hm hj
                ⟨hxlt, hylt, hxinj, hyinj, hdiag', hsucc', hclose'⟩
            rcases hcyc with ⟨hxlt', hylt', hxinj', hyinj', hdiag'', hsucc'', hclose''⟩
            have hx0 : x' 0 = a := hx0'.trans hja
            have hyclose : y' (m - 1) = b := hyclose'.trans hjb
            have hdiagE : ∀ i < m, F (x' i) (y' i) ∨ (x' i = a ∧ y' i = b) := by
              intro i hi
              simpa [F'] using hdiag'' i hi
            have hsuccE : ∀ i, i + 1 < m →
                F (x' (i + 1)) (y' i) ∨ (x' (i + 1) = a ∧ y' i = b) := by
              intro i hi1
              simpa [F'] using hsucc'' i hi1
            exact cycle_close_added m F a b x' y' hm ha hb hnotFab hF
              hxlt' hylt' hxinj' hyinj' hdiagE hsuccE hx0 hyclose
          · rcases hrest with hsuccOcc | hcloseOcc
            · obtain ⟨j, hj1, hja, hjb⟩ := hsuccOcc
              obtain ⟨x', y', hcyc, hx0', hyclose'⟩ :=
                rotA m F' x y j hm hj1
                  ⟨hxlt, hylt, hxinj, hyinj, hdiag', hsucc', hclose'⟩
              rcases hcyc with ⟨hxlt', hylt', hxinj', hyinj', hdiag'', hsucc'', hclose''⟩
              have hx0 : x' 0 = a := hx0'.trans hja
              have hyclose : y' (m - 1) = b := hyclose'.trans hjb
              have hdiagE : ∀ i < m, F (x' i) (y' i) ∨ (x' i = a ∧ y' i = b) := by
                intro i hi
                simpa [F'] using hdiag'' i hi
              have hsuccE : ∀ i, i + 1 < m →
                  F (x' (i + 1)) (y' i) ∨ (x' (i + 1) = a ∧ y' i = b) := by
                intro i hi1
                simpa [F'] using hsucc'' i hi1
              exact cycle_close_added m F a b x' y' hm ha hb hnotFab hF
                hxlt' hylt' hxinj' hyinj' hdiagE hsuccE hx0 hyclose
            · obtain ⟨hx0, hyclose⟩ := hcloseOcc
              have hdiagE : ∀ i < m, F (x i) (y i) ∨ (x i = a ∧ y i = b) := by
                intro i hi
                simpa [F'] using hdiag' i hi
              have hsuccE : ∀ i, i + 1 < m →
                  F (x (i + 1)) (y i) ∨ (x (i + 1) = a ∧ y i = b) := by
                intro i hi1
                simpa [F'] using hsucc' i hi1
              exact cycle_close_added m F a b x y hm ha hb hnotFab hF
                hxlt hylt hxinj hyinj hdiagE hsuccE hx0 hyclose
        · have hdiagE : ∀ i < m, F (x i) (y i) := by
            intro i hi
            have hd : F (x i) (y i) ∨ (x i = a ∧ y i = b) := by
              simpa [F'] using hdiag' i hi
            rcases hd with hFedge | hpair
            · exact hFedge
            · exfalso
              apply hOcc
              left
              exact ⟨i, hi, hpair.1, hpair.2⟩
          have hsuccE : ∀ i, i + 1 < m → F (x (i + 1)) (y i) := by
            intro i hi1
            have hs : F (x (i + 1)) (y i) ∨ (x (i + 1) = a ∧ y i = b) := by
              simpa [F'] using hsucc' i hi1
            rcases hs with hFedge | hpair
            · exact hFedge
            · exfalso
              apply hOcc
              right
              left
              exact ⟨i, hi1, hpair.1, hpair.2⟩
          have hcloseE : F (x 0) (y (m - 1)) := by
            have hc : F (x 0) (y (m - 1)) ∨ (x 0 = a ∧ y (m - 1) = b) := by
              simpa [F'] using hclose'
            rcases hc with hFedge | hpair
            · exact hFedge
            · exfalso
              apply hOcc
              right
              right
              exact hpair
          exact ⟨x, y, hxlt, hylt, hxinj, hyinj, hdiagE, hsuccE, hcloseE⟩
  exact hmain (cycleMissingCard m E) E rfl hE
end P_cycle
theorem cycle : ∀ (m : ℕ), 2 ≤ m → ∀ (E : ℕ → ℕ → Prop), (∀ i < m, ∀ j < m, ¬ E i j → m < Nat.card {b : ℕ // b < m ∧ E i b} + Nat.card {a : ℕ // a < m ∧ E a j}) →
  ∃ x y : ℕ → ℕ, (∀ i < m, x i < m) ∧ (∀ i < m, y i < m) ∧ (∀ i < m, ∀ j < m, x i = x j → i = j) ∧ (∀ i < m, ∀ j < m, y i = y j → i = j) ∧
    (∀ i < m, E (x i) (y i)) ∧ (∀ i, i + 1 < m → E (x (i + 1)) (y i)) ∧ E (x 0) (y (m - 1)) := P_cycle.cycle
namespace P_listpath
theorem listpath : ∀ (V : Type) [Fintype V] [DecidableEq V] (G : SimpleGraph V) (n : ℕ) (f : ℕ → V),
    n + 1 = Fintype.card V →
    (∀ i ≤ n, ∀ j ≤ n, f i = f j → i = j) →
    (∀ i < n, G.Adj (f i) (f (i + 1))) →
    ∃ (a b : V) (p : G.Walk a b), p.IsHamiltonian := by
  intro V hF hD G n f hcard hinj hadj
  have chain : ∀ (m : ℕ) (g : ℕ → V),
      (∀ i < m, G.Adj (g i) (g (i + 1))) →
      ∃ p : G.Walk (g 0) (g m), p.support = (List.range (m + 1)).map g := by
    intro m
    induction m with
    | zero =>
        intro g h
        refine ⟨SimpleGraph.Walk.nil, ?_⟩
        simp
    | succ n ih =>
        intro g h
        have h' : ∀ i < n, G.Adj (g i) (g (i + 1)) := by
          intro i hi
          exact h i (Nat.lt_trans hi (Nat.lt_succ_self n))
        rcases ih g h' with ⟨p, hp⟩
        refine ⟨p.concat (h n (Nat.lt_succ_self n)), ?_⟩
        rw [SimpleGraph.Walk.support_concat, hp]
        have hrange : (List.range (Nat.succ n + 1)).map g =
            (List.range (n + 1)).map g ++ [g (n + 1)] := by
          calc
            (List.range (Nat.succ n + 1)).map g
                = (List.range (Nat.succ n) ++ [Nat.succ n]).map g := by
                    rw [List.range_succ]
            _ = (List.range (n + 1)).map g ++ [g (n + 1)] := by
                    simp [List.map_append, Nat.succ_eq_add_one]
        rw [hrange]
  rcases chain n f hadj with ⟨p, hp⟩
  have hnodup : p.support.Nodup := by
    rw [hp]
    have hnodup_range : (List.range (n + 1)).Nodup := List.nodup_range
    have hinj' : ∀ x ∈ List.range (n + 1), ∀ y ∈ List.range (n + 1),
        f x = f y → x = y := by
      intro i hi j hj hij
      have hi' : i ≤ n := by
        have hi_lt : i < n + 1 := List.mem_range.mp hi
        omega
      have hj' : j ≤ n := by
        have hj_lt : j < n + 1 := List.mem_range.mp hj
        omega
      exact hinj i hi' j hj' hij
    exact List.Nodup.map_on hinj' hnodup_range
  have hlen : p.support.length = n + 1 := by
    rw [hp]
    simp
  have hcard_support : p.support.toFinset.card = Fintype.card V := by
    rw [List.toFinset_card_of_nodup hnodup]
    rw [hlen, hcard]
  have huniv : p.support.toFinset = Finset.univ :=
    Finset.eq_univ_of_card p.support.toFinset hcard_support
  refine ⟨f 0, f n, p, ?_⟩
  intro a
  have ha : a ∈ p.support.toFinset := by rw [huniv]; exact Finset.mem_univ a
  exact List.count_eq_one_of_mem hnodup (List.mem_toFinset.mp ha)
end P_listpath
theorem listpath : ∀ (V : Type) [Fintype V] [DecidableEq V] (G : SimpleGraph V) (n : ℕ) (f : ℕ → V), n + 1 = Fintype.card V → (∀ i ≤ n, ∀ j ≤ n, f i = f j → i = j) →
  (∀ i < n, G.Adj (f i) (f (i + 1))) → ∃ (a b : V) (p : G.Walk a b), p.IsHamiltonian := P_listpath.listpath
namespace P_smallk
lemma exists_edge_out {V : Type} (G : SimpleGraph V) (s : List V) :
    ∀ {x y : V} (w : G.Walk x y), x ∈ s → y ∉ s → ∃ u v : V, u ∈ s ∧ v ∉ s ∧ G.Adj u v := by
  intro x y w
  induction w with
  | nil => intro hx hy; exact absurd hx hy
  | @cons a b c h q ih =>
      intro hx hy
      by_cases hb : b ∈ s
      · exact ih hb hy
      · exact ⟨a, b, hx, hb, h⟩

lemma two_nbrs {V : Type} {G : SimpleGraph V} :
    ∀ {a b : V} (p : G.Walk a b) {u : V}, p.IsPath → u ∈ p.support → u ≠ a → u ≠ b →
      ∃ x y : V, x ∈ p.support ∧ y ∈ p.support ∧ x ≠ y ∧ G.Adj x u ∧ G.Adj u y := by
  intro a b p
  induction p with
  | nil =>
      intro u _ hu ha _
      simp at hu
      exact absurd hu ha
  | @cons a' v b' h q ih =>
      intro u hp hu ha hb
      rw [SimpleGraph.Walk.cons_isPath_iff] at hp
      obtain ⟨hq, ha'q⟩ := hp
      rw [SimpleGraph.Walk.support_cons, List.mem_cons] at hu
      rcases hu with hu | hu
      · exact absurd hu ha
      · by_cases huv : u = v
        · subst huv
          cases q with
          | nil => exact absurd rfl hb
          | @cons _ y _ h2 q2 =>
              refine ⟨a', y, by simp, by simp, ?_, h, h2⟩
              intro hay
              apply ha'q
              rw [hay]
              simp
        · obtain ⟨x, y, hx, hy, hxy, hxu, huy⟩ := ih hq hu huv hb
          exact ⟨x, y, by simp [hx], by simp [hy], hxy, hxu, huy⟩

theorem smallk : ∀ (V : Type) [Fintype V] [DecidableEq V] (G : SimpleGraph V) (N : V → Finset V), (∀ x y, y ∈ N x ↔ G.Adj x y) → G.Connected → (∀ x, (N x).card ≤ 2) →
  ∃ (a b : V) (p : G.Walk a b), p.IsHamiltonian := by
  classical
  intro V instF instDec G N hN hconn hdeg
  have hne : Nonempty V := hconn.nonempty
  let P : ℕ → Prop := fun n =>
    ∃ (a b : V) (p : G.Walk a b), p.IsPath ∧ p.length = n
  let S : Finset ℕ := (Finset.range (Fintype.card V + 1)).filter P
  have hP0 : P 0 := by
    rcases hne with ⟨a0⟩
    refine ⟨a0, a0, SimpleGraph.Walk.nil, ?_, ?_⟩
    · simp [SimpleGraph.Walk.IsPath]
    · simp
  have hS : S.Nonempty := by
    refine ⟨0, ?_⟩
    dsimp [S]
    exact Finset.mem_filter.mpr ⟨by simp, hP0⟩
  let m := S.max' hS
  have hmS : m ∈ S := by
    dsimp [m]
    exact Finset.max'_mem S hS
  have hmP : P m := by
    have hmS' : m ∈ (Finset.range (Fintype.card V + 1)).filter P := by
      simpa [S] using hmS
    exact (Finset.mem_filter.mp hmS').2
  have hmax : ∀ {a b : V} (q : G.Walk a b), q.IsPath → q.length ≤ m := by
    intro a' b' q hq
    have hqS : q.length ∈ S := by
      dsimp [S]
      refine Finset.mem_filter.mpr ⟨?_, ?_⟩
      · rw [Finset.mem_range]
        have hlt : q.length < Fintype.card V := hq.length_lt
        omega
      · exact ⟨a', b', q, hq, rfl⟩
    dsimp [m]
    exact Finset.le_max' S q.length hqS
  rcases hmP with ⟨a, b, p, hp, hplen⟩
  have hspan : ∀ x, x ∈ p.support := by
    intro v
    by_contra hv
    have ha_mem : a ∈ p.support := p.start_mem_support
    rcases hconn.preconnected a v with ⟨q⟩
    have hcross := exists_edge_out G p.support q ha_mem hv
    rcases hcross with ⟨u, w, hu_mem, hw_not, hadj⟩
    by_cases hua : u = a
    · subst u
      let qnew := SimpleGraph.Walk.cons hadj.symm p
      have hqpath : qnew.IsPath := by
        dsimp [qnew]
        exact (SimpleGraph.Walk.cons_isPath_iff _ _).mpr ⟨hp, hw_not⟩
      have hqlen : qnew.length = m + 1 := by
        dsimp [qnew]
        simp [hplen]
      have hle := hmax qnew hqpath
      omega
    · by_cases hub : u = b
      · subst u
        let qnew := SimpleGraph.Walk.cons hadj.symm p.reverse
        have hp_rev : p.reverse.IsPath := hp.reverse
        have hw_rev : w ∉ p.reverse.support := by
          simpa using hw_not
        have hqpath : qnew.IsPath := by
          dsimp [qnew]
          exact (SimpleGraph.Walk.cons_isPath_iff _ _).mpr ⟨hp_rev, hw_rev⟩
        have hqlen : qnew.length = m + 1 := by
          dsimp [qnew]
          simp [hplen]
        have hle := hmax qnew hqpath
        omega
      · have htwo := two_nbrs p hp hu_mem hua hub
        rcases htwo with ⟨x, y, hx_mem, hy_mem, hxy, hxu, huy⟩
        have hxN : x ∈ N u := (hN u x).2 hxu.symm
        have hyN : y ∈ N u := (hN u y).2 huy
        have hwN : w ∈ N u := (hN u w).2 hadj
        have hxw : x ≠ w := by
          intro hxw
          apply hw_not
          simpa [hxw] using hx_mem
        have hyw : y ≠ w := by
          intro hyw
          apply hw_not
          simpa [hyw] using hy_mem
        have hsub : ({x, y, w} : Finset V) ⊆ N u := by
          intro z hz
          simp only [Finset.mem_insert, Finset.mem_singleton] at hz
          rcases hz with hz | hz | hz
          · subst z
            exact hxN
          · subst z
            exact hyN
          · subst z
            exact hwN
        have hcard : ({x, y, w} : Finset V).card = 3 := by
          exact Finset.card_eq_three.mpr ⟨x, y, w, hxy, hxw, hyw, rfl⟩
        have h3le : 3 ≤ (N u).card := by
          calc
            3 = ({x, y, w} : Finset V).card := hcard.symm
            _ ≤ (N u).card := Finset.card_le_card hsub
        have h2 := hdeg u
        omega
  exact ⟨a, b, p, hp.isHamiltonian_of_mem hspan⟩
end P_smallk
theorem smallk : ∀ (V : Type) [Fintype V] [DecidableEq V] (G : SimpleGraph V) (N : V → Finset V), (∀ x y, y ∈ N x ↔ G.Adj x y) → G.Connected → (∀ x, (N x).card ≤ 2) →
  ∃ (a b : V) (p : G.Walk a b), p.IsHamiltonian := P_smallk.smallk
namespace P_bip
lemma ncard_eq (n : ℕ) (q : ℕ → Prop) [DecidablePred q] :
    Nat.card {i : ℕ // i < n ∧ q i} = ((Finset.range n).filter q).card := by
  rw [← Nat.card_eq_finsetCard]
  apply Nat.card_congr
  exact Equiv.subtypeEquivRight (fun i => by simp)

lemma cnt (V : Type) [DecidableEq V] (s : Finset V) (f : ℕ → V) (n : ℕ)
    (hmem : ∀ i < n, f i ∈ s) (hinj : ∀ i < n, ∀ j < n, f i = f j → i = j)
    (hsurj : ∀ x ∈ s, ∃ i < n, f i = x) (p : V → Prop) [DecidablePred p] :
    Nat.card {i : ℕ // i < n ∧ p (f i)} = (s.filter p).card := by
  rw [ncard_eq, ← Finset.card_image_of_injOn (f := f)]
  · congr 1
    ext x
    simp only [Finset.mem_image, Finset.mem_filter, Finset.mem_range]
    constructor
    · rintro ⟨i, ⟨hi, hp⟩, rfl⟩
      exact ⟨hmem i hi, hp⟩
    · rintro ⟨hx, hp⟩
      obtain ⟨i, hi, rfl⟩ := hsurj x hx
      exact ⟨i, ⟨hi, hp⟩, rfl⟩
  · intro i hi j hj hij
    simp only [Finset.coe_filter, Finset.mem_range, Set.mem_setOf_eq] at hi hj
    exact hinj i hi.1 j hj.1 hij

lemma enum (V : Type) (s : Finset V) (d : V) :
    ∃ f : ℕ → V, (∀ i < s.card, f i ∈ s) ∧ (∀ i < s.card, ∀ j < s.card, f i = f j → i = j) ∧
      (∀ x ∈ s, ∃ i < s.card, f i = x) := by
  classical
  let e := s.equivFin
  refine ⟨fun i => if h : i < s.card then (e.symm ⟨i, h⟩).1 else d, ?_, ?_, ?_⟩
  · intro i hi
    simp only [hi, dite_true]
    exact (e.symm ⟨i, hi⟩).2
  · intro i hi j hj hij
    simp only [hi, hj, dite_true] at hij
    have := e.symm.injective (Subtype.ext hij)
    simpa using congrArg Fin.val this
  · intro x hx
    refine ⟨(e ⟨x, hx⟩).1, (e ⟨x, hx⟩).2, ?_⟩
    simp

lemma bij_of_inj (m : ℕ) (x : ℕ → ℕ) (hlt : ∀ i < m, x i < m)
    (hinj : ∀ i < m, ∀ j < m, x i = x j → i = j) : ∀ v < m, ∃ i < m, x i = v := by
  intro v hv
  have himg : (Finset.range m).image x = Finset.range m := by
    apply Finset.eq_of_subset_of_card_le
    · intro y hy
      simp only [Finset.mem_image, Finset.mem_range] at hy ⊢
      obtain ⟨i, hi, rfl⟩ := hy
      exact hlt i hi
    · rw [Finset.card_image_of_injOn]
      intro i hi j hj h
      exact hinj i (by simpa using hi) j (by simpa using hj) h
  have : v ∈ (Finset.range m).image x := by
    rw [himg]
    simpa using hv
  simp only [Finset.mem_image, Finset.mem_range] at this
  exact this

lemma altpath (V : Type) [Fintype V] [DecidableEq V] (G : SimpleGraph V) (n : ℕ) (a b : ℕ → V)
    (hcard : n + 1 = Fintype.card V)
    (ha : ∀ i j, 2 * i ≤ n → 2 * j ≤ n → a i = a j → i = j)
    (hb : ∀ i j, 2 * i + 1 ≤ n → 2 * j + 1 ≤ n → b i = b j → i = j)
    (hab : ∀ i j, 2 * i ≤ n → 2 * j + 1 ≤ n → a i ≠ b j)
    (h1 : ∀ i, 2 * i + 1 ≤ n → G.Adj (a i) (b i))
    (h2 : ∀ i, 2 * i + 2 ≤ n → G.Adj (b i) (a (i + 1))) :
    ∃ (u v : V) (p : G.Walk u v), p.IsHamiltonian := by
  let h : ℕ → V := fun i => if i % 2 = 0 then a (i / 2) else b (i / 2)
  apply listpath V G n h hcard
  · intro i hi j hj hij
    rcases Nat.mod_two_eq_zero_or_one i with hi2 | hi2 <;>
      rcases Nat.mod_two_eq_zero_or_one j with hj2 | hj2 <;>
      simp [h, hi2, hj2] at hij
    · have := ha (i / 2) (j / 2) (by omega) (by omega) hij
      omega
    · exact absurd hij (hab _ _ (by omega) (by omega))
    · exact absurd hij.symm (hab _ _ (by omega) (by omega))
    · have := hb (i / 2) (j / 2) (by omega) (by omega) hij
      omega
  · intro i hi
    rcases Nat.mod_two_eq_zero_or_one i with hi2 | hi2
    · have e1 : (i + 1) % 2 = 1 := by omega
      have e2 : (i + 1) / 2 = i / 2 := by omega
      simp only [h, hi2, e1, e2]
      simpa using h1 (i / 2) (by omega)
    · have e1 : (i + 1) % 2 = 0 := by omega
      have e2 : (i + 1) / 2 = i / 2 + 1 := by omega
      simp only [h, hi2, e1, e2]
      simpa using h2 (i / 2) (by omega)

theorem bip : ∀ (V : Type) [Fintype V] [DecidableEq V] (G : SimpleGraph V) (N : V → Finset V) (X Y : Finset V) (k : ℕ), (∀ x y, y ∈ N x ↔ G.Adj x y) → 2 ≤ k → 0 < Fintype.card V →
  (∀ x, k ≤ (N x).card) → Disjoint X Y → X ∪ Y = Finset.univ → (∀ x ∈ X, N x ⊆ Y) → (∀ y ∈ Y, N y ⊆ X) → X.card ≤ Y.card → Y.card ≤ X.card + 1 → Y.card ≤ 2 * k →
  (Y.card = 2 * k → ∀ y ∈ Y, k + 1 ≤ (N y).card) → (X.card = 2 * k → ∀ x ∈ X, k + 1 ≤ (N x).card) → ∃ (a b : V) (p : G.Walk a b), p.IsHamiltonian := by
  classical
  intro V _ _ G N X Y k hN hk hne hdeg hdisj hunion hNX hNY hXY hYX hYle hYspec hXspec
  have hXne : X.Nonempty := by
    have hu : (Finset.univ : Finset V).Nonempty := by
      rw [← Finset.card_pos, Finset.card_univ]
      exact hne
    obtain ⟨v, -⟩ := hu
    by_contra hX
    rw [Finset.not_nonempty_iff_eq_empty] at hX
    have hvY : v ∈ Y := by
      have := Finset.mem_univ v
      rw [← hunion, hX] at this
      simpa using this
    have h2 : N v = ∅ := Finset.subset_empty.mp (hX ▸ hNY v hvY)
    have h1 := hdeg v
    rw [h2] at h1
    simp at h1
    omega
  obtain ⟨x0, hx0⟩ := hXne
  have hYne : 2 ≤ Y.card := by
    have := Finset.card_le_card (hNX x0 hx0)
    have := hdeg x0
    omega
  obtain ⟨f, hfm, hfi, hfs⟩ := enum V X x0
  obtain ⟨g, hgm, hgi, hgs⟩ := enum V Y x0
  have hcardV : Fintype.card V = X.card + Y.card := by
    rw [← Finset.card_univ, ← hunion, Finset.card_union_of_disjoint hdisj]
  have rowc : ∀ i < X.card, Nat.card {b : ℕ // b < Y.card ∧ G.Adj (f i) (g b)} = (N (f i)).card := by
    intro i hi
    rw [cnt V Y g Y.card hgm hgi hgs (fun y => G.Adj (f i) y)]
    congr 1
    ext y
    simp only [Finset.mem_filter]
    exact ⟨fun h => (hN _ _).2 h.2, fun h => ⟨hNX _ (hfm i hi) h, (hN _ _).1 h⟩⟩
  have colc : ∀ j < Y.card, Nat.card {a : ℕ // a < X.card ∧ G.Adj (f a) (g j)} = (N (g j)).card := by
    intro j hj
    rw [cnt V X f X.card hfm hfi hfs (fun x => G.Adj x (g j))]
    congr 1
    ext x
    simp only [Finset.mem_filter]
    exact ⟨fun h => (hN _ _).2 h.2.symm, fun h => ⟨hNY _ (hgm j hj) h, ((hN _ _).1 h).symm⟩⟩
  rcases (show X.card = Y.card ∨ X.card + 1 = Y.card by omega) with hc | hc
  · obtain ⟨x, y, hxl, hyl, hxi, hyi, hd, hs, hcl⟩ :=
      cycle Y.card hYne (fun i j => G.Adj (f i) (g j)) (by
        intro i hi j hj _
        show Y.card < Nat.card {b : ℕ // b < Y.card ∧ G.Adj (f i) (g b)} +
          Nat.card {a : ℕ // a < Y.card ∧ G.Adj (f a) (g j)}
        have r := rowc i (by omega)
        have c := colc j hj
        rw [hc] at c
        rw [r, c]
        have := hdeg (f i)
        have := hdeg (g j)
        by_cases hmk : Y.card = 2 * k
        · have := hYspec hmk (g j) (hgm j hj)
          omega
        · omega)
    apply altpath V G (2 * Y.card - 1) (fun t => f (x t)) (fun t => g (y t))
    · rw [hcardV]
      omega
    · intro i j hi hj h
      exact hxi i (by omega) j (by omega)
        (hfi _ (by have := hxl i (by omega); omega) _ (by have := hxl j (by omega); omega) h)
    · intro i j hi hj h
      exact hyi i (by omega) j (by omega) (hgi _ (hyl i (by omega)) _ (hyl j (by omega)) h)
    · intro i j hi hj h
      have h1 : f (x i) ∈ X := hfm _ (by have := hxl i (by omega); omega)
      have h2 : g (y j) ∈ Y := hgm _ (hyl j (by omega))
      exact Finset.disjoint_left.mp hdisj h1 (by rw [h]; exact h2)
    · intro i hi
      exact hd i (by omega)
    · intro i hi
      exact (hs i (by omega)).symm
  · let E : ℕ → ℕ → Prop := fun i j => i = Y.card - 1 ∨ G.Adj (f i) (g j)
    have hE : ∀ i < Y.card, ∀ j < Y.card, ¬ E i j →
        Y.card < Nat.card {b : ℕ // b < Y.card ∧ E i b} + Nat.card {a : ℕ // a < Y.card ∧ E a j} := by
      intro i hi j hj hnot
      have hi' : i ≠ Y.card - 1 := fun h => hnot (Or.inl h)
      have r : Nat.card {b : ℕ // b < Y.card ∧ E i b} = (N (f i)).card := by
        rw [← rowc i (by omega)]
        apply Nat.card_congr
        exact Equiv.subtypeEquivRight (fun b => by simp only [E]; tauto)
      have c : (N (g j)).card + 1 ≤ Nat.card {a : ℕ // a < Y.card ∧ E a j} := by
        rw [← colc j hj, ncard_eq, ncard_eq]
        have hsub : insert (Y.card - 1) ((Finset.range X.card).filter (fun a => G.Adj (f a) (g j))) ⊆
            (Finset.range Y.card).filter (fun a => E a j) := by
          intro a ha
          simp only [Finset.mem_insert, Finset.mem_filter, Finset.mem_range, E] at ha ⊢
          rcases ha with ha | ⟨h1, h2⟩
          · exact ⟨by omega, Or.inl ha⟩
          · exact ⟨by omega, Or.inr h2⟩
        have hnm : Y.card - 1 ∉ (Finset.range X.card).filter (fun a => G.Adj (f a) (g j)) := by
          simp only [Finset.mem_filter, Finset.mem_range]
          intro h
          exact absurd h.1 (by omega)
        have := Finset.card_le_card hsub
        rw [Finset.card_insert_of_notMem hnm] at this
        exact this
      rw [r]
      have := hdeg (f i)
      have := hdeg (g j)
      omega
    obtain ⟨x, y, hxl, hyl, hxi, hyi, hd, hs, hcl⟩ := cycle Y.card hYne E hE
    obtain ⟨i0, hi0, hxi0⟩ := bij_of_inj Y.card x hxl hxi (Y.card - 1) (by omega)
    obtain ⟨x', y', ⟨hxl', hyl', hxi', hyi', hd', hs', hcl'⟩, hx0'⟩ : ∃ x' y' : ℕ → ℕ,
        ((∀ i < Y.card, x' i < Y.card) ∧ (∀ i < Y.card, y' i < Y.card) ∧
          (∀ i < Y.card, ∀ j < Y.card, x' i = x' j → i = j) ∧
          (∀ i < Y.card, ∀ j < Y.card, y' i = y' j → i = j) ∧
          (∀ i < Y.card, E (x' i) (y' i)) ∧ (∀ i, i + 1 < Y.card → E (x' (i + 1)) (y' i)) ∧
          E (x' 0) (y' (Y.card - 1))) ∧ x' 0 = Y.card - 1 := by
      rcases Nat.eq_zero_or_pos i0 with h0 | h0
      · subst h0
        exact ⟨x, y, ⟨hxl, hyl, hxi, hyi, hd, hs, hcl⟩, hxi0⟩
      · obtain ⟨x', y', hc', hx0, -⟩ :=
          rotA Y.card E x y (i0 - 1) hYne (by omega) ⟨hxl, hyl, hxi, hyi, hd, hs, hcl⟩
        refine ⟨x', y', hc', ?_⟩
        rw [hx0, show i0 - 1 + 1 = i0 by omega, hxi0]
    have hx'ne : ∀ t, 1 ≤ t → t < Y.card → x' t < X.card := by
      intro t ht1 ht2
      have h1 := hxl' t ht2
      have h2 : x' t ≠ Y.card - 1 := by
        intro h
        have := hxi' t ht2 0 (by omega) (h.trans hx0'.symm)
        omega
      omega
    apply altpath V G (2 * Y.card - 2) (fun t => g (y' t)) (fun t => f (x' (t + 1)))
    · rw [hcardV]
      omega
    · intro i j hi hj h
      exact hyi' i (by omega) j (by omega) (hgi _ (hyl' i (by omega)) _ (hyl' j (by omega)) h)
    · intro i j hi hj h
      have := hxi' (i + 1) (by omega) (j + 1) (by omega)
        (hfi _ (hx'ne _ (by omega) (by omega)) _ (hx'ne _ (by omega) (by omega)) h)
      omega
    · intro i j hi hj h
      have h1 : g (y' i) ∈ Y := hgm _ (hyl' i (by omega))
      have h2 : f (x' (j + 1)) ∈ X := hfm _ (hx'ne _ (by omega) (by omega))
      exact Finset.disjoint_left.mp hdisj h2 (by rw [← h]; exact h1)
    · intro i hi
      rcases hs' i (by omega) with h | h
      · have := hx'ne (i + 1) (by omega) (by omega)
        omega
      · exact h.symm
    · intro i hi
      rcases hd' (i + 1) (by omega) with h | h
      · have := hx'ne (i + 1) (by omega) (by omega)
        omega
      · exact h

end P_bip
theorem bip : ∀ (V : Type) [Fintype V] [DecidableEq V] (G : SimpleGraph V) (N : V → Finset V) (X Y : Finset V) (k : ℕ), (∀ x y, y ∈ N x ↔ G.Adj x y) → 2 ≤ k → 0 < Fintype.card V →
  (∀ x, k ≤ (N x).card) → Disjoint X Y → X ∪ Y = Finset.univ → (∀ x ∈ X, N x ⊆ Y) → (∀ y ∈ Y, N y ⊆ X) → X.card ≤ Y.card → Y.card ≤ X.card + 1 → Y.card ≤ 2 * k →
  (Y.card = 2 * k → ∀ y ∈ Y, k + 1 ≤ (N y).card) → (X.card = 2 * k → ∀ x ∈ X, k + 1 ≤ (N x).card) → ∃ (a b : V) (p : G.Walk a b), p.IsHamiltonian := P_bip.bip
namespace P_main
theorem main : ∀ (V : Type) [Fintype V] [DecidableEq V] (G : SimpleGraph V) (k : ℕ), 1 ≤ k → k < Fintype.card V →
  (∀ S : Finset V, S.card < k → (G.induce ((S : Set V)ᶜ)).Connected) →
  (∀ s : Finset V, (G.induce (s : Set V)).IsTree → s.card ≤ k + 2) →
  (∀ a b c : V, G.Adj a b → G.Adj b c → ¬ G.Adj a c) →
  (∀ a b c d e : V, G.Adj a b → G.Adj b c → G.Adj c d → G.Adj d e → ¬ G.Adj e a) →
  ∃ (a b : V) (p : G.Walk a b), p.IsHamiltonian := by
  intro V _ _ G k hk hklt hconn htree htri hno5
  classical
  let N : V → Finset V := fun x => Finset.univ.filter (fun y => G.Adj x y)
  have hN : ∀ x y, y ∈ N x ↔ G.Adj x y := by
    intro x y
    simp [N]
  have hNsym : ∀ x y, y ∈ N x ↔ x ∈ N y := by
    intro x y
    rw [hN x y, hN y x]
    exact ⟨fun h => h.symm, fun h => h.symm⟩
  have hkpos : 0 < k := by omega
  have hGconn : G.Connected := by
    have hconnU : (G.induce (((∅ : Finset V) : Set V)ᶜ)).Connected :=
      hconn (∅ : Finset V) (by simpa using hkpos)
    have hpreU : (G.induce (((∅ : Finset V) : Set V)ᶜ)).Preconnected :=
      hconnU.preconnected
    have hpre : G.Preconnected := by
      intro u v
      let u' : ↥(((∅ : Finset V) : Set V)ᶜ) := ⟨u, by simp⟩
      let v' : ↥(((∅ : Finset V) : Set V)ᶜ) := ⟨v, by simp⟩
      have huv : (G.induce (((∅ : Finset V) : Set V)ᶜ)).Reachable u' v' :=
        hpreU u' v'
      let e : (G.induce (((∅ : Finset V) : Set V)ᶜ)) →g G :=
        (SimpleGraph.Embedding.induce (((∅ : Finset V) : Set V)ᶜ)).toHom
      have hm : G.Reachable (e u') (e v') := huv.map e
      exact hm
    have hcardpos : 0 < Fintype.card V := by omega
    have hne : Nonempty V := Fintype.card_pos_iff.mp hcardpos
    haveI := hne
    exact SimpleGraph.Connected.mk hpre
  by_cases hk1 : k = 1
  · subst k
    have hle2 : ∀ x, (N x).card ≤ 2 := by
      intro x
      have h := maxdeg V G N hN 1 htree htri x
      omega
    exact smallk V G N hN hGconn hle2
  · have hk2 : 2 ≤ k := by omega
    have hmin : ∀ x, k ≤ (N x).card := by
      intro x
      exact mindeg V G N hN k hklt hconn x
    have hmax : ∀ x, (N x).card ≤ k + 1 := by
      intro x
      exact maxdeg V G N hN k htree htri x
    have hov : ∀ x z y : V, x ≠ z → y ∈ N x → y ∈ N z →
        (N x).card + 1 ≤ 2 * (N x ∩ N z).card := by
      exact overlap V G N hN k htree htri hno5 hmin
    have hcardgt : 1 < Fintype.card V := by omega
    have hparts := parts V G N hN hGconn hcardgt htri hov
    rcases hparts with ⟨X, Y, hdisj, hunion, hNXY, hNYX, hXclique, hYclique⟩
    have hXsizeCond : ∀ x ∈ X, ∀ x' ∈ X, x ≠ x' →
        (N x).card + 1 ≤ 2 * (N x ∩ N x').card := by
      intro x hx x' hx' hne
      rcases hXclique x hx x' hx' with ⟨y, hy⟩
      have hyx : y ∈ N x := (Finset.mem_inter.mp hy).1
      have hyx' : y ∈ N x' := (Finset.mem_inter.mp hy).2
      exact hov x x' y hne hyx hyx'
    have hYsizeCond : ∀ y ∈ Y, ∀ y' ∈ Y, y ≠ y' →
        (N y).card + 1 ≤ 2 * (N y ∩ N y').card := by
      intro y hy y' hy' hne
      rcases hYclique y hy y' hy' with ⟨z, hz⟩
      have hzy : z ∈ N y := (Finset.mem_inter.mp hz).1
      have hzy' : z ∈ N y' := (Finset.mem_inter.mp hz).2
      exact hov y y' z hne hzy hzy'
    have hXsize := size V N X k hk2 hNsym hmin hmax hXsizeCond
    have hYsize := size V N Y k hk2 hNsym hmin hmax hYsizeCond
    have hYbal : Y.card ≤ X.card + 1 :=
      balance V N X Y k hNsym hmin hmax hNXY hNYX hYsize.1
    have hXbal : X.card ≤ Y.card + 1 :=
      balance V N Y X k hNsym hmin hmax hNYX hNXY hXsize.1
    have hcardpos : 0 < Fintype.card V := by omega
    by_cases hXY : X.card ≤ Y.card
    · exact bip V G N X Y k hN hk2 hcardpos hmin hdisj hunion hNXY hNYX
        hXY hYbal hYsize.1 hYsize.2 hXsize.2
    · have hYX : Y.card ≤ X.card := by omega
      have hdisj' : Disjoint Y X := hdisj.symm
      have hunion' : Y ∪ X = Finset.univ := by
        rw [Finset.union_comm]
        exact hunion
      exact bip V G N Y X k hN hk2 hcardpos hmin hdisj' hunion' hNYX hNXY
        hYX hXbal hXsize.1 hXsize.2 hYsize.2
end P_main
theorem main : ∀ (V : Type) [Fintype V] [DecidableEq V] (G : SimpleGraph V) (k : ℕ), 1 ≤ k → k < Fintype.card V →
  (∀ S : Finset V, S.card < k → (G.induce ((S : Set V)ᶜ)).Connected) →
  (∀ s : Finset V, (G.induce (s : Set V)).IsTree → s.card ≤ k + 2) →
  (∀ a b c : V, G.Adj a b → G.Adj b c → ¬ G.Adj a c) →
  (∀ a b c d e : V, G.Adj a b → G.Adj b c → G.Adj c d → G.Adj d e → ¬ G.Adj e a) →
  ∃ (a b : V) (p : G.Walk a b), p.IsHamiltonian := P_main.main
theorem solution : FSpec.Target := main

复核记录

2026-11-30T00:50:43Z独立 Claude agent(只读 lemmas.py 和附录 A):leaf → ok
2026-11-30T00:50:43Z独立 Claude agent(只读 lemmas.py 和附录 A):leaves → ok
2026-11-30T00:50:43Z独立 Claude agent(只读 lemmas.py 和附录 A):mindeg → ok
2026-11-30T00:50:43Z独立 Claude agent(只读 lemmas.py 和附录 A):maxdeg → ok
2026-11-30T00:50:43Z独立 Claude agent(只读 lemmas.py 和附录 A):overlap → ok
2026-11-30T00:50:43Z独立 Claude agent(只读 lemmas.py 和附录 A):parts → ok
2026-11-30T00:50:43Z独立 Claude agent(只读 lemmas.py 和附录 A):size → ok
2026-11-30T00:50:43Z独立 Claude agent(只读 lemmas.py 和附录 A):balance → ok
2026-11-30T00:50:43Z独立 Claude agent(只读 lemmas.py 和附录 A):cross → ok
2026-11-30T00:50:43Z独立 Claude agent(只读 lemmas.py 和附录 A):cycle → ok
2026-11-30T00:50:43Z独立 Claude agent(只读 lemmas.py 和附录 A):listpath → ok
2026-11-30T00:50:43Z独立 Claude agent(只读 lemmas.py 和附录 A):smallk → ok
2026-11-30T00:50:43Z独立 Claude agent(只读 lemmas.py 和附录 A):bip → ok
2026-11-30T00:50:43Z独立 Claude agent(只读 lemmas.py 和附录 A):main → ok
2026-11-30T01:29:41Z独立 Claude agent(只读 astra2_prompt.md 和 astra2_answer.md):proof_review → ok
2026-11-30T01:42:13Z独立 Claude agent(网页检索):priority → partial
2026-11-30T02:15:11ZGPT-6 Pro(网页,单线程审查):proof_review → major_attribution
2026-11-30T02:15:11Z独立 Claude agent:proof_review → minor
2026-11-30T02:15:11Z独立 Claude agent(只读 lemmas.py 和附录 A):final_statement → ok
2026-11-30T02:19:48ZGPT-6 Pro(网页,单线程审查):proof_review → minor_accept_after_wording

AI 和人各做了什么

AI:Claude 从 formal-conjectures 选题、写小图搜索程序、写 Lean 陈述和引理链;Astra(gpt-6.2-astra)提出了两批部分结果的证明(两次调用);DeepSeek 写了大部分 Lean 证明代码(17 条引理里 4 条完全自己证出,其余多数是草稿只差几处),Claude 修好卡住的引理,重写了 leaf、overlap 和 bip;DeepSeek 起草论文,Claude 修改;独立的 Claude agent 和 GPT-6 Pro 审稿。

人:项目负责人提出目标并定规则,没有审过这道题的数学内容。

局限

只覆盖上面几种情形。有三角形时两种证明都用不上;没有三角形但有 5 圈、κ ≥ 4 且有带悬挂邻点的 5 圈时,第二批的论证也停下了。小图搜索(7–13 个点,模拟退火,不是穷举)没有找到反例,范围很小,不能当作猜想成立的证据。