-
Notifications
You must be signed in to change notification settings - Fork 103
feat: simple undirected graph definitions #427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /- | ||
| Copyright (c) 2026 Sorrachai Yingchareonthawornhcai. All rights reserved. | ||
| Released under Apache 2.0 license as described in the file LICENSE. | ||
| Authors: Sorrachai Yingchareonthawornhcai | ||
| -/ | ||
|
|
||
| module | ||
| public import Cslib.Init | ||
| public import Mathlib.Data.Sym.Sym2 | ||
|
|
||
| @[expose] public section | ||
|
|
||
| /-! | ||
| # Simple Undirected Graphs | ||
| This file defines simple undirected graphs and basic definitions. | ||
|
|
||
| -- | ||
| ## Main definitions | ||
| - `Edge`: A type alias for `Sym2 α`, representing an undirected edge as an | ||
| unordered pair. | ||
| - `SimpleGraph`: A structure encoding a simple graph with a finite vertex set, | ||
| a finite edge set, an incidence condition (every endpoint of every edge is a | ||
| vertex), and a looplessness condition (no edge is a diagonal of `Sym2`). | ||
| - `IncidentEdgeSet`: The set of all edges in a graph that are incident to a | ||
| given vertex. | ||
| - `Neighbors`: The set of all vertices adjacent to a given vertex, i.e. the | ||
| open neighbourhood of that vertex. | ||
| - `subgraphOf`: A relation asserting that one graph is a subgraph of another; | ||
| both the vertex set and the edge set of the first are subsets of those of the | ||
| second. | ||
|
|
||
| ## Implementation notes | ||
|
|
||
| Edges are represented as elements of `Sym2 α` (i.e., `{x, y}` with `x ≠ y` | ||
| enforced via the `loopless` field rather than the type). This mirrors the | ||
| approach used in Mathlib's combinatorics library. | ||
|
|
||
| The graph definitions in this file intentionally diverge from those in Mathlib; | ||
| we prioritise representations that support algorithmic reasoning over those designed for | ||
| classical combinatorial arguments. | ||
| -/ | ||
|
|
||
| namespace Cslib.Algorithms.Lean.Graph.Core.Simple | ||
|
|
||
| variable {α : Type*} [DecidableEq α] | ||
|
|
||
|
|
||
| /-- An undirected edge, represented as an unordered pair via `Sym2`. -/ | ||
| abbrev Edge := Sym2 | ||
|
|
||
| /-- A simple graph on vertex type `α` consists of a finite vertex set, a finite | ||
| edge set, and two well-formedness conditions: every edge endpoint is a vertex, | ||
| and no edge is a self-loop. -/ | ||
| structure SimpleGraph (α : Type*) where | ||
| /-- The finite set of vertices of the graph. -/ | ||
| vertexSet : Finset α | ||
| /-- The finite set of edges of the graph. -/ | ||
| edgeSet : Finset (Edge α) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. For SimpleGraph, it suffices to have the edge prop as in mathlib. Then neighborFinset can be used with LocallyFinite typeclass. This can further be converted to lists if desired. |
||
| /-- Every endpoint of every edge is a vertex. -/ | ||
| incidence : ∀ e ∈ edgeSet, ∀ v ∈ e, v ∈ vertexSet | ||
| /-- The graph has no self-loops. -/ | ||
| loopless : ∀ e ∈ edgeSet, ¬ e.IsDiag | ||
|
|
||
| open Finset | ||
|
|
||
|
|
||
| /-- `V(G)` denotes the `vertexSet` of a graph `G`. -/ | ||
| scoped notation "V(" G ")" => SimpleGraph.vertexSet G | ||
|
|
||
| /-- `E(G)` denotes the `edgeSet` of a graph `G`. -/ | ||
| scoped notation "E(" G ")" => SimpleGraph.edgeSet G | ||
|
|
||
| /-- The set of all edges in a graph that are incident to a given vertex. -/ | ||
| abbrev IncidentEdgeSet (G : SimpleGraph α) (s : α) : | ||
| Finset (Edge α) := {e ∈ E(G) | s ∈ e} | ||
|
|
||
| /-- `δ(G,v)` denotes the `edge-incident-set` of a vertex `v` in `G`. -/ | ||
| scoped notation "δ(" G "," v ")" => SimpleGraph.IncidentEdgeSet G v | ||
|
|
||
| /-- The set of all vertices adjacent to a given vertex, i.e. the | ||
| open neighbourhood of that vertex. -/ | ||
| abbrev Neighbors (G : SimpleGraph α) (s : α) : | ||
| Finset α := {u ∈ V(G) | ∃ e ∈ E(G), s ∈ e ∧ u ∈ e ∧ u ≠ s} | ||
|
|
||
| /-- `N(G,v)` denotes the `neighbors` of a vertex `v` in `G`. -/ | ||
| scoped notation "N(" G "," v ")" => SimpleGraph.Neighbors G v | ||
|
|
||
| /-- `deg(G)` denotes the `degree` of a vertex `v` in `G`. -/ | ||
| scoped notation "deg(" G "," v ")" => #δ(G,v) | ||
|
|
||
| /-- A graph `H` is a subgraph of `G` if both vertex and edge sets are subsets of those in G -/ | ||
| abbrev subgraphOf (H G : SimpleGraph α) : Prop := | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case again I think it is better to adapt IsSubgraph. Concretely because all the order theory already exists for this (and it matters even in algorithms where we use them implicitly). |
||
| V(H) ⊆ V(G) ∧ E(H) ⊆ E(G) | ||
|
|
||
| /-- Notation for the subgraph relation -/ | ||
| scoped infix:50 " ⊆ᴳ " => SimpleGraph.subgraphOf | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Continuing the above, you should in all likelihood introduce the relevant order theory instances. See mathlib4#33466 for an example of graphs with vertex sets getting order theory instances. The notation for subgraphs follows from that. |
||
|
|
||
| end Cslib.Algorithms.Lean.Graph.Core.Simple | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't want to put Finset here. Set is enough, followed by finite instances. There are instances in CS theory where we do actually deal with arbitrary sets of edges without using finiteness. Just not in algorithms.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The focus here is on graph algorithms, which by default require finite graphs. Those with infinite graphs should use the definition from Mathlib instead.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not my point. Getting finite graphs from mathlib is trivial. Replicating and maintaining compatible API with mathlib is complex. For most operations you perform with Finsets you only need Sets.
Where you need finiteness, it suffices to put Finite and Fintype instances.