Skip to content

Commit 94741f9

Browse files
author
Release Manager
committed
gh-38638: interface to new nauty generator for Hasse diagrams add a basic interface to the new capability of nauty, namely a generator for Hasse diagrams of posets ### 📝 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. - [x] I have created tests covering the changes. - [x] I have updated the documentation and checked the documentation preview. URL: #38638 Reported by: Frédéric Chapoton Reviewer(s): David Coudert
2 parents c0e0c37 + e94f42f commit 94741f9

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

src/sage/graphs/digraph_generators.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
:meth:`~DiGraphGenerators.ImaseItoh` | Return the digraph of Imase and Itoh of order `n` and degree `d`.
2929
:meth:`~DiGraphGenerators.Kautz` | Return the Kautz digraph of degree `d` and diameter `D`.
3030
:meth:`~DiGraphGenerators.nauty_directg` | Return an iterator yielding digraphs using nauty's ``directg`` program.
31+
:meth:`~DiGraphGenerators.nauty_posetg` | Return an iterator yielding Hasse diagrams of posets using nauty's ``genposetg`` program.
3132
:meth:`~DiGraphGenerators.Paley` | Return a Paley digraph on `q` vertices.
3233
:meth:`~DiGraphGenerators.Path` | Return a directed path on `n` vertices.
3334
:meth:`~DiGraphGenerators.RandomDirectedAcyclicGraph` | Return a random (weighted) directed acyclic graph of order `n`.
@@ -758,6 +759,63 @@ def nauty_directg(self, graphs, options='', debug=False):
758759
if line and line[0] == '&':
759760
yield DiGraph(line[1:], format='dig6')
760761

762+
def nauty_posetg(self, options='', debug=False):
763+
r"""
764+
Return a generator which creates all posets using ``nauty``.
765+
766+
Here a poset is seen through its Hasse diagram, which is
767+
an acyclic and transitively reduced digraph.
768+
769+
INPUT:
770+
771+
- ``options`` -- string (default: ``""``); a string passed to
772+
``genposetg`` as if it was run at a system command line.
773+
At a minimum, you *must* pass the number of vertices you desire
774+
and a choice between ``o`` and ``t`` for the output order.
775+
776+
- ``debug`` -- boolean (default: ``False``); if ``True`` the first line
777+
of ``genposetg``'s output to standard error is captured and the first
778+
call to the generator's ``next()`` function will return this line as a
779+
string. A line leading with ">A" indicates a successful initiation of
780+
the program with some information on the arguments, while a line
781+
beginning with ">E" indicates an error with the input.
782+
783+
The possible options, obtained as output of ``genposetg --help``::
784+
785+
n: the number of vertices, between 0 and 16
786+
o: digraph6 output in arbitrary order
787+
t: digraph6 output in topological order
788+
789+
EXAMPLES::
790+
791+
sage: gen = digraphs.nauty_posetg("5 o")
792+
sage: len(list(gen))
793+
63
794+
795+
This coincides with :oeis:`A000112`.
796+
"""
797+
import shlex
798+
from sage.features.nauty import NautyExecutable
799+
geng_path = NautyExecutable("genposetg").absolute_filename()
800+
sp = subprocess.Popen(shlex.quote(geng_path) + f" {options}", shell=True,
801+
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
802+
stderr=subprocess.PIPE, close_fds=True,
803+
encoding='latin-1')
804+
msg = sp.stderr.readline()
805+
if debug:
806+
yield msg
807+
elif msg.startswith('>E'):
808+
raise ValueError('wrong format of parameter option')
809+
gen = sp.stdout
810+
while True:
811+
try:
812+
s = next(gen)
813+
except StopIteration:
814+
# Exhausted list of graphs from nauty genposetg
815+
return
816+
G = DiGraph(s[1:-1], format='dig6')
817+
yield G
818+
761819
def Complete(self, n, loops=False):
762820
r"""
763821
Return the complete digraph on `n` vertices.
@@ -1486,7 +1544,9 @@ def RandomDirectedGNM(self, n, m, loops=False):
14861544
sage: D.num_verts()
14871545
10
14881546
sage: D.loops()
1489-
[(0, 0, None), (1, 1, None), (2, 2, None), (3, 3, None), (4, 4, None), (5, 5, None), (6, 6, None), (7, 7, None), (8, 8, None), (9, 9, None)]
1547+
[(0, 0, None), (1, 1, None), (2, 2, None), (3, 3, None),
1548+
(4, 4, None), (5, 5, None), (6, 6, None), (7, 7, None),
1549+
(8, 8, None), (9, 9, None)]
14901550
14911551
TESTS::
14921552

0 commit comments

Comments
 (0)