-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathWalkCounting.lean
More file actions
291 lines (234 loc) · 11.7 KB
/
WalkCounting.lean
File metadata and controls
291 lines (234 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/-
Copyright (c) 2021 Kyle Miller. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kyle Miller
-/
import Mathlib.Algebra.BigOperators.Ring.Nat
import Mathlib.Combinatorics.SimpleGraph.Path
import Mathlib.Data.Finite.Card
import Mathlib.Data.Set.Card
import Mathlib.Data.Set.Finite.Lattice
/-!
# Counting walks of a given length
## Main definitions
- `walkLengthTwoEquivCommonNeighbors`: bijective correspondence between walks of length two
from `u` to `v` and common neighbours of `u` and `v`. Note that `u` and `v` may be the same.
- `finsetWalkLength`: the `Finset` of length-`n` walks from `u` to `v`.
This is used to give `{p : G.walk u v | p.length = n}` a `Fintype` instance, and it
can also be useful as a recursive description of this set when `V` is finite.
TODO: should this be extended further?
-/
assert_not_exists Field
open Finset Function
universe u v w
namespace SimpleGraph
variable {V : Type u} (G : SimpleGraph V)
/-! ### Walks of a given length -/
section WalkCounting
theorem set_walk_self_length_zero_eq (u : V) : {p : G.Walk u u | p.length = 0} = {Walk.nil} := by
ext p
simp
theorem set_walk_length_zero_eq_of_ne {u v : V} (h : u ≠ v) :
{p : G.Walk u v | p.length = 0} = ∅ := by
ext p
simp only [Set.mem_setOf_eq, Set.mem_empty_iff_false, iff_false]
exact fun h' => absurd (Walk.eq_of_length_eq_zero h') h
theorem set_walk_length_succ_eq (u v : V) (n : ℕ) :
{p : G.Walk u v | p.length = n.succ} =
⋃ (w : V) (h : G.Adj u w), Walk.cons h '' {p' : G.Walk w v | p'.length = n} := by
ext p
cases p with
| nil => simp [eq_comm]
| cons huw pwv =>
simp only [Nat.succ_eq_add_one, Set.mem_setOf_eq, Walk.length_cons, add_left_inj,
Set.mem_iUnion, Set.mem_image, exists_prop]
constructor
· rintro rfl
exact ⟨_, huw, pwv, rfl, rfl⟩
· rintro ⟨w, huw, pwv, rfl, rfl, rfl⟩
rfl
/-- Walks of length two from `u` to `v` correspond bijectively to common neighbours of `u` and `v`.
Note that `u` and `v` may be the same. -/
@[simps]
def walkLengthTwoEquivCommonNeighbors (u v : V) :
{p : G.Walk u v // p.length = 2} ≃ G.commonNeighbors u v where
toFun p := ⟨p.val.snd, match p with
| ⟨.cons _ (.cons _ .nil), _⟩ => ⟨‹G.Adj u _›, ‹G.Adj _ v›.symm⟩⟩
invFun w := ⟨w.prop.1.toWalk.concat w.prop.2.symm, rfl⟩
left_inv | ⟨.cons _ (.cons _ .nil), hp⟩ => by rfl
right_inv _ := rfl
section LocallyFinite
variable [DecidableEq V] [LocallyFinite G]
/-- The `Finset` of length-`n` walks from `u` to `v`.
This is used to give `{p : G.walk u v | p.length = n}` a `Fintype` instance, and it
can also be useful as a recursive description of this set when `V` is finite.
See `SimpleGraph.coe_finsetWalkLength_eq` for the relationship between this `Finset` and
the set of length-`n` walks. -/
def finsetWalkLength (n : ℕ) (u v : V) : Finset (G.Walk u v) :=
match n with
| 0 =>
if h : u = v then by
subst u
exact {Walk.nil}
else ∅
| n + 1 =>
Finset.univ.biUnion fun (w : G.neighborSet u) =>
(finsetWalkLength n w v).map ⟨fun p => Walk.cons w.property p, fun _ _ => by simp⟩
theorem coe_finsetWalkLength_eq (n : ℕ) (u v : V) :
(G.finsetWalkLength n u v : Set (G.Walk u v)) = {p : G.Walk u v | p.length = n} := by
induction n generalizing u v with
| zero =>
obtain rfl | huv := eq_or_ne u v <;> simp [finsetWalkLength, set_walk_length_zero_eq_of_ne, *]
| succ n ih =>
simp only [finsetWalkLength, set_walk_length_succ_eq, Finset.coe_biUnion, Finset.mem_coe,
Finset.mem_univ, Set.iUnion_true]
ext p
simp only [mem_neighborSet, Finset.coe_map, Embedding.coeFn_mk, Set.iUnion_coe_set,
Set.mem_iUnion, Set.mem_image, Finset.mem_coe, Set.mem_setOf_eq]
congr!
rename_i w _ q
have := Set.ext_iff.mp (ih w v) q
simp only [Finset.mem_coe, Set.mem_setOf_eq] at this
rw [← this]
variable {G}
theorem mem_finsetWalkLength_iff {n : ℕ} {u v : V} {p : G.Walk u v} :
p ∈ G.finsetWalkLength n u v ↔ p.length = n :=
Set.ext_iff.mp (G.coe_finsetWalkLength_eq n u v) p
variable (G)
/-- The `Finset` of walks from `u` to `v` with length less than `n`. See `finsetWalkLength` for
context. In particular, we use this definition for `SimpleGraph.Path.instFintype`. -/
def finsetWalkLengthLT (n : ℕ) (u v : V) : Finset (G.Walk u v) :=
(Finset.range n).disjiUnion
(fun l ↦ G.finsetWalkLength l u v)
(fun l _ l' _ hne _ hsl hsl' p hp ↦
have hl : p.length = l := mem_finsetWalkLength_iff.mp (hsl hp)
have hl' : p.length = l' := mem_finsetWalkLength_iff.mp (hsl' hp)
False.elim <| hne <| hl.symm.trans hl')
open Finset in
theorem coe_finsetWalkLengthLT_eq (n : ℕ) (u v : V) :
(G.finsetWalkLengthLT n u v : Set (G.Walk u v)) = {p : G.Walk u v | p.length < n} := by
ext p
simp [finsetWalkLengthLT, mem_coe, mem_disjiUnion, mem_finsetWalkLength_iff]
variable {G}
theorem mem_finsetWalkLengthLT_iff {n : ℕ} {u v : V} {p : G.Walk u v} :
p ∈ G.finsetWalkLengthLT n u v ↔ p.length < n :=
Set.ext_iff.mp (G.coe_finsetWalkLengthLT_eq n u v) p
variable (G)
instance fintypeSetWalkLength (u v : V) (n : ℕ) : Fintype {p : G.Walk u v | p.length = n} :=
Fintype.ofFinset (G.finsetWalkLength n u v) fun p => by
rw [← Finset.mem_coe, coe_finsetWalkLength_eq]
instance fintypeSubtypeWalkLength (u v : V) (n : ℕ) : Fintype {p : G.Walk u v // p.length = n} :=
fintypeSetWalkLength G u v n
theorem set_walk_length_toFinset_eq (n : ℕ) (u v : V) :
{p : G.Walk u v | p.length = n}.toFinset = G.finsetWalkLength n u v := by
ext p
simp [← coe_finsetWalkLength_eq]
/- See `SimpleGraph.adjMatrix_pow_apply_eq_card_walk` for the cardinality in terms of the `n`th
power of the adjacency matrix. -/
theorem card_set_walk_length_eq (u v : V) (n : ℕ) :
Fintype.card {p : G.Walk u v | p.length = n} = #(G.finsetWalkLength n u v) :=
Fintype.card_ofFinset (G.finsetWalkLength n u v) fun p => by
rw [← Finset.mem_coe, coe_finsetWalkLength_eq]
instance fintypeSetWalkLengthLT (u v : V) (n : ℕ) : Fintype {p : G.Walk u v | p.length < n} :=
Fintype.ofFinset (G.finsetWalkLengthLT n u v) fun p ↦ by
rw [← Finset.mem_coe, coe_finsetWalkLengthLT_eq]
instance fintypeSubtypeWalkLengthLT (u v : V) (n : ℕ) : Fintype {p : G.Walk u v // p.length < n} :=
fintypeSetWalkLengthLT G u v n
instance fintypeSetPathLength (u v : V) (n : ℕ) :
Fintype {p : G.Walk u v | p.IsPath ∧ p.length = n} :=
Fintype.ofFinset {w ∈ G.finsetWalkLength n u v | w.IsPath} <| by
simp [mem_finsetWalkLength_iff, and_comm]
instance fintypeSubtypePathLength (u v : V) (n : ℕ) :
Fintype {p : G.Walk u v // p.IsPath ∧ p.length = n} :=
fintypeSetPathLength G u v n
instance fintypeSetPathLengthLT (u v : V) (n : ℕ) :
Fintype {p : G.Walk u v | p.IsPath ∧ p.length < n} :=
Fintype.ofFinset {w ∈ G.finsetWalkLengthLT n u v | w.IsPath} <| by
simp [mem_finsetWalkLengthLT_iff, and_comm]
instance fintypeSubtypePathLengthLT (u v : V) (n : ℕ) :
Fintype {p : G.Walk u v // p.IsPath ∧ p.length < n} :=
fintypeSetPathLengthLT G u v n
end LocallyFinite
instance [Finite V] : Finite G.ConnectedComponent := Quot.finite _
section Fintype
variable [DecidableEq V] [Fintype V] [DecidableRel G.Adj]
theorem reachable_iff_exists_finsetWalkLength_nonempty (u v : V) :
G.Reachable u v ↔ ∃ n : Fin (Fintype.card V), (G.finsetWalkLength n u v).Nonempty := by
constructor
· intro r
refine r.elim_path fun p => ?_
refine ⟨⟨_, p.isPath.length_lt⟩, p, ?_⟩
simp [mem_finsetWalkLength_iff]
· rintro ⟨_, p, _⟩
exact ⟨p⟩
instance : DecidableRel G.Reachable := fun u v =>
decidable_of_iff' _ (reachable_iff_exists_finsetWalkLength_nonempty G u v)
instance : Fintype G.ConnectedComponent :=
@Quotient.fintype _ _ G.reachableSetoid (inferInstance : DecidableRel G.Reachable)
instance : Decidable G.Preconnected :=
inferInstanceAs <| Decidable (∀ u v, G.Reachable u v)
instance : Decidable G.Connected :=
decidable_of_iff (G.Preconnected ∧ (Finset.univ : Finset V).Nonempty) <| by
rw [connected_iff, ← Finset.univ_nonempty_iff]
instance Path.instFintype {u v : V} : Fintype (G.Path u v) where
elems := (univ (α := { p : G.Walk u v | p.IsPath ∧ p.length < Fintype.card V })).map
⟨fun p ↦ { val := p.val, property := p.prop.left },
fun _ _ h ↦ SetCoe.ext <| Subtype.mk.injEq .. ▸ h⟩
complete p := mem_map.mpr ⟨
⟨p.val, ⟨p.prop, p.prop.length_lt⟩⟩,
⟨mem_univ _, rfl⟩⟩
instance instDecidableMemSupp (c : G.ConnectedComponent) (v : V) : Decidable (v ∈ c.supp) :=
c.recOn (fun w ↦ decidable_of_iff (G.Reachable v w) <| by simp)
(fun _ _ _ _ ↦ Subsingleton.elim _ _)
variable {G} in
lemma disjiUnion_supp_toFinset_eq_supp_toFinset {G' : SimpleGraph V} (h : G ≤ G')
(c' : ConnectedComponent G') [Fintype c'.supp]
[DecidablePred fun c : G.ConnectedComponent ↦ c.supp ⊆ c'.supp] :
.disjiUnion {c : ConnectedComponent G | c.supp ⊆ c'.supp} (fun c ↦ c.supp.toFinset)
(fun x _ y _ hxy ↦ by simpa using pairwise_disjoint_supp_connectedComponent _ hxy) =
c'.supp.toFinset :=
Finset.coe_injective <| by simpa using ConnectedComponent.biUnion_supp_eq_supp h _
end Fintype
/-- The odd components are the connected components of odd cardinality. This definition excludes
infinite components. -/
abbrev oddComponents : Set G.ConnectedComponent := {c : G.ConnectedComponent | Odd c.supp.ncard}
lemma ConnectedComponent.odd_card_supp_iff_odd_subcomponents [Finite V] {G'}
(h : G ≤ G') (c' : ConnectedComponent G') :
Odd c'.supp.ncard ↔
Odd {c : ConnectedComponent G | c.supp ⊆ c'.supp ∧ Odd c.supp.ncard}.ncard := by
simp_rw [← Set.Nat.card_coe_set_eq]
classical
cases nonempty_fintype V
rw [Nat.card_eq_card_toFinset, ← disjiUnion_supp_toFinset_eq_supp_toFinset h]
simp only [Finset.card_disjiUnion, Set.toFinset_card]
rw [Finset.odd_sum_iff_odd_card_odd, Nat.card_eq_fintype_card, Fintype.card_ofFinset]
simp only [Nat.card_eq_fintype_card, Finset.filter_filter]
rfl
lemma odd_card_iff_odd_components [Finite V] : Odd (Nat.card V) ↔ Odd G.oddComponents.ncard := by
classical
cases nonempty_fintype V
rw [Nat.card_eq_fintype_card]
simp only [← (set_fintype_card_eq_univ_iff _).mpr G.iUnion_connectedComponentSupp,
ConnectedComponent.mem_supp_iff, Fintype.card_subtype_compl,
← Set.toFinset_card, Set.toFinset_iUnion ConnectedComponent.supp]
rw [Finset.card_biUnion
(fun x _ y _ hxy ↦ Set.disjoint_toFinset.mpr (pairwise_disjoint_supp_connectedComponent _ hxy))]
simp_rw [Set.toFinset_card, ← Nat.card_eq_fintype_card, ← Finset.coe_filter_univ,
Set.ncard_coe_Finset, Set.Nat.card_coe_set_eq]
exact (Finset.odd_sum_iff_odd_card_odd (fun x : G.ConnectedComponent ↦ x.supp.ncard))
lemma ncard_odd_components_mono [Finite V] {G' : SimpleGraph V} (h : G ≤ G') :
G'.oddComponents.ncard ≤ G.oddComponents.ncard := by
have aux (c : G'.ConnectedComponent) (hc : Odd c.supp.ncard) :
{c' : G.ConnectedComponent | c'.supp ⊆ c.supp ∧ Odd c'.supp.ncard}.Nonempty := by
refine Set.nonempty_of_ncard_ne_zero fun h' ↦ ?_
simpa [-Nat.card_eq_fintype_card, -Set.coe_setOf, h']
using (c.odd_card_supp_iff_odd_subcomponents _ h).mp hc
let f : G'.oddComponents → G.oddComponents :=
fun ⟨c, hc⟩ ↦ ⟨(aux c hc).choose, (aux c hc).choose_spec.2⟩
refine Finite.card_le_of_injective f fun c c' fcc' ↦ ?_
simp only [Subtype.mk.injEq, f] at fcc'
exact Subtype.val_injective (ConnectedComponent.eq_of_common_vertex
((fcc' ▸ (aux c.1 c.2).choose_spec.1) (ConnectedComponent.nonempty_supp _).some_mem)
((aux c'.1 c'.2).choose_spec.1 (ConnectedComponent.nonempty_supp _).some_mem))
end WalkCounting
end SimpleGraph