forked from leanprover-community/mathlib4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubgraph.lean
More file actions
720 lines (598 loc) · 31 KB
/
Copy pathSubgraph.lean
File metadata and controls
720 lines (598 loc) · 31 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/-
Copyright (c) 2023 Kyle Miller, Rémi Bottinelli. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kyle Miller, Rémi Bottinelli
-/
module
public import Mathlib.Combinatorics.SimpleGraph.Connectivity.Connected
public import Mathlib.Combinatorics.SimpleGraph.Walk.Chord
public import Mathlib.Data.Set.Card
/-!
# Connectivity of subgraphs and induced graphs
## Main definitions
* `SimpleGraph.Subgraph.Preconnected` and `SimpleGraph.Subgraph.Connected` give subgraphs
connectivity predicates via `SimpleGraph.Subgraph.coe`.
-/
@[expose] public section
namespace SimpleGraph
universe u v
variable {V : Type u} {V' : Type v} {G : SimpleGraph V} {G' : SimpleGraph V'}
namespace Subgraph
/-- A subgraph is preconnected if it is preconnected when coerced to be a simple graph.
Note: This is a structure to make it so one can be precise about how dot notation resolves. -/
protected structure Preconnected (H : G.Subgraph) : Prop where
protected coe : H.coe.Preconnected
instance {H : G.Subgraph} : Coe H.Preconnected H.coe.Preconnected := ⟨Preconnected.coe⟩
instance {H : G.Subgraph} : CoeFun H.Preconnected (fun _ => ∀ u v : H.verts, H.coe.Reachable u v) :=
⟨fun h => h.coe⟩
protected lemma preconnected_iff {H : G.Subgraph} :
H.Preconnected ↔ H.coe.Preconnected := ⟨fun ⟨h⟩ => h, .mk⟩
/-- A subgraph is connected if it is connected when coerced to be a simple graph.
Note: This is a structure to make it so one can be precise about how dot notation resolves. -/
protected structure Connected (H : G.Subgraph) : Prop where
protected coe : H.coe.Connected
instance {H : G.Subgraph} : Coe H.Connected H.coe.Connected := ⟨Connected.coe⟩
instance {H : G.Subgraph} : CoeFun H.Connected (fun _ => ∀ u v : H.verts, H.coe.Reachable u v) :=
⟨fun h => h.coe⟩
protected lemma connected_iff' {H : G.Subgraph} :
H.Connected ↔ H.coe.Connected := ⟨fun ⟨h⟩ => h, .mk⟩
protected lemma connected_iff {H : G.Subgraph} :
H.Connected ↔ H.Preconnected ∧ H.verts.Nonempty := by
rw [H.connected_iff', connected_iff, H.preconnected_iff, Set.nonempty_coe_sort]
protected lemma Connected.preconnected {H : G.Subgraph} (h : H.Connected) : H.Preconnected := by
rw [H.connected_iff] at h; exact h.1
protected lemma Connected.nonempty {H : G.Subgraph} (h : H.Connected) : H.verts.Nonempty := by
rw [H.connected_iff] at h; exact h.2
theorem singletonSubgraph_connected {v : V} : (G.singletonSubgraph v).Connected :=
⟨⟨Preconnected.of_subsingleton⟩⟩
@[simp]
theorem subgraphOfAdj_connected {v w : V} (hvw : G.Adj v w) : (G.subgraphOfAdj hvw).Connected := by
refine ⟨⟨?_⟩⟩
rintro ⟨a, ha⟩ ⟨b, hb⟩
simp only [subgraphOfAdj_verts, Set.mem_insert_iff, Set.mem_singleton_iff] at ha hb
obtain rfl | rfl := ha <;> obtain rfl | rfl := hb <;>
first | rfl | (apply Adj.reachable; simp)
lemma top_induce_pair_connected_of_adj {u v : V} (huv : G.Adj u v) :
((⊤ : G.Subgraph).induce {u, v}).Connected := by
rw [← subgraphOfAdj_eq_induce huv]
exact subgraphOfAdj_connected huv
@[gcongr, mono]
protected lemma Connected.mono {H H' : G.Subgraph} (hle : H ≤ H') (hv : H.verts = H'.verts)
(h : H.Connected) : H'.Connected := by
rw [← Subgraph.copy_eq H' H.verts hv H'.Adj rfl]
refine ⟨h.coe.mono ?_⟩
rintro ⟨v, hv⟩ ⟨w, hw⟩ hvw
exact hle.2 hvw
protected lemma Connected.mono' {H H' : G.Subgraph}
(hle : ∀ v w, H.Adj v w → H'.Adj v w) (hv : H.verts = H'.verts)
(h : H.Connected) : H'.Connected := by
exact h.mono ⟨hv.le, hle⟩ hv
lemma connected_sup {H K : G.Subgraph}
(hH : H.Preconnected) (hK : K.Preconnected) (hn : (H ⊓ K).verts.Nonempty) :
(H ⊔ K).Connected := by
rw [Subgraph.connected_iff', connected_iff_exists_forall_reachable]
obtain ⟨u, hu, hu'⟩ := hn
exists ⟨u, Or.inl hu⟩
rintro ⟨v, (hv | hv)⟩
· exact Reachable.map (Subgraph.inclusion (le_sup_left : H ≤ H ⊔ K)) (hH ⟨u, hu⟩ ⟨v, hv⟩)
· exact Reachable.map (Subgraph.inclusion (le_sup_right : K ≤ H ⊔ K)) (hK ⟨u, hu'⟩ ⟨v, hv⟩)
lemma Preconnected.degree_zero_iff {H : G.Subgraph} (h : H.Preconnected) (v : H.verts)
[Fintype (H.neighborSet v)] : H.degree v = 0 ↔ H.verts.Subsingleton := by
refine ⟨fun hv ↦ Set.not_nontrivial_iff.mp fun hn ↦ ?_, (degree_eq_zero_of_subsingleton H _ ·)⟩
have := hn.coe_sort
simpa [hv] using h.coe.degree_pos_of_nontrivial v
lemma Preconnected.exists_adj_of_nontrivial {H : G.Subgraph} [Nontrivial H.verts]
(h : H.Preconnected) (v : H.verts) : ∃ u, H.Adj v u := by
have := h.coe.exists_adj_of_nontrivial v
tauto
/--
This lemma establishes a condition under which a subgraph is the same as a connected component.
Note the asymmetry in the hypothesis `h`: `v` is in `H.verts`, but `w` is not required to be.
-/
lemma Connected.exists_verts_eq_connectedComponentSupp {H : Subgraph G}
(hc : H.Connected) (h : ∀ v ∈ H.verts, ∀ w, G.Adj v w → H.Adj v w) :
∃ c : G.ConnectedComponent, H.verts = c.supp := by
rw [SimpleGraph.ConnectedComponent.exists]
obtain ⟨v, hv⟩ := hc.nonempty
use v
ext w
simp only [ConnectedComponent.mem_supp_iff, ConnectedComponent.eq]
exact ⟨fun hw ↦ by simpa using (hc ⟨w, hw⟩ ⟨v, hv⟩).map H.hom,
fun a ↦ a.symm.mem_subgraphVerts h hv⟩
end Subgraph
namespace ConnectedComponent
variable (C : G.ConnectedComponent)
/-- The induced subgraph of a connected component. -/
abbrev toSubgraph : G.Subgraph :=
.induce ⊤ C.supp
@[simp]
lemma coe_toSubgraph : C.toSubgraph.coe = C.toSimpleGraph :=
induce_eq_coe_induce_top C.supp |>.symm
@[simp]
lemma spanningCoe_toSubgraph : C.toSubgraph.spanningCoe = C.toSimpleGraph.spanningCoe :=
spanningCoe_induce_top _
lemma connected_toSubgraph : C.toSubgraph.Connected :=
⟨C.coe_toSubgraph ▸ C.connected_toSimpleGraph⟩
theorem maximal_connected_toSubgraph (C : G.ConnectedComponent) :
Maximal Subgraph.Connected C.toSubgraph := by
refine C.ind fun v ↦ ⟨connected_toSubgraph _, fun G' hconn hle ↦ ?_⟩
refine le_trans Subgraph.le_induce_top_verts <| Subgraph.induce_mono_right fun u hu ↦ ?_
exact ConnectedComponent.sound <| hconn.coe.preconnected ⟨u, hu⟩ ⟨v, hle.left rfl⟩ |>.map G'.hom
theorem maximal_subgraph_connected_iff (G' : G.Subgraph) :
Maximal Subgraph.Connected G' ↔ ∃ C : G.ConnectedComponent, C.toSubgraph = G' := by
refine ⟨fun ⟨hconn, h⟩ ↦ ?_, fun ⟨C, h⟩ ↦ ?_⟩
· have ⟨v, hv⟩ := hconn.nonempty
suffices G' ≤ (G.connectedComponentMk v).toSubgraph from
⟨G.connectedComponentMk v, le_antisymm (h (connected_toSubgraph _) this) this⟩
exact le_trans Subgraph.le_induce_top_verts <| Subgraph.induce_mono_right fun u hu ↦
ConnectedComponent.sound <| hconn.coe.preconnected ⟨u, hu⟩ ⟨v, hv⟩ |>.map G'.hom
· exact h ▸ maximal_connected_toSubgraph _
end ConnectedComponent
/-! ### Walks as subgraphs -/
namespace Walk
variable {u v w : V}
/-- The subgraph consisting of the vertices and edges of the walk. -/
@[simp]
protected def toSubgraph {u v : V} : G.Walk u v → G.Subgraph
| nil => G.singletonSubgraph u
| cons h p => G.subgraphOfAdj h ⊔ p.toSubgraph
theorem toSubgraph_cons_nil_eq_subgraphOfAdj (h : G.Adj u v) :
(cons h nil).toSubgraph = G.subgraphOfAdj h := by simp
theorem mem_verts_toSubgraph (p : G.Walk u v) : w ∈ p.toSubgraph.verts ↔ w ∈ p.support := by
induction p with
| nil => simp
| cons h p' ih =>
rename_i x y z
have : w = y ∨ w ∈ p'.support ↔ w ∈ p'.support :=
⟨by rintro (rfl | h) <;> simp [*], by simp +contextual⟩
simp [ih, or_assoc, this]
lemma not_nil_of_adj_toSubgraph {u v} {x : V} {p : G.Walk u v} (hadj : p.toSubgraph.Adj w x) :
¬p.Nil := by
cases p <;> simp_all
lemma start_mem_verts_toSubgraph (p : G.Walk u v) : u ∈ p.toSubgraph.verts := by
simp [mem_verts_toSubgraph]
lemma end_mem_verts_toSubgraph (p : G.Walk u v) : v ∈ p.toSubgraph.verts := by
simp [mem_verts_toSubgraph]
@[simp]
theorem verts_toSubgraph (p : G.Walk u v) : p.toSubgraph.verts = { w | w ∈ p.support } :=
Set.ext fun _ => p.mem_verts_toSubgraph
theorem mem_edges_toSubgraph (p : G.Walk u v) {e : Sym2 V} :
e ∈ p.toSubgraph.edgeSet ↔ e ∈ p.edges := by induction p <;> simp [*]
@[simp]
theorem edgeSet_toSubgraph (p : G.Walk u v) : p.toSubgraph.edgeSet = p.edgeSet :=
Set.ext fun _ => p.mem_edges_toSubgraph
@[simp]
theorem toSubgraph_append (p : G.Walk u v) (q : G.Walk v w) :
(p.append q).toSubgraph = p.toSubgraph ⊔ q.toSubgraph := by induction p <;> simp [*, sup_assoc]
@[simp]
theorem toSubgraph_reverse (p : G.Walk u v) : p.reverse.toSubgraph = p.toSubgraph := by
induction p with
| nil => simp
| cons _ _ _ =>
simp only [*, Walk.toSubgraph, reverse_cons, toSubgraph_append, subgraphOfAdj_symm]
rw [sup_comm]
congr
ext <;> simp [-Set.bot_eq_empty]
@[simp]
theorem toSubgraph_rotate [DecidableEq V] (c : G.Walk v v) (h : u ∈ c.support) :
(c.rotate u h).toSubgraph = c.toSubgraph := by
rw [rotate, toSubgraph_append, sup_comm, ← toSubgraph_append, take_spec]
@[simp]
theorem toSubgraph_map (f : G →g G') (p : G.Walk u v) :
(p.map f).toSubgraph = p.toSubgraph.map f := by induction p <;> simp [*, Subgraph.map_sup]
set_option backward.isDefEq.respectTransparency false in
lemma adj_toSubgraph_mapLe {G' : SimpleGraph V} {w x : V} {p : G.Walk u v} (h : G ≤ G') :
(p.mapLe h).toSubgraph.Adj w x ↔ p.toSubgraph.Adj w x := by
simp
@[simp]
theorem finite_neighborSet_toSubgraph (p : G.Walk u v) : (p.toSubgraph.neighborSet w).Finite := by
induction p with
| nil =>
rw [Walk.toSubgraph, neighborSet_singletonSubgraph]
apply Set.toFinite
| cons ha _ ih =>
rw [Walk.toSubgraph, Subgraph.neighborSet_sup]
refine Set.Finite.union ?_ ih
refine Set.Finite.subset ?_ (neighborSet_subgraphOfAdj_subset ha)
apply Set.toFinite
lemma toSubgraph_le_induce_support (p : G.Walk u v) :
p.toSubgraph ≤ (⊤ : G.Subgraph).induce {v | v ∈ p.support} := by
convert! Subgraph.le_induce_top_verts
exact p.verts_toSubgraph.symm
theorem toSubgraph_adj_getVert {u v} (w : G.Walk u v) {i : ℕ} (hi : i < w.length) :
w.toSubgraph.Adj (w.getVert i) (w.getVert (i + 1)) := by
induction w generalizing i with
| nil => cases hi
| cons hxy i' ih =>
cases i
· simp
· simp only [Walk.toSubgraph, getVert_cons_succ, Subgraph.sup_adj, subgraphOfAdj_adj, Sym2.eq,
Sym2.rel_iff', Prod.mk.injEq, Prod.swap_prod_mk]
right
exact ih (Nat.succ_lt_succ_iff.mp hi)
theorem toSubgraph_adj_snd {u v} (w : G.Walk u v) (h : ¬ w.Nil) : w.toSubgraph.Adj u w.snd := by
simpa using w.toSubgraph_adj_getVert (not_nil_iff_lt_length.mp h)
theorem toSubgraph_adj_penultimate {u v} (w : G.Walk u v) (h : ¬ w.Nil) :
w.toSubgraph.Adj w.penultimate v := by
rw [not_nil_iff_lt_length] at h
simpa [show w.length - 1 + 1 = w.length by lia]
using w.toSubgraph_adj_getVert (by lia : w.length - 1 < w.length)
theorem toSubgraph_adj_iff {u v u' v'} (w : G.Walk u v) :
w.toSubgraph.Adj u' v' ↔ ∃ i, s(w.getVert i, w.getVert (i + 1)) =
s(u', v') ∧ i < w.length := by
constructor
· intro hadj
unfold Walk.toSubgraph at hadj
match w with
| .nil =>
simp only [singletonSubgraph_adj, Pi.bot_apply, Prop.bot_eq_false] at hadj
| .cons h p =>
simp only [Subgraph.sup_adj, subgraphOfAdj_adj, Sym2.eq, Sym2.rel_iff', Prod.mk.injEq,
Prod.swap_prod_mk] at hadj
cases hadj with
| inl hl =>
use 0
simp only [Walk.getVert_zero, zero_add, getVert_cons_succ]
refine ⟨?_, by simp only [length_cons, Nat.zero_lt_succ]⟩
exact Sym2.eq_iff.mpr hl
| inr hr =>
obtain ⟨i, hi⟩ := (toSubgraph_adj_iff _).mp hr
use i + 1
simp only [getVert_cons_succ]
constructor
· exact hi.1
· simp only [Walk.length_cons, Nat.add_lt_add_right hi.2 1]
· rintro ⟨i, hi⟩
rw [← Subgraph.mem_edgeSet, ← hi.1, Subgraph.mem_edgeSet]
exact toSubgraph_adj_getVert _ hi.2
lemma mem_support_of_adj_toSubgraph {u v u' v' : V} {p : G.Walk u v} (hp : p.toSubgraph.Adj u' v') :
u' ∈ p.support := p.mem_verts_toSubgraph.mp (p.toSubgraph.edge_vert hp)
lemma adj_toSubgraph_iff_mem_edges {u v u' v' : V} {p : G.Walk u v} :
p.toSubgraph.Adj u' v' ↔ s(u', v') ∈ p.edges := by
rw [← p.mem_edges_toSubgraph, Subgraph.mem_edgeSet]
theorem toSubgraph_le_iff {w : G.Walk u v} (hnil : ¬w.Nil) {G' : G.Subgraph} :
w.toSubgraph ≤ G' ↔ w.edgeSet ⊆ G'.edgeSet := by
refine ⟨fun hw e he ↦ Subgraph.edgeSet_mono hw <| w.mem_edges_toSubgraph.mpr he, fun hw ↦ ?_⟩
refine ⟨fun v' hv' ↦ ?_, fun u' v' hadj ↦ hw <| w.mem_edges_toSubgraph.mp (hadj : s(_, _) ∈ _)⟩
rw [mem_verts_toSubgraph, mem_support_iff_exists_mem_edges_of_not_nil hnil] at hv'
have ⟨e, he, hv'e⟩ := hv'
exact G'.mem_verts_of_mem_edge (hw he) hv'e
lemma toSubgraph_bypass_le_toSubgraph {u v : V} {p : G.Walk u v} [DecidableEq V] :
p.bypass.toSubgraph ≤ p.toSubgraph := by
constructor
· simpa using! p.support_bypass_subset_support
· simpa [adj_toSubgraph_iff_mem_edges] using! fun _ _ h ↦ p.edges_toPath_subset_edges h
/-- Map a walk to its own subgraph. -/
def mapToSubgraph {u v : V} : ∀ w : G.Walk u v, w.toSubgraph.coe.Walk
⟨_, w.start_mem_verts_toSubgraph⟩ ⟨_, w.end_mem_verts_toSubgraph⟩
| nil => nil
| cons .. =>
let h : cons .. |>.toSubgraph.Adj .. := (le_sup_left : _ ≤ Walk.toSubgraph _).right rfl
let h : cons .. |>.toSubgraph.coe.Adj ⟨_, h.fst_mem⟩ ⟨_, h.snd_mem⟩ := h
cons h <| mapToSubgraph _ |>.map <| Subgraph.inclusion le_sup_right
set_option backward.isDefEq.respectTransparency false in
/-- Mapping a walk to its own subgraph and then to the original graph produces the same walk. -/
theorem map_mapToSubgraph_hom {u v : V} : ∀ w : G.Walk u v, w.mapToSubgraph.map w.toSubgraph.hom = w
| nil => rfl
| cons _ w => by
rw [mapToSubgraph, Walk.map, map_map]
exact congrArg₂ _ rfl w.map_mapToSubgraph_hom
set_option backward.isDefEq.respectTransparency false in
/-- Mapping a walk to its own subgraph and then to `G[s]` where `s` contains the walk's support is
the same as inducing the walk to `s`. -/
theorem map_mapToSubgraph_eq_induce (s : Set V) {u v : V} :
∀ (w : G.Walk u v) (hs : ∀ x ∈ w.support, x ∈ s),
w.mapToSubgraph.map (⟨(⟨·, by grind [mem_verts_toSubgraph]⟩), w.toSubgraph.adj_sub⟩ :
w.toSubgraph.coe →g G.induce s) = w.induce s hs
| nil, hs => rfl
| cons hadj w, hs => by
rw [mapToSubgraph, map_cons, map_map]
exact congrArg _ <| w.map_mapToSubgraph_eq_induce s (hs · <| List.mem_of_mem_tail ·)
/-- Mapping a walk to its own subgraph and then to `G[w.support]` is the same as inducing the walk
to its support. -/
theorem map_mapToSubgraph_eq_induce_id {u v : V} (w : G.Walk u v) :
w.mapToSubgraph.map (⟨fun v ↦ ⟨v, w.mem_verts_toSubgraph.mp v.prop⟩, w.toSubgraph.adj_sub⟩ :
w.toSubgraph.coe →g G.induce _) = w.induce _ (fun _ ↦ id) :=
w.map_mapToSubgraph_eq_induce ..
theorem isInduced_toSubgraph {w : G.Walk u v} : w.toSubgraph.IsInduced ↔ w.IsChordless := by
simp_rw [Subgraph.IsInduced, IsChordless, IsChord, Sym2.forall, Sym2.lift_mk, G.mem_edgeSet,
mem_verts_toSubgraph, adj_toSubgraph_iff_mem_edges]
grind only
namespace IsPath
lemma neighborSet_toSubgraph_startpoint {u v} {p : G.Walk u v}
(hp : p.IsPath) (hnp : ¬ p.Nil) : p.toSubgraph.neighborSet u = {p.snd} := by
have hadj1 := p.toSubgraph_adj_snd hnp
ext v
simp_all only [Subgraph.mem_neighborSet, Set.mem_singleton_iff,
SimpleGraph.Walk.toSubgraph_adj_iff, Sym2.eq, Sym2.rel_iff', Prod.mk.injEq, Prod.swap_prod_mk]
grind [getVert_eq_start_iff]
lemma neighborSet_toSubgraph_endpoint {u v} {p : G.Walk u v}
(hp : p.IsPath) (hnp : ¬ p.Nil) : p.toSubgraph.neighborSet v = {p.penultimate} := by
simpa using IsPath.neighborSet_toSubgraph_startpoint hp.reverse
(by rw [Walk.not_nil_iff_lt_length, Walk.length_reverse]; exact
Walk.not_nil_iff_lt_length.mp hnp)
lemma neighborSet_toSubgraph_internal {u} {i : ℕ} {p : G.Walk u v} (hp : p.IsPath)
(h : i ≠ 0) (h' : i < p.length) :
p.toSubgraph.neighborSet (p.getVert i) = {p.getVert (i - 1), p.getVert (i + 1)} := by
have hadj1 := ((show i - 1 + 1 = i by lia) ▸
p.toSubgraph_adj_getVert (by lia : (i - 1) < p.length)).symm
ext v
simp_all only [ne_eq, Subgraph.mem_neighborSet, Set.mem_insert_iff, Set.mem_singleton_iff,
SimpleGraph.Walk.toSubgraph_adj_iff, Sym2.eq, Sym2.rel_iff', Prod.mk.injEq,
Prod.swap_prod_mk]
refine ⟨?_, by aesop⟩
rintro ⟨i', ⟨hl, _⟩ | ⟨_, hl⟩⟩ <;>
apply hp.getVert_injOn (by rw [Set.mem_setOf_eq]; lia)
(by rw [Set.mem_setOf_eq]; lia) at hl <;> aesop
lemma ncard_neighborSet_toSubgraph_internal_eq_two {u} {i : ℕ} {p : G.Walk u v} (hp : p.IsPath)
(h : i ≠ 0) (h' : i < p.length) :
(p.toSubgraph.neighborSet (p.getVert i)).ncard = 2 := by
rw [hp.neighborSet_toSubgraph_internal h h']
have : p.getVert (i - 1) ≠ p.getVert (i + 1) := by
intro h
have := hp.getVert_injOn (by rw [Set.mem_setOf_eq]; lia) (by rw [Set.mem_setOf_eq]; lia) h
lia
simp_all
lemma snd_of_toSubgraph_adj {u v v'} {p : G.Walk u v} (hp : p.IsPath)
(hadj : p.toSubgraph.Adj u v') : p.snd = v' := by
have ⟨i, hi⟩ := p.toSubgraph_adj_iff.mp hadj
simp only [Sym2.eq, Sym2.rel_iff', Prod.mk.injEq, Prod.swap_prod_mk] at hi
rcases hi.1 with ⟨hl1, rfl⟩ | ⟨hr1, hr2⟩
· have : i = 0 := by
apply hp.getVert_injOn (by rw [Set.mem_setOf]; lia) (by rw [Set.mem_setOf]; lia)
rw [p.getVert_zero, hl1]
simp [this]
· have : i + 1 = 0 := by
apply hp.getVert_injOn (by rw [Set.mem_setOf]; lia) (by rw [Set.mem_setOf]; lia)
rw [p.getVert_zero, hr2]
contradiction
end IsPath
namespace IsCycle
lemma neighborSet_toSubgraph_endpoint {u} {p : G.Walk u u} (hpc : p.IsCycle) :
p.toSubgraph.neighborSet u = {p.snd, p.penultimate} := by
have hadj1 := p.toSubgraph_adj_snd hpc.not_nil
ext v
simp_all only [Subgraph.mem_neighborSet, Set.mem_insert_iff, Set.mem_singleton_iff,
SimpleGraph.Walk.toSubgraph_adj_iff, Sym2.eq, Sym2.rel_iff', Prod.mk.injEq, Prod.swap_prod_mk]
grind [getVert_endpoint_iff, add_tsub_cancel_right]
lemma neighborSet_toSubgraph_internal {u} {i : ℕ} {p : G.Walk u u} (hpc : p.IsCycle)
(h : i ≠ 0) (h' : i < p.length) :
p.toSubgraph.neighborSet (p.getVert i) = {p.getVert (i - 1), p.getVert (i + 1)} := by
have hadj1 := ((show i - 1 + 1 = i by lia) ▸
p.toSubgraph_adj_getVert (by lia : (i - 1) < p.length)).symm
ext v
simp_all only [ne_eq, Subgraph.mem_neighborSet, Set.mem_insert_iff, Set.mem_singleton_iff,
SimpleGraph.Walk.toSubgraph_adj_iff, Sym2.eq, Sym2.rel_iff', Prod.mk.injEq,
Prod.swap_prod_mk]
refine ⟨?_, by aesop⟩
rintro ⟨i', ⟨hl1, hl2⟩ | ⟨hr1, hr2⟩⟩
· apply hpc.getVert_injOn' (by rw [Set.mem_setOf_eq]; lia)
(by rw [Set.mem_setOf_eq]; lia) at hl1
simp_all
· apply hpc.getVert_injOn (by rw [Set.mem_setOf_eq]; lia)
(by rw [Set.mem_setOf_eq]; lia) at hr2
aesop
lemma ncard_neighborSet_toSubgraph_eq_two {u v} {p : G.Walk u u} (hpc : p.IsCycle)
(h : v ∈ p.support) : (p.toSubgraph.neighborSet v).ncard = 2 := by
simp only [SimpleGraph.Walk.mem_support_iff_exists_getVert] at h ⊢
obtain ⟨i, hi⟩ := h
by_cases! he : i = 0 ∨ i = p.length
· have huv : u = v := by aesop
rw [← huv, hpc.neighborSet_toSubgraph_endpoint]
exact Set.ncard_pair hpc.snd_ne_penultimate
rw [← hi.1, hpc.neighborSet_toSubgraph_internal he.1 (by lia)]
exact Set.ncard_pair (hpc.getVert_sub_one_ne_getVert_add_one (by lia))
lemma exists_isCycle_snd_verts_eq {p : G.Walk v v} (h : p.IsCycle) (hadj : p.toSubgraph.Adj v w) :
∃ (p' : G.Walk v v), p'.IsCycle ∧ p'.snd = w ∧ p'.toSubgraph.verts = p.toSubgraph.verts := by
have : w ∈ p.toSubgraph.neighborSet v := hadj
rw [h.neighborSet_toSubgraph_endpoint] at this
push _ ∈ _ at this
obtain hl | hr := this
· exact ⟨p, ⟨h, hl.symm, rfl⟩⟩
· use p.reverse
rw [penultimate, ← getVert_reverse] at hr
exact ⟨h.reverse, hr.symm, by rw [toSubgraph_reverse _]⟩
end IsCycle
open Finset
variable [DecidableEq V] {u v : V} {p : G.Walk u v}
/-- This lemma states that given some finite set of vertices, of which at least one is in the
support of a given walk, one of them is the first to be encountered. This consequence is encoded
as the set of vertices, restricted to those in the support, except for the first, being empty.
You could interpret this as being `takeUntilSet`, but defining this is slightly involved due to
not knowing what the final vertex is. This could be done by defining a function to obtain the
first encountered vertex and then use that to define `takeUntilSet`. That direction could be
worthwhile if this concept is used more widely. -/
lemma exists_mem_support_mem_erase_mem_support_takeUntil_eq_empty (s : Finset V)
(h : {x ∈ s | x ∈ p.support}.Nonempty) :
∃ x ∈ s, ∃ hx : x ∈ p.support, {t ∈ s.erase x | t ∈ (p.takeUntil x hx).support} = ∅ := by
simp only [← Finset.subset_empty]
induction hp : p.length + #s using Nat.strong_induction_on generalizing s v with | _ n ih
simp only [Finset.Nonempty, mem_filter] at h
obtain ⟨x, hxs, hx⟩ := h
obtain h | h := Finset.eq_empty_or_nonempty {t ∈ s.erase x | t ∈ (p.takeUntil x hx).support}
· use x, hxs, hx, h.le
have : (p.takeUntil x hx).length + #(s.erase x) < n := by
rw [← card_erase_add_one hxs] at hp
have := p.length_takeUntil_le_length hx
lia
obtain ⟨y, hys, hyp, h⟩ := ih _ this (s.erase x) h rfl
use y, mem_of_mem_erase hys, support_takeUntil_subset_support p hx hyp
rwa [takeUntil_takeUntil, erase_right_comm, filter_erase, erase_eq_of_notMem] at h
simp only [mem_filter, mem_erase, ne_eq, not_and, and_imp]
rintro hxy -
exact notMem_support_takeUntil_support_takeUntil_subset (Ne.symm hxy) hx hyp
lemma exists_mem_support_forall_mem_support_imp_eq (s : Finset V)
(h : {x ∈ s | x ∈ p.support}.Nonempty) :
∃ x ∈ s, ∃ (hx : x ∈ p.support),
∀ t ∈ s, t ∈ (p.takeUntil x hx).support → t = x := by
obtain ⟨x, hxs, hx, h⟩ := p.exists_mem_support_mem_erase_mem_support_takeUntil_eq_empty s h
use x, hxs, hx
suffices {t ∈ s | t ∈ (p.takeUntil x hx).support} ⊆ {x} by simpa [Finset.subset_iff] using this
rwa [Finset.filter_erase, ← Finset.subset_empty, ← Finset.subset_insert_iff,
LawfulSingleton.insert_empty_eq] at h
end Walk
namespace Subgraph
lemma _root_.SimpleGraph.Walk.toSubgraph_connected {u v : V} (p : G.Walk u v) :
p.toSubgraph.Connected := by
induction p with
| nil => apply singletonSubgraph_connected
| @cons _ w _ h p ih =>
apply Subgraph.connected_sup (subgraphOfAdj_connected h).preconnected ih.preconnected
exists w
simp
lemma induce_union_connected {H : G.Subgraph} {s t : Set V}
(sconn : (H.induce s).Preconnected) (tconn : (H.induce t).Preconnected)
(sintert : (s ⊓ t).Nonempty) :
(H.induce (s ∪ t)).Connected :=
(Subgraph.connected_sup sconn tconn sintert).mono le_induce_union <| by simp
lemma connected_induce_top_sup {H K : G.Subgraph} (Hconn : H.Preconnected) (Kconn : K.Preconnected)
{u v : V} (uH : u ∈ H.verts) (vK : v ∈ K.verts) (huv : G.Adj u v) :
((⊤ : G.Subgraph).induce {u, v} ⊔ H ⊔ K).Connected := by
refine Subgraph.connected_sup (Subgraph.connected_sup ?_ Hconn ?_).preconnected Kconn ?_
· exact (top_induce_pair_connected_of_adj huv).preconnected
· exact ⟨u, by simp [uH]⟩
· exact ⟨v, by simp [vK]⟩
set_option backward.isDefEq.respectTransparency false in
lemma preconnected_iff_forall_exists_walk_subgraph (H : G.Subgraph) :
H.Preconnected ↔ ∀ {u v}, u ∈ H.verts → v ∈ H.verts → ∃ p : G.Walk u v, p.toSubgraph ≤ H := by
constructor
· intro hc u v hu hv
refine (hc ⟨_, hu⟩ ⟨_, hv⟩).elim fun p => ?_
exists p.map (Subgraph.hom _)
simp [coeSubgraph_le]
· intro hw
rw [Subgraph.preconnected_iff]
rintro ⟨u, hu⟩ ⟨v, hv⟩
obtain ⟨p, h⟩ := hw hu hv
exact Reachable.map (Subgraph.inclusion h)
(p.toSubgraph_connected ⟨_, p.start_mem_verts_toSubgraph⟩ ⟨_, p.end_mem_verts_toSubgraph⟩)
lemma connected_iff_forall_exists_walk_subgraph (H : G.Subgraph) :
H.Connected ↔
H.verts.Nonempty ∧
∀ {u v}, u ∈ H.verts → v ∈ H.verts → ∃ p : G.Walk u v, p.toSubgraph ≤ H := by
rw [H.connected_iff, preconnected_iff_forall_exists_walk_subgraph, and_comm]
end Subgraph
section induced_subgraphs
set_option backward.isDefEq.respectTransparency false in
lemma preconnected_induce_iff {s : Set V} :
(G.induce s).Preconnected ↔ ((⊤ : G.Subgraph).induce s).Preconnected := by
rw [induce_eq_coe_induce_top, ← Subgraph.preconnected_iff]
set_option backward.isDefEq.respectTransparency false in
lemma connected_induce_iff {s : Set V} :
(G.induce s).Connected ↔ ((⊤ : G.Subgraph).induce s).Connected := by
rw [induce_eq_coe_induce_top, ← Subgraph.connected_iff']
lemma induce_union_connected {s t : Set V}
(sconn : (G.induce s).Preconnected) (tconn : (G.induce t).Preconnected)
(sintert : (s ∩ t).Nonempty) :
(G.induce (s ∪ t)).Connected := by
rw [connected_induce_iff]
rw [preconnected_induce_iff] at sconn tconn
exact Subgraph.induce_union_connected sconn tconn sintert
lemma induce_pair_connected_of_adj {u v : V} (huv : G.Adj u v) :
(G.induce {u, v}).Connected := by
rw [connected_induce_iff]
exact Subgraph.top_induce_pair_connected_of_adj huv
lemma Subgraph.Connected.induce_verts {H : G.Subgraph} (h : H.Connected) :
(G.induce H.verts).Connected := by
rw [connected_induce_iff]
exact h.mono le_induce_top_verts (by exact rfl)
lemma Walk.connected_induce_support {u v : V} (p : G.Walk u v) :
(G.induce {v | v ∈ p.support}).Connected := by
rw [← p.verts_toSubgraph]
exact p.toSubgraph_connected.induce_verts
lemma connected_induce_union {v w : V} {s t : Set V}
(sconn : (G.induce s).Preconnected) (tconn : (G.induce t).Preconnected)
(hv : v ∈ s) (hw : w ∈ t) (ha : G.Adj v w) :
(G.induce (s ∪ t)).Connected := by
rw [connected_induce_iff]
rw [preconnected_induce_iff] at sconn tconn
apply (Subgraph.connected_induce_top_sup sconn tconn hv hw ha).mono
· simp only [sup_le_iff, Subgraph.le_induce_union_left,
Subgraph.le_induce_union_right, and_true, ← Subgraph.subgraphOfAdj_eq_induce ha]
apply subgraphOfAdj_le_of_adj
simp [hv, hw, ha]
· simp only [Subgraph.verts_sup, Subgraph.induce_verts]
rw [Set.union_assoc]
simp [Set.insert_subset_iff, Set.singleton_subset_iff, hv, hw]
lemma induce_connected_of_patches {s : Set V} (u : V) (hu : u ∈ s)
(patches : ∀ {v}, v ∈ s → ∃ s' ⊆ s, ∃ (hu' : u ∈ s') (hv' : v ∈ s'),
(G.induce s').Reachable ⟨u, hu'⟩ ⟨v, hv'⟩) : (G.induce s).Connected := by
rw [connected_iff_exists_forall_reachable]
refine ⟨⟨u, hu⟩, ?_⟩
rintro ⟨v, hv⟩
obtain ⟨sv, svs, hu', hv', uv⟩ := patches hv
exact uv.map (induceHomOfLE _ svs).toHom
lemma induce_sUnion_connected_of_pairwise_not_disjoint {S : Set (Set V)} (Sn : S.Nonempty)
(Snd : ∀ {s t}, s ∈ S → t ∈ S → (s ∩ t).Nonempty)
(Sc : ∀ {s}, s ∈ S → (G.induce s).Connected) :
(G.induce (⋃₀ S)).Connected := by
obtain ⟨s, sS⟩ := Sn
obtain ⟨v, vs⟩ := (Sc sS).nonempty
apply G.induce_connected_of_patches _ (Set.subset_sUnion_of_mem sS vs)
rintro w hw
simp only [Set.mem_sUnion] at hw
obtain ⟨t, tS, wt⟩ := hw
refine ⟨s ∪ t, Set.union_subset (Set.subset_sUnion_of_mem sS) (Set.subset_sUnion_of_mem tS),
Or.inl vs, Or.inr wt,
induce_union_connected (Sc sS).preconnected (Sc tS).preconnected (Snd sS tS) _ _⟩
lemma extend_finset_to_connected (Gpc : G.Preconnected) {t : Finset V} (tn : t.Nonempty) :
∃ (t' : Finset V), t ⊆ t' ∧ (G.induce (t' : Set V)).Connected := by
classical
obtain ⟨u, ut⟩ := tn
refine ⟨t.biUnion (fun v => (Gpc u v).some.support.toFinset), fun v vt => ?_, ?_⟩
· simp only [Finset.mem_biUnion, List.mem_toFinset]
exact ⟨v, vt, Walk.end_mem_support _⟩
· apply G.induce_connected_of_patches u
· simp only [Finset.coe_biUnion, Finset.mem_coe, List.coe_toFinset, Set.mem_iUnion,
Set.mem_setOf_eq, Walk.start_mem_support, exists_prop, and_true]
exact ⟨u, ut⟩
intro v hv
simp only [Finset.mem_coe, Finset.mem_biUnion, List.mem_toFinset] at hv
obtain ⟨w, wt, hw⟩ := hv
refine ⟨{x | x ∈ (Gpc u w).some.support}, ?_, ?_⟩
· simp only [Finset.coe_biUnion, Finset.mem_coe, List.coe_toFinset]
exact fun x xw => Set.mem_iUnion₂.mpr ⟨w, wt, xw⟩
· simp only [Set.mem_setOf_eq, Walk.start_mem_support, exists_true_left]
refine ⟨hw, Walk.connected_induce_support _ _ _⟩
end induced_subgraphs
protected lemma Reachable.coe_toSubgraph {H : SimpleGraph V} {u v : V} (h : H ≤ G)
(hreachable : H.Reachable u v) :
(toSubgraph H h).coe.Reachable ⟨u, trivial⟩ ⟨v, trivial⟩ :=
hreachable.map ⟨((toSubgraph H h).vert · _), (·)⟩
protected lemma Preconnected.toSubgraph {H : SimpleGraph V} (h : H ≤ G)
(hpreconn : H.Preconnected) : (toSubgraph H h).Preconnected :=
Subgraph.preconnected_iff.mpr (fun u v ↦ (hpreconn u v).coe_toSubgraph h)
protected lemma Connected.toSubgraph {H : SimpleGraph V} (h : H ≤ G) (hconn : H.Connected) :
(toSubgraph H h).Connected :=
Subgraph.connected_iff.mpr ⟨hconn.preconnected.toSubgraph h, by simp [hconn.nonempty]⟩
protected lemma Reachable.coe_subgraphMap {G' : G.Subgraph} {G'' : G'.coe.Subgraph}
(f : G'.coe →g G) {u v : G''.verts} (hreachable : G''.coe.Reachable u v) :
(G''.map f).coe.Reachable ⟨f u, Set.mem_image_of_mem _ u.prop⟩
⟨f v, Set.mem_image_of_mem _ v.prop⟩ :=
hreachable.map {
toFun v := (G''.map f).vert _ (Set.mem_image_of_mem f v.prop)
map_rel' r := Relation.map_apply.mpr (by tauto)
}
protected lemma Reachable.coe_coeSubgraph {G' : G.Subgraph} (G'' : G'.coe.Subgraph)
{u v : G''.verts} (hreachable : G''.coe.Reachable u v) :
(Subgraph.coeSubgraph G'').coe.Reachable (Subgraph.vert _ u (by simp_all))
(Subgraph.vert _ v (by simp_all)) :=
hreachable.coe_subgraphMap G'.hom
namespace Subgraph
protected lemma Preconnected.map {G' : G.Subgraph} {G'' : G'.coe.Subgraph}
(f : G'.coe →g G) (hpreconn : G''.Preconnected) : (G''.map f).Preconnected := by
rw [Subgraph.preconnected_iff]
intro ⟨u', u, hu, hfu⟩ ⟨v', v, hv, hfv⟩
simp_rw [← hfu, ← hfv]
exact (hpreconn.coe ⟨u, hu⟩ ⟨v, hv⟩).coe_subgraphMap f
protected lemma Connected.map {G' : G.Subgraph} {G'' : G'.coe.Subgraph}
(f : G'.coe →g G) (hconn : G''.Connected) : (G''.map f).Connected :=
Subgraph.connected_iff.mpr ⟨hconn.preconnected.map f, by simp [hconn.nonempty]⟩
protected lemma Preconnected.coeSubgraph {G' : G.Subgraph} (G'' : G'.coe.Subgraph)
(hpreconn : G''.Preconnected) : (Subgraph.coeSubgraph G'').Preconnected :=
hpreconn.map G'.hom
protected lemma Connected.coeSubgraph {G' : G.Subgraph} (G'' : G'.coe.Subgraph)
(hconn : G''.Connected) : (Subgraph.coeSubgraph G'').Connected :=
hconn.map G'.hom
end Subgraph
end SimpleGraph