Skip to content

Commit

Permalink
sagemathgh-38795: switch to nauty for generating posets
Browse files Browse the repository at this point in the history
as there is now a direct method in `nauty` and we have added the
interface previously

### 📝 Checklist

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [ ] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

URL: sagemath#38795
Reported by: Frédéric Chapoton
Reviewer(s): David Coudert, Frédéric Chapoton, Martin Rubey
  • Loading branch information
Release Manager committed Oct 11, 2024
2 parents 17791d4 + 17f72fa commit a1d9c1e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
4 changes: 2 additions & 2 deletions build/pkgs/configure/checksums.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
tarball=configure-VERSION.tar.gz
sha1=140d921780212198287a0d626b8c9655d4408f7a
sha256=4f82ec8bdb67c4dffd43daddd5160e8f9c624f6a5cf16c4128bfab2e5ddca459
sha1=3400a561c09f24335cfca3729446f13b9e994161
sha256=daf6df8c79f195ec5c155636ff3c2f7212f86ea912556c46d8c728c466be1980
2 changes: 1 addition & 1 deletion build/pkgs/configure/package-version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2b4fe39e420df022d34a67fbc820252618e15f83
7a3bca1ba9a11f0b67e2bf78172cf9709b4d011c
33 changes: 23 additions & 10 deletions src/sage/combinat/posets/posets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8884,11 +8884,11 @@ class FinitePosets_n(UniqueRepresentation, Parent):
sage: P.cardinality()
5
sage: for p in P: print(p.cover_relations())
[]
[[1, 2]]
[[1, 2], [0, 2]]
[[0, 1], [0, 2]]
[[0, 2]]
[[0, 1], [1, 2]]
[[1, 2], [0, 2]]
[]
"""

def __init__(self, n) -> None:
Expand Down Expand Up @@ -8930,27 +8930,40 @@ def __contains__(self, P) -> bool:
return P in FinitePosets() and P.cardinality() == self._n

def __iter__(self):
"""
r"""
Return an iterator of representatives of the isomorphism classes
of finite posets of a given size.
.. NOTE::
This uses the DiGraph iterator as a backend to construct
transitively-reduced, acyclic digraphs.
If the size `n\leq 16`, this uses an iterator from
``nauty`` as a backend to construct only transitively-reduced,
acyclic digraphs. Otherwise it uses a slow naive iterator,
as the ``nauty`` iterator is not available.
EXAMPLES::
sage: P = Posets(2)
sage: list(P)
[Finite poset containing 2 elements, Finite poset containing 2 elements]
TESTS::
sage: it = iter(Posets(17))
sage: next(it)
Finite poset containing 17 elements
"""
from sage.graphs.digraph_generators import DiGraphGenerators
for dig in DiGraphGenerators()(self._n, is_poset):
if self._n <= 16:
it = digraphs.nauty_posetg(f"{self._n} o")
else:
it = digraphs(self._n, is_poset)

for dig in it:
# We need to relabel the digraph since range(self._n) must be a linear
# extension. Too bad we need to compute this again. TODO: Fix this.
label_dict = dict(zip(dig.topological_sort(), range(dig.order())))
yield FinitePoset(dig.relabel(label_dict, inplace=False))
label_dict = dict(zip(dig.topological_sort(), range(self._n)))
dig.relabel(label_dict, inplace=True)
yield FinitePoset(dig)

def cardinality(self, from_iterator=False):
r"""
Expand Down
8 changes: 4 additions & 4 deletions src/sage/combinat/tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,15 +844,15 @@
Partial orders on a set of `8` elements, up to isomorphism::
sage: C = Posets(8); C
Posets containing 8 elements
sage: C = Posets(7); C
Posets containing 7 elements
sage: C.cardinality()
16999
2045
::
sage: C.unrank(20).plot()
Graphics object consisting of 20 graphics primitives
Graphics object consisting of ... graphics primitives
.. image:: ../../media/a_poset.png
Expand Down
9 changes: 5 additions & 4 deletions src/sage/databases/findstat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4002,16 +4002,17 @@ def _finite_lattices(n):
TESTS::
sage: from sage.databases.findstat import _finite_lattices
sage: [L.cover_relations() for L in _finite_lattices(4)]
[[['bottom', 0], ['bottom', 1], [0, 'top'], [1, 'top']],
[['bottom', 0], [0, 1], [1, 'top']]]
sage: sorted((L.cover_relations() for L in _finite_lattices(4)),
....: key=len)
[[['bottom', 0], [0, 1], [1, 'top']],
[['bottom', 0], ['bottom', 1], [0, 'top'], [1, 'top']]]
"""
if n <= 2:
for P in Posets(n):
if P.is_lattice():
yield LatticePoset(P)
else:
for P in Posets(n-2):
for P in Posets(n - 2):
Q = P.with_bounds()
if Q.is_lattice():
yield LatticePoset(Q)
Expand Down

0 comments on commit a1d9c1e

Please sign in to comment.