Skip to content

Commit

Permalink
sagemathgh-38638: interface to new nauty generator for Hasse diagrams
Browse files Browse the repository at this point in the history
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: sagemath#38638
Reported by: Frédéric Chapoton
Reviewer(s): David Coudert
  • Loading branch information
Release Manager committed Sep 10, 2024
2 parents db2c09c + e94f42f commit 5d5310c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 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=0f6355fc136bb6619585863b9e4bc954cc6e0e3d
sha256=5b618581d51997afa78b5e6647584f7ef4e6d5844823dd44e607f2accd7abba5
sha1=21037348abae35bf0a24ff94652c349db7bd0623
sha256=5a6fc76cc8a549faff06be7a3012d9615f16bf9ab44ecd2a0869cef79785e9cd
2 changes: 1 addition & 1 deletion build/pkgs/configure/package-version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b9cb7bc2559cde857d84d77f0b37a3616ce1eb6c
be1de0cea3ea36bd99f1ddfc30bc81a38fdf2fc7
62 changes: 61 additions & 1 deletion src/sage/graphs/digraph_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
:meth:`~DiGraphGenerators.ImaseItoh` | Return the digraph of Imase and Itoh of order `n` and degree `d`.
:meth:`~DiGraphGenerators.Kautz` | Return the Kautz digraph of degree `d` and diameter `D`.
:meth:`~DiGraphGenerators.nauty_directg` | Return an iterator yielding digraphs using nauty's ``directg`` program.
:meth:`~DiGraphGenerators.nauty_posetg` | Return an iterator yielding Hasse diagrams of posets using nauty's ``genposetg`` program.
:meth:`~DiGraphGenerators.Paley` | Return a Paley digraph on `q` vertices.
:meth:`~DiGraphGenerators.Path` | Return a directed path on `n` vertices.
:meth:`~DiGraphGenerators.RandomDirectedAcyclicGraph` | Return a random (weighted) directed acyclic graph of order `n`.
Expand Down Expand Up @@ -758,6 +759,63 @@ def nauty_directg(self, graphs, options='', debug=False):
if line and line[0] == '&':
yield DiGraph(line[1:], format='dig6')

def nauty_posetg(self, options='', debug=False):
r"""
Return a generator which creates all posets using ``nauty``.
Here a poset is seen through its Hasse diagram, which is
an acyclic and transitively reduced digraph.
INPUT:
- ``options`` -- string (default: ``""``); a string passed to
``genposetg`` as if it was run at a system command line.
At a minimum, you *must* pass the number of vertices you desire
and a choice between ``o`` and ``t`` for the output order.
- ``debug`` -- boolean (default: ``False``); if ``True`` the first line
of ``genposetg``'s output to standard error is captured and the first
call to the generator's ``next()`` function will return this line as a
string. A line leading with ">A" indicates a successful initiation of
the program with some information on the arguments, while a line
beginning with ">E" indicates an error with the input.
The possible options, obtained as output of ``genposetg --help``::
n: the number of vertices, between 0 and 16
o: digraph6 output in arbitrary order
t: digraph6 output in topological order
EXAMPLES::
sage: gen = digraphs.nauty_posetg("5 o")
sage: len(list(gen))
63
This coincides with :oeis:`A000112`.
"""
import shlex
from sage.features.nauty import NautyExecutable
geng_path = NautyExecutable("genposetg").absolute_filename()
sp = subprocess.Popen(shlex.quote(geng_path) + f" {options}", shell=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, close_fds=True,
encoding='latin-1')
msg = sp.stderr.readline()
if debug:
yield msg
elif msg.startswith('>E'):
raise ValueError('wrong format of parameter option')
gen = sp.stdout
while True:
try:
s = next(gen)
except StopIteration:
# Exhausted list of graphs from nauty genposetg
return
G = DiGraph(s[1:-1], format='dig6')
yield G

def Complete(self, n, loops=False):
r"""
Return the complete digraph on `n` vertices.
Expand Down Expand Up @@ -1486,7 +1544,9 @@ def RandomDirectedGNM(self, n, m, loops=False):
sage: D.num_verts()
10
sage: D.loops()
[(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)]
[(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)]
TESTS::
Expand Down

0 comments on commit 5d5310c

Please sign in to comment.