Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions Cslib.lean
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module -- shake: keep-all

public import Cslib.Algorithms.Lean.Graph.Core.Simple.Undirected
public import Cslib.Algorithms.Lean.MergeSort.MergeSort
public import Cslib.Algorithms.Lean.TimeM
public import Cslib.Computability.Automata.Acceptors.Acceptor
Expand Down
98 changes: 98 additions & 0 deletions Cslib/Algorithms/Lean/Graph/Core/Simple/Undirected.lean
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 α
Copy link
Contributor

@Shreyas4991 Shreyas4991 Mar 15, 2026

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.

Copy link
Collaborator Author

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.

Copy link
Contributor

@Shreyas4991 Shreyas4991 Mar 16, 2026

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.

/-- The finite set of edges of the graph. -/
edgeSet : Finset (Edge α)
Copy link
Contributor

Choose a reason for hiding this comment

The 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 :=
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

@Shreyas4991 Shreyas4991 Mar 15, 2026

Choose a reason for hiding this comment

The 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
Loading