-
Notifications
You must be signed in to change notification settings - Fork 297
/
connectivity.lean
2099 lines (1651 loc) · 77.2 KB
/
connectivity.lean
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
-/
import combinatorics.simple_graph.basic
import combinatorics.simple_graph.subgraph
import data.list.rotate
/-!
# Graph connectivity
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In a simple graph,
* A *walk* is a finite sequence of adjacent vertices, and can be
thought of equally well as a sequence of directed edges.
* A *trail* is a walk whose edges each appear no more than once.
* 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
* `simple_graph.walk` (with accompanying pattern definitions
`simple_graph.walk.nil'` and `simple_graph.walk.cons'`)
* `simple_graph.walk.is_trail`, `simple_graph.walk.is_path`, and `simple_graph.walk.is_cycle`.
* `simple_graph.path`
* `simple_graph.walk.map` and `simple_graph.path.map` for the induced map on walks,
given an (injective) graph homomorphism.
* `simple_graph.reachable` for the relation of whether there exists
a walk between a given pair of vertices
* `simple_graph.preconnected` and `simple_graph.connected` are predicates
on simple graphs for whether every vertex can be reached from every other,
and in the latter case, whether the vertex type is nonempty.
* `simple_graph.subgraph.connected` gives subgraphs the connectivity
predicate via `simple_graph.subgraph.coe`.
* `simple_graph.connected_component` is the type of connected components of
a given graph.
* `simple_graph.is_bridge` for whether an edge is a bridge edge
## Main statements
* `simple_graph.is_bridge_iff_mem_and_forall_cycle_not_mem` characterizes bridge edges in terms of
there being no cycle containing them.
## Tags
walks, trails, paths, circuits, cycles, bridge edges
-/
open function
universes u v w
namespace simple_graph
variables {V : Type u} {V' : Type v} {V'' : Type w}
variables (G : simple_graph V) (G' : simple_graph V') (G'' : simple_graph V'')
/-- A walk is a sequence of adjacent vertices. For vertices `u v : V`,
the type `walk u v` consists of all walks starting at `u` and ending at `v`.
We say that a walk *visits* the vertices it contains. The set of vertices a
walk visits is `simple_graph.walk.support`.
See `simple_graph.walk.nil'` and `simple_graph.walk.cons'` for patterns that
can be useful in definitions since they make the vertices explicit. -/
@[derive decidable_eq]
inductive walk : V → V → Type u
| nil {u : V} : walk u u
| cons {u v w: V} (h : G.adj u v) (p : walk v w) : walk u w
attribute [refl] walk.nil
@[simps] instance walk.inhabited (v : V) : inhabited (G.walk v v) := ⟨walk.nil⟩
/-- The one-edge walk associated to a pair of adjacent vertices. -/
@[pattern, reducible] def adj.to_walk {G : simple_graph V} {u v : V} (h : G.adj u v) :
G.walk u v := walk.cons h walk.nil
namespace walk
variables {G}
/-- Pattern to get `walk.nil` with the vertex as an explicit argument. -/
@[pattern] abbreviation nil' (u : V) : G.walk u u := walk.nil
/-- Pattern to get `walk.cons` with the vertices as explicit arguments. -/
@[pattern] abbreviation cons' (u v w : V) (h : G.adj u v) (p : G.walk v w) : G.walk u w :=
walk.cons h p
/-- Change the endpoints of a walk using equalities. This is helpful for relaxing
definitional equality constraints and to be able to state otherwise difficult-to-state
lemmas. While this is a simple wrapper around `eq.rec`, it gives a canonical way to write it.
The simp-normal form is for the `copy` to be pushed outward. That way calculations can
occur within the "copy context." -/
protected def copy {u v u' v'} (p : G.walk u v) (hu : u = u') (hv : v = v') : G.walk u' v' :=
eq.rec (eq.rec p hv) hu
@[simp] lemma copy_rfl_rfl {u v} (p : G.walk u v) :
p.copy rfl rfl = p := rfl
@[simp] lemma copy_copy {u v u' v' u'' v''} (p : G.walk u v)
(hu : u = u') (hv : v = v') (hu' : u' = u'') (hv' : v' = v'') :
(p.copy hu hv).copy hu' hv' = p.copy (hu.trans hu') (hv.trans hv') :=
by { subst_vars, refl }
@[simp] lemma copy_nil {u u'} (hu : u = u') : (walk.nil : G.walk u u).copy hu hu = walk.nil :=
by { subst_vars, refl }
lemma copy_cons {u v w u' w'} (h : G.adj u v) (p : G.walk v w) (hu : u = u') (hw : w = w') :
(walk.cons h p).copy hu hw = walk.cons (by rwa ← hu) (p.copy rfl hw) :=
by { subst_vars, refl }
@[simp]
lemma cons_copy {u v w v' w'} (h : G.adj u v) (p : G.walk v' w') (hv : v' = v) (hw : w' = w) :
walk.cons h (p.copy hv hw) = (walk.cons (by rwa hv) p).copy rfl hw :=
by { subst_vars, refl }
lemma exists_eq_cons_of_ne : Π {u v : V} (hne : u ≠ v) (p : G.walk u v),
∃ (w : V) (h : G.adj u w) (p' : G.walk w v), p = cons h p'
| _ _ hne nil := (hne rfl).elim
| _ _ _ (cons h p') := ⟨_, h, p', rfl⟩
/-- The length of a walk is the number of edges/darts along it. -/
def length : Π {u v : V}, G.walk u v → ℕ
| _ _ nil := 0
| _ _ (cons _ q) := q.length.succ
/-- The concatenation of two compatible walks. -/
@[trans]
def append : Π {u v w : V}, G.walk u v → G.walk v w → G.walk u w
| _ _ _ nil q := q
| _ _ _ (cons h p) q := cons h (p.append q)
/-- The reversed version of `simple_graph.walk.cons`, concatenating an edge to
the end of a walk. -/
def concat {u v w : V} (p : G.walk u v) (h : G.adj v w) : G.walk u w := p.append (cons h nil)
lemma concat_eq_append {u v w : V} (p : G.walk u v) (h : G.adj v w) :
p.concat h = p.append (cons h nil) := rfl
/-- The concatenation of the reverse of the first walk with the second walk. -/
protected def reverse_aux : Π {u v w : V}, G.walk u v → G.walk u w → G.walk v w
| _ _ _ nil q := q
| _ _ _ (cons h p) q := reverse_aux p (cons (G.symm h) q)
/-- The walk in reverse. -/
@[symm]
def reverse {u v : V} (w : G.walk u v) : G.walk v u := w.reverse_aux nil
/-- Get the `n`th vertex from a walk, where `n` is generally expected to be
between `0` and `p.length`, inclusive.
If `n` is greater than or equal to `p.length`, the result is the path's endpoint. -/
def get_vert : Π {u v : V} (p : G.walk u v) (n : ℕ), V
| u v nil _ := u
| u v (cons _ _) 0 := u
| u v (cons _ q) (n+1) := q.get_vert n
@[simp] lemma get_vert_zero {u v} (w : G.walk u v) : w.get_vert 0 = u :=
by { cases w; refl }
lemma get_vert_of_length_le {u v} (w : G.walk u v) {i : ℕ} (hi : w.length ≤ i) :
w.get_vert i = v :=
begin
induction w with _ x y z hxy wyz IH generalizing i,
{ refl },
{ cases i,
{ cases hi, },
{ exact IH (nat.succ_le_succ_iff.1 hi) } }
end
@[simp] lemma get_vert_length {u v} (w : G.walk u v) : w.get_vert w.length = v :=
w.get_vert_of_length_le rfl.le
lemma adj_get_vert_succ {u v} (w : G.walk u v) {i : ℕ} (hi : i < w.length) :
G.adj (w.get_vert i) (w.get_vert (i+1)) :=
begin
induction w with _ x y z hxy wyz IH generalizing i,
{ cases hi, },
{ cases i,
{ simp [get_vert, hxy] },
{ exact IH (nat.succ_lt_succ_iff.1 hi) } },
end
@[simp] lemma cons_append {u v w x : V} (h : G.adj u v) (p : G.walk v w) (q : G.walk w x) :
(cons h p).append q = cons h (p.append q) := rfl
@[simp] lemma cons_nil_append {u v w : V} (h : G.adj u v) (p : G.walk v w) :
(cons h nil).append p = cons h p := rfl
@[simp] lemma append_nil : Π {u v : V} (p : G.walk u v), p.append nil = p
| _ _ nil := rfl
| _ _ (cons h p) := by rw [cons_append, append_nil]
@[simp] lemma nil_append {u v : V} (p : G.walk u v) : nil.append p = p := rfl
lemma append_assoc : Π {u v w x : V} (p : G.walk u v) (q : G.walk v w) (r : G.walk w x),
p.append (q.append r) = (p.append q).append r
| _ _ _ _ nil _ _ := rfl
| _ _ _ _ (cons h p') q r := by { dunfold append, rw append_assoc, }
@[simp] lemma append_copy_copy {u v w u' v' w'} (p : G.walk u v) (q : G.walk v w)
(hu : u = u') (hv : v = v') (hw : w = w') :
(p.copy hu hv).append (q.copy hv hw) = (p.append q).copy hu hw := by { subst_vars, refl }
lemma concat_nil {u v : V} (h : G.adj u v) : nil.concat h = cons h nil := rfl
@[simp] lemma concat_cons {u v w x : V} (h : G.adj u v) (p : G.walk v w) (h' : G.adj w x) :
(cons h p).concat h' = cons h (p.concat h') := rfl
lemma append_concat {u v w x : V} (p : G.walk u v) (q : G.walk v w) (h : G.adj w x) :
p.append (q.concat h) = (p.append q).concat h := append_assoc _ _ _
lemma concat_append {u v w x : V} (p : G.walk u v) (h : G.adj v w) (q : G.walk w x) :
(p.concat h).append q = p.append (cons h q) :=
by rw [concat_eq_append, ← append_assoc, cons_nil_append]
/-- A non-trivial `cons` walk is representable as a `concat` walk. -/
lemma exists_cons_eq_concat : Π {u v w : V} (h : G.adj u v) (p : G.walk v w),
∃ (x : V) (q : G.walk u x) (h' : G.adj x w), cons h p = q.concat h'
| _ _ _ h nil := ⟨_, nil, h, rfl⟩
| _ _ _ h (cons h' p) :=
begin
obtain ⟨y, q, h'', hc⟩ := exists_cons_eq_concat h' p,
refine ⟨y, cons h q, h'', _⟩,
rw [concat_cons, hc],
end
/-- A non-trivial `concat` walk is representable as a `cons` walk. -/
lemma exists_concat_eq_cons : Π {u v w : V} (p : G.walk u v) (h : G.adj v w),
∃ (x : V) (h' : G.adj u x) (q : G.walk x w), p.concat h = cons h' q
| _ _ _ nil h := ⟨_, h, nil, rfl⟩
| _ _ _ (cons h' p) h := ⟨_, h', walk.concat p h, concat_cons _ _ _⟩
@[simp] lemma reverse_nil {u : V} : (nil : G.walk u u).reverse = nil := rfl
lemma reverse_singleton {u v : V} (h : G.adj u v) :
(cons h nil).reverse = cons (G.symm h) nil := rfl
@[simp] lemma cons_reverse_aux {u v w x : V} (p : G.walk u v) (q : G.walk w x) (h : G.adj w u) :
(cons h p).reverse_aux q = p.reverse_aux (cons (G.symm h) q) := rfl
@[simp] protected lemma append_reverse_aux : Π {u v w x : V}
(p : G.walk u v) (q : G.walk v w) (r : G.walk u x),
(p.append q).reverse_aux r = q.reverse_aux (p.reverse_aux r)
| _ _ _ _ nil _ _ := rfl
| _ _ _ _ (cons h p') q r := append_reverse_aux p' q (cons (G.symm h) r)
@[simp] protected lemma reverse_aux_append : Π {u v w x : V}
(p : G.walk u v) (q : G.walk u w) (r : G.walk w x),
(p.reverse_aux q).append r = p.reverse_aux (q.append r)
| _ _ _ _ nil _ _ := rfl
| _ _ _ _ (cons h p') q r := by simp [reverse_aux_append p' (cons (G.symm h) q) r]
protected lemma reverse_aux_eq_reverse_append {u v w : V} (p : G.walk u v) (q : G.walk u w) :
p.reverse_aux q = p.reverse.append q :=
by simp [reverse]
@[simp] lemma reverse_cons {u v w : V} (h : G.adj u v) (p : G.walk v w) :
(cons h p).reverse = p.reverse.append (cons (G.symm h) nil) :=
by simp [reverse]
@[simp] lemma reverse_copy {u v u' v'} (p : G.walk u v) (hu : u = u') (hv : v = v') :
(p.copy hu hv).reverse = p.reverse.copy hv hu := by { subst_vars, refl }
@[simp] lemma reverse_append {u v w : V} (p : G.walk u v) (q : G.walk v w) :
(p.append q).reverse = q.reverse.append p.reverse :=
by simp [reverse]
@[simp] lemma reverse_concat {u v w : V} (p : G.walk u v) (h : G.adj v w) :
(p.concat h).reverse = cons (G.symm h) p.reverse :=
by simp [concat_eq_append]
@[simp] lemma reverse_reverse : Π {u v : V} (p : G.walk u v), p.reverse.reverse = p
| _ _ nil := rfl
| _ _ (cons h p) := by simp [reverse_reverse]
@[simp] lemma length_nil {u : V} : (nil : G.walk u u).length = 0 := rfl
@[simp] lemma length_cons {u v w : V} (h : G.adj u v) (p : G.walk v w) :
(cons h p).length = p.length + 1 := rfl
@[simp] lemma length_copy {u v u' v'} (p : G.walk u v) (hu : u = u') (hv : v = v') :
(p.copy hu hv).length = p.length :=
by { subst_vars, refl }
@[simp] lemma length_append : Π {u v w : V} (p : G.walk u v) (q : G.walk v w),
(p.append q).length = p.length + q.length
| _ _ _ nil _ := by simp
| _ _ _ (cons _ _) _ := by simp [length_append, add_left_comm, add_comm]
@[simp] lemma length_concat {u v w : V} (p : G.walk u v) (h : G.adj v w) :
(p.concat h).length = p.length + 1 := length_append _ _
@[simp] protected lemma length_reverse_aux : Π {u v w : V} (p : G.walk u v) (q : G.walk u w),
(p.reverse_aux q).length = p.length + q.length
| _ _ _ nil _ := by simp!
| _ _ _ (cons _ _) _ := by simp [length_reverse_aux, nat.add_succ, nat.succ_add]
@[simp] lemma length_reverse {u v : V} (p : G.walk u v) : p.reverse.length = p.length :=
by simp [reverse]
lemma eq_of_length_eq_zero : Π {u v : V} {p : G.walk u v}, p.length = 0 → u = v
| _ _ nil _ := rfl
@[simp] lemma exists_length_eq_zero_iff {u v : V} : (∃ (p : G.walk u v), p.length = 0) ↔ u = v :=
begin
split,
{ rintro ⟨p, hp⟩,
exact eq_of_length_eq_zero hp, },
{ rintro rfl,
exact ⟨nil, rfl⟩, },
end
@[simp] lemma length_eq_zero_iff {u : V} {p : G.walk u u} : p.length = 0 ↔ p = nil :=
by cases p; simp
section concat_rec
variables
{motive : Π (u v : V), G.walk u v → Sort*}
(Hnil : Π {u : V}, motive u u nil)
(Hconcat : Π {u v w : V} (p : G.walk u v) (h : G.adj v w), motive u v p → motive u w (p.concat h))
/-- Auxiliary definition for `simple_graph.walk.concat_rec` -/
def concat_rec_aux : Π {u v : V} (p : G.walk u v), motive v u p.reverse
| _ _ nil := Hnil
| _ _ (cons h p) := eq.rec (Hconcat p.reverse (G.symm h) (concat_rec_aux p)) (reverse_cons h p).symm
/-- Recursor on walks by inducting on `simple_graph.walk.concat`.
This is inducting from the opposite end of the walk compared
to `simple_graph.walk.rec`, which inducts on `simple_graph.walk.cons`. -/
@[elab_as_eliminator]
def concat_rec {u v : V} (p : G.walk u v) : motive u v p :=
eq.rec (concat_rec_aux @Hnil @Hconcat p.reverse) (reverse_reverse p)
@[simp] lemma concat_rec_nil (u : V) :
@concat_rec _ _ motive @Hnil @Hconcat _ _ (nil : G.walk u u) = Hnil := rfl
@[simp] lemma concat_rec_concat {u v w : V} (p : G.walk u v) (h : G.adj v w) :
@concat_rec _ _ motive @Hnil @Hconcat _ _ (p.concat h)
= Hconcat p h (concat_rec @Hnil @Hconcat p) :=
begin
simp only [concat_rec],
apply eq_of_heq,
apply rec_heq_of_heq,
transitivity concat_rec_aux @Hnil @Hconcat (cons h.symm p.reverse),
{ congr, simp },
{ rw [concat_rec_aux, rec_heq_iff_heq],
congr; simp [heq_rec_iff_heq], }
end
end concat_rec
lemma concat_ne_nil {u v : V} (p : G.walk u v) (h : G.adj v u) :
p.concat h ≠ nil :=
by cases p; simp [concat]
lemma concat_inj {u v v' w : V}
{p : G.walk u v} {h : G.adj v w} {p' : G.walk u v'} {h' : G.adj v' w}
(he : p.concat h = p'.concat h') :
∃ (hv : v = v'), p.copy rfl hv = p' :=
begin
induction p,
{ cases p',
{ exact ⟨rfl, rfl⟩ },
{ exfalso,
simp only [concat_nil, concat_cons] at he,
obtain ⟨rfl, he⟩ := he,
simp only [heq_iff_eq] at he,
exact concat_ne_nil _ _ he.symm, } },
{ rw concat_cons at he,
cases p',
{ exfalso,
simp only [concat_nil] at he,
obtain ⟨rfl, he⟩ := he,
rw [heq_iff_eq] at he,
exact concat_ne_nil _ _ he, },
{ rw concat_cons at he,
simp only at he,
obtain ⟨rfl, he⟩ := he,
rw [heq_iff_eq] at he,
obtain ⟨rfl, rfl⟩ := p_ih he,
exact ⟨rfl, rfl⟩, } }
end
/-- The `support` of a walk is the list of vertices it visits in order. -/
def support : Π {u v : V}, G.walk u v → list V
| u v nil := [u]
| u v (cons h p) := u :: p.support
/-- The `darts` of a walk is the list of darts it visits in order. -/
def darts : Π {u v : V}, G.walk u v → list G.dart
| u v nil := []
| u v (cons h p) := ⟨(u, _), h⟩ :: p.darts
/-- The `edges` of a walk is the list of edges it visits in order.
This is defined to be the list of edges underlying `simple_graph.walk.darts`. -/
def edges {u v : V} (p : G.walk u v) : list (sym2 V) := p.darts.map dart.edge
@[simp] lemma support_nil {u : V} : (nil : G.walk u u).support = [u] := rfl
@[simp] lemma support_cons {u v w : V} (h : G.adj u v) (p : G.walk v w) :
(cons h p).support = u :: p.support := rfl
@[simp] lemma support_concat {u v w : V} (p : G.walk u v) (h : G.adj v w) :
(p.concat h).support = p.support.concat w := by induction p; simp [*, concat_nil]
@[simp] lemma support_copy {u v u' v'} (p : G.walk u v) (hu : u = u') (hv : v = v') :
(p.copy hu hv).support = p.support := by { subst_vars, refl }
lemma support_append {u v w : V} (p : G.walk u v) (p' : G.walk v w) :
(p.append p').support = p.support ++ p'.support.tail :=
by induction p; cases p'; simp [*]
@[simp]
lemma support_reverse {u v : V} (p : G.walk u v) : p.reverse.support = p.support.reverse :=
by induction p; simp [support_append, *]
lemma support_ne_nil {u v : V} (p : G.walk u v) : p.support ≠ [] :=
by cases p; simp
lemma tail_support_append {u v w : V} (p : G.walk u v) (p' : G.walk v w) :
(p.append p').support.tail = p.support.tail ++ p'.support.tail :=
by rw [support_append, list.tail_append_of_ne_nil _ _ (support_ne_nil _)]
lemma support_eq_cons {u v : V} (p : G.walk u v) : p.support = u :: p.support.tail :=
by cases p; simp
@[simp] lemma start_mem_support {u v : V} (p : G.walk u v) : u ∈ p.support :=
by cases p; simp
@[simp] lemma end_mem_support {u v : V} (p : G.walk u v) : v ∈ p.support :=
by induction p; simp [*]
@[simp] lemma support_nonempty {u v : V} (p : G.walk u v) : {w | w ∈ p.support}.nonempty :=
⟨u, by simp⟩
lemma mem_support_iff {u v w : V} (p : G.walk u v) :
w ∈ p.support ↔ w = u ∨ w ∈ p.support.tail :=
by cases p; simp
lemma mem_support_nil_iff {u v : V} : u ∈ (nil : G.walk v v).support ↔ u = v := by simp
@[simp]
lemma mem_tail_support_append_iff {t u v w : V} (p : G.walk u v) (p' : G.walk v w) :
t ∈ (p.append p').support.tail ↔ t ∈ p.support.tail ∨ t ∈ p'.support.tail :=
by rw [tail_support_append, list.mem_append]
@[simp] lemma end_mem_tail_support_of_ne {u v : V} (h : u ≠ v) (p : G.walk u v) :
v ∈ p.support.tail :=
by { obtain ⟨_, _, _, rfl⟩ := exists_eq_cons_of_ne h p, simp }
@[simp]
lemma mem_support_append_iff {t u v w : V} (p : G.walk u v) (p' : G.walk v w) :
t ∈ (p.append p').support ↔ t ∈ p.support ∨ t ∈ p'.support :=
begin
simp only [mem_support_iff, mem_tail_support_append_iff],
by_cases h : t = v; by_cases h' : t = u;
subst_vars;
try { have := ne.symm h' };
simp [*],
end
@[simp]
lemma subset_support_append_left {V : Type u} {G : simple_graph V} {u v w : V}
(p : G.walk u v) (q : G.walk v w) :
p.support ⊆ (p.append q).support :=
by simp only [walk.support_append, list.subset_append_left]
@[simp]
lemma subset_support_append_right {V : Type u} {G : simple_graph V} {u v w : V}
(p : G.walk u v) (q : G.walk v w) :
q.support ⊆ (p.append q).support :=
by { intro h, simp only [mem_support_append_iff, or_true, implies_true_iff] { contextual := tt }}
lemma coe_support {u v : V} (p : G.walk u v) :
(p.support : multiset V) = {u} + p.support.tail :=
by cases p; refl
lemma coe_support_append {u v w : V} (p : G.walk u v) (p' : G.walk v w) :
((p.append p').support : multiset V) = {u} + p.support.tail + p'.support.tail :=
by rw [support_append, ←multiset.coe_add, coe_support]
lemma coe_support_append' [decidable_eq V] {u v w : V} (p : G.walk u v) (p' : G.walk v w) :
((p.append p').support : multiset V) = p.support + p'.support - {v} :=
begin
rw [support_append, ←multiset.coe_add],
simp only [coe_support],
rw add_comm {v},
simp only [← add_assoc, add_tsub_cancel_right],
end
lemma chain_adj_support : Π {u v w : V} (h : G.adj u v) (p : G.walk v w),
list.chain G.adj u p.support
| _ _ _ h nil := list.chain.cons h list.chain.nil
| _ _ _ h (cons h' p) := list.chain.cons h (chain_adj_support h' p)
lemma chain'_adj_support : Π {u v : V} (p : G.walk u v), list.chain' G.adj p.support
| _ _ nil := list.chain.nil
| _ _ (cons h p) := chain_adj_support h p
lemma chain_dart_adj_darts : Π {d : G.dart} {v w : V} (h : d.snd = v) (p : G.walk v w),
list.chain G.dart_adj d p.darts
| _ _ _ h nil := list.chain.nil
| _ _ _ h (cons h' p) := list.chain.cons h (chain_dart_adj_darts (by exact rfl) p)
lemma chain'_dart_adj_darts : Π {u v : V} (p : G.walk u v), list.chain' G.dart_adj p.darts
| _ _ nil := trivial
| _ _ (cons h p) := chain_dart_adj_darts rfl p
/-- Every edge in a walk's edge list is an edge of the graph.
It is written in this form (rather than using `⊆`) to avoid unsightly coercions. -/
lemma edges_subset_edge_set : Π {u v : V} (p : G.walk u v) ⦃e : sym2 V⦄
(h : e ∈ p.edges), e ∈ G.edge_set
| _ _ (cons h' p') e h := by rcases h with ⟨rfl, h⟩; solve_by_elim
lemma adj_of_mem_edges {u v x y : V} (p : G.walk u v) (h : ⟦(x, y)⟧ ∈ p.edges) : G.adj x y :=
edges_subset_edge_set p h
@[simp] lemma darts_nil {u : V} : (nil : G.walk u u).darts = [] := rfl
@[simp] lemma darts_cons {u v w : V} (h : G.adj u v) (p : G.walk v w) :
(cons h p).darts = ⟨(u, v), h⟩ :: p.darts := rfl
@[simp] lemma darts_concat {u v w : V} (p : G.walk u v) (h : G.adj v w) :
(p.concat h).darts = p.darts.concat ⟨(v, w), h⟩ := by induction p; simp [*, concat_nil]
@[simp] lemma darts_copy {u v u' v'} (p : G.walk u v) (hu : u = u') (hv : v = v') :
(p.copy hu hv).darts = p.darts := by { subst_vars, refl }
@[simp] lemma darts_append {u v w : V} (p : G.walk u v) (p' : G.walk v w) :
(p.append p').darts = p.darts ++ p'.darts :=
by induction p; simp [*]
@[simp] lemma darts_reverse {u v : V} (p : G.walk u v) :
p.reverse.darts = (p.darts.map dart.symm).reverse :=
by induction p; simp [*, sym2.eq_swap]
lemma mem_darts_reverse {u v : V} {d : G.dart} {p : G.walk u v} :
d ∈ p.reverse.darts ↔ d.symm ∈ p.darts :=
by simp
lemma cons_map_snd_darts {u v : V} (p : G.walk u v) :
u :: p.darts.map dart.snd = p.support :=
by induction p; simp! [*]
lemma map_snd_darts {u v : V} (p : G.walk u v) :
p.darts.map dart.snd = p.support.tail :=
by simpa using congr_arg list.tail (cons_map_snd_darts p)
lemma map_fst_darts_append {u v : V} (p : G.walk u v) :
p.darts.map dart.fst ++ [v] = p.support :=
by induction p; simp! [*]
lemma map_fst_darts {u v : V} (p : G.walk u v) :
p.darts.map dart.fst = p.support.init :=
by simpa! using congr_arg list.init (map_fst_darts_append p)
@[simp] lemma edges_nil {u : V} : (nil : G.walk u u).edges = [] := rfl
@[simp] lemma edges_cons {u v w : V} (h : G.adj u v) (p : G.walk v w) :
(cons h p).edges = ⟦(u, v)⟧ :: p.edges := rfl
@[simp] lemma edges_concat {u v w : V} (p : G.walk u v) (h : G.adj v w) :
(p.concat h).edges = p.edges.concat ⟦(v, w)⟧ := by simp [edges]
@[simp] lemma edges_copy {u v u' v'} (p : G.walk u v) (hu : u = u') (hv : v = v') :
(p.copy hu hv).edges = p.edges := by { subst_vars, refl }
@[simp] lemma edges_append {u v w : V} (p : G.walk u v) (p' : G.walk v w) :
(p.append p').edges = p.edges ++ p'.edges :=
by simp [edges]
@[simp] lemma edges_reverse {u v : V} (p : G.walk u v) : p.reverse.edges = p.edges.reverse :=
by simp [edges]
@[simp] lemma length_support {u v : V} (p : G.walk u v) : p.support.length = p.length + 1 :=
by induction p; simp *
@[simp] lemma length_darts {u v : V} (p : G.walk u v) : p.darts.length = p.length :=
by induction p; simp *
@[simp] lemma length_edges {u v : V} (p : G.walk u v) : p.edges.length = p.length :=
by simp [edges]
lemma dart_fst_mem_support_of_mem_darts :
Π {u v : V} (p : G.walk u v) {d : G.dart}, d ∈ p.darts → d.fst ∈ p.support
| u v (cons h p') d hd := begin
simp only [support_cons, darts_cons, list.mem_cons_iff] at hd ⊢,
rcases hd with (rfl|hd),
{ exact or.inl rfl, },
{ exact or.inr (dart_fst_mem_support_of_mem_darts _ hd), },
end
lemma dart_snd_mem_support_of_mem_darts {u v : V} (p : G.walk u v) {d : G.dart} (h : d ∈ p.darts) :
d.snd ∈ p.support :=
by simpa using p.reverse.dart_fst_mem_support_of_mem_darts (by simp [h] : d.symm ∈ p.reverse.darts)
lemma fst_mem_support_of_mem_edges {t u v w : V} (p : G.walk v w) (he : ⟦(t, u)⟧ ∈ p.edges) :
t ∈ p.support :=
begin
obtain ⟨d, hd, he⟩ := list.mem_map.mp he,
rw dart_edge_eq_mk_iff' at he,
rcases he with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩,
{ exact dart_fst_mem_support_of_mem_darts _ hd, },
{ exact dart_snd_mem_support_of_mem_darts _ hd, },
end
lemma snd_mem_support_of_mem_edges {t u v w : V} (p : G.walk v w) (he : ⟦(t, u)⟧ ∈ p.edges) :
u ∈ p.support :=
by { rw sym2.eq_swap at he, exact p.fst_mem_support_of_mem_edges he }
lemma darts_nodup_of_support_nodup {u v : V} {p : G.walk u v} (h : p.support.nodup) :
p.darts.nodup :=
begin
induction p,
{ simp, },
{ simp only [darts_cons, support_cons, list.nodup_cons] at h ⊢,
refine ⟨λ h', h.1 (dart_fst_mem_support_of_mem_darts p_p h'), p_ih h.2⟩, }
end
lemma edges_nodup_of_support_nodup {u v : V} {p : G.walk u v} (h : p.support.nodup) :
p.edges.nodup :=
begin
induction p,
{ simp, },
{ simp only [edges_cons, support_cons, list.nodup_cons] at h ⊢,
exact ⟨λ h', h.1 (fst_mem_support_of_mem_edges p_p h'), p_ih h.2⟩, }
end
/-! ### Trails, paths, circuits, cycles -/
/-- A *trail* is a walk with no repeating edges. -/
structure is_trail {u v : V} (p : G.walk u v) : Prop :=
(edges_nodup : p.edges.nodup)
/-- A *path* is a walk with no repeating vertices.
Use `simple_graph.walk.is_path.mk'` for a simpler constructor. -/
structure is_path {u v : V} (p : G.walk u v) extends to_trail : is_trail p : Prop :=
(support_nodup : p.support.nodup)
/-- A *circuit* at `u : V` is a nonempty trail beginning and ending at `u`. -/
structure is_circuit {u : V} (p : G.walk u u) extends to_trail : is_trail p : Prop :=
(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 is_cycle {u : V} (p : G.walk u u)
extends to_circuit : is_circuit p : Prop :=
(support_nodup : p.support.tail.nodup)
lemma is_trail_def {u v : V} (p : G.walk u v) : p.is_trail ↔ p.edges.nodup :=
⟨is_trail.edges_nodup, λ h, ⟨h⟩⟩
@[simp] lemma is_trail_copy {u v u' v'} (p : G.walk u v) (hu : u = u') (hv : v = v') :
(p.copy hu hv).is_trail ↔ p.is_trail := by { subst_vars, refl }
lemma is_path.mk' {u v : V} {p : G.walk u v} (h : p.support.nodup) : is_path p :=
⟨⟨edges_nodup_of_support_nodup h⟩, h⟩
lemma is_path_def {u v : V} (p : G.walk u v) : p.is_path ↔ p.support.nodup :=
⟨is_path.support_nodup, is_path.mk'⟩
@[simp] lemma is_path_copy {u v u' v'} (p : G.walk u v) (hu : u = u') (hv : v = v') :
(p.copy hu hv).is_path ↔ p.is_path := by { subst_vars, refl }
lemma is_circuit_def {u : V} (p : G.walk u u) :
p.is_circuit ↔ is_trail p ∧ p ≠ nil :=
iff.intro (λ h, ⟨h.1, h.2⟩) (λ h, ⟨h.1, h.2⟩)
@[simp] lemma is_circuit_copy {u u'} (p : G.walk u u) (hu : u = u') :
(p.copy hu hu).is_circuit ↔ p.is_circuit := by { subst_vars, refl }
lemma is_cycle_def {u : V} (p : G.walk u u) :
p.is_cycle ↔ is_trail p ∧ p ≠ nil ∧ p.support.tail.nodup :=
iff.intro (λ h, ⟨h.1.1, h.1.2, h.2⟩) (λ h, ⟨⟨h.1, h.2.1⟩, h.2.2⟩)
@[simp] lemma is_cycle_copy {u u'} (p : G.walk u u) (hu : u = u') :
(p.copy hu hu).is_cycle ↔ p.is_cycle := by { subst_vars, refl }
@[simp] lemma is_trail.nil {u : V} : (nil : G.walk u u).is_trail :=
⟨by simp [edges]⟩
lemma is_trail.of_cons {u v w : V} {h : G.adj u v} {p : G.walk v w} :
(cons h p).is_trail → p.is_trail :=
by simp [is_trail_def]
@[simp] lemma cons_is_trail_iff {u v w : V} (h : G.adj u v) (p : G.walk v w) :
(cons h p).is_trail ↔ p.is_trail ∧ ⟦(u, v)⟧ ∉ p.edges :=
by simp [is_trail_def, and_comm]
lemma is_trail.reverse {u v : V} (p : G.walk u v) (h : p.is_trail) : p.reverse.is_trail :=
by simpa [is_trail_def] using h
@[simp] lemma reverse_is_trail_iff {u v : V} (p : G.walk u v) : p.reverse.is_trail ↔ p.is_trail :=
by split; { intro h, convert h.reverse _, try { rw reverse_reverse } }
lemma is_trail.of_append_left {u v w : V} {p : G.walk u v} {q : G.walk v w}
(h : (p.append q).is_trail) : p.is_trail :=
by { rw [is_trail_def, edges_append, list.nodup_append] at h, exact ⟨h.1⟩ }
lemma is_trail.of_append_right {u v w : V} {p : G.walk u v} {q : G.walk v w}
(h : (p.append q).is_trail) : q.is_trail :=
by { rw [is_trail_def, edges_append, list.nodup_append] at h, exact ⟨h.2.1⟩ }
lemma is_trail.count_edges_le_one [decidable_eq V] {u v : V}
{p : G.walk u v} (h : p.is_trail) (e : sym2 V) : p.edges.count e ≤ 1 :=
list.nodup_iff_count_le_one.mp h.edges_nodup e
lemma is_trail.count_edges_eq_one [decidable_eq V] {u v : V}
{p : G.walk u v} (h : p.is_trail) {e : sym2 V} (he : e ∈ p.edges) :
p.edges.count e = 1 :=
list.count_eq_one_of_mem h.edges_nodup he
lemma is_path.nil {u : V} : (nil : G.walk u u).is_path :=
by { fsplit; simp }
lemma is_path.of_cons {u v w : V} {h : G.adj u v} {p : G.walk v w} :
(cons h p).is_path → p.is_path :=
by simp [is_path_def]
@[simp] lemma cons_is_path_iff {u v w : V} (h : G.adj u v) (p : G.walk v w) :
(cons h p).is_path ↔ p.is_path ∧ u ∉ p.support :=
by split; simp [is_path_def] { contextual := tt }
@[simp] lemma is_path_iff_eq_nil {u : V} (p : G.walk u u) : p.is_path ↔ p = nil :=
by { cases p; simp [is_path.nil] }
lemma is_path.reverse {u v : V} {p : G.walk u v} (h : p.is_path) : p.reverse.is_path :=
by simpa [is_path_def] using h
@[simp] lemma is_path_reverse_iff {u v : V} (p : G.walk u v) : p.reverse.is_path ↔ p.is_path :=
by split; intro h; convert h.reverse; simp
lemma is_path.of_append_left {u v w : V} {p : G.walk u v} {q : G.walk v w} :
(p.append q).is_path → p.is_path :=
by { simp only [is_path_def, support_append], exact list.nodup.of_append_left }
lemma is_path.of_append_right {u v w : V} {p : G.walk u v} {q : G.walk v w}
(h : (p.append q).is_path) : q.is_path :=
begin
rw ←is_path_reverse_iff at h ⊢,
rw reverse_append at h,
apply h.of_append_left,
end
@[simp] lemma is_cycle.not_of_nil {u : V} : ¬ (nil : G.walk u u).is_cycle :=
λ h, h.ne_nil rfl
lemma cons_is_cycle_iff {u v : V} (p : G.walk v u) (h : G.adj u v) :
(walk.cons h p).is_cycle ↔ p.is_path ∧ ¬ ⟦(u, v)⟧ ∈ p.edges :=
begin
simp only [walk.is_cycle_def, walk.is_path_def, walk.is_trail_def, edges_cons, list.nodup_cons,
support_cons, list.tail_cons],
have : p.support.nodup → p.edges.nodup := edges_nodup_of_support_nodup,
tauto,
end
/-! ### About paths -/
instance [decidable_eq V] {u v : V} (p : G.walk u v) : decidable p.is_path :=
by { rw is_path_def, apply_instance }
lemma is_path.length_lt [fintype V] {u v : V} {p : G.walk u v} (hp : p.is_path) :
p.length < fintype.card V :=
by { rw [nat.lt_iff_add_one_le, ← length_support], exact hp.support_nodup.length_le_card }
/-! ### Walk decompositions -/
section walk_decomp
variables [decidable_eq V]
/-- Given a vertex in the support of a path, give the path up until (and including) that vertex. -/
def take_until : Π {v w : V} (p : G.walk v w) (u : V) (h : u ∈ p.support), G.walk v u
| v w nil u h := by rw mem_support_nil_iff.mp h
| v w (cons r p) u h :=
if hx : v = u
then by subst u
else cons r (take_until p _ $ h.cases_on (λ h', (hx h'.symm).elim) id)
/-- Given a vertex in the support of a path, give the path from (and including) that vertex to
the end. In other words, drop vertices from the front of a path until (and not including)
that vertex. -/
def drop_until : Π {v w : V} (p : G.walk v w) (u : V) (h : u ∈ p.support), G.walk u w
| v w nil u h := by rw mem_support_nil_iff.mp h
| v w (cons r p) u h :=
if hx : v = u
then by { subst u, exact cons r p }
else drop_until p _ $ h.cases_on (λ h', (hx h'.symm).elim) id
/-- The `take_until` and `drop_until` functions split a walk into two pieces.
The lemma `count_support_take_until_eq_one` specifies where this split occurs. -/
@[simp]
lemma take_spec {u v w : V} (p : G.walk v w) (h : u ∈ p.support) :
(p.take_until u h).append (p.drop_until u h) = p :=
begin
induction p,
{ rw mem_support_nil_iff at h,
subst u,
refl, },
{ obtain (rfl|h) := h,
{ simp! },
{ simp! only,
split_ifs with h'; subst_vars; simp [*], } },
end
lemma mem_support_iff_exists_append {V : Type u} {G : simple_graph V} {u v w : V}
{p : G.walk u v} :
w ∈ p.support ↔ ∃ (q : G.walk u w) (r : G.walk w v), p = q.append r :=
begin
classical,
split,
{ exact λ h, ⟨_, _, (p.take_spec h).symm⟩ },
{ rintro ⟨q, r, rfl⟩,
simp only [mem_support_append_iff, end_mem_support, start_mem_support, or_self], },
end
@[simp]
lemma count_support_take_until_eq_one {u v w : V} (p : G.walk v w) (h : u ∈ p.support) :
(p.take_until u h).support.count u = 1 :=
begin
induction p,
{ rw mem_support_nil_iff at h,
subst u,
simp!, },
{ obtain (rfl|h) := h,
{ simp! },
{ simp! only,
split_ifs with h'; rw eq_comm at h'; subst_vars; simp! [*, list.count_cons], } },
end
lemma count_edges_take_until_le_one {u v w : V} (p : G.walk v w) (h : u ∈ p.support) (x : V) :
(p.take_until u h).edges.count ⟦(u, x)⟧ ≤ 1 :=
begin
induction p with u' u' v' w' ha p' ih,
{ rw mem_support_nil_iff at h,
subst u,
simp!, },
{ obtain (rfl|h) := h,
{ simp!, },
{ simp! only,
split_ifs with h',
{ subst h',
simp, },
{ rw [edges_cons, list.count_cons],
split_ifs with h'',
{ rw sym2.eq_iff at h'',
obtain (⟨rfl,rfl⟩|⟨rfl,rfl⟩) := h'',
{ exact (h' rfl).elim },
{ cases p'; simp! } },
{ apply ih, } } } },
end
@[simp] lemma take_until_copy {u v w v' w'} (p : G.walk v w)
(hv : v = v') (hw : w = w') (h : u ∈ (p.copy hv hw).support) :
(p.copy hv hw).take_until u h = (p.take_until u (by { subst_vars, exact h })).copy hv rfl :=
by { subst_vars, refl }
@[simp] lemma drop_until_copy {u v w v' w'} (p : G.walk v w)
(hv : v = v') (hw : w = w') (h : u ∈ (p.copy hv hw).support) :
(p.copy hv hw).drop_until u h = (p.drop_until u (by { subst_vars, exact h })).copy rfl hw :=
by { subst_vars, refl }
lemma support_take_until_subset {u v w : V} (p : G.walk v w) (h : u ∈ p.support) :
(p.take_until u h).support ⊆ p.support :=
λ x hx, by { rw [← take_spec p h, mem_support_append_iff], exact or.inl hx }
lemma support_drop_until_subset {u v w : V} (p : G.walk v w) (h : u ∈ p.support) :
(p.drop_until u h).support ⊆ p.support :=
λ x hx, by { rw [← take_spec p h, mem_support_append_iff], exact or.inr hx }
lemma darts_take_until_subset {u v w : V} (p : G.walk v w) (h : u ∈ p.support) :
(p.take_until u h).darts ⊆ p.darts :=
λ x hx, by { rw [← take_spec p h, darts_append, list.mem_append], exact or.inl hx }
lemma darts_drop_until_subset {u v w : V} (p : G.walk v w) (h : u ∈ p.support) :
(p.drop_until u h).darts ⊆ p.darts :=
λ x hx, by { rw [← take_spec p h, darts_append, list.mem_append], exact or.inr hx }
lemma edges_take_until_subset {u v w : V} (p : G.walk v w) (h : u ∈ p.support) :
(p.take_until u h).edges ⊆ p.edges :=
list.map_subset _ (p.darts_take_until_subset h)
lemma edges_drop_until_subset {u v w : V} (p : G.walk v w) (h : u ∈ p.support) :
(p.drop_until u h).edges ⊆ p.edges :=
list.map_subset _ (p.darts_drop_until_subset h)
lemma length_take_until_le {u v w : V} (p : G.walk v w) (h : u ∈ p.support) :
(p.take_until u h).length ≤ p.length :=
begin
have := congr_arg walk.length (p.take_spec h),
rw [length_append] at this,
exact nat.le.intro this,
end
lemma length_drop_until_le {u v w : V} (p : G.walk v w) (h : u ∈ p.support) :
(p.drop_until u h).length ≤ p.length :=
begin
have := congr_arg walk.length (p.take_spec h),
rw [length_append, add_comm] at this,
exact nat.le.intro this,
end
protected
lemma is_trail.take_until {u v w : V} {p : G.walk v w} (hc : p.is_trail) (h : u ∈ p.support) :
(p.take_until u h).is_trail :=
is_trail.of_append_left (by rwa ← take_spec _ h at hc)
protected
lemma is_trail.drop_until {u v w : V} {p : G.walk v w} (hc : p.is_trail) (h : u ∈ p.support) :
(p.drop_until u h).is_trail :=
is_trail.of_append_right (by rwa ← take_spec _ h at hc)
protected
lemma is_path.take_until {u v w : V} {p : G.walk v w} (hc : p.is_path) (h : u ∈ p.support) :
(p.take_until u h).is_path :=
is_path.of_append_left (by rwa ← take_spec _ h at hc)
protected
lemma is_path.drop_until {u v w : V} (p : G.walk v w) (hc : p.is_path) (h : u ∈ p.support) :
(p.drop_until u h).is_path :=
is_path.of_append_right (by rwa ← take_spec _ h at hc)
/-- Rotate a loop walk such that it is centered at the given vertex. -/
def rotate {u v : V} (c : G.walk v v) (h : u ∈ c.support) : G.walk u u :=
(c.drop_until u h).append (c.take_until u h)
@[simp]
lemma support_rotate {u v : V} (c : G.walk v v) (h : u ∈ c.support) :
(c.rotate h).support.tail ~r c.support.tail :=
begin
simp only [rotate, tail_support_append],
apply list.is_rotated.trans list.is_rotated_append,
rw [←tail_support_append, take_spec],
end
lemma rotate_darts {u v : V} (c : G.walk v v) (h : u ∈ c.support) :
(c.rotate h).darts ~r c.darts :=
begin
simp only [rotate, darts_append],
apply list.is_rotated.trans list.is_rotated_append,
rw [←darts_append, take_spec],
end
lemma rotate_edges {u v : V} (c : G.walk v v) (h : u ∈ c.support) :
(c.rotate h).edges ~r c.edges :=
(rotate_darts c h).map _
protected
lemma is_trail.rotate {u v : V} {c : G.walk v v} (hc : c.is_trail) (h : u ∈ c.support) :
(c.rotate h).is_trail :=
begin
rw [is_trail_def, (c.rotate_edges h).perm.nodup_iff],
exact hc.edges_nodup,
end
protected
lemma is_circuit.rotate {u v : V} {c : G.walk v v} (hc : c.is_circuit) (h : u ∈ c.support) :
(c.rotate h).is_circuit :=
begin
refine ⟨hc.to_trail.rotate _, _⟩,
cases c,
{ exact (hc.ne_nil rfl).elim, },
{ intro hn,
have hn' := congr_arg length hn,
rw [rotate, length_append, add_comm, ← length_append, take_spec] at hn',
simpa using hn', },
end
protected
lemma is_cycle.rotate {u v : V} {c : G.walk v v} (hc : c.is_cycle) (h : u ∈ c.support) :
(c.rotate h).is_cycle :=
begin
refine ⟨hc.to_circuit.rotate _, _⟩,
rw list.is_rotated.nodup_iff (support_rotate _ _),
exact hc.support_nodup,