Skip to content

feat(Combinatorics/SimpleGraph/Degree): degrees for infinite graphs - #33501

Open
SnirBroshi wants to merge 85 commits into
leanprover-community:masterfrom
SnirBroshi:feature/simple-graph/edegree
Open

SnirBroshi wants to merge 85 commits into
leanprover-community:masterfrom
SnirBroshi:feature/simple-graph/edegree

Conversation

@SnirBroshi

@SnirBroshi SnirBroshi commented Jan 3, 2026 •

Copy link
Copy Markdown
Collaborator

Define ENat versions of degree/minDegree/maxDegree called edegree/minEDegree/maxEDegree respectively.

The e prefix matches the existing edist/ediam/egirth.


TODO (future work): tsum G.edegree = 2 * G.edgeSet.encard and then ENat.card V * G.minEDegree ≤ 2 * G.edgeSet.encard ≤ ENat.card V * G.maxEDegree (can we avoid topology somehow?)

Note: this PR used to be a refactor of the existing degree/maxDegree/minDegree, see the edits of this description.

Open in Gitpod

@github-actions github-actions Bot added the large-import Automatically added label for PRs with a significant increase in transitive imports label Jan 3, 2026
@github-actions

github-actions Bot commented Jan 3, 2026 •

Copy link
Copy Markdown

PR summary 3a8e583834

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference
Mathlib.Combinatorics.SimpleGraph.Degree (new file) 926

Declarations diff (regex)

+ Adj.commonNeighbors_ssubset_neighborSet_left
+ Adj.commonNeighbors_ssubset_neighborSet_right
+ Adj.edegree_ne_zero_left
+ Adj.edegree_ne_zero_right
+ Adj.encard_commonNeighbors_lt_edegree_left
+ Adj.encard_commonNeighbors_lt_edegree_right
+ Copy.edegree_le
+ Copy.minEDegree_le
+ Embedding.edegree_eq_of_neighborSet_subset_range
+ Hom.minEDegree_le
+ IsContained.maxEDegree_le
+ IsIsolated.edegree_eq
+ IsRegularOfDegree.edegree_eq
+ IsRegularOfDegree.maxEDegree_eq
+ IsRegularOfDegree.minEDegree_eq
+ Iso.edegree_eq
+ Iso.maxEDegree_eq
+ Iso.minEDegree_eq
+ edegree
+ edegree_bot
+ edegree_eq_natCast_iff
+ edegree_eq_one_iff_existsUnique_adj
+ edegree_eq_zero_iff_isIsolated
+ edegree_eq_zero_of_subsingleton
+ edegree_induce_of_neighborSet_subset
+ edegree_le_card
+ edegree_le_encard_edgeSet
+ edegree_le_maxEDegree
+ edegree_lt_card_of_ne_top
+ edegree_map_apply
+ edegree_mono
+ edegree_ne_top_iff_finite_neighborSet
+ edegree_ne_top_of_finite
+ edegree_top
+ encard_commonNeighbors_le_edegree_left
+ encard_commonNeighbors_le_edegree_right
+ encard_incidenceSet_of_fintype_neighborSet
+ encard_neighborSet_of_fintype_neighborSet
+ exists_edegree_eq_maxEDegree
+ exists_edegree_eq_minEDegree
+ forall_bot_lt
+ iSup_eq_top_iff_le
+ iSup_eq_top_iff_lt
+ le_minEDegree_induce_of_support_subset
+ le_minEDegree_of_forall_le_edegree
+ maxEDegree
+ maxEDegree_bot
+ maxEDegree_eq_iSup
+ maxEDegree_eq_natCast_iff
+ maxEDegree_eq_top_iff_le
+ maxEDegree_eq_top_iff_lt
+ maxEDegree_eq_top_of_edegree_eq_top
+ maxEDegree_eq_zero_iff_eq_bot
+ maxEDegree_induce_of_support_subset
+ maxEDegree_le_card
+ maxEDegree_le_encard_edgeSet
+ maxEDegree_le_of_forall_edegree_le
+ maxEDegree_lt_card_of_ne_top
+ maxEDegree_mono
+ maxEDegree_ne_top_of_finite
+ maxEDegree_of_subsingleton
+ maxEDegree_top
+ minEDegree
+ minEDegree_bot
+ minEDegree_eq_iInf
+ minEDegree_eq_natCast_iff
+ minEDegree_eq_natCast_iff_of_neZero
+ minEDegree_eq_top_iff
+ minEDegree_eq_zero_iff_isIsolated
+ minEDegree_eq_zero_iff_support_ne
+ minEDegree_eq_zero_of_edegree_eq_zero
+ minEDegree_le_card
+ minEDegree_le_edegree
+ minEDegree_le_encard_edgeSet
+ minEDegree_le_maxEDegree
+ minEDegree_lt_card_of_ne_top
+ minEDegree_mono
+ minEDegree_ne_top_of_finite
+ minEDegree_of_isEmpty
+ minEDegree_of_subsingleton_of_nonempty
+ minEDegree_top
+ natCast_degree_eq_edegree
+ natCast_maxDegree_eq_maxEDegree
+ natCast_minDegree_eq_minEDegree
+ natCast_toNat_edegree_eq_self
+ nontrivial_of_edegree_ne_zero
+ sSup_eq_top_iff_le
+ sSup_eq_top_iff_lt
+ toNat_edegree
+ toNat_maxEDegree
+ toNat_minEDegree

You can run this locally as follows
## from your `mathlib4` directory:
git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci

## summary with just the declaration names:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh <optional_commit>

## more verbose report:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh long <optional_commit>

The doc-module for scripts/pr_summary/declarations_diff.sh in the mathlib-ci repository contains some details about this script.

Declarations diff (Lean)

✅ Lean-aware diff — post-build, computed from the Lean environment (commit 3a8e583).

  • +92 new declarations
  • −0 removed declarations
+SimpleGraph.Adj.commonNeighbors_ssubset_neighborSet_left
+SimpleGraph.Adj.commonNeighbors_ssubset_neighborSet_right
+SimpleGraph.Adj.edegree_ne_zero_left
+SimpleGraph.Adj.edegree_ne_zero_right
+SimpleGraph.Adj.encard_commonNeighbors_lt_edegree_left
+SimpleGraph.Adj.encard_commonNeighbors_lt_edegree_right
+SimpleGraph.Copy.edegree_le
+SimpleGraph.Copy.minEDegree_le
+SimpleGraph.Embedding.edegree_eq_of_neighborSet_subset_range
+SimpleGraph.Hom.minEDegree_le
+SimpleGraph.IsContained.maxEDegree_le
+SimpleGraph.IsIsolated.edegree_eq
+SimpleGraph.IsRegularOfDegree.edegree_eq
+SimpleGraph.IsRegularOfDegree.maxEDegree_eq
+SimpleGraph.IsRegularOfDegree.minEDegree_eq
+SimpleGraph.Iso.edegree_eq
+SimpleGraph.Iso.maxEDegree_eq
+SimpleGraph.Iso.minEDegree_eq
+SimpleGraph.edegree
+SimpleGraph.edegree_bot
+SimpleGraph.edegree_eq_natCast_iff
+SimpleGraph.edegree_eq_one_iff_existsUnique_adj
+SimpleGraph.edegree_eq_zero_iff_isIsolated
+SimpleGraph.edegree_eq_zero_of_subsingleton
+SimpleGraph.edegree_induce_of_neighborSet_subset
+SimpleGraph.edegree_le_card
+SimpleGraph.edegree_le_encard_edgeSet
+SimpleGraph.edegree_le_maxEDegree
+SimpleGraph.edegree_lt_card_of_ne_top
+SimpleGraph.edegree_map_apply
+SimpleGraph.edegree_mono
+SimpleGraph.edegree_ne_top_iff_finite_neighborSet
+SimpleGraph.edegree_ne_top_of_finite
+SimpleGraph.edegree_top
+SimpleGraph.encard_commonNeighbors_le_edegree_left
+SimpleGraph.encard_commonNeighbors_le_edegree_right
+SimpleGraph.encard_incidenceSet_of_fintype_neighborSet
+SimpleGraph.encard_neighborSet_of_fintype_neighborSet
+SimpleGraph.exists_edegree_eq_maxEDegree
+SimpleGraph.exists_edegree_eq_minEDegree
+SimpleGraph.le_minEDegree_induce_of_support_subset
+SimpleGraph.le_minEDegree_of_forall_le_edegree
+SimpleGraph.maxEDegree
+SimpleGraph.maxEDegree_bot
+SimpleGraph.maxEDegree_eq_iSup
+SimpleGraph.maxEDegree_eq_natCast_iff
+SimpleGraph.maxEDegree_eq_top_iff_le
+SimpleGraph.maxEDegree_eq_top_iff_lt
+SimpleGraph.maxEDegree_eq_top_of_edegree_eq_top
+SimpleGraph.maxEDegree_eq_zero_iff_eq_bot
+SimpleGraph.maxEDegree_induce_of_support_subset
+SimpleGraph.maxEDegree_le_card
+SimpleGraph.maxEDegree_le_encard_edgeSet
+SimpleGraph.maxEDegree_le_of_forall_edegree_le
+SimpleGraph.maxEDegree_lt_card_of_ne_top
+SimpleGraph.maxEDegree_mono
+SimpleGraph.maxEDegree_ne_top_of_finite
+SimpleGraph.maxEDegree_of_subsingleton
+SimpleGraph.maxEDegree_top
+SimpleGraph.minEDegree
+SimpleGraph.minEDegree_bot
+SimpleGraph.minEDegree_eq_iInf
+SimpleGraph.minEDegree_eq_natCast_iff
+SimpleGraph.minEDegree_eq_natCast_iff_of_neZero
+SimpleGraph.minEDegree_eq_top_iff
+SimpleGraph.minEDegree_eq_zero_iff_isIsolated
+SimpleGraph.minEDegree_eq_zero_iff_support_ne
+SimpleGraph.minEDegree_eq_zero_of_edegree_eq_zero
+SimpleGraph.minEDegree_le_card
+SimpleGraph.minEDegree_le_edegree
+SimpleGraph.minEDegree_le_encard_edgeSet
+SimpleGraph.minEDegree_le_maxEDegree
+SimpleGraph.minEDegree_lt_card_of_ne_top
+SimpleGraph.minEDegree_mono
+SimpleGraph.minEDegree_ne_top_of_finite
+SimpleGraph.minEDegree_of_isEmpty
+SimpleGraph.minEDegree_of_subsingleton_of_nonempty
+SimpleGraph.minEDegree_top
+SimpleGraph.natCast_degree_eq_edegree
+SimpleGraph.natCast_maxDegree_eq_maxEDegree
+SimpleGraph.natCast_minDegree_eq_minEDegree
+SimpleGraph.natCast_toNat_edegree_eq_self
+SimpleGraph.nontrivial_of_edegree_ne_zero
+SimpleGraph.toNat_edegree
+SimpleGraph.toNat_maxEDegree
+SimpleGraph.toNat_minEDegree
+WithBot.forall_bot_lt
+WithTop.forall_lt_top
+WithTop.iSup_eq_top_iff_le
+WithTop.iSup_eq_top_iff_lt
+WithTop.sSup_eq_top_iff_le
+WithTop.sSup_eq_top_iff_lt

No changes to strong technical debt.
No changes to weak technical debt.

Current commit 3a8e583834
Reference commit e83c9eaaf4

This script lives in the mathlib-ci repository. To run it locally, from your mathlib4 directory:

git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci
../mathlib-ci/scripts/reporting/technical-debt-metrics.py pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

Comment thread Mathlib/Data/Set/Card.lean Resolved
@YaelDillies YaelDillies added the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Jan 17, 2026
@mathlib4-merge-conflict-bot mathlib4-merge-conflict-bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Jan 24, 2026
@mathlib4-merge-conflict-bot

Copy link
Copy Markdown

This pull request has conflicts, please merge master and resolve them.

@github-actions github-actions Bot removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Jan 24, 2026
@SnirBroshi SnirBroshi added awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. and removed awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. labels Jan 24, 2026
@SnirBroshi SnirBroshi removed the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Jan 31, 2026
@YaelDillies YaelDillies added the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Feb 1, 2026
@b-mehta

b-mehta commented Feb 1, 2026

Copy link
Copy Markdown
Contributor

I'm not sure that this change is the right way to achieve degrees for infinite graphs. Instead, we should change degree v to ask that the neighbourhood of v is a fintype. I agree it's bad to ask for finiteness of the entire vertex set, but I don't think this is the right way to go about fixing it.

@SnirBroshi

SnirBroshi commented Feb 2, 2026 •

Copy link
Copy Markdown
Collaborator Author

Instead, we should change degree v to ask that the neighbourhood of v is a fintype. I agree it's bad to ask for finiteness of the entire vertex set

Isn't that how degrees are currently defined? (they require Fintype <| G.neighborSet v)

I disagree that Fintype <| G.neighborSet v is the correct way, that makes it very hard to work with infinite graphs.
I'm not asking for cardinals, just to avoid this hypothesis. Like in other places in Mathilb, if we add a junk value of 0 when the neighborhood isn't finite that'll make things much easier. This is why I suggest G.neighborSet v |>.ncard.
Like division by zero, I don't want to justify Fintype every time I talk about the degree of a vertex.

@b-mehta

b-mehta commented Feb 2, 2026

Copy link
Copy Markdown
Contributor

Isn't that how degrees are currently defined? (they require Fintype <| G.neighborSet v)

Oops, yes, the only way I could imagine infinite graphs being difficult was with a worse definition than this one!

that makes it very hard to work with infinite graphs.

Do you have examples of where this makes it hard to talk about infinite graphs? In my experience, infinite graphs work quite nicely with this setup. This is the general pattern in Lean combinatorics, more generally. Since the Fintype is an instance argument, most of the time it doesn't need to be explicitly justified anyway.

@SnirBroshi

SnirBroshi commented Feb 2, 2026 •

Copy link
Copy Markdown
Collaborator Author

I'm pretty baffled at why "junk values are better than dragging hypotheses around" isn't a good reason for the change, I thought that's the conventional wisdom in Mathlib.

Do you have examples of where this makes it hard to talk about infinite graphs? In my experience, infinite graphs work quite nicely with this setup.

Take any statement from https://loogle.lean-lang.org/?q=SimpleGraph.degree and try to apply it to an infinite graph. There's no reason why statements such as nontrivial_of_degree_ne_zero or degree_eq_one_iff_existsUnique_adj should require me to first prove that the vertex doesn't have too much neighbors.

Another random example I just thought of: "a graph is Hamiltonian 2-regular iff it's isomorphic to a cycle" is impossible to state for infinite graphs right now, even if we make IsHamiltonian not require a [Fintype V]:

theorem foo {V : Type*} [DecidableEq V] [Fintype V] (G : SimpleGraph V) [DecidableRel G.Adj] :
    G.IsHamiltonian ∧ G.IsRegularOfDegree 2 ↔ Nonempty (G ≃g SimpleGraph.cycleGraph (Nat.card V)) := by
  sorry

Another user with a similar frustration: #graph theory > IsRegularOfDegree without DecidableRel @ 💬

But most importantly: minDegree and maxDegree require [Fintype V] [DecidableRel G.Adj], which means there's no way to use them in infinite graphs, even if I reluctantly prove an instance of G.LocallyFinite.

This is the general pattern in Lean combinatorics, more generally. Since the Fintype is an instance argument, most of the time it doesn't need to be explicitly justified anyway.

I don't understand how your fintypes don't have to be justified. Do you have such instances in your local context, or do you only use degrees on Fintype V? I'm really not sure where such instances should automagically come from, they don't for me.

@github-actions github-actions Bot removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Jun 15, 2026
@mathlib-merge-conflicts mathlib-merge-conflicts Bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Sep 4, 2026
@mathlib-merge-conflicts

Copy link
Copy Markdown

This pull request has conflicts, please merge master and resolve them.

@github-actions github-actions Bot removed merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) large-import Automatically added label for PRs with a significant increase in transitive imports labels Sep 4, 2026

This branch has not been deployed

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

Labels

blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) t-combinatorics Combinatorics

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants