From 50dced7f1a5090e1b1616d7013c3e0c854c3cac0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Sun, 8 Sep 2024 10:19:21 +0200 Subject: [PATCH 1/5] interface to new nauty generator for Hasse diagrams --- src/sage/graphs/digraph_generators.py | 59 +++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/sage/graphs/digraph_generators.py b/src/sage/graphs/digraph_generators.py index 02590ac8f7c..081235de342 100644 --- a/src/sage/graphs/digraph_generators.py +++ b/src/sage/graphs/digraph_generators.py @@ -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`. @@ -758,6 +759,64 @@ 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 + o: digraph6 output in arbitrary order + t: digraph6 output in topological order + q: supresses statistics except for the final count + + EXAMPLES:: + + sage: gen = digraphs.nauty_posetg("5 o q") + 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. From f6b9fa165f66f402d09ab10670d922a84bd613c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Sun, 8 Sep 2024 10:29:39 +0200 Subject: [PATCH 2/5] fix docstring --- src/sage/graphs/digraph_generators.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/sage/graphs/digraph_generators.py b/src/sage/graphs/digraph_generators.py index 081235de342..efe45f2dcbf 100644 --- a/src/sage/graphs/digraph_generators.py +++ b/src/sage/graphs/digraph_generators.py @@ -771,7 +771,7 @@ def nauty_posetg(self, options='', debug=False): - ``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. + 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 @@ -782,10 +782,10 @@ def nauty_posetg(self, options='', debug=False): The possible options, obtained as output of ``genposetg --help``:: - n: the number of vertices - o: digraph6 output in arbitrary order - t: digraph6 output in topological order - q: supresses statistics except for the final count + n: the number of vertices + o: digraph6 output in arbitrary order + t: digraph6 output in topological order + q: supresses statistics except for the final count EXAMPLES:: From a4411905719792fd8a73d204f4b3d1c064e69899 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Sun, 8 Sep 2024 11:04:32 +0200 Subject: [PATCH 3/5] fix suggestions + cut one long line --- src/sage/graphs/digraph_generators.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/sage/graphs/digraph_generators.py b/src/sage/graphs/digraph_generators.py index efe45f2dcbf..d3d19959399 100644 --- a/src/sage/graphs/digraph_generators.py +++ b/src/sage/graphs/digraph_generators.py @@ -782,10 +782,9 @@ def nauty_posetg(self, options='', debug=False): The possible options, obtained as output of ``genposetg --help``:: - n: the number of vertices + n: the number of vertices, between 0 and 16 o: digraph6 output in arbitrary order t: digraph6 output in topological order - q: supresses statistics except for the final count EXAMPLES:: @@ -1545,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:: From 43fabb091cc85b0f6f49d3b77a25ae70d210024b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Sun, 8 Sep 2024 11:16:56 +0200 Subject: [PATCH 4/5] simplify the doctest --- src/sage/graphs/digraph_generators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sage/graphs/digraph_generators.py b/src/sage/graphs/digraph_generators.py index d3d19959399..d4dfbdaddea 100644 --- a/src/sage/graphs/digraph_generators.py +++ b/src/sage/graphs/digraph_generators.py @@ -788,7 +788,7 @@ def nauty_posetg(self, options='', debug=False): EXAMPLES:: - sage: gen = digraphs.nauty_posetg("5 o q") + sage: gen = digraphs.nauty_posetg("5 o") sage: len(list(gen)) 63 From e94f42f5aae52dd70debc8da4d54fc5a9cee4438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Sun, 8 Sep 2024 12:13:49 +0200 Subject: [PATCH 5/5] minor mistake in doc --- src/sage/graphs/digraph_generators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sage/graphs/digraph_generators.py b/src/sage/graphs/digraph_generators.py index d4dfbdaddea..cbea708d998 100644 --- a/src/sage/graphs/digraph_generators.py +++ b/src/sage/graphs/digraph_generators.py @@ -771,7 +771,7 @@ def nauty_posetg(self, options='', debug=False): - ``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. + 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