Graph.lean

Lean 4 source, lean/DataMiningAsObservation/Graph.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.

/-
Graph.lean. Chapter 0 section 0.10, the graph.

A neighbourhood graph joins each row of a dataset to its nearest rows. In the directed
form every node has exactly `k` out-neighbours. The mutual form keeps an edge only when
each node is among the other's neighbours, so it is symmetric, it is a subgraph of the
directed form, and no node has more than `k` mutual neighbours. The handshake lemma and the
degree bound are in the degree file, from Mathlib's simple graphs.
-/
import Mathlib

namespace DMO.Graph

open Finset

variable {V : Type*} [DecidableEq V]

/-- A directed neighbourhood graph, each node's `k` nearest rows. -/
structure KNN (V : Type*) (k : ℕ) where
  nbrs : V → Finset V
  card_nbrs : ∀ v, (nbrs v).card = k

omit [DecidableEq V] in
/-- **Every node has exactly `k` out-neighbours.** -/
theorem outDegree {k : ℕ} (G : KNN V k) (v : V) : (G.nbrs v).card = k := G.card_nbrs v

/-- The mutual neighbourhood relation, each among the other's neighbours. -/
def isMutual {k : ℕ} (G : KNN V k) (u v : V) : Prop := v ∈ G.nbrs u ∧ u ∈ G.nbrs v

omit [DecidableEq V] in
/-- **The mutual graph is symmetric.** -/
theorem mutual_symm {k : ℕ} (G : KNN V k) (u v : V) : isMutual G u v ↔ isMutual G v u := by
  unfold isMutual; exact and_comm

omit [DecidableEq V] in
/-- **The mutual graph is a subgraph of the directed one.** -/
theorem mutual_sub {k : ℕ} (G : KNN V k) (u v : V) (h : isMutual G u v) : v ∈ G.nbrs u := h.1

/-- The mutual neighbours of a node. -/
def mutualNbrs {k : ℕ} (G : KNN V k) (u : V) : Finset V := (G.nbrs u).filter fun v => u ∈ G.nbrs v

/-- **No node has more than `k` mutual neighbours.** -/
theorem mutualNbrs_card_le {k : ℕ} (G : KNN V k) (u : V) : (mutualNbrs G u).card ≤ k := by
  unfold mutualNbrs
  exact (Finset.card_filter_le _ _).trans (G.card_nbrs u).le

/-- A node's mutual neighbours are exactly those the relation names. -/
theorem mem_mutualNbrs {k : ℕ} (G : KNN V k) (u v : V) : v ∈ mutualNbrs G u ↔ isMutual G u v := by
  unfold mutualNbrs isMutual
  simp

end DMO.Graph