Skip to content

feat: basic graph definitions#503

Open
BasilRohner wants to merge 11 commits intoleanprover:mainfrom
BasilRohner:main
Open

feat: basic graph definitions#503
BasilRohner wants to merge 11 commits intoleanprover:mainfrom
BasilRohner:main

Conversation

@BasilRohner
Copy link
Copy Markdown

@BasilRohner BasilRohner commented Apr 19, 2026

This PR introduces basic graph definitions: Graph, SimpleGraph, DiGraph, and SimpleDiGraph, along with coercions and notation that form the basis of algorithmic graph theory in CSLib.

Design. We intentionally diverge from Mathlib's graph definitions, prioritizing representations that support algorithmic reasoning. In graph algorithm design, it is common to manipulate graphs in many ways (e.g., dynamic graph operations such as adding or removing nodes/edges, contracting edges, and contracting vertex sets). As a result, we prioritize a minimal, simple definition of graphs that best aligns with how they are defined in the literature on algorithmic graph theory. In particular, we use set-based definitions of both the vertex and edge sets, along with minimal additional attributes, to reduce early proof obligations and select those whose proofs are closer to their textbook counterparts.

General structures. Graph and DiGraph (undirected and directed multigraphs) use Set to allow statements about possibly infinite graphs. Specialized structures (SimpleGraph, SimpleDiGraph) use Finset directly rather than enforcing finiteness through additional properties, keeping the most commonly used types lightweight and directly computable. The definitions have been tested in practice by further formalizing concepts such as walks (see the GraphAlgorithms Walk module). However, to keep this PR concise, we defer discussion of the Walk design to a later PR.

Comparison with PR #427. PR #427 introduced SimpleGraph with Finset-based vertex and edge sets and edges via Sym2. Here, we introduce general structures (Graph, DiGraph) that use Set, while keeping Finset in the specialized types (SimpleGraph, SimpleDiGraph) commonly used in algorithmic reasoning. Additionally, we drop edge labels in SimpleGraph and SimpleDiGraph, since multi-edges are disallowed, simplifying downstream reasoning.

Main definitions.

  • Graph: (possibly infinite) undirected multigraph
  • SimpleGraph: finite undirected graph without loops or multi-edges
  • DiGraph: (possibly infinite) directed graph
  • SimpleDiGraph: finite directed graph without loops or multi-edges

Future work. We plan to add fundamental graph algorithms building on these definitions.

Co-authored-by: Sorrachai Yingchareonthawornchai sorrachai.cp@gmail.com

@ctchou
Copy link
Copy Markdown
Collaborator

ctchou commented Apr 19, 2026

I have some questions/comments:
(1) Does the Graph in this PR differ in a substantive way from the Graph in Mathlib.Combinatorics.Graph.Basic? It seems to me that they are basically equivalent to each other. If my understanding is correct, then can't you just define Graph.endpoints and Graph.incidence on mathlib's Graph, if they are the API you prefer?
(2) A Set that is Set.Finite can be converted into a Finset using:
https://leanprover-community.github.io/mathlib4_docs/Mathlib/Data/Set/Finite/Basic.html#Set.Finite.toFinset
I know that this API is "noncomputable". But once you obtain the Finset versions of vertexSet and edgeSet, you can operate on them in a completely "computable" manner. By having both Set and Finset versions of basically the same definition, I'm afraid that you will have to repeat the statements and proofs of many theorems for the two versions.
(3) It seems to me that you can define DiGraph by extending Quivers in Mathlib.Combinatorics.Quiver.Basic, which is so general that I'm pretty sure it can accommodate any notion of "digraph" you have.

@Shreyas4991
Copy link
Copy Markdown
Contributor

Discussion thread on this PR :
Leanprover Zulip thread link

/-- The endpoint map, sending each edge to its unordered pair of endpoints. -/
endpoints : ε → Sym2 α
/-- Every endpoint of an edge is a vertex. -/
incidence : ∀ e ∈ edgeSet, ∀ v ∈ endpoints e, v ∈ vertexSet
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may cause a junk value problem on endpoints where an endpoint outside the set must be specified.

To avoid the junk value problem, we can define an edge object separately and define the graph directly over the edgeSet. Note that we use endpoints_id as the identifier for the multiplicity of an edge of the same endpoints

structure UndirectedEdge (α β : Type*) where
  endpoints_id : β
  endpoints : Sym2 α
deriving DecidableEq

abbrev Edge := UndirectedEdge

structure UndirectedGraph (α β : Type*) where
  vertexSet : Set α
  edgeSet : Set (Edge α β)
  incidence : ∀ e ∈ edgeSet, ∀ v ∈ e.endpoints, v ∈ vertexSet

Comment on lines +114 to +122
/-- Typeclass for graph-like structures that have a vertex set. -/
class HasVertexSet (G : Type*) (V : outParam Type*) where
/-- The vertex set of the graph. -/
vertexSet : G → V

/-- Typeclass for graph-like structures that have an edge set. -/
class HasEdgeSet (G : Type*) (E : outParam Type*) where
/-- The edge set of the graph. -/
edgeSet : G → E
Copy link
Copy Markdown
Collaborator

@eric-wieser eric-wieser Apr 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you make these the canonical spelling then you need to restate all the loopless and incidence theorems in terms of them (and probably rename those fields to be loopless' to avoid stealing the preferred name)

@eric-wieser
Copy link
Copy Markdown
Collaborator

Design. We intentionally diverge from Mathlib's graph definitions, ...

Please record these design decisions in the module docstring for future readers, not just the git history / PR title.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants