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

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.

3 participants