/-
Copyright (c) 2022 Kyle Miller. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kyle Miller
-/
module
public import Mathlib.Combinatorics.SimpleGraph.Connectivity.Connected
/-!
# Trails and Eulerian trails
This module contains additional theory about trails, including Eulerian trails (also known
as Eulerian circuits).
## Main definitions
* `SimpleGraph.Walk.IsEulerian` is the predicate that a trail is an Eulerian trail.
* `SimpleGraph.Walk.IsTrail.even_countP_edges_iff` gives a condition on the number of edges
in a trail that can be incident to a given vertex.
* `SimpleGraph.Walk.IsEulerian.even_degree_iff` gives a condition on the degrees of vertices
when there exists an Eulerian trail.
* `SimpleGraph.Walk.IsEulerian.card_odd_degree` gives the possible numbers of odd-degree
vertices when there exists an Eulerian trail.
## TODO
* Prove that there exists an Eulerian trail when the conclusion to
`SimpleGraph.Walk.IsEulerian.card_odd_degree` holds.
## Tags
Eulerian trails
-/
@[expose] public section
namespace SimpleGraph
variable {V : Type*} {G : SimpleGraph V} {u v w : V} {p : G.Walk u v}
namespace Walk
/-- The edges of a trail as a finset, since each edge in a trail appears exactly once. -/
abbrev IsTrail.edgesFinset (h : p.IsTrail) : Finset (Sym2 V) :=
⟨p.edges, Multiset.coe_nodup.mpr h.edges_nodup⟩
variable [DecidableEq V]
theorem IsTrail.even_countP_edges_iff (ht : p.IsTrail) (x : V) :
Even (p.edges.countP fun e ↦ x ∈ e) ↔ u ≠ v → x ≠ u ∧ x ≠ v := by
induction p with
| nil => simp
| cons huv p ih => grind [isTrail_cons, edges_cons, G.irrefl]
/-- An *Eulerian trail* (also known as an "Eulerian path") is a walk
`p` that visits every edge exactly once. The lemma `SimpleGraph.Walk.IsEulerian.IsTrail` shows
that these are trails.
Combine with `p.IsCircuit` to get an Eulerian circuit (also known as an "Eulerian cycle"). -/
def IsEulerian (p : G.Walk u v) : Prop :=
∀ e, e ∈ G.edgeSet → p.edges.count e = 1
theorem IsEulerian.isTrail (h : p.IsEulerian) : p.IsTrail := by
rw [isTrail_def, List.nodup_iff_count_eq_one]
exact (h · <| p.edges_subset_edgeSet ·)
theorem IsEulerian.mem_edges_iff (h : p.IsEulerian) {e : Sym2 V} : e ∈ p.edges ↔ e ∈ G.edgeSet :=
⟨fun h ↦ p.edges_subset_edgeSet h, fun he ↦ by simpa using (h e he).ge⟩
/-- The edge set of an Eulerian graph is finite. -/
@[instance_reducible]
def IsEulerian.fintypeEdgeSet (h : p.IsEulerian) : Fintype G.edgeSet :=
.ofFinset h.isTrail.edgesFinset <| by simp [h.mem_edges_iff]
theorem IsTrail.isEulerian_of_forall_mem (h : p.IsTrail) (hc : ∀ e, e ∈ G.edgeSet → e ∈ p.edges) :
p.IsEulerian :=
(List.count_eq_one_of_mem h.edges_nodup <| hc · ·)
theorem isEulerian_iff (p : G.Walk u v) :
p.IsEulerian ↔ p.IsTrail ∧ ∀ e, e ∈ G.edgeSet → e ∈ p.edges where
mp h := ⟨h.isTrail, fun _ ↦ h.mem_edges_iff.mpr⟩
mpr := fun ⟨h, hl⟩ ↦ h.isEulerian_of_forall_mem hl
theorem isEulerian_iff_isTrail_and_edgeSet_eq :
p.IsEulerian ↔ p.IsTrail ∧ p.edgeSet = G.edgeSet := by
rw [isEulerian_iff, and_congr_right_iff]
exact fun _ ↦ ⟨(Set.Subset.antisymm p.edges_subset_edgeSet ·), fun h ↦ by simp [← h]⟩
theorem isEulerian_iff_isTrail_and_length_eq_encard_edgeSet :
p.IsEulerian ↔ p.IsTrail ∧ p.length = G.edgeSet.encard := by
rw [isEulerian_iff_isTrail_and_edgeSet_eq, and_congr_right_iff, ← length_edges]
intro hp
rw [← hp.edges_nodup.dedup, ← List.card_toFinset, ← Set.ncard_coe_finset, List.coe_toFinset,
p.edges.finite_toSet.cast_ncard_eq, ← edgeSet]
refine ⟨congrArg _, fun h ↦ ?_⟩
exact p.edges.finite_toSet.eq_of_subset_of_encard_le p.edges_subset_edgeSet h.symm.le
theorem IsTrail.isEulerian_iff (hp : p.IsTrail) : p.IsEulerian ↔ p.edgeSet = G.edgeSet := by
simp [isEulerian_iff_isTrail_and_edgeSet_eq, hp]
theorem IsEulerian.edgeSet_eq (h : p.IsEulerian) : p.edgeSet = G.edgeSet := by
rwa [← h.isTrail.isEulerian_iff]
theorem IsEulerian.finite_edgeSet (h : p.IsEulerian) : G.edgeSet.Finite :=
h.edgeSet_eq ▸ p.edges.finite_toSet
theorem IsEulerian.length_eq_ncard_edgeSet (h : p.IsEulerian) : p.length = G.edgeSet.ncard := by
rw [← h.isTrail.ncard_edgeSet, h.edgeSet_eq]
theorem IsEulerian.edgesFinset_eq [Fintype G.edgeSet] (h : p.IsEulerian) :
h.isTrail.edgesFinset = G.edgeFinset := by
ext e
simp [h.mem_edges_iff]
theorem IsEulerian.mem_support_of_not_isIsolated (hp : p.IsEulerian) (hw : ¬G.IsIsolated w) :
w ∈ p.support :=
have ⟨_, hadj⟩ := exists_adj_iff_not_isIsolated.mpr hw
p.fst_mem_support_of_mem_edges <| hp.mem_edges_iff.mpr hadj
theorem IsEulerian.nil_iff (hp : p.IsEulerian) : p.Nil ↔ G = ⊥ := by
simp [← edgeSet_eq_empty, hp.edgeSet_eq]
theorem isEulerian_of_bot (p : Walk ⊥ u v) : p.IsEulerian := by
intro e h
simp at h
/-- The support of a non-nil Eulerian trail equals the support of the graph. -/
theorem IsEulerian.mem_support_iff (hp : p.IsEulerian) (hnil : ¬p.Nil) :
w ∈ p.support ↔ ¬G.IsIsolated w :=
⟨fun hwp hw ↦ hnil <| p.nil_of_isIsolated_of_mem_support hw hwp, hp.mem_support_of_not_isIsolated⟩
theorem IsEulerian.connected_of_forall_not_isIsolated (hp : p.IsEulerian)
(hG : ∀ v, ¬G.IsIsolated v) : G.Connected where
preconnected a b :=
have ha : a ∈ p.support := hp.mem_support_of_not_isIsolated <| hG a
have hb : b ∈ p.support := hp.mem_support_of_not_isIsolated <| hG b
⟨p.takeUntil a ha |>.reverse.append <| p.takeUntil b hb⟩
nonempty := ⟨u⟩
theorem IsEulerian.even_degree_iff (ht : p.IsEulerian) [Fintype V] [DecidableRel G.Adj] :
Even (G.degree w) ↔ u ≠ v → w ≠ u ∧ w ≠ v := by
rw [← ht.isTrail.even_countP_edges_iff, ← Multiset.coe_countP, Multiset.countP_eq_card_filter,
← card_incidenceFinset_eq_degree, incidenceFinset_eq_filter, ← ht.edgesFinset_eq]
simp [Finset.card_def]
theorem IsEulerian.card_filter_odd_degree [Fintype V] [DecidableRel G.Adj] (ht : p.IsEulerian) {s}
(h : s = ({ v | Odd (G.degree v) } : Finset V)) : s.card = 0 ∨ s.card = 2 := by
subst s
simp_rw [← Nat.not_even_iff_odd, ht.even_degree_iff]
obtain rfl | hn := eq_or_ne u v
· simp
· grind [Finset.card_pair hn, congrArg Finset.card]
theorem IsEulerian.card_odd_degree [Fintype V] [DecidableRel G.Adj] (ht : p.IsEulerian) :
Fintype.card { v | Odd (G.degree v) } = 0 ∨ Fintype.card { v | Odd (G.degree v) } = 2 := by
rw [← Set.toFinset_card]
apply ht.card_filter_odd_degree
simp
end Walk
end SimpleGraph
¤ Dauer der Verarbeitung: 0.12 Sekunden
¤
*© Formatika GbR, Deutschland