Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
eeeba14
init
Jun2M Mar 16, 2026
72a23c6
mkall
Jun2M Mar 16, 2026
ab6b965
docs
Jun2M Mar 16, 2026
0e06a4a
inherit_doc
Jun2M Mar 16, 2026
e759a8f
prodDart
Jun2M Mar 16, 2026
391eca7
doc
Jun2M Mar 16, 2026
4d2c7a7
HasDart instances
Jun2M Mar 16, 2026
7a76b33
minor cleanup
Jun2M Mar 16, 2026
0396da0
mk_all
Jun2M Mar 16, 2026
f6b609d
Reflect doc
Jun2M Mar 16, 2026
757ed33
dart_iff_adj
Jun2M Mar 17, 2026
e2efa7c
apply Laura's suggsetions
Jun2M Mar 17, 2026
5a1787d
Reverse of darts
Jun2M Mar 17, 2026
45003bf
update instances
Jun2M Mar 17, 2026
79cfe9b
back to Dart
Jun2M Mar 17, 2026
b9957af
No fix points
Jun2M Mar 17, 2026
8c9442b
DartLike & step
Jun2M Mar 18, 2026
8996b14
doc change
Jun2M Mar 18, 2026
5051f88
mk_all
Jun2M Mar 18, 2026
f2115cb
doc
Jun2M Mar 18, 2026
cbfaa20
dartSym2 & dartSymm
Jun2M Mar 18, 2026
581877d
mv Symm to new PR
Jun2M Mar 18, 2026
62f39fd
init
Jun2M Mar 18, 2026
8902248
mk_all
Jun2M Mar 18, 2026
0e84c35
mk_all
Jun2M Mar 18, 2026
0e58e59
build fix
Jun2M Mar 18, 2026
773bdf7
quick save
Jun2M Mar 18, 2026
728055c
Merge branch 'HasDartDef' into SymmGraphLike
Jun2M Mar 18, 2026
ffe6e2b
update doc for step
Jun2M Mar 18, 2026
fad6117
darts_def
Jun2M Mar 19, 2026
4958d00
Digraph instance
Jun2M Mar 20, 2026
3064d67
Instances
Jun2M Mar 20, 2026
313de21
lint
Jun2M Mar 20, 2026
98ba7c4
lowercase
Jun2M Mar 20, 2026
a73392c
Merge branch 'master' into HasDartDef
Jun2M Mar 21, 2026
99f39d6
rm Graph GraphLike for now
Jun2M Mar 21, 2026
c35faee
mk_all
Jun2M Mar 21, 2026
c6206e4
Merge branch 'master' into SymmGraphLike
Jun2M Mar 21, 2026
1586764
accept review feedback
Jun2M Mar 27, 2026
6b57d36
Merge branch 'master' into HasDartDef
Jun2M Mar 27, 2026
e82c435
Merge branch 'master' into HasDartDef
Jun2M Apr 7, 2026
568873d
change type name
Jun2M Apr 7, 2026
285031b
Update Mathlib/Combinatorics/Digraph/GraphLike.lean
Jun2M Apr 7, 2026
24d44ff
Update Mathlib/Combinatorics/SimpleGraph/GraphLike.lean
Jun2M Apr 7, 2026
ae27cff
Type name change
Jun2M Apr 7, 2026
be0121a
Change other files too
Jun2M Apr 7, 2026
46c5063
Merge branch 'master' into HasDartDef
Jun2M Apr 15, 2026
4cb77ef
Merge branch 'master' into SymmGraphLike
Jun2M Apr 15, 2026
a63ff26
GraphLike per Graph
Jun2M Apr 17, 2026
13cc160
docs
Jun2M Apr 17, 2026
d4b55f8
Merge branch 'master' into SymmGraphLike
Jun2M Apr 17, 2026
525ad36
Merge branch 'master' into HasDartDef
Jun2M Apr 17, 2026
8000b7c
Merge branch 'HasDartDef' into SymmGraphLike
Jun2M Apr 17, 2026
e61a791
Merge remote-tracking branch 'origin/HasDartDef' into SymmGraphLike
Jun2M Apr 17, 2026
e863a26
save
Jun2M Apr 17, 2026
83d4bdb
docs
Jun2M Apr 17, 2026
c9ddb2a
Merge branch 'master' into HasDartDef
Jun2M Apr 20, 2026
492fb23
update
Jun2M Apr 21, 2026
9ab3d69
Merge remote-tracking branch 'origin/HasDartDef' into SymmGraphLike
Jun2M Apr 21, 2026
f1badf7
Merge branch 'master' into HasDartDef
Jun2M Apr 25, 2026
9d804ef
update
Jun2M Apr 26, 2026
848f90f
docs
Jun2M Apr 26, 2026
2e8ce55
docs
Jun2M Apr 26, 2026
e29bad2
Merge branch 'master' into HasDartDef
Jun2M Apr 27, 2026
c3dff34
Add edge
Jun2M Apr 27, 2026
bfa4df6
Merge branch 'HasDartDef' into SymmGraphLike
Jun2M Apr 27, 2026
a046328
Add edge
Jun2M Apr 27, 2026
c467255
docs
Jun2M Apr 27, 2026
61d32d7
inv
Jun2M Apr 29, 2026
44873f9
streamline
Jun2M May 7, 2026
3c08e98
init
Jun2M May 7, 2026
fa4285a
docs
Jun2M May 7, 2026
f5aa42e
Merge branch 'SymmGraphLike' into GraphGraphLike
Jun2M May 7, 2026
007f818
rm Step
Jun2M May 13, 2026
455aa1d
mk_all
Jun2M May 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Mathlib.lean
Original file line number Diff line number Diff line change
Expand Up @@ -3463,8 +3463,10 @@ public import Mathlib.Combinatorics.Enumerative.Stirling
public import Mathlib.Combinatorics.Extremal.RuzsaSzemeredi
public import Mathlib.Combinatorics.Graph.Basic
public import Mathlib.Combinatorics.Graph.Delete
public import Mathlib.Combinatorics.Graph.GraphLike
public import Mathlib.Combinatorics.Graph.Lattice
public import Mathlib.Combinatorics.Graph.Subgraph
public import Mathlib.Combinatorics.GraphLike.Basic
public import Mathlib.Combinatorics.HalesJewett
public import Mathlib.Combinatorics.Hall.Basic
public import Mathlib.Combinatorics.Hall.Finite
Expand Down
84 changes: 84 additions & 0 deletions Mathlib/Combinatorics/Graph/GraphLike.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/-
Copyright (c) 2026 Jun Kwon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jun Kwon, Laura Monk, Freddie Nash
-/
module

public import Mathlib.Combinatorics.GraphLike.Basic
public import Mathlib.Combinatorics.Graph.Basic

/-!
We define a `Dart` type as a directed edge, and a `GraphLike` instance for `Graph`.
-/

public section

variable {α β γ : Type*} {G : Graph α β}

namespace Graph

/-- `Graph.Dart` is a type for darts or length 1 walks of `Graph`. Every edge of a graph is composed
of two darts: for loops, there are `fwd` and `bwd` darts, and for non-loops, there are two `dir`
darts. -/
inductive Dart (α β : Type*) : Type _ where
| dir : β → ∀ (u v : α), u ≠ v → Dart α β
| fwd : β → α → Dart α β
| bwd : β → α → Dart α β

open Dart GraphLike SymmGraphLike

@[simps (attr := grind =) -isSimp]
instance : SymmGraphLike α (Dart α β) β (Graph α β) where
source G d := match d with
| dir _ u _ _ => u
| fwd _ u => u
| bwd _ u => u
target G d := match d with
| dir _ _ v _ => v
| fwd _ v => v
| bwd _ v => v
edge G d := match d with
| dir e _ _ _ => e
| fwd e _ => e
| bwd e _ => e
inv d := match d with
| dir e u v h => dir e v u h.symm
| fwd e u => bwd e u
| bwd e u => fwd e u
inv_invol := by grind
inv_source G d := by grind
inv_target G d := by grind
verts G := V(G)
darts G :=
let s : Dart α β → α := fun d ↦ match d with
| dir _ u _ _ => u
| fwd _ u => u
| bwd _ u => u
let t : Dart α β → α := fun d ↦ match d with
| dir _ _ v _ => v
| fwd _ v => v
| bwd _ v => v
let e : Dart α β → β := fun d ↦ match d with
| dir e _ _ _ => e
| fwd e _ => e
| bwd e _ => e
{d : Dart α β | G.IsLink (e d) (s d) (t d)}
edges G := E(G)
source_mem_of_darts _ _ := IsLink.left_mem
target_mem_of_darts _ _ := IsLink.right_mem
edge_mem_of_darts _ _ := IsLink.edge_mem
inv_ne G d hd := by grind
inv_mem_darts_iff G d := by grind [isLink_comm]
edge_eq_edge_iff G d d' hd hd' := by
cases d <;> cases d' <;> grind [IsLink.eq_and_eq_or_eq_and_eq]
Adj G u v := G.Adj u v
exists_darts_iff_adj G u v := by
refine ⟨fun ⟨d, hd, hu, hv⟩ ↦ hu ▸ hv ▸ hd.adj, fun ⟨e, he⟩ => ?_⟩
obtain rfl | hne := eq_or_ne u v
· exact ⟨Dart.fwd e u, by grind⟩
exact ⟨Dart.dir e u v hne, by grind⟩

attribute [simp] source_def target_def edge_def

end Graph
240 changes: 240 additions & 0 deletions Mathlib/Combinatorics/GraphLike/Basic.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
/-
Copyright (c) 2026 Jun Kwon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jun Kwon
-/
module

public import Mathlib.Data.Sym.Sym2

/-!
# Typeclass for different kinds of graphs

This module defines the typeclass `GraphLike` for capturing the common structure of different kinds
of graph structures including `SimpleGraph`, `Graph`, and `Digraph`.

## Main definitions

* `GraphLike`: is the main typeclass for capturing the common notion of graphs.
The field `verts` gives the set of vertices of a graph-like structure,
the field `darts` gives the set of darts, which is an oriented edge, of a graph-like structure,
the field `edges` gives the set of edges of a graph-like structure,
and the field `Adj` gives the adjacency relation between vertices.
* `NoMultiEdgeGraphLike`: is the typeclass for graph-like structures with no multi-edge.
* `SymmGraphLike`: extends `GraphLike` for graph-like structures with symmetric darts.
* `noMultiEdgeSymmGraphLike`: extends `SymmGraphLike` and `NoMultiEdgeGraphLike` for graph-like
structures with no multi-edge and symmetric darts.

## Notes

* `GraphLike V D E Gr` generalizes `SimpleGraph`, `Digraph`, and `Graph`. When multi-digraph and
hypergraphs are formalized, they can also use this typeclass.

-/

public section

/-- The `GraphLike` typeclass abstracts over graph-like structures including hypergraphs.
It has vertex and edge sets so subgraph relations can be handled within the same type.
The "darts" terminology comes from combinatorial maps, and they are also known as "half-edges" or
"bonds." They represents the ways an edge can be traversed: if `d` is a dart with `edge d = e`,
`source d = u` and `target d = v` then `d` is walk of length 1 from `u` to `v` with edge `e`. In an
undirected graph, each edge is composed of two darts.
`Adj` is the adjacency relation of a graph-like structure. Two vertices, `u` & `v`, are adjacent iff
there is a dart between them and therefore there is an edge that can be traversed from `u` to `v`.
(See `exists_darts_iff_adj`.) -/
class GraphLike (V D E : outParam Type*) (Gr : Type*) where
/-- The set of vertices of a graph-like structure. -/
verts : Gr → Set V
/-- The set of darts (oriented edges) of a graph-like structure. -/
darts : Gr → Set D
/-- The set of edges of a graph-like structure. -/
edges : Gr → Set E
/-- The source vertex of a dart. -/
source : Gr → D → V
/-- The target vertex of a dart. -/
target : Gr → D → V
/-- The edge of a dart. -/
edge : Gr → D → E
source_mem_of_darts : ∀ ⦃G d⦄, d ∈ darts G → source G d ∈ verts G
target_mem_of_darts : ∀ ⦃G d⦄, d ∈ darts G → target G d ∈ verts G
edge_mem_of_darts : ∀ ⦃G d⦄, d ∈ darts G → edge G d ∈ edges G
/-- The adjacency relation of a graph-like structure. -/
Adj : Gr → V → V → Prop := fun G u v ↦ ∃ d ∈ darts G, source G d = u ∧ target G d = v
/-- Two vertices are adjacent if and only if there is a dart between them. -/
exists_darts_iff_adj : ∀ ⦃G u v⦄, (∃ d ∈ darts G, source G d = u ∧ target G d = v) ↔ Adj G u v

namespace GraphLike

@[inherit_doc verts]
scoped notation "V(" G ")" => verts G

@[inherit_doc darts]
scoped notation "D(" G ")" => darts G

@[inherit_doc edges]
scoped notation "E(" G ")" => edges G

variable {V D E Gr : Type*} {G : Gr} {u u' v v' w : V} {d : D} {e : E}

section GraphLike

variable [GraphLike V D E Gr]

@[ext] theorem darts_ext (d₁ d₂ : D(G)) (h : d₁.val = d₂.val) : d₁ = d₂ := Subtype.ext h

lemma adj_source_target (hd : d ∈ D(G)) : Adj G (source G d) (target G d) :=
exists_darts_iff_adj.mp ⟨d, hd, rfl, rfl⟩

lemma Adj.left_mem (h : Adj G v w) : v ∈ V(G) := by
obtain ⟨d, hd, rfl, rfl⟩ := exists_darts_iff_adj.mpr h
exact source_mem_of_darts hd

lemma Adj.right_mem (h : Adj G v w) : w ∈ V(G) := by
obtain ⟨d, hd, rfl, rfl⟩ := exists_darts_iff_adj.mpr h
exact target_mem_of_darts hd

/-- Convert a dart to a pair of vertices. -/
@[expose] def toProd (d : D(G)) : V × V := (source G d.val, target G d.val)

/-- Two darts are said to be adjacent if they could be consecutive darts in a walk -- that is, the
first dart's target is equal to the second dart's source. -/
@[expose] def DartAdj (d d' : D(G)) : Prop := target G d.val = source G d'.val

end GraphLike

section noMultiEdgeGraphLike

/-
### GraphLike with no multi-edge

Some graph-like structures, such as `SimpleGraph` and `Digraph`, does not allow multiple darts/edges
between the same pair of vertices. This section defines a typeclass `NoMultiEdgeGraphLike` for such
graph-like structures.
-/

/-- A graph-like structure with no multi-edge. This includes `SimpleGraph` and `Digraph`. -/
class NoMultiEdgeGraphLike (V D E : outParam Type*) (Gr : Type*) extends GraphLike V D E Gr where
protected src_tgt_inj (G : Gr) : Function.Injective (fun d ↦ (source G d, target G d))

variable [NoMultiEdgeGraphLike V D E Gr]

lemma dart_eq_of_src_tgt_eq {d₁ d₂ : D} (h : source G d₁ = source G d₂)
(h' : target G d₁ = target G d₂) : d₁ = d₂ := by
apply NoMultiEdgeGraphLike.src_tgt_inj G
grind

lemma src_tgt_inj (d₁ d₂ : D) : source G d₁ = source G d₂ ∧ target G d₁ = target G d₂ ↔ d₁ = d₂ :=
⟨fun h => dart_eq_of_src_tgt_eq h.1 h.2, by grind⟩

@[simp]
lemma mem_darts_iff_adj : d ∈ D(G) ↔ Adj G (source G d) (target G d) := by
rw [← exists_darts_iff_adj]
refine ⟨fun h => (by use d), fun ⟨d', hd', hs, ht⟩ => ?_⟩
obtain rfl := src_tgt_inj d' d |>.mp ⟨hs, ht⟩
exact hd'

instance [DecidableRel (Adj G)] : DecidablePred (· ∈ D(G)) :=
fun d => decidable_of_iff (Adj G (source G d) (target G d)) mem_darts_iff_adj.symm

end noMultiEdgeGraphLike

section SymmGraphLike

/-- `SymmGraphLike` extends `GraphLike` for graph-like structures where darts are symmetric. -/
class SymmGraphLike (V D E : outParam Type*) (Gr : Type*) extends GraphLike V D E Gr where
/-- The inverse of a dart. -/
inv : D → D
inv_invol : ∀ ⦃d⦄, inv (inv d) = d
inv_source (G : Gr) (d : D) : source G (inv d) = (target G d : V)
inv_target (G : Gr) (d : D) : target G (inv d) = (source G d : V)
inv_ne : ∀ ⦃G d⦄, d ∈ darts G → inv d ≠ d
inv_mem_darts_iff : ∀ ⦃G d⦄, inv d ∈ darts G ↔ d ∈ darts G
edge_eq_edge_iff : ∀ ⦃G d d'⦄, d ∈ darts G → d' ∈ darts G →
(edge G d = edge G d' ↔ d = d' ∨ inv d = d')

open SymmGraphLike

attribute [simp, grind =] inv_invol inv_source inv_target inv_mem_darts_iff
attribute [grind →] inv_ne

variable [SymmGraphLike V D E Gr]

lemma inv_mem_darts (hd : d ∈ D(G)) : inv Gr d ∈ D(G) := inv_mem_darts_iff.mpr hd

instance : Std.Symm (Adj G) where
symm _ _ h := by
rw [← exists_darts_iff_adj] at h ⊢
obtain ⟨d, hd, rfl, rfl⟩ := h
exact ⟨SymmGraphLike.inv Gr d, inv_mem_darts hd, inv_source G d, inv_target G d⟩

@[symm] lemma Adj.symm (h : Adj G v w) : Adj G w v := symm_of (Adj G) h

lemma adj_comm : Adj G v w ↔ Adj G w v := ⟨symm_of (Adj G), symm_of (Adj G)⟩

lemma adj_tgt_src (hd : d ∈ D(G)) : Adj G (target G d) (source G d) :=
(adj_source_target hd).symm

/-- The two vertices of the dart as an unordered pair. -/
@[expose] def dartSym2 (d : D(G)) : Sym2 V := s(source G d.val, target G d.val)

@[simp]
theorem dartSym2_mk (h : d ∈ D(G)) : dartSym2 (⟨d, h⟩ : D(G)) = s(source G d, target G d) := rfl

/-- The dart with reversed orientation from a given dart. -/
@[expose] def dartSymm (d : D(G)) : D(G) := ⟨inv Gr d.val, inv_mem_darts_iff.mpr d.prop⟩

@[simp]
theorem dartSymm_mk (h : d ∈ D(G)) : dartSymm (⟨d, h⟩) = ⟨inv Gr d, inv_mem_darts_iff.mpr h⟩ := rfl

@[simp]
theorem dartSym2_inv (d : D(G)) : dartSym2 (dartSymm d) = dartSym2 d := by
simp [dartSym2, dartSymm]

@[simp]
theorem dartSym2_comp_inv : dartSym2 ∘ dartSymm = (dartSym2 : D(G) → Sym2 _) :=
funext dartSym2_inv

@[simp]
theorem dartSymm_dartSymm (d : D(G)) : dartSymm (dartSymm d) = d :=
darts_ext _ _ <| by simp [dartSymm]

@[simp]
theorem dartSymm_involutive : Function.Involutive (dartSymm : D(G) → D(G)) :=
dartSymm_dartSymm

theorem dartSym2_eq_mk'_iff {d : D(G)} :
dartSym2 d = s(u, v) ↔ toProd d = (u, v) ∨ toProd d = (v, u) := by
obtain ⟨p, hp⟩ := d
simp [toProd]

theorem dartSym2_eq_mk'_iff' {d : D(G)} : dartSym2 d = s(u, v) ↔
source G d.val = u ∧ target G d.val = v ∨ source G d.val = v ∧ target G d.val = u := by
obtain ⟨p, hp⟩ := d
simp

end SymmGraphLike

section noMultiEdgeGraphLike

open SymmGraphLike

/-- A graph-like structure with no multi-edge and symmetric darts. -/
class noMultiEdgeSymmGraphLike (V D E : outParam Type*) (Gr : Type*) extends
SymmGraphLike V D E Gr, NoMultiEdgeGraphLike V D E Gr where

@[simp]
theorem dartSym2_eq_iff [noMultiEdgeSymmGraphLike V D E Gr] (d₁ d₂ : D(G)) :
dartSym2 d₁ = dartSym2 d₂ ↔ d₁ = d₂ ∨ d₁ = dartSymm d₂ := by
obtain ⟨p, hp⟩ := d₁
obtain ⟨q, hq⟩ := d₂
simp only [dartSym2_mk, Sym2.eq, Sym2.rel_iff', Prod.mk.injEq, Prod.swap_prod_mk,
Subtype.mk.injEq, dartSymm_mk]
refine ⟨?_, by rintro (rfl | rfl) <;> simp⟩
rintro (⟨h1, h2⟩ | ⟨h1, h2⟩)
· exact Or.inl <| dart_eq_of_src_tgt_eq h1 h2
exact Or.inr <| dart_eq_of_src_tgt_eq (inv_source G q ▸ h1) (inv_target G q ▸ h2)

end noMultiEdgeGraphLike

end GraphLike
Loading