Skip to content

feat: simple undirected graph definitions#427

Open
sorrachai wants to merge 5 commits intoleanprover:mainfrom
sorrachai:simpleGraphs
Open

feat: simple undirected graph definitions#427
sorrachai wants to merge 5 commits intoleanprover:mainfrom
sorrachai:simpleGraphs

Conversation

@sorrachai
Copy link
Collaborator

This PR introduces the foundational graph definitions for CSLib, including Edge, SimpleGraph, IncidentEdgeSet, Neighbors, and subgraphOf.

Motivation 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. In particular, the use of Finset throughout — rather than decidable predicates — makes definitions more directly computable and better suited for verified algorithms.

Main definitions
Edge — undirected edge as an unordered pair via Sym2
SimpleGraph — finite, loopless, simple graph structure
IncidentEdgeSet — edges incident to a given vertex
Neighbors — open neighbourhood of a given vertex
subgraphOf — subgraph relation on SimpleGraph

Future work
We plan to add more fundamental graph algorithms that build on top of this definition.

Copy link
Contributor

@Shreyas4991 Shreyas4991 left a comment

Choose a reason for hiding this comment

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

The design and purpose of this PR needs substantial Zulip discusssion.

Pros:

It adds vertex sets to simple graph. A longstanding demand of mine which I am implementing for digraphs. I am glad that this issue is being addressed here. This means we can do away with the duplication introduced by a separate subgraph type.

Cons:

The other claims are far from clear. All features algorithmists need from SimpleGraph can be deduced from the mathlib API already. For instance getting the neighborFinset is a trivial step. One just needs the decidable declarations on top of one's file.
This PR will have to reinvent several thousand lines of mathlib API and they will be largely duplicate. Further this will also duplicate Peter Nelson's Graph API that's being built up in mathlib.

My recommendation:

Please discuss in Zulip first. It might be worth a few mathlib PRs to add vertex sets to SimpleGraph. But that is better than carrying this parallel copy without a consensus in the community.

Extra suggestion : if you find the existing API inconvenient then it is a sign of missing simp or grind lemmas. It can make sense to have a math folder where we place this lemmas before upstreaming. This PR could instead provide the interface lemmas. Of course adding vertex sets will require a mathlib PR.

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 vertices of the graph. -/
vertexSet : Finset α
/-- 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.

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.

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.

2 participants