-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathPaths.lean
More file actions
1017 lines (811 loc) · 37.7 KB
/
Paths.lean
File metadata and controls
1017 lines (811 loc) · 37.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
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/-
Copyright (c) 2021 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.Walk.Decomp
public import Mathlib.Combinatorics.SimpleGraph.Walk.Maps
public import Mathlib.Combinatorics.SimpleGraph.Walk.Subwalks
public import Mathlib.Order.Preorder.Finite
/-!
# Trail, Path, and Cycle
In a simple graph,
* A *trail* is a walk whose edges each appear no more than once.
* A *circuit* is a nonempty trail whose first and last vertices are the
same.
* A *path* is a trail whose vertices appear no more than once.
* A *cycle* is a nonempty trail whose first and last vertices are the
same and whose vertices except for the first appear no more than once.
**Warning:** graph theorists mean something different by "path" than
do homotopy theorists. A "walk" in graph theory is a "path" in
homotopy theory. Another warning: some graph theorists use "path" and
"simple path" for "walk" and "path."
Some definitions and theorems have inspiration from multigraph
counterparts in [Chou1994].
## Main definitions
* `SimpleGraph.Walk.IsTrail`, `SimpleGraph.Walk.IsPath`, and `SimpleGraph.Walk.IsCycle`.
* `SimpleGraph.Path`
* `SimpleGraph.Path.map` for the induced map on paths,
given an (injective) graph homomorphism.
## Tags
trails, paths, circuits, cycles
-/
@[expose] public section
open Function
universe u v w
namespace SimpleGraph
variable {V : Type u} {V' : Type v}
variable (G : SimpleGraph V) (G' : SimpleGraph V')
namespace Walk
variable {G G'} {u u' v w : V} {p : G.Walk u v} {f : G →g G'}
/-! ### Trails, paths, circuits, cycles -/
/-- A *trail* is a walk with no repeating edges. -/
@[mk_iff isTrail_def]
structure IsTrail {u v : V} (p : G.Walk u v) : Prop where
edges_nodup : p.edges.Nodup
/-- A *path* is a walk with no repeating vertices.
Use `SimpleGraph.Walk.IsPath.mk'` for a simpler constructor. -/
structure IsPath {u v : V} (p : G.Walk u v) : Prop extends isTrail : IsTrail p where
support_nodup : p.support.Nodup
/-- A *circuit* at `u : V` is a nonempty trail beginning and ending at `u`. -/
@[mk_iff isCircuit_def]
structure IsCircuit {u : V} (p : G.Walk u u) : Prop extends isTrail : IsTrail p where
ne_nil : p ≠ nil
/-- A *cycle* at `u : V` is a circuit at `u` whose only repeating vertex
is `u` (which appears exactly twice). -/
structure IsCycle {u : V} (p : G.Walk u u) : Prop extends isCircuit : IsCircuit p where
support_nodup : p.support.tail.Nodup
@[simp]
theorem isTrail_copy {u v u' v'} (p : G.Walk u v) (hu : u = u') (hv : v = v') :
(p.copy hu hv).IsTrail ↔ p.IsTrail := by
subst_vars
rfl
theorem IsPath.mk' {u v : V} {p : G.Walk u v} (h : p.support.Nodup) : p.IsPath :=
⟨⟨edges_nodup_of_support_nodup h⟩, h⟩
theorem isPath_def {u v : V} (p : G.Walk u v) : p.IsPath ↔ p.support.Nodup :=
⟨IsPath.support_nodup, IsPath.mk'⟩
theorem isPath_iff_injective_get_support {u v : V} (p : G.Walk u v) :
p.IsPath ↔ (p.support.get ·).Injective :=
p.isPath_def.trans List.nodup_iff_injective_get
@[simp]
theorem isPath_copy {u v u' v'} (p : G.Walk u v) (hu : u = u') (hv : v = v') :
(p.copy hu hv).IsPath ↔ p.IsPath := by
subst_vars
rfl
@[simp]
theorem isCircuit_copy {u u'} (p : G.Walk u u) (hu : u = u') :
(p.copy hu hu).IsCircuit ↔ p.IsCircuit := by
subst_vars
rfl
lemma IsCircuit.not_nil {p : G.Walk v v} (hp : IsCircuit p) : ¬ p.Nil := (hp.ne_nil ·.eq_nil)
theorem isCycle_def {u : V} (p : G.Walk u u) :
p.IsCycle ↔ p.IsTrail ∧ p ≠ nil ∧ p.support.tail.Nodup :=
Iff.intro (fun h => ⟨h.1.1, h.1.2, h.2⟩) fun h => ⟨⟨h.1, h.2.1⟩, h.2.2⟩
@[simp]
theorem isCycle_copy {u u'} (p : G.Walk u u) (hu : u = u') :
(p.copy hu hu).IsCycle ↔ p.IsCycle := by
subst_vars
rfl
lemma IsCycle.not_nil {p : G.Walk v v} (hp : IsCycle p) : ¬ p.Nil := (hp.ne_nil ·.eq_nil)
@[simp]
theorem IsTrail.nil {u : V} : (nil : G.Walk u u).IsTrail :=
⟨by simp [edges]⟩
theorem IsTrail.of_cons {u v w : V} {h : G.Adj u v} {p : G.Walk v w} :
(cons h p).IsTrail → p.IsTrail := by simp [isTrail_def]
@[simp]
theorem isTrail_cons {u v w : V} (h : G.Adj u v) (p : G.Walk v w) :
(cons h p).IsTrail ↔ p.IsTrail ∧ s(u, v) ∉ p.edges := by simp [isTrail_def, and_comm]
@[deprecated (since := "2025-11-03")] alias cons_isTrail_iff := isTrail_cons
protected lemma IsTrail.cons {w : G.Walk u' v} (hw : w.IsTrail) (hu : G.Adj u u')
(hu' : s(u, u') ∉ w.edges) : (w.cons hu).IsTrail := by simp [*]
theorem IsTrail.reverse {u v : V} (p : G.Walk u v) (h : p.IsTrail) : p.reverse.IsTrail := by
simpa [isTrail_def] using h
@[simp]
theorem reverse_isTrail_iff {u v : V} (p : G.Walk u v) : p.reverse.IsTrail ↔ p.IsTrail := by
constructor <;>
· intro h
convert h.reverse _
try rw [reverse_reverse]
theorem IsTrail.of_append_left {u v w : V} {p : G.Walk u v} {q : G.Walk v w}
(h : (p.append q).IsTrail) : p.IsTrail := by
rw [isTrail_def, edges_append, List.nodup_append] at h
exact ⟨h.1⟩
theorem IsTrail.of_append_right {u v w : V} {p : G.Walk u v} {q : G.Walk v w}
(h : (p.append q).IsTrail) : q.IsTrail := by
rw [isTrail_def, edges_append, List.nodup_append] at h
exact ⟨h.2.1⟩
theorem IsTrail.count_edges_le_one [DecidableEq V] {u v : V} {p : G.Walk u v} (h : p.IsTrail)
(e : Sym2 V) : p.edges.count e ≤ 1 :=
List.nodup_iff_count_le_one.mp h.edges_nodup e
theorem IsTrail.count_edges_eq_one [DecidableEq V] {u v : V} {p : G.Walk u v} (h : p.IsTrail)
{e : Sym2 V} (he : e ∈ p.edges) : p.edges.count e = 1 :=
List.count_eq_one_of_mem h.edges_nodup he
theorem IsTrail.length_le_card_edgeFinset [Fintype G.edgeSet] {u v : V}
{w : G.Walk u v} (h : w.IsTrail) : w.length ≤ G.edgeFinset.card := by
classical
let edges := w.edges.toFinset
have : edges.card = w.length := length_edges _ ▸ List.toFinset_card_of_nodup h.edges_nodup
rw [← this]
have : edges ⊆ G.edgeFinset := by
intro e h
refine mem_edgeFinset.mpr ?_
apply w.edges_subset_edgeSet
simpa [edges] using h
exact Finset.card_le_card this
theorem IsPath.nil {u : V} : (nil : G.Walk u u).IsPath := by constructor <;> simp
theorem IsPath.of_cons {u v w : V} {h : G.Adj u v} {p : G.Walk v w} :
(cons h p).IsPath → p.IsPath := by simp [isPath_def]
@[simp]
theorem cons_isPath_iff {u v w : V} (h : G.Adj u v) (p : G.Walk v w) :
(cons h p).IsPath ↔ p.IsPath ∧ u ∉ p.support := by
constructor <;> simp +contextual [isPath_def]
protected lemma IsPath.cons {p : Walk G v w} (hp : p.IsPath) (hu : u ∉ p.support) {h : G.Adj u v} :
(cons h p).IsPath :=
(cons_isPath_iff _ _).2 ⟨hp, hu⟩
@[simp]
theorem isPath_iff_eq_nil {u : V} (p : G.Walk u u) : p.IsPath ↔ p = nil := by
cases p <;> simp [IsPath.nil]
theorem IsPath.reverse {u v : V} {p : G.Walk u v} (h : p.IsPath) : p.reverse.IsPath := by
simpa [isPath_def] using h
@[simp]
theorem isPath_reverse_iff {u v : V} (p : G.Walk u v) : p.reverse.IsPath ↔ p.IsPath := by
constructor <;> intro h <;> convert h.reverse; simp
theorem IsPath.of_append_left {u v w : V} {p : G.Walk u v} {q : G.Walk v w} :
(p.append q).IsPath → p.IsPath := by
simp only [isPath_def, support_append]
exact List.Nodup.of_append_left
theorem IsPath.of_append_right {u v w : V} {p : G.Walk u v} {q : G.Walk v w}
(h : (p.append q).IsPath) : q.IsPath := by
rw [← isPath_reverse_iff] at h ⊢
rw [reverse_append] at h
apply h.of_append_left
theorem isTrail_of_isSubwalk {v w v' w'} {p₁ : G.Walk v w} {p₂ : G.Walk v' w'}
(h : p₁.IsSubwalk p₂) (h₂ : p₂.IsTrail) : p₁.IsTrail := by
obtain ⟨_, _, h⟩ := h
rw [h] at h₂
exact h₂.of_append_left.of_append_right
theorem isPath_of_isSubwalk {v w v' w' : V} {p₁ : G.Walk v w} {p₂ : G.Walk v' w'}
(h : p₁.IsSubwalk p₂) (h₂ : p₂.IsPath) : p₁.IsPath := by
obtain ⟨_, _, h⟩ := h
rw [h] at h₂
exact h₂.of_append_left.of_append_right
lemma IsPath.of_adj {G : SimpleGraph V} {u v : V} (h : G.Adj u v) : h.toWalk.IsPath := by
aesop
theorem concat_isPath_iff {p : G.Walk u v} (h : G.Adj v w) :
(p.concat h).IsPath ↔ p.IsPath ∧ w ∉ p.support := by
rw [← (p.concat h).isPath_reverse_iff, ← p.isPath_reverse_iff, reverse_concat, ← List.mem_reverse,
← support_reverse]
exact cons_isPath_iff h.symm p.reverse
theorem IsPath.concat {p : G.Walk u v} (hp : p.IsPath) (hw : w ∉ p.support)
(h : G.Adj v w) : (p.concat h).IsPath :=
(concat_isPath_iff h).mpr ⟨hp, hw⟩
lemma IsPath.take_of_take {n k} {p : G.Walk u v} (h : (p.take k).IsPath) (hle : n ≤ k) :
(p.take n).IsPath :=
isPath_of_isSubwalk (p.take_isSubwalk_take hle) h
lemma IsPath.drop_of_drop {n k} {p : G.Walk u v} (h : (p.drop k).IsPath) (hle : k ≤ n) :
(p.drop n).IsPath :=
isPath_of_isSubwalk (p.drop_isSubwalk_drop hle) h
lemma IsPath.take {p : G.Walk u v} (h : p.IsPath) (n : ℕ) :
(p.take n).IsPath :=
isPath_of_isSubwalk (p.isSubwalk_take n) h
lemma IsPath.drop {p : G.Walk u v} (h : p.IsPath) (n : ℕ) :
(p.drop n).IsPath :=
isPath_of_isSubwalk (p.isSubwalk_drop n) h
lemma IsPath.mem_support_iff_exists_append {p : G.Walk u v} (hp : p.IsPath) :
w ∈ p.support ↔ ∃ (q : G.Walk u w) (r : G.Walk w v), q.IsPath ∧ r.IsPath ∧ p = q.append r := by
refine ⟨fun hw ↦ ?_, fun ⟨q, r, hq, hr, hqr⟩ ↦ p.mem_support_iff_exists_append.mpr ⟨q, r, hqr⟩⟩
obtain ⟨q, r, hqr⟩ := p.mem_support_iff_exists_append.mp hw
have : (q.append r).IsPath := hqr ▸ hp
exact ⟨q, r, this.of_append_left, this.of_append_right, hqr⟩
lemma IsPath.disjoint_support_of_append {p : G.Walk u v} {q : G.Walk v w}
(hpq : (p.append q).IsPath) (hq : ¬q.Nil) : p.support.Disjoint q.tail.support := by
have hpq' := hpq.support_nodup
rw [support_append] at hpq'
rw [support_tail_of_not_nil q hq]
exact List.disjoint_of_nodup_append hpq'
lemma IsPath.ne_of_mem_support_of_append {p : G.Walk u v} {q : G.Walk v w}
(hpq : (p.append q).IsPath) {x y : V} (hyv : y ≠ v) (hx : x ∈ p.support) (hy : y ∈ q.support) :
x ≠ y := by
rintro rfl
have hq : ¬q.Nil := by
intro hq
simp [nil_iff_support_eq.mp hq, hyv] at hy
have hx' : x ∈ q.tail.support := by
rw [support_tail_of_not_nil q hq]
rw [mem_support_iff] at hy
exact hy.resolve_left hyv
exact IsPath.disjoint_support_of_append hpq hq hx hx'
@[simp]
theorem IsCycle.not_of_nil {u : V} : ¬(nil : G.Walk u u).IsCycle := fun h => h.ne_nil rfl
lemma IsCycle.ne_bot : ∀ {p : G.Walk u u}, p.IsCycle → G ≠ ⊥
| nil, hp => by cases hp.ne_nil rfl
| cons h _, hp => by rintro rfl; exact h
lemma IsCycle.three_le_length {v : V} {p : G.Walk v v} (hp : p.IsCycle) : 3 ≤ p.length := by
have ⟨⟨hp, hp'⟩, _⟩ := hp
match p with
| .nil => simp at hp'
| .cons h .nil => simp at h
| .cons _ (.cons _ .nil) => simp at hp
| .cons _ (.cons _ (.cons _ _)) => simp_rw [SimpleGraph.Walk.length_cons]; lia
lemma not_nil_of_isCycle_cons {p : G.Walk u v} {h : G.Adj v u} (hc : (Walk.cons h p).IsCycle) :
¬ p.Nil := by
have := Walk.length_cons _ _ ▸ Walk.IsCycle.three_le_length hc
rw [Walk.not_nil_iff_lt_length]
lia
theorem cons_isCycle_iff {u v : V} (p : G.Walk v u) (h : G.Adj u v) :
(Walk.cons h p).IsCycle ↔ p.IsPath ∧ s(u, v) ∉ p.edges := by
simp only [Walk.isCycle_def, Walk.isPath_def, Walk.isTrail_def, edges_cons, List.nodup_cons,
support_cons, List.tail_cons]
have : p.support.Nodup → p.edges.Nodup := edges_nodup_of_support_nodup
tauto
protected lemma IsCycle.reverse {p : G.Walk u u} (h : p.IsCycle) : p.reverse.IsCycle := by
simp only [Walk.isCycle_def, nodup_tail_support_reverse] at h ⊢
exact ⟨h.1.reverse, fun h' ↦ h.2.1 (by simp_all [← Walk.length_eq_zero_iff]), h.2.2⟩
@[simp]
lemma isCycle_reverse {p : G.Walk u u} : p.reverse.IsCycle ↔ p.IsCycle where
mp h := by simpa using h.reverse
mpr := .reverse
lemma IsCycle.isPath_of_append_right {p : G.Walk u v} {q : G.Walk v u} (h : ¬ p.Nil)
(hcyc : (p.append q).IsCycle) : q.IsPath := by
have := hcyc.2
rw [tail_support_append, List.nodup_append'] at this
rw [isPath_def, ← cons_tail_support, List.nodup_cons]
exact ⟨this.2.2 (p.end_mem_tail_support h), this.2.1⟩
lemma IsCycle.isPath_of_append_left {p : G.Walk u v} {q : G.Walk v u} (h : ¬ q.Nil)
(hcyc : (p.append q).IsCycle) : p.IsPath :=
p.isPath_reverse_iff.mp ((reverse_append _ _ ▸ hcyc.reverse).isPath_of_append_right (by simpa))
theorem IsCycle.isPath_tail {p : G.Walk u u} (h : p.IsCycle) : p.tail.IsPath :=
IsPath.mk' <| p.support_tail_of_not_nil h.not_nil ▸ h.support_nodup
lemma IsPath.tail {p : G.Walk u v} (hp : p.IsPath) : p.tail.IsPath := by
cases p with
| nil => simp
| cons hadj p =>
simp_all [Walk.isPath_def]
/-- There exists a trail of maximal length in a non-empty graph on finite edges. -/
lemma exists_isTrail_forall_isTrail_length_le_length (G : SimpleGraph V) [N : Nonempty V]
[Finite G.edgeSet] :
∃ (u v : V) (p : G.Walk u v) (_ : p.IsTrail),
∀ (u' v' : V) (p' : G.Walk u' v') (_ : p'.IsTrail), p'.length ≤ p.length := by
have := Fintype.ofFinite G.edgeSet
let s := {n | ∃ (u v : V) (p : G.Walk u v), p.IsTrail ∧ p.length = n}
have : s.Finite := Set.Finite.subset (Set.finite_le_nat G.edgeFinset.card)
fun n ⟨_, _, _, hp, hn⟩ ↦ hn ▸ hp.length_le_card_edgeFinset
obtain ⟨x⟩ := N
obtain ⟨_, ⟨⟨u, v, p, hp, _⟩, hn⟩⟩ := this.exists_maximal ⟨0, ⟨x, x, Walk.nil, by simp⟩⟩
refine ⟨u, v, p, hp, fun u' v' p' hp' ↦ ?_⟩
have := hn ⟨u', v', p', hp', Eq.refl p'.length⟩
lia
/-- There exists a path of maximal length in a non-empty graph on finite edges. -/
lemma exists_isPath_forall_isPath_length_le_length (G : SimpleGraph V) [N : Nonempty V]
[Finite G.edgeSet] :
∃ (u v : V) (p : G.Walk u v) (_ : p.IsPath),
∀ (u' v' : V) (p' : G.Walk u' v') (_ : p'.IsPath), p'.length ≤ p.length := by
have := Fintype.ofFinite G.edgeSet
let s := {n | ∃ (u v : V) (p : G.Walk u v), p.IsPath ∧ p.length = n}
have : s.Finite := Set.Finite.subset (Set.finite_le_nat G.edgeFinset.card)
fun n ⟨_, _, _, hp, hn⟩ ↦ hn ▸ hp.isTrail.length_le_card_edgeFinset
obtain ⟨x⟩ := N
obtain ⟨_, ⟨⟨u, v, p, hp, _⟩, hn⟩⟩ := this.exists_maximal ⟨0, ⟨x, x, Walk.nil, by simp⟩⟩
refine ⟨u, v, p, hp, fun u' v' p' hp' ↦ ?_⟩
have := hn ⟨u', v', p', hp', Eq.refl p'.length⟩
lia
/-! ### About paths -/
instance [DecidableEq V] {u v : V} (p : G.Walk u v) : Decidable p.IsPath := by
rw [isPath_def]
infer_instance
theorem IsPath.length_lt [Fintype V] {u v : V} {p : G.Walk u v} (hp : p.IsPath) :
p.length < Fintype.card V := by
rw [Nat.lt_iff_add_one_le, ← length_support]
exact hp.support_nodup.length_le_card
lemma IsPath.getVert_injOn {p : G.Walk u v} (hp : p.IsPath) :
Set.InjOn p.getVert {i | i ≤ p.length} := by
intro n hn m hm hnm
induction p generalizing n m with
| nil => simp_all
| @cons v w u h p ihp =>
simp only [length_cons, Set.mem_setOf_eq] at hn hm hnm
by_cases hn0 : n = 0 <;> by_cases hm0 : m = 0
· lia
· simp only [hn0, getVert_zero, Walk.getVert_cons p h hm0] at hnm
have hvp : v ∉ p.support := by aesop
exact (hvp (Walk.mem_support_iff_exists_getVert.mpr ⟨(m - 1), ⟨hnm.symm, by lia⟩⟩)).elim
· simp only [hm0, Walk.getVert_cons p h hn0] at hnm
have hvp : v ∉ p.support := by simp_all
exact (hvp (Walk.mem_support_iff_exists_getVert.mpr ⟨(n - 1), ⟨hnm, by lia⟩⟩)).elim
· simp only [Walk.getVert_cons _ _ hn0, Walk.getVert_cons _ _ hm0] at hnm
have := ihp hp.of_cons (by lia : (n - 1) ≤ p.length)
(by lia : (m - 1) ≤ p.length) hnm
lia
lemma IsPath.getVert_eq_start_iff_of_not_nil {i : ℕ} {p : G.Walk u w} (hp : p.IsPath) (h : ¬p.Nil) :
p.getVert i = u ↔ i = 0 := by
refine ⟨fun h ↦ ?_, by simp_all⟩
by_cases h' : i ≤ p.length
· apply hp.getVert_injOn (by rw [Set.mem_setOf]; lia) (by rw [Set.mem_setOf]; lia)
simp [h]
· rw [p.getVert_of_length_le (le_of_not_ge h')] at h
subst h
simp_all
lemma IsPath.getVert_eq_start_iff {i : ℕ} {p : G.Walk u w} (hp : p.IsPath) (hi : i ≤ p.length) :
p.getVert i = u ↔ i = 0 := by
by_cases h' : p.Nil
· simp_all [nil_iff_length_eq.mp h']
· exact hp.getVert_eq_start_iff_of_not_nil h'
lemma IsPath.getVert_eq_end_iff {i : ℕ} {p : G.Walk u w} (hp : p.IsPath) (hi : i ≤ p.length) :
p.getVert i = w ↔ i = p.length := by
have := hp.reverse.getVert_eq_start_iff (by lia : p.reverse.length - i ≤ p.reverse.length)
simp only [length_reverse, getVert_reverse, show p.length - (p.length - i) = i by lia] at this
rw [this]
lia
lemma IsPath.getVert_injOn_iff (p : G.Walk u v) : Set.InjOn p.getVert {i | i ≤ p.length} ↔
p.IsPath := by
refine ⟨?_, fun a => a.getVert_injOn⟩
induction p with
| nil => simp
| cons h q ih =>
intro hinj
rw [cons_isPath_iff]
refine ⟨ih (by
intro n hn m hm hnm
simp only [Set.mem_setOf_eq] at hn hm
have := hinj
(by rw [length_cons]; lia : n + 1 ≤ (q.cons h).length)
(by rw [length_cons]; lia : m + 1 ≤ (q.cons h).length)
(by simpa [getVert_cons] using hnm)
lia), fun h' => ?_⟩
obtain ⟨n, ⟨hn, hnl⟩⟩ := mem_support_iff_exists_getVert.mp h'
have := hinj
(by rw [length_cons]; lia : (n + 1) ≤ (q.cons h).length)
(by lia : 0 ≤ (q.cons h).length)
(by rwa [getVert_cons _ _ n.add_one_ne_zero, getVert_zero])
lia
theorem IsPath.eq_snd_of_mem_edges {p : G.Walk u v} (hp : p.IsPath) (hmem : s(u, w) ∈ p.edges) :
w = p.snd := by
have hnil := edges_eq_nil.not.mp <| List.ne_nil_of_mem hmem
rw [← cons_tail_eq _ hnil, edges_cons, List.mem_cons, Sym2.eq, Sym2.rel_iff'] at hmem
have : u ∉ p.tail.support := by induction p <;> simp_all
grind [fst_mem_support_of_mem_edges]
theorem IsPath.eq_penultimate_of_mem_edges {p : G.Walk u v} (hp : p.IsPath)
(hmem : s(v, w) ∈ p.edges) : w = p.penultimate := by
simpa [hmem] using isPath_reverse_iff p |>.mpr hp |>.eq_snd_of_mem_edges (w := w)
theorem IsPath.injOn_support_of_isPath_map (h : (p.map f).IsPath) :
Set.InjOn f {w | w ∈ p.support} := by
intro u hu v hv hf
obtain ⟨u, rfl⟩ := List.get_of_mem hu
obtain ⟨v, rfl⟩ := List.get_of_mem hv
congr
have := List.nodup_iff_injective_getElem.mp h.support_nodup
rw! (castMode := .all) [support_map, List.length_map] at this
apply this
simpa
/-! ### About cycles -/
-- TODO: These results could possibly be less laborious with a periodic function getCycleVert
lemma IsCycle.getVert_injOn {p : G.Walk u u} (hpc : p.IsCycle) :
Set.InjOn p.getVert {i | 1 ≤ i ∧ i ≤ p.length} := by
rw [← p.cons_tail_eq hpc.not_nil] at hpc
intro n hn m hm hnm
rw [← SimpleGraph.Walk.length_tail_add_one
(p.not_nil_of_tail_not_nil (not_nil_of_isCycle_cons hpc)), Set.mem_setOf] at hn hm
have := ((Walk.cons_isCycle_iff _ _).mp hpc).1.getVert_injOn
(by lia : n - 1 ≤ p.tail.length) (by lia : m - 1 ≤ p.tail.length)
(by simp_all)
lia
lemma IsCycle.getVert_injOn' {p : G.Walk u u} (hpc : p.IsCycle) :
Set.InjOn p.getVert {i | i ≤ p.length - 1} := by
intro n hn m hm hnm
simp only [Set.mem_setOf_eq] at *
have := hpc.three_le_length
have : p.length - n = p.length - m := Walk.length_reverse _ ▸ hpc.reverse.getVert_injOn
(by simp only [Walk.length_reverse, Set.mem_setOf_eq]; lia)
(by simp only [Walk.length_reverse, Set.mem_setOf_eq]; lia)
(by simp [Walk.getVert_reverse, show p.length - (p.length - n) = n by lia, hnm,
show p.length - (p.length - m) = m by lia])
lia
lemma IsCycle.snd_ne_penultimate {p : G.Walk u u} (hp : p.IsCycle) : p.snd ≠ p.penultimate := by
intro h
have := hp.three_le_length
apply hp.getVert_injOn (by simp; lia) (by simp; lia) at h
lia
lemma IsCycle.getVert_endpoint_iff {i : ℕ} {p : G.Walk u u} (hpc : p.IsCycle) (hl : i ≤ p.length) :
p.getVert i = u ↔ i = 0 ∨ i = p.length := by
refine ⟨?_, by aesop⟩
rw [or_iff_not_imp_left]
intro h hi
exact hpc.getVert_injOn (by simp only [Set.mem_setOf_eq]; lia)
(by simp only [Set.mem_setOf_eq]; lia) (h.symm ▸ (Walk.getVert_length p).symm)
lemma IsCycle.getVert_sub_one_ne_getVert_add_one {i : ℕ} {p : G.Walk u u} (hpc : p.IsCycle)
(h : i ≤ p.length) : p.getVert (i - 1) ≠ p.getVert (i + 1) := by
intro h'
have hl := hpc.three_le_length
by_cases hi' : i ≥ p.length - 1
· rw [p.getVert_of_length_le (by lia : p.length ≤ i + 1),
hpc.getVert_endpoint_iff (by lia)] at h'
lia
have := hpc.getVert_injOn' (by simp only [Set.mem_setOf_eq, Nat.sub_le_iff_le_add]; lia)
(by simp only [Set.mem_setOf_eq]; lia) h'
lia
theorem isCycle_iff_isPath_tail_and_le_length {p : G.Walk u u} :
p.IsCycle ↔ p.tail.IsPath ∧ 3 ≤ p.length := by
refine ⟨fun h ↦ ⟨h.isPath_tail, h.three_le_length⟩, fun ⟨h₁, h₂⟩ ↦ ?_⟩
cases p with
| nil => simp_all
| cons h' p =>
simp only [getVert_cons_succ, tail_cons, isPath_copy, length_cons] at h₁ h₂
refine p.cons_isCycle_iff h' |>.mpr ⟨h₁, fun hh ↦ ?_⟩
have : p.support[0] = p.support[p.length - 1] := by
simp [← List.head_eq_getElem_zero, h₁.eq_penultimate_of_mem_edges hh]
have := p.isPath_iff_injective_get_support.mp h₁ this
lia
/-! ### Walk decompositions -/
section WalkDecomp
variable [DecidableEq V]
protected theorem IsTrail.takeUntil {u v w : V} {p : G.Walk v w} (hc : p.IsTrail)
(h : u ∈ p.support) : (p.takeUntil u h).IsTrail :=
IsTrail.of_append_left (q := p.dropUntil u h) (by rwa [← take_spec _ h] at hc)
protected theorem IsTrail.dropUntil {u v w : V} {p : G.Walk v w} (hc : p.IsTrail)
(h : u ∈ p.support) : (p.dropUntil u h).IsTrail :=
IsTrail.of_append_right (p := p.takeUntil u h) (q := p.dropUntil u h)
(by rwa [← take_spec _ h] at hc)
protected theorem IsPath.takeUntil {u v w : V} {p : G.Walk v w} (hc : p.IsPath)
(h : u ∈ p.support) : (p.takeUntil u h).IsPath :=
IsPath.of_append_left (q := p.dropUntil u h) (by rwa [← take_spec _ h] at hc)
protected theorem IsPath.dropUntil {u v w : V} {p : G.Walk v w} (hc : p.IsPath)
(h : u ∈ p.support) : (p.dropUntil u h).IsPath :=
IsPath.of_append_right (p := p.takeUntil u h) (q := p.dropUntil u h)
(by rwa [← take_spec _ h] at hc)
lemma IsTrail.disjoint_edges_takeUntil_dropUntil {x : V} {w : G.Walk u v} (hw : w.IsTrail)
(hx : x ∈ w.support) : (w.takeUntil x hx).edges.Disjoint (w.dropUntil x hx).edges :=
List.disjoint_of_nodup_append <| by simpa [← edges_append] using hw.edges_nodup
@[simp] lemma isTrail_rotate {c : G.Walk v v} (hu : u ∈ c.support) :
(c.rotate u hu).IsTrail ↔ c.IsTrail := by
rw [isTrail_def, isTrail_def, (c.rotate_edges u hu).perm.nodup_iff]
@[simp] lemma isCircuit_rotate {c : G.Walk v v} (hu : u ∈ c.support) :
(c.rotate u hu).IsCircuit ↔ c.IsCircuit := by simp [isCircuit_def]
@[simp] lemma isCycle_rotate {c : G.Walk v v} (hu : u ∈ c.support) :
(c.rotate u hu).IsCycle ↔ c.IsCycle := by simp [isCycle_def, (support_rotate ..).perm.nodup_iff]
protected alias ⟨IsTrail.of_rotate, IsTrail.rotate⟩ := isTrail_rotate
protected alias ⟨IsCircuit.of_rotate, IsCircuit.rotate⟩ := isCircuit_rotate
protected alias ⟨IsCycle.of_rotate, IsCycle.rotate⟩ := isCycle_rotate
lemma IsCycle.isPath_takeUntil {c : G.Walk v v} (hc : c.IsCycle) (h : w ∈ c.support) :
(c.takeUntil w h).IsPath := by
by_cases hvw : v = w
· subst hvw
simp
rw [← isCycle_reverse, ← take_spec c h, reverse_append] at hc
exact (c.takeUntil w h).isPath_reverse_iff.mp (hc.isPath_of_append_right (not_nil_of_ne hvw))
theorem IsCycle.count_support {c : G.Walk v v} (hc : c.IsCycle) : c.support.count v = 2 := by
have := List.count_eq_one_of_mem hc.support_nodup <| c.end_mem_tail_support hc.not_nil
have := c.head_support ▸ List.head?_eq_some_head c.support_ne_nil
grind
theorem IsCycle.count_support_of_mem {c : G.Walk v v} (hc : c.IsCycle) (hu : u ∈ c.support)
(hv : u ≠ v) : c.support.count u = 1 := by
have := List.eq_or_mem_of_mem_cons <| List.cons_head_tail c.support_ne_nil ▸ hu
have := List.count_eq_one_of_mem hc.support_nodup <| this.resolve_left <| head_support _ ▸ hv
have := c.head_support ▸ List.head?_eq_some_head c.support_ne_nil
grind
/-- Taking a strict initial segment of a path removes the end vertex from the support. -/
lemma endpoint_notMem_support_takeUntil {p : G.Walk u v} (hp : p.IsPath) (hw : w ∈ p.support)
(h : v ≠ w) : v ∉ (p.takeUntil w hw).support := by
intro hv
rw [Walk.mem_support_iff_exists_getVert] at hv
obtain ⟨n, ⟨hn, hnl⟩⟩ := hv
rw [getVert_takeUntil hw hnl] at hn
have := p.length_takeUntil_lt hw h.symm
have : n = p.length := hp.getVert_injOn (by rw [Set.mem_setOf]; lia) (by simp)
(hn.symm ▸ p.getVert_length.symm)
lia
end WalkDecomp
end Walk
/-! ### Type of paths -/
/-- The type for paths between two vertices. -/
abbrev Path (u v : V) := { p : G.Walk u v // p.IsPath }
namespace Path
variable {G G'}
@[simp]
protected theorem isPath {u v : V} (p : G.Path u v) : (p : G.Walk u v).IsPath := p.property
@[simp]
protected theorem isTrail {u v : V} (p : G.Path u v) : (p : G.Walk u v).IsTrail :=
p.property.isTrail
/-- The length-0 path at a vertex. -/
@[refl, simps]
protected def nil {u : V} : G.Path u u :=
⟨Walk.nil, Walk.IsPath.nil⟩
/-- The length-1 path between a pair of adjacent vertices. -/
@[simps]
def singleton {u v : V} (h : G.Adj u v) : G.Path u v :=
⟨Walk.cons h Walk.nil, by simp [h.ne]⟩
theorem mk'_mem_edges_singleton {u v : V} (h : G.Adj u v) :
s(u, v) ∈ (singleton h : G.Walk u v).edges := by simp [singleton]
/-- The reverse of a path is another path. See also `SimpleGraph.Walk.reverse`. -/
@[symm, simps]
def reverse {u v : V} (p : G.Path u v) : G.Path v u :=
⟨Walk.reverse p, p.property.reverse⟩
theorem count_support_eq_one [DecidableEq V] {u v w : V} {p : G.Path u v}
(hw : w ∈ (p : G.Walk u v).support) : (p : G.Walk u v).support.count w = 1 :=
List.count_eq_one_of_mem p.property.support_nodup hw
theorem count_edges_eq_one [DecidableEq V] {u v : V} {p : G.Path u v} (e : Sym2 V)
(hw : e ∈ (p : G.Walk u v).edges) : (p : G.Walk u v).edges.count e = 1 :=
List.count_eq_one_of_mem p.property.isTrail.edges_nodup hw
@[simp]
theorem nodup_support {u v : V} (p : G.Path u v) : (p : G.Walk u v).support.Nodup :=
(Walk.isPath_def _).mp p.property
theorem loop_eq {v : V} (p : G.Path v v) : p = Path.nil := by
obtain ⟨_ | _, h⟩ := p
· rfl
· simp at h
theorem notMem_edges_of_loop {v : V} {e : Sym2 V} {p : G.Path v v} :
e ∉ (p : G.Walk v v).edges := by simp [p.loop_eq]
theorem cons_isCycle {u v : V} (p : G.Path v u) (h : G.Adj u v)
(he : s(u, v) ∉ (p : G.Walk v u).edges) : (Walk.cons h ↑p).IsCycle := by
simp [Walk.isCycle_def, Walk.isTrail_cons, he]
end Path
/-! ### Walks to paths -/
namespace Walk
variable {G} [DecidableEq V] {u u' v v' : V}
/-- Given a walk, produces a walk from it by bypassing subwalks between repeated vertices.
The result is a path, as shown in `SimpleGraph.Walk.bypass_isPath`.
This is packaged up in `SimpleGraph.Walk.toPath`. -/
def bypass {u v : V} : G.Walk u v → G.Walk u v
| nil => nil
| cons ha p =>
let p' := p.bypass
if hs : u ∈ p'.support then
p'.dropUntil u hs
else
cons ha p'
@[simp]
theorem bypass_copy (p : G.Walk u v) (hu : u = u') (hv : v = v') :
(p.copy hu hv).bypass = p.bypass.copy hu hv := by
subst_vars
rfl
theorem bypass_isPath (p : G.Walk u v) : p.bypass.IsPath := by
induction p with
| nil => simp!
| cons _ p' ih =>
simp only [bypass]
split_ifs with hs
· exact ih.dropUntil hs
· simp [*, cons_isPath_iff]
theorem length_bypass_le (p : G.Walk u v) : p.bypass.length ≤ p.length := by
induction p with
| nil => rfl
| cons _ _ ih =>
simp only [bypass]
split_ifs
· trans
· apply length_dropUntil_le
rw [length_cons]
lia
· rw [length_cons, length_cons]
exact Nat.add_le_add_right ih 1
lemma bypass_eq_self_of_length_le (p : G.Walk u v) (h : p.length ≤ p.bypass.length) :
p.bypass = p := by
induction p with
| nil => rfl
| cons h p ih =>
simp only [Walk.bypass]
split_ifs with hb
· exfalso
simp only [hb, Walk.bypass, Walk.length_cons, dif_pos] at h
apply Nat.not_succ_le_self p.length
calc p.length + 1
_ ≤ (p.bypass.dropUntil _ _).length := h
_ ≤ p.bypass.length := Walk.length_dropUntil_le p.bypass hb
_ ≤ p.length := Walk.length_bypass_le _
· simp only [hb, Walk.bypass, Walk.length_cons, not_false_iff, dif_neg,
Nat.add_le_add_iff_right] at h
rw [ih h]
lemma bypass_eq_self_iff_length_le (p : G.Walk u v) : p.bypass = p ↔ p.length ≤ p.bypass.length :=
⟨fun hp ↦ Nat.le_of_eq (congrArg length hp.symm), p.bypass_eq_self_of_length_le⟩
/-- Given a walk, produces a path with the same endpoints using `SimpleGraph.Walk.bypass`. -/
def toPath (p : G.Walk u v) : G.Path u v :=
⟨p.bypass, p.bypass_isPath⟩
theorem support_bypass_subset (p : G.Walk u v) : p.bypass.support ⊆ p.support := by
induction p with
| nil => simp!
| cons _ _ ih =>
simp! only
split_ifs
· apply List.Subset.trans (support_dropUntil_subset _ _)
apply List.subset_cons_of_subset
assumption
· rw [support_cons]
apply List.cons_subset_cons
assumption
theorem support_toPath_subset (p : G.Walk u v) :
(p.toPath : G.Walk u v).support ⊆ p.support :=
support_bypass_subset _
theorem darts_bypass_subset (p : G.Walk u v) : p.bypass.darts ⊆ p.darts := by
induction p with
| nil => simp!
| cons _ _ ih =>
simp! only
split_ifs
· apply List.Subset.trans (darts_dropUntil_subset _ _)
apply List.subset_cons_of_subset _ ih
· rw [darts_cons]
exact List.cons_subset_cons _ ih
theorem edges_bypass_subset (p : G.Walk u v) : p.bypass.edges ⊆ p.edges :=
List.map_subset _ p.darts_bypass_subset
theorem darts_toPath_subset (p : G.Walk u v) : (p.toPath : G.Walk u v).darts ⊆ p.darts :=
darts_bypass_subset _
theorem edges_toPath_subset (p : G.Walk u v) : (p.toPath : G.Walk u v).edges ⊆ p.edges :=
edges_bypass_subset _
theorem IsPath.bypass_eq_self {p : G.Walk u v} (hp : p.IsPath) : p.bypass = p := by
induction p with
| nil => rfl
| @cons u v w h p ih =>
simp only [bypass]
have hu : u ∉ p.support := ((p.cons_isPath_iff h).mp hp).right
have hu' : u ∉ p.bypass.support := List.notMem_subset p.support_bypass_subset hu
simp only [hu', reduceDIte, cons.injEq, heq_eq_eq, true_and]
exact ih hp.of_cons
theorem bypass_eq_self_iff_isPath (p : G.Walk u v) : p.bypass = p ↔ p.IsPath := by
constructor
· intro hp
rw [← bypass_eq_self_of_length_le p (Nat.le_of_eq (congrArg length hp.symm))]
exact p.bypass_isPath
· exact IsPath.bypass_eq_self
theorem length_bypass_lt_iff_not_isPath (p : G.Walk u v) :
p.bypass.length < p.length ↔ ¬p.IsPath := by
rw [iff_not_comm, Nat.not_lt, ← bypass_eq_self_iff_isPath, bypass_eq_self_iff_length_le]
/-- Bypass repeated vertices like `Walk.bypass`, except the starting vertex.
This is intended to be used for closed walks, for which `Walk.bypass` unhelpfully returns the empty
walk. -/
def cycleBypass : G.Walk v v → G.Walk v v
| .nil => .nil
| .cons hvv' w => .cons hvv' w.bypass
@[simp] lemma cycleBypass_nil : (.nil : G.Walk v v).cycleBypass = .nil := rfl
lemma edges_cycleBypass_subset : ∀ {w : G.Walk v v}, w.cycleBypass.edges ⊆ w.edges
| .nil => by simp
| .cons (v := v') hvv' w => by
classical
dsimp only [cycleBypass, edges_cons]
gcongr
exact edges_bypass_subset _
lemma IsTrail.isCycle_cycleBypass : ∀ {w : G.Walk v v}, w ≠ .nil → w.IsTrail → w.cycleBypass.IsCycle
| .cons (v := v') hvv' w, _, hw => by
dsimp [cycleBypass]
refine ⟨⟨(bypass_isPath _).isTrail.cons _ fun hvv' ↦ ?_, by simp⟩, ?_⟩
· simp only [isTrail_cons] at hw
exact hw.2 <| edges_bypass_subset _ hvv'
· simpa using (bypass_isPath _).support_nodup
lemma length_cycleBypass_le {w : G.Walk v v} : w.cycleBypass.length ≤ w.length := by
cases w <;> simp [cycleBypass, length_bypass_le]
lemma cycleBypass_eq_self_of_length_le (w : G.Walk v v) (h : w.length ≤ w.cycleBypass.length) :
w.cycleBypass = w := by
cases w
· simp
· simp only [length_cons, cycleBypass, Nat.add_le_add_iff_right, cons.injEq, heq_eq_eq,
true_and] at h ⊢
exact bypass_eq_self_of_length_le _ h
theorem cycleBypass_eq_self_iff_length_le (w : G.Walk v v) :
w.cycleBypass = w ↔ w.length ≤ w.cycleBypass.length := by
cases w <;> simp [cycleBypass, bypass_eq_self_iff_length_le]
theorem IsCycle.cycleBypass_eq_self {w : G.Walk v v} (hw : w.IsCycle) : w.cycleBypass = w := by
cases w
· simp
· have hw' := (cons_isCycle_iff ..).mp hw
simp [cycleBypass, hw'.left.bypass_eq_self]
theorem IsTrail.cycleBypass_eq_self_iff_isCycle_or_nil {w : G.Walk v v} (hw : w.IsTrail) :
w.cycleBypass = w ↔ w.IsCycle ∨ w.Nil := by
cases w
· simp
· simp [cycleBypass, bypass_eq_self_iff_isPath, cons_isCycle_iff, (isTrail_cons ..).mp hw]
theorem IsTrail.length_cycleBypass_lt_iff_not_isCycle_and_not_nil {w : G.Walk v v}
(hw : w.IsTrail) :
w.cycleBypass.length < w.length ↔ ¬w.IsCycle ∧ ¬w.Nil := by
cases w
· simp
· simp [cycleBypass, length_bypass_lt_iff_not_isPath, cons_isCycle_iff, (isTrail_cons ..).mp hw]
end Walk
/-! ### Mapping paths -/
namespace Walk
variable {G G'}
variable (f : G →g G') {u v : V} (p : G.Walk u v)
variable {p f}
theorem map_isPath_of_injective (hinj : Function.Injective f) (hp : p.IsPath) :
(p.map f).IsPath := by
induction p with
| nil => simp
| cons _ _ ih =>
rw [Walk.cons_isPath_iff] at hp
simp only [map_cons, cons_isPath_iff, ih hp.1, support_map, List.mem_map, not_exists, not_and,
true_and]
intro x hx hf
cases hinj hf
exact hp.2 hx
protected theorem IsPath.of_map {f : G →g G'} (hp : (p.map f).IsPath) : p.IsPath := by
induction p with
| nil => simp
| cons _ _ ih => grind [map_cons, Walk.cons_isPath_iff, support_map]
theorem map_isPath_iff_of_injective (hinj : Function.Injective f) : (p.map f).IsPath ↔ p.IsPath :=
⟨IsPath.of_map, map_isPath_of_injective hinj⟩
theorem map_isTrail_iff_of_injective (hinj : Function.Injective f) :
(p.map f).IsTrail ↔ p.IsTrail := by
induction p with
| nil => simp
| cons _ _ ih =>
rw [map_cons, isTrail_cons, ih, isTrail_cons]
apply and_congr_right'
rw [← Sym2.map_mk, edges_map, ← List.mem_map_of_injective (Sym2.map.injective hinj)]
alias ⟨_, map_isTrail_of_injective⟩ := map_isTrail_iff_of_injective
theorem map_isCycle_iff_of_injective {p : G.Walk u u} (hinj : Function.Injective f) :
(p.map f).IsCycle ↔ p.IsCycle := by
rw [isCycle_def, isCycle_def, map_isTrail_iff_of_injective hinj, Ne, map_eq_nil_iff,
support_map, ← List.map_tail, List.nodup_map_iff hinj]
alias ⟨_, IsCycle.map⟩ := map_isCycle_iff_of_injective
@[simp]
theorem mapLe_isTrail {G G' : SimpleGraph V} (h : G ≤ G') {u v : V} {p : G.Walk u v} :
(p.mapLe h).IsTrail ↔ p.IsTrail :=
map_isTrail_iff_of_injective Function.injective_id
alias ⟨IsTrail.of_mapLe, IsTrail.mapLe⟩ := mapLe_isTrail
@[simp]
theorem mapLe_isPath {G G' : SimpleGraph V} (h : G ≤ G') {u v : V} {p : G.Walk u v} :
(p.mapLe h).IsPath ↔ p.IsPath :=
map_isPath_iff_of_injective Function.injective_id
alias ⟨IsPath.of_mapLe, IsPath.mapLe⟩ := mapLe_isPath
@[simp]
theorem mapLe_isCycle {G G' : SimpleGraph V} (h : G ≤ G') {u : V} {p : G.Walk u u} :
(p.mapLe h).IsCycle ↔ p.IsCycle :=
map_isCycle_iff_of_injective Function.injective_id
alias ⟨IsCycle.of_mapLe, IsCycle.mapLe⟩ := mapLe_isCycle
end Walk
namespace Path
variable {G G'}
/-- Given an injective graph homomorphism, map paths to paths. -/
@[simps]
protected def map (f : G →g G') (hinj : Function.Injective f) {u v : V} (p : G.Path u v) :
G'.Path (f u) (f v) :=
⟨Walk.map f p, Walk.map_isPath_of_injective hinj p.2⟩
theorem map_injective {f : G →g G'} (hinj : Function.Injective f) (u v : V) :
Function.Injective (Path.map f hinj : G.Path u v → G'.Path (f u) (f v)) := by
rintro ⟨p, hp⟩ ⟨p', hp'⟩ h
simp only [Path.map, Subtype.mk.injEq] at h
simp [Walk.map_injective_of_injective hinj u v h]
/-- Given a graph embedding, map paths to paths. -/
@[simps!]
protected def mapEmbedding (f : G ↪g G') {u v : V} (p : G.Path u v) : G'.Path (f u) (f v) :=
Path.map f.toHom f.injective p
theorem mapEmbedding_injective (f : G ↪g G') (u v : V) :
Function.Injective (Path.mapEmbedding f : G.Path u v → G'.Path (f u) (f v)) :=
map_injective f.injective u v
end Path
/-! ### Transferring between graphs -/
namespace Walk
variable {G} {u v : V} {H : SimpleGraph V}
variable {p : G.Walk u v}
protected theorem IsPath.transfer (hp) (pp : p.IsPath) :
(p.transfer H hp).IsPath := by
induction p with
| nil => simp
| cons _ _ ih =>
simp only [Walk.transfer, cons_isPath_iff, support_transfer _] at pp ⊢
exact ⟨ih _ pp.1, pp.2⟩
protected theorem IsCycle.transfer {q : G.Walk u u} (qc : q.IsCycle) (hq) :
(q.transfer H hq).IsCycle := by
cases q with
| nil => simp at qc
| cons _ q =>
simp only [edges_cons, List.mem_cons, forall_eq_or_imp] at hq
simp only [Walk.transfer, cons_isCycle_iff, edges_transfer q hq.2] at qc ⊢
exact ⟨qc.1.transfer hq.2, qc.2⟩
end Walk
/-! ## Deleting edges -/
namespace Walk
variable {v w : V}
protected theorem IsPath.toDeleteEdges (s : Set (Sym2 V))
{p : G.Walk v w} (h : p.IsPath) (hp) : (p.toDeleteEdges s hp).IsPath :=