forked from leanprover-community/mathlib4
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCopy.lean
More file actions
686 lines (513 loc) · 28.3 KB
/
Copy pathCopy.lean
File metadata and controls
686 lines (513 loc) · 28.3 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
/-
Copyright (c) 2023 Yaël Dillies, Mitchell Horner. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yaël Dillies, Mitchell Horner
-/
module
public import Mathlib.Algebra.Order.Group.Nat
public import Mathlib.Combinatorics.SimpleGraph.Subgraph
/-!
# Containment of graphs
This file introduces the concept of one simple graph containing a copy of another.
For two simple graphs `G` and `H`, a *copy* of `G` in `H` is a (not necessarily induced) subgraph of
`H` isomorphic to `G`.
If there exists a copy of `G` in `H`, we say that `H` *contains* `G`. This is equivalent to saying
that there is an injective graph homomorphism `G → H` between them (this is **not** the same as a
graph embedding, as we do not require the subgraph to be induced).
If there exists an induced copy of `G` in `H`, we say that `H` *inducingly contains* `G`. This is
equivalent to saying that there is a graph embedding `G ↪ H`.
## Main declarations
Containment:
* `SimpleGraph.Copy G H` is the type of copies of `G` in `H`, implemented as the subtype of
*injective* homomorphisms.
* `SimpleGraph.IsContained G H`, `G ⊑ H` is the relation that `H` contains a copy of `G`, that
is, the type of copies of `G` in `H` is nonempty. This is equivalent to the existence of an
isomorphism from `G` to a subgraph of `H`.
This is similar to `SimpleGraph.IsSubgraph` except that the simple graphs here need not have the
same underlying vertex type.
* `SimpleGraph.Free` is the predicate that `H` is `G`-free, that is, `H` does not contain a copy of
`G`. This is the negation of `SimpleGraph.IsContained` implemented for convenience.
* `SimpleGraph.killCopies G H`: Subgraph of `G` that does not contain `H`. Obtained by arbitrarily
removing an edge from each copy of `H` in `G`.
* `SimpleGraph.copyCount G H`: Number of copies of `H` in `G`, i.e. number of subgraphs of `G`
isomorphic to `H`.
* `SimpleGraph.labelledCopyCount G H`: Number of labelled copies of `H` in `G`, i.e. number of
graph embeddings from `H` to `G`.
Induced containment:
* Induced copies of `G` inside `H` are already defined as `G ↪g H`.
* `SimpleGraph.IsIndContained G H` : `G` is contained as an induced subgraph in `H`.
## Notation
The following notation is declared in scope `SimpleGraph`:
* `G ⊑ H` for `SimpleGraph.IsContained G H`.
* `G ⊴ H` for `SimpleGraph.IsIndContained G H`.
## TODO
* Relate `⊥ ⊴ H` to there being an independent set in `H`.
* Count induced copies of a graph inside another.
* Make `copyCount`/`labelledCopyCount` computable (not necessarily efficiently).
-/
@[expose] public section
open Finset Function
open Fintype (card)
namespace SimpleGraph
variable {V W X α β γ : Type*} {G G₁ G₂ G₃ : SimpleGraph V} {H : SimpleGraph W} {I : SimpleGraph X}
{A : SimpleGraph α} {B : SimpleGraph β} {C : SimpleGraph γ}
/-!
### Copies
#### Not necessarily induced copies
A copy of a subgraph `G` inside a subgraph `H` is an embedding of the vertices of `G` into the
vertices of `H`, such that adjacency in `G` implies adjacency in `H`.
We capture this concept by injective graph homomorphisms.
-/
section Copy
/-- The type of copies as a subtype of *injective* homomorphisms. -/
structure Copy (A : SimpleGraph α) (B : SimpleGraph β) where
/-- A copy gives rise to a homomorphism. -/
toHom : A →g B
injective' : Injective toHom
/-- An injective homomorphism gives rise to a copy. -/
abbrev Hom.toCopy (f : A →g B) (h : Injective f) : Copy A B := .mk f h
/-- An embedding gives rise to a copy. -/
abbrev Embedding.toCopy (f : A ↪g B) : Copy A B := f.toHom.toCopy f.injective
/-- An isomorphism gives rise to a copy. -/
abbrev Iso.toCopy (f : A ≃g B) : Copy A B := f.toEmbedding.toCopy
namespace Copy
instance : FunLike (Copy A B) α β where
coe f := DFunLike.coe f.toHom
coe_injective' f g h := by obtain ⟨⟨_, _⟩, _⟩ := f; congr!
lemma injective (f : Copy A B) : Injective f.toHom := f.injective'
@[ext] lemma ext {f g : Copy A B} : (∀ a, f a = g a) → f = g := DFunLike.ext _ _
@[simp] lemma coe_toHom (f : Copy A B) : ⇑f.toHom = f := rfl
@[simp] lemma toHom_apply (f : Copy A B) (a : α) : ⇑f.toHom a = f a := rfl
@[simp] lemma coe_mk (f : A →g B) (hf) : ⇑(.mk f hf : Copy A B) = f := rfl
/-- A copy induces an embedding of edge sets. -/
def mapEdgeSet (f : Copy A B) : A.edgeSet ↪ B.edgeSet where
toFun := f.toHom.mapEdgeSet
inj' := Hom.mapEdgeSet.injective f.toHom f.injective
/-- A copy induces an embedding of neighbor sets. -/
def mapNeighborSet (f : Copy A B) (a : α) :
A.neighborSet a ↪ B.neighborSet (f a) where
toFun v := ⟨f v, f.toHom.apply_mem_neighborSet v.prop⟩
inj' _ _ h := by
rw [Subtype.mk_eq_mk] at h ⊢
exact f.injective h
/-- A copy gives rise to an embedding of vertex types. -/
def toEmbedding (f : Copy A B) : α ↪ β := ⟨f, f.injective⟩
/-- The identity copy from a simple graph to itself. -/
@[refl] def id (G : SimpleGraph V) : Copy G G := ⟨Hom.id, Function.injective_id⟩
@[simp, norm_cast] lemma coe_id : ⇑(id G) = _root_.id := rfl
/-- The composition of copies is a copy. -/
def comp (g : Copy B C) (f : Copy A B) : Copy A C := by
use g.toHom.comp f.toHom
rw [Hom.coe_comp]
exact g.injective.comp f.injective
@[simp]
theorem comp_apply (g : Copy B C) (f : Copy A B) (a : α) : g.comp f a = g (f a) :=
RelHom.comp_apply g.toHom f.toHom a
/-- The copy from a subgraph to the supergraph. -/
def ofLE (G₁ G₂ : SimpleGraph V) (h : G₁ ≤ G₂) : Copy G₁ G₂ := ⟨Hom.ofLE h, Function.injective_id⟩
@[simp, norm_cast]
theorem coe_comp (g : Copy B C) (f : Copy A B) : ⇑(g.comp f) = g ∘ f := by ext; simp
@[simp, norm_cast] lemma coe_ofLE (h : G₁ ≤ G₂) : ⇑(ofLE G₁ G₂ h) = _root_.id := rfl
@[simp] theorem ofLE_refl : ofLE G G le_rfl = id G := by ext; simp
@[simp]
theorem ofLE_comp (h₁₂ : G₁ ≤ G₂) (h₂₃ : G₂ ≤ G₃) :
(ofLE _ _ h₂₃).comp (ofLE _ _ h₁₂) = ofLE _ _ (h₁₂.trans h₂₃) := by ext; simp
/-- The copy from an induced subgraph to the initial simple graph. -/
def induce (G : SimpleGraph V) (s : Set V) : Copy (G.induce s) G := (Embedding.induce s).toCopy
/-- The copy of `⊥` in any simple graph that can embed its vertices. -/
protected def bot (f : α ↪ β) : Copy (⊥ : SimpleGraph α) B := ⟨⟨f, False.elim⟩, f.injective⟩
set_option backward.isDefEq.respectTransparency false in
/-- The isomorphism from a subgraph of `A` to its map under a copy `f : Copy A B`. -/
noncomputable def isoSubgraphMap (f : Copy A B) (A' : A.Subgraph) :
A'.coe ≃g (A'.map f.toHom).coe := by
use Equiv.Set.image f.toHom _ f.injective
simp_rw [Subgraph.map_verts, Equiv.Set.image_apply, Subgraph.coe_adj, Subgraph.map_adj,
Relation.map_apply, f.injective.eq_iff, exists_eq_right_right, exists_eq_right, forall_true_iff]
/-- The subgraph of `B` corresponding to a copy of `A` inside `B`. -/
abbrev toSubgraph (f : Copy A B) : B.Subgraph := .map f.toHom ⊤
/-- The isomorphism from `A` to its copy under `f : Copy A B`. -/
noncomputable def isoToSubgraph (f : Copy A B) : A ≃g f.toSubgraph.coe :=
(f.isoSubgraphMap ⊤).comp Subgraph.topIso.symm
@[simp] lemma range_toSubgraph :
.range (toSubgraph (A := A)) = {B' : B.Subgraph | Nonempty (A ≃g B'.coe)} := by
ext H'
constructor
· rintro ⟨f, hf, rfl⟩
simpa [toSubgraph] using ⟨f.isoToSubgraph⟩
· rintro ⟨e⟩
refine ⟨⟨H'.hom.comp e.toHom, Subgraph.hom_injective.comp e.injective⟩, ?_⟩
simp [toSubgraph, Subgraph.map_comp]
lemma toSubgraph_surjOn :
Set.SurjOn (toSubgraph (A := A)) .univ {B' : B.Subgraph | Nonempty (A ≃g B'.coe)} :=
fun H' hH' ↦ by simpa
instance [Subsingleton (V → W)] : Subsingleton (G.Copy H) := DFunLike.coe_injective.subsingleton
instance [Fintype {f : G →g H // Injective f}] : Fintype (G.Copy H) :=
.ofEquiv {f : G →g H // Injective f} {
toFun f := ⟨f.1, f.2⟩
invFun f := ⟨f.1, f.2⟩
}
instance instFintypeInjectiveHom
[Fintype V] [Fintype W] [DecidableEq V] [DecidableEq W]
[DecidableRel G.Adj] [DecidableRel H.Adj] :
Fintype {f : G →g H // Function.Injective f} :=
Fintype.ofEquiv {f : V → W // (∀ v w, G.Adj v w → H.Adj (f v) (f w)) ∧ Function.Injective f}
{ toFun := fun f => ⟨⟨f.1, fun h => f.2.1 _ _ h⟩, f.2.2⟩
invFun := fun f => ⟨f.1, ⟨fun v w h => f.1.map_adj h, f.2⟩⟩
left_inv := by intro f; rfl
right_inv := by intro f; rfl }
/-- A copy of `⊤` gives rise to an embedding of `⊤`. -/
@[simps!]
def topEmbedding (f : Copy (⊤ : SimpleGraph α) G) : (⊤ : SimpleGraph α) ↪g G :=
{ f.toEmbedding with
map_rel_iff' := fun {v w} ↦ ⟨fun h ↦ by simpa using h.ne, f.toHom.map_adj⟩}
end Copy
/-- A `Subgraph G` gives rise to a copy from the coercion to `G`. -/
def Subgraph.coeCopy (G' : G.Subgraph) : Copy G'.coe G := G'.hom.toCopy hom_injective
end Copy
/-!
#### Induced copies
An induced copy of a graph `G` inside a graph `H` is an embedding from the vertices of
`G` into the vertices of `H` which preserves the adjacency relation.
This is already captured by the notion of graph embeddings, defined as `G ↪g H`.
### Containment
#### Not necessarily induced containment
A graph `H` *contains* a graph `G` if there is some copy `f : Copy G H` of `G` inside `H`. This
amounts to `H` having a subgraph isomorphic to `G`.
We denote "`G` is contained in `H`" by `G ⊑ H` (`\squb`).
-/
section IsContained
/-- The relation `IsContained A B`, `A ⊑ B` says that `B` contains a copy of `A`.
This is equivalent to the existence of an isomorphism from `A` to a subgraph of `B`. -/
abbrev IsContained (A : SimpleGraph α) (B : SimpleGraph β) := Nonempty (Copy A B)
@[inherit_doc] scoped infixl:50 " ⊑ " => SimpleGraph.IsContained
/-- A simple graph contains itself. -/
@[refl] protected theorem IsContained.refl (G : SimpleGraph V) : G ⊑ G := ⟨.id G⟩
protected theorem IsContained.rfl : G ⊑ G := IsContained.refl G
/-- A simple graph contains its subgraphs. -/
theorem IsContained.of_le (h : G₁ ≤ G₂) : G₁ ⊑ G₂ := ⟨.ofLE G₁ G₂ h⟩
/-- If `A` contains `B` and `B` contains `C`, then `A` contains `C`. -/
theorem IsContained.trans : A ⊑ B → B ⊑ C → A ⊑ C := fun ⟨f⟩ ⟨g⟩ ↦ ⟨g.comp f⟩
/-- If `B` contains `C` and `A` contains `B`, then `A` contains `C`. -/
theorem IsContained.trans' : B ⊑ C → A ⊑ B → A ⊑ C := flip IsContained.trans
@[gcongr]
lemma IsContained.mono_right {B' : SimpleGraph β} (h_isub : A ⊑ B) (h_sub : B ≤ B') : A ⊑ B' :=
h_isub.trans <| IsContained.of_le h_sub
alias IsContained.trans_le := IsContained.mono_right
@[gcongr]
lemma IsContained.mono_left {A' : SimpleGraph α} (h_sub : A ≤ A') (h_isub : A' ⊑ B) : A ⊑ B :=
(IsContained.of_le h_sub).trans h_isub
alias IsContained.trans_le' := IsContained.mono_left
/-- If `A ≃g H` and `B ≃g G` then `A` is contained in `B` if and only if `H` is contained
in `G`. -/
theorem isContained_congr (e₁ : A ≃g H) (e₂ : B ≃g G) : A ⊑ B ↔ H ⊑ G :=
⟨.trans' ⟨e₂.toCopy⟩ ∘ .trans ⟨e₁.symm.toCopy⟩, .trans' ⟨e₂.symm.toCopy⟩ ∘ .trans ⟨e₁.toCopy⟩⟩
lemma isContained_congr_left (e₁ : A ≃g B) : A ⊑ C ↔ B ⊑ C := isContained_congr e₁ .refl
alias ⟨_, IsContained.congr_left⟩ := isContained_congr_left
lemma isContained_congr_right (e₂ : B ≃g C) : A ⊑ B ↔ A ⊑ C := isContained_congr .refl e₂
alias ⟨_, IsContained.congr_right⟩ := isContained_congr_right
instance : IsPreorder (SimpleGraph α) IsContained where
refl := .refl
trans _ _ _ := .trans
instance :
Trans (α := SimpleGraph α) (β := SimpleGraph β) (γ := SimpleGraph γ)
IsContained IsContained IsContained where
trans := .trans
/-- A simple graph having no vertices is contained in any simple graph. -/
lemma IsContained.of_isEmpty [IsEmpty α] : A ⊑ B :=
⟨⟨isEmptyElim, fun {a} ↦ isEmptyElim a⟩, isEmptyElim⟩
/-- `⊥` is contained in any simple graph having sufficiently many vertices. -/
lemma bot_isContained_iff_card_le [Fintype α] [Fintype β] :
(⊥ : SimpleGraph α) ⊑ B ↔ Fintype.card α ≤ Fintype.card β :=
⟨fun ⟨f⟩ ↦ Fintype.card_le_of_embedding f.toEmbedding,
fun h ↦ ⟨Copy.bot (Function.Embedding.nonempty_of_card_le h).some⟩⟩
protected alias IsContained.bot := bot_isContained_iff_card_le
/-- A simple graph `G` contains all `Subgraph G` coercions. -/
lemma Subgraph.coe_isContained (G' : G.Subgraph) : G'.coe ⊑ G := ⟨G'.coeCopy⟩
/-- `B` contains `A` if and only if `B` has a subgraph `B'` and `B'` is isomorphic to `A`. -/
theorem isContained_iff_exists_iso_subgraph :
A ⊑ B ↔ ∃ B' : B.Subgraph, Nonempty (A ≃g B'.coe) where
mp := fun ⟨f⟩ ↦ ⟨.map f.toHom ⊤, ⟨f.isoToSubgraph⟩⟩
mpr := fun ⟨B', ⟨e⟩⟩ ↦ B'.coe_isContained.trans' ⟨e.toCopy⟩
alias ⟨IsContained.exists_iso_subgraph, IsContained.of_exists_iso_subgraph⟩ :=
isContained_iff_exists_iso_subgraph
theorem Copy.degree_le (f : Copy G H) (v : V) [Fintype <| G.neighborSet v]
[Fintype <| H.neighborSet (f v)] : G.degree v ≤ H.degree (f v) := by
simpa using Fintype.card_le_of_injective _ (f.mapNeighborSet v).injective
theorem Copy.max_degree_le [Fintype V] [Fintype W] [DecidableRel G.Adj] [DecidableRel H.Adj]
(f : Copy G H) : G.maxDegree ≤ H.maxDegree := by
cases isEmpty_or_nonempty V
· simp
obtain ⟨v, h⟩ := exists_maximal_degree_vertex G
grind [degree_le_maxDegree H (f v), f.degree_le v]
theorem IsContained.max_degree_le [Fintype V] [Fintype W] [DecidableRel G.Adj] [DecidableRel H.Adj]
(h : G ⊑ H) : G.maxDegree ≤ H.maxDegree := by
have ⟨f⟩ := h
exact f.max_degree_le
@[gcongr]
lemma maxDegree_mono {H : SimpleGraph V} [Fintype V] [DecidableRel G.Adj] [DecidableRel H.Adj]
(hle : G ≤ H) : G.maxDegree ≤ H.maxDegree :=
IsContained.of_le hle |>.max_degree_le
theorem Copy.minDegree_le [Fintype V] [Fintype W] [DecidableRel G.Adj] [DecidableRel H.Adj]
{f : Copy G H} (hf : Function.Surjective f) : G.minDegree ≤ H.minDegree := by
cases isEmpty_or_nonempty W
· simp [Function.isEmpty f]
refine H.le_minDegree_of_forall_le_degree _ fun w ↦ ?_
obtain ⟨v, rfl⟩ := hf w
grw [← f.degree_le, ← minDegree_le_degree]
theorem Hom.minDegree_le [Fintype V] [Fintype W] [DecidableRel G.Adj] [DecidableRel H.Adj]
{f : G →g H} (hf : Function.Bijective f) : G.minDegree ≤ H.minDegree :=
Copy.minDegree_le (f := ⟨f, hf.injective⟩) hf.surjective
end IsContained
section Free
/-- `A.Free B` means that `B` does not contain a copy of `A`. -/
abbrev Free (A : SimpleGraph α) (B : SimpleGraph β) := ¬A ⊑ B
lemma not_free : ¬A.Free B ↔ A ⊑ B := not_not
/-- If `A ≃g H` and `B ≃g G` then `B` is `A`-free if and only if `G` is `H`-free. -/
theorem free_congr (e₁ : A ≃g H) (e₂ : B ≃g G) : A.Free B ↔ H.Free G :=
(isContained_congr e₁ e₂).not
lemma free_congr_left (e₁ : A ≃g B) : A.Free C ↔ B.Free C := free_congr e₁ .refl
alias ⟨_, Free.congr_left⟩ := free_congr_left
lemma free_congr_right (e₂ : B ≃g C) : A.Free B ↔ A.Free C := free_congr .refl e₂
alias ⟨_, Free.congr_right⟩ := free_congr_right
lemma free_bot (h : A ≠ ⊥) : A.Free (⊥ : SimpleGraph β) := by
rw [← edgeSet_nonempty] at h
intro ⟨f, hf⟩
absurd f.map_mem_edgeSet h.choose_spec
rw [edgeSet_bot]
exact Set.notMem_empty (h.choose.map f)
end Free
/-!
#### Induced containment
A graph `H` *inducingly contains* a graph `G` if there is some graph embedding `G ↪ H`. This amounts
to `H` having an induced subgraph isomorphic to `G`.
We denote "`G` is inducingly contained in `H`" by `G ⊴ H` (`\trianglelefteq`).
-/
/-- A simple graph `G` is inducingly contained in a simple graph `H` if there exists an induced
subgraph of `H` isomorphic to `G`. This is denoted by `G ⊴ H`. -/
def IsIndContained (G : SimpleGraph V) (H : SimpleGraph W) : Prop := Nonempty (G ↪g H)
@[inherit_doc] scoped infixl:50 " ⊴ " => SimpleGraph.IsIndContained
protected lemma Copy.isContained (f : Copy G H) : G ⊑ H := ⟨f⟩
protected lemma Embedding.isIndContained (f : G ↪g H) : G ⊴ H := ⟨f⟩
protected lemma Embedding.isContained (f : G ↪g H) : G ⊑ H := f.toCopy.isContained
protected lemma IsIndContained.isContained : G ⊴ H → G ⊑ H := fun ⟨f⟩ ↦ f.isContained
/-- If `G` is isomorphic to `H`, then `G` is contained in `H`. -/
protected lemma Iso.isContained (e : G ≃g H) : G ⊑ H := e.toCopy.isContained
/-- If `G` is isomorphic to `H`, then `H` is contained in `G`. -/
protected lemma Iso.isContained' (e : G ≃g H) : H ⊑ G := e.symm.isContained
/-- If `G` is isomorphic to `H`, then `G` is inducingly contained in `H`. -/
protected lemma Iso.isIndContained (e : G ≃g H) : G ⊴ H := e.toEmbedding.isIndContained
/-- If `G` is isomorphic to `H`, then `H` is inducingly contained in `G`. -/
protected lemma Iso.isIndContained' (e : G ≃g H) : H ⊴ G := e.symm.isIndContained
protected lemma Subgraph.IsInduced.isIndContained {G' : G.Subgraph} (hG' : G'.IsInduced) :
G'.coe ⊴ G :=
⟨{ toFun := (↑)
inj' := Subtype.coe_injective
map_rel_iff' := hG'.adj.symm }⟩
@[refl] lemma IsIndContained.refl (G : SimpleGraph V) : G ⊴ G := ⟨Embedding.refl⟩
lemma IsIndContained.rfl : G ⊴ G := .refl _
@[trans] lemma IsIndContained.trans : G ⊴ H → H ⊴ I → G ⊴ I := fun ⟨f⟩ ⟨g⟩ ↦ ⟨g.comp f⟩
instance : IsPreorder (SimpleGraph α) IsIndContained where
refl := .refl
trans _ _ _ := .trans
instance :
Trans (α := SimpleGraph α) (β := SimpleGraph β) (γ := SimpleGraph γ)
IsIndContained IsIndContained IsIndContained where
trans := .trans
lemma IsIndContained.of_isEmpty [IsEmpty V] : G ⊴ H :=
⟨{ toFun := isEmptyElim
inj' := isEmptyElim
map_rel_iff' := fun {a} ↦ isEmptyElim a }⟩
lemma isIndContained_iff_exists_iso_subgraph :
G ⊴ H ↔ ∃ (H' : H.Subgraph) (_e : G ≃g H'.coe), H'.IsInduced := by
constructor
· rintro ⟨f⟩
refine ⟨Subgraph.map f.toHom ⊤, f.toCopy.isoToSubgraph, ?_⟩
simp [Subgraph.IsInduced, Relation.map_apply_apply, f.injective]
· rintro ⟨H', e, hH'⟩
exact e.isIndContained.trans hH'.isIndContained
alias ⟨IsIndContained.exists_iso_subgraph, IsIndContained.of_exists_iso_subgraph⟩ :=
isIndContained_iff_exists_iso_subgraph
theorem isIndContained_iff_exists_iso_induce : G ⊴ H ↔ ∃ s, Nonempty (G ≃g H.induce s) :=
⟨fun ⟨f⟩ ↦ ⟨Set.range f, ⟨f.isoInduceRange⟩⟩, fun ⟨s, ⟨f⟩⟩ ↦ ⟨.comp (.induce s) f⟩⟩
@[simp] lemma top_isIndContained_iff_top_isContained :
(⊤ : SimpleGraph V) ⊴ H ↔ (⊤ : SimpleGraph V) ⊑ H :=
⟨IsIndContained.isContained, fun ⟨f⟩ ↦ ⟨f.topEmbedding⟩⟩
@[simp] lemma compl_isIndContained_compl : Gᶜ ⊴ Hᶜ ↔ G ⊴ H :=
Embedding.complEquiv.symm.nonempty_congr
protected alias ⟨IsIndContained.of_compl, IsIndContained.compl⟩ := compl_isIndContained_compl
theorem isContained_iff_exists_le_comap : H ⊑ G ↔ ∃ (f : W ↪ V), H ≤ G.comap f :=
⟨fun ⟨f⟩ ↦ ⟨f.toEmbedding, f.toHom.le_comap⟩, fun ⟨f, h⟩ ↦ ⟨⟨f, (h ·)⟩, f.injective⟩⟩
theorem isIndContained_iff_exists_comap_eq : H ⊴ G ↔ ∃ (f : W ↪ V), G.comap f = H :=
⟨fun ⟨f⟩ ↦ ⟨f.toEmbedding, f.comap_eq⟩, fun ⟨f, h⟩ ↦ ⟨f, h ▸ .rfl⟩⟩
/-!
### Counting the copies
If `G` and `H` are finite graphs, we can count the number of unlabelled and labelled copies of `G`
in `H`.
#### Not necessarily induced copies
-/
section LabelledCopyCount
variable [Fintype V] [Fintype W]
/-- `G.labelledCopyCount H` is the number of labelled copies of `H` in `G`, i.e. the number of graph
embeddings from `H` to `G`. See `SimpleGraph.copyCount` for the number of unlabelled copies. -/
noncomputable def labelledCopyCount (G : SimpleGraph V) (H : SimpleGraph W) : ℕ := by
classical exact Fintype.card (Copy H G)
def labelledCopyCountComputable (G : SimpleGraph V) (H : SimpleGraph W) [Fintype V] [Fintype W]
[DecidableEq V] [DecidableEq W]
[DecidableRel G.Adj] [DecidableRel H.Adj] : ℕ := Fintype.card (Copy H G)
@[simp] lemma labelledCopyCount_of_isEmpty [IsEmpty W] (G : SimpleGraph V) (H : SimpleGraph W) :
G.labelledCopyCount H = 1 := by
convert Fintype.card_unique
exact { default := ⟨default, isEmptyElim⟩, uniq := fun _ ↦ Subsingleton.elim _ _ }
@[simp] lemma labelledCopyCount_eq_zero : G.labelledCopyCount H = 0 ↔ H.Free G := by
simp [labelledCopyCount, Fintype.card_eq_zero_iff]
@[simp] lemma labelledCopyCount_pos : 0 < G.labelledCopyCount H ↔ H ⊑ G := by
simp [labelledCopyCount, IsContained, Fintype.card_pos_iff]
end LabelledCopyCount
section CopyCount
variable [Fintype V]
/-- `G.copyCount H` is the number of unlabelled copies of `H` in `G`, i.e. the number of subgraphs
of `G` isomorphic to `H`. See `SimpleGraph.labelledCopyCount` for the number of labelled copies. -/
noncomputable def copyCount (G : SimpleGraph V) (H : SimpleGraph W) : ℕ := by
classical exact #{G' : G.Subgraph | Nonempty (H ≃g G'.coe)}
lemma copyCount_eq_card_image_copyToSubgraph [Fintype {f : H →g G // Injective f}]
[DecidableEq G.Subgraph] :
copyCount G H = #((Finset.univ : Finset (H.Copy G)).image Copy.toSubgraph) := by
rw [copyCount]
congr
refine Finset.coe_injective ?_
simpa [-Copy.range_toSubgraph] using Copy.range_toSubgraph.symm
@[simp] lemma copyCount_eq_zero : G.copyCount H = 0 ↔ H.Free G := by
simp [copyCount, Free, -nonempty_subtype, isContained_iff_exists_iso_subgraph,
filter_eq_empty_iff]
@[simp] lemma copyCount_pos : 0 < G.copyCount H ↔ H ⊑ G := by
simp [copyCount, -nonempty_subtype, isContained_iff_exists_iso_subgraph, card_pos,
filter_nonempty_iff]
/-- There's at least as many labelled copies of `H` in `G` than unlabelled ones. -/
lemma copyCount_le_labelledCopyCount [Fintype W] : G.copyCount H ≤ G.labelledCopyCount H := by
classical rw [copyCount_eq_card_image_copyToSubgraph]; exact card_image_le
@[simp] lemma copyCount_bot (G : SimpleGraph V) : copyCount G (⊥ : SimpleGraph V) = 1 := by
classical
rw [copyCount]
convert card_singleton (α := G.Subgraph)
{ verts := .univ
Adj := ⊥
adj_sub := False.elim
edge_vert := False.elim }
simp only [eq_singleton_iff_unique_mem, mem_filter_univ, Nonempty.forall]
refine ⟨⟨⟨(Equiv.Set.univ _).symm, by simp⟩⟩, fun H' e ↦
Subgraph.ext ((set_fintype_card_eq_univ_iff _).1 <| Fintype.card_congr e.toEquiv.symm) ?_⟩
ext a b
simp only [Prop.bot_eq_false, Pi.bot_apply, iff_false]
exact fun hab ↦ e.symm.map_rel_iff.2 hab.coe
@[simp] lemma copyCount_of_isEmpty [IsEmpty W] (G : SimpleGraph V) (H : SimpleGraph W) :
G.copyCount H = 1 := by
cases nonempty_fintype W
exact (copyCount_le_labelledCopyCount.trans_eq <| labelledCopyCount_of_isEmpty ..).antisymm <|
copyCount_pos.2 <| .of_isEmpty
end CopyCount
/-!
#### Induced copies
TODO
### Killing a subgraph
An important aspect of graph containment is that we can remove not too many edges from a graph `H`
to get a graph `H'` that doesn't contain `G`.
#### Killing not necessarily induced copies
`SimpleGraph.killCopies G H` is a subgraph of `G` where an edge was removed from each copy of `H` in
`G`. By construction, it doesn't contain `H` and has at most the number of copies of `H` edges less
than `G`.
-/
set_option backward.privateInPublic true in
private lemma aux (hH : H ≠ ⊥) {G' : G.Subgraph} :
Nonempty (H ≃g G'.coe) → G'.edgeSet.Nonempty := by
obtain ⟨e, he⟩ := edgeSet_nonempty.2 hH
rw [← Subgraph.image_coe_edgeSet_coe]
exact fun ⟨f⟩ ↦ Set.Nonempty.image _ ⟨_, f.map_mem_edgeSet_iff.2 he⟩
set_option backward.privateInPublic true in
set_option backward.privateInPublic.warn false in
/-- `G.killCopies H` is a subgraph of `G` where an *arbitrary* edge was removed from each copy of
`H` in `G`. By construction, it doesn't contain `H` (unless `H` had no edges) and has at most the
number of copies of `H` edges less than `G`. See `free_killCopies` and
`le_card_edgeFinset_killCopies` for these two properties. -/
noncomputable irreducible_def killCopies (G : SimpleGraph V) (H : SimpleGraph W) :
SimpleGraph V := by
classical exact
if hH : H = ⊥ then G
else G.deleteEdges <| ⋃ (G' : G.Subgraph) (hG' : Nonempty (H ≃g G'.coe)), {(aux hH hG').some}
/-- Removing an edge from `G` for each subgraph isomorphic to `H` results in a subgraph of `G`. -/
lemma killCopies_le_left : G.killCopies H ≤ G := by
rw [killCopies]; split_ifs; exacts [le_rfl, deleteEdges_le _]
@[simp] lemma killCopies_bot (G : SimpleGraph V) : G.killCopies (⊥ : SimpleGraph W) = G := by
rw [killCopies]; exact dif_pos rfl
private lemma killCopies_of_ne_bot (hH : H ≠ ⊥) (G : SimpleGraph V) :
G.killCopies H =
G.deleteEdges (⋃ (G' : G.Subgraph) (hG' : Nonempty (H ≃g G'.coe)), {(aux hH hG').some}) := by
rw [killCopies]; exact dif_neg hH
/-- `G.killCopies H` has no effect on `G` if and only if `G` already contained no copies of `H`. See
`Free.killCopies_eq_left` for the reverse implication with no assumption on `H`. -/
lemma killCopies_eq_left (hH : H ≠ ⊥) : G.killCopies H = G ↔ H.Free G := by
simp only [killCopies_of_ne_bot hH, Set.disjoint_left, isContained_iff_exists_iso_subgraph,
@forall_comm _ G.Subgraph, deleteEdges_eq_self, Set.mem_iUnion,
not_exists, not_nonempty_iff, Nonempty.forall, Free]
exact forall_congr' fun G' ↦ ⟨fun h ↦ ⟨fun f ↦ h _
(Subgraph.edgeSet_subset _ <| (aux hH ⟨f⟩).choose_spec) f rfl⟩, fun h _ _ ↦ h.elim⟩
protected lemma Free.killCopies_eq_left (hHG : H.Free G) : G.killCopies H = G := by
obtain rfl | hH := eq_or_ne H ⊥
· exact killCopies_bot _
· exact (killCopies_eq_left hH).2 hHG
/-- Removing an edge from `G` for each subgraph isomorphic to `H` results in a graph that doesn't
contain `H`. -/
lemma free_killCopies (hH : H ≠ ⊥) : H.Free (G.killCopies H) := by
rw [killCopies_of_ne_bot hH, deleteEdges, Free, isContained_iff_exists_iso_subgraph]
rintro ⟨G', hHG'⟩
have hG' : (G'.map <| .ofLE (sdiff_le : G \ _ ≤ G)).edgeSet.Nonempty := by
rw [Subgraph.edgeSet_map]
exact (aux hH hHG').image _
set e := hG'.some with he
have : e ∈ _ := hG'.some_mem
clear_value e
rw [← Subgraph.image_coe_edgeSet_coe] at this
subst he
obtain ⟨e, he₀, he₁⟩ := this
let e' : Sym2 G'.verts := Sym2.map (Copy.isoSubgraphMap (.ofLE _ _ _) _).symm e
have he' : e' ∈ G'.coe.edgeSet := (Iso.map_mem_edgeSet_iff _).2 he₀
rw [Subgraph.edgeSet_coe] at he'
have := Subgraph.edgeSet_subset _ he'
simp only [edgeSet_sdiff, edgeSet_fromEdgeSet, edgeSet_sdiff_sdiff_isDiag, Set.mem_diff,
Set.mem_iUnion, not_exists] at this
refine this.2 (G'.map <| .ofLE sdiff_le) ⟨((Copy.ofLE _ _ _).isoSubgraphMap _).comp hHG'.some⟩ ?_
rw [Sym2.map_map, Set.mem_singleton_iff, ← he₁]
congr 1 with x
exact congr_arg _ (Equiv.Set.image_symm_apply _ _ injective_id _ _)
variable [Fintype G.edgeSet]
noncomputable instance killCopies.edgeSet.instFintype : Fintype (G.killCopies H).edgeSet :=
.ofInjective (Set.inclusion <| edgeSet_mono killCopies_le_left) <| Set.inclusion_injective _
/-- Removing an edge from `H` for each subgraph isomorphic to `G` means that the number of edges
we've removed is at most the number of copies of `G` in `H`. -/
lemma le_card_edgeFinset_killCopies [Fintype V] :
#G.edgeFinset - G.copyCount H ≤ #(G.killCopies H).edgeFinset := by
classical
obtain rfl | hH := eq_or_ne H ⊥
· simp [← card_edgeSet]
let f (G' : {G' : G.Subgraph // Nonempty (H ≃g G'.coe)}) := (aux hH G'.2).some
calc
_ = #G.edgeFinset - card {G' : G.Subgraph // Nonempty (H ≃g G'.coe)} := ?_
_ ≤ #G.edgeFinset - #(univ.image f) := Nat.sub_le_sub_left card_image_le _
_ = #G.edgeFinset - #(Set.range f).toFinset := by rw [Set.toFinset_range]
_ ≤ #(G.edgeFinset \ (Set.range f).toFinset) := le_card_sdiff ..
_ = #(G.killCopies H).edgeFinset := ?_
· simp only [edgeFinset, Set.toFinset_card]
rw [← Set.toFinset_card, ← edgeFinset, copyCount, ← card_subtype, subtype_univ, card_univ]
congr 1
ext e
induction e using Sym2.inductionOn with | hf v w
simp [mem_edgeSet, killCopies_of_ne_bot hH, f, eq_comm]
/-- Removing an edge from `H` for each subgraph isomorphic to `G` means that the number of edges
we've removed is at most the number of copies of `G` in `H`. -/
lemma le_card_edgeFinset_killCopies_add_copyCount [Fintype V] :
#G.edgeFinset ≤ #(G.killCopies H).edgeFinset + G.copyCount H :=
tsub_le_iff_right.1 le_card_edgeFinset_killCopies
/-!
#### Killing induced copies
TODO
-/
end SimpleGraph