Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/graphs/hypergraph_generators.py
8815 views
1
r"""
2
Hypergraph generators
3
4
At the moment this module only implement one method, which calls Brendan McKay's
5
Nauty (`<http://cs.anu.edu.au/~bdm/nauty/>`_) to enumerate hypergraphs up to
6
isomorphism.
7
"""
8
9
class HyperGraphGenerators():
10
r"""
11
A class consisting of constructors for common hypergraphs.
12
"""
13
14
def nauty(self, number_of_sets, number_of_vertices,
15
multiple_sets = False,
16
vertex_min_degree = None, vertex_max_degree = None,
17
set_max_size = None, set_min_size = None,
18
regular = False, uniform = False,
19
max_intersection = None,
20
connected = False,
21
options="", debug=False):
22
r"""
23
Enumerates hypergraphs up to isomorphism using Nauty.
24
25
INPUT:
26
27
- ``number_of_sets``, ``number_of_vertices`` (integers)
28
29
- ``multiple_sets`` (boolean) -- whether to allow several sets
30
of the hypergraph to be equal (set to ``False`` by default).
31
32
- ``vertex_min_degree``, ``vertex_max_degree`` (integers) -- define the
33
maximum and minimum degree of an element from the ground set (i.e. the
34
number of sets which contain it). Set to ``None`` by default.
35
36
- ``set_min_size``, ``set_max_size`` (integers) -- define the maximum
37
and minimum size of a set. Set to ``None`` by default.
38
39
- ``regular`` (integer) -- if set to an integer value `k`, requires the
40
hypergraphs to be `k`-regular. It is actually a shortcut for the
41
corresponing min/max values.
42
43
- ``uniform`` (integer) -- if set to an integer value `k`, requires the
44
hypergraphs to be `k`-uniform. It is actually a shortcut for the
45
corresponing min/max values.
46
47
- ``max_intersection`` (integer) -- constraints the maximum cardinality
48
of the intersection of two sets fro the hypergraphs. Set to ``None``
49
by default.
50
51
- ``connected`` (boolean) -- whether to require the hypergraphs to be
52
connected. Set to ``False`` by default.
53
54
- ``debug`` (boolean) -- if ``True`` the first line of genbg's output to
55
standard error is captured and the first call to the generator's
56
``next()`` function will return this line as a string. A line leading
57
with ">A" indicates a successful initiation of the program with some
58
information on the arguments, while a line beginning with ">E"
59
indicates an error with the input.
60
61
- ``options`` (string) -- anything else that should be forwarded as
62
input to Nauty's genbg. See its documentation for more information :
63
`<http://cs.anu.edu.au/~bdm/nauty/>`_.
64
65
.. NOTE::
66
67
For genbg the *first class* elements are vertices, and *second
68
class* elements are the hypergraph's sets.
69
70
OUTPUT:
71
72
A tuple of tuples.
73
74
EXAMPLES:
75
76
Small hypergraphs::
77
78
sage: list(hypergraphs.nauty(4,2)) # optional - nauty
79
[((), (0,), (1,), (0, 1))]
80
81
Only connected ones::
82
83
sage: list(hypergraphs.nauty(2,2, connected = True)) # optional - nauty
84
[((0,), (0, 1))]
85
86
Non-empty sets only::
87
88
sage: list(hypergraphs.nauty(3,2, set_min_size = 1)) # optional - nauty
89
[((0,), (1,), (0, 1))]
90
91
The Fano Plane, as the only 3-uniform hypergraph with 7 sets and 7
92
vertices::
93
94
sage: fano = hypergraphs.nauty(7,7, uniform = 3, max_intersection =1).next() # optional - nauty
95
sage: print fano # optional - nauty
96
((0, 1, 2), (0, 3, 4), (0, 5, 6), (1, 3, 5), (2, 4, 5), (2, 3, 6), (1, 4, 6))
97
98
The Fano Plane, as the only 3-regular hypergraph with 7 sets and 7
99
vertices::
100
101
sage: fano = hypergraphs.nauty(7,7, regular = 3, max_intersection =1).next() # optional - nauty
102
sage: print fano # optional - nauty
103
((0, 1, 2), (0, 3, 4), (0, 5, 6), (1, 3, 5), (2, 4, 5), (2, 3, 6), (1, 4, 6))
104
"""
105
import subprocess
106
from sage.misc.package import is_package_installed
107
if not is_package_installed("nauty"):
108
raise TypeError("The optional nauty spkg does not seem to be installed")
109
110
nauty_input = options
111
112
if connected:
113
nauty_input += " -c"
114
115
if not multiple_sets:
116
nauty_input += " -z"
117
118
if not max_intersection is None:
119
nauty_input += " -Z"+str(max_intersection)
120
121
# degrees and sizes
122
if not regular is False:
123
vertex_max_degree = vertex_min_degree = regular
124
if vertex_max_degree is None:
125
vertex_max_degree = number_of_sets
126
if vertex_min_degree is None:
127
vertex_min_degree = 0
128
129
if not uniform is False:
130
set_max_size = set_min_size = uniform
131
if set_max_size is None:
132
set_max_size = number_of_vertices
133
if set_min_size is None:
134
set_min_size = 0
135
136
nauty_input += " -d"+str(vertex_min_degree)+":"+str(set_min_size)
137
nauty_input += " -D"+str(vertex_max_degree)+":"+str(set_max_size)
138
139
140
nauty_input += " "+str(number_of_vertices) +" "+str(number_of_sets)+" "
141
142
sp = subprocess.Popen("nauty-genbg {0}".format(nauty_input), shell=True,
143
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
144
stderr=subprocess.PIPE, close_fds=True)
145
146
if debug:
147
yield sp.stderr.readline()
148
149
gen = sp.stdout
150
total = number_of_sets + number_of_vertices
151
while True:
152
try:
153
s = gen.next()
154
except StopIteration:
155
raise StopIteration("Exhausted list of graphs from nauty geng")
156
157
from sage.graphs.graph import Graph
158
G = Graph(s[:-1], format='graph6')
159
160
yield tuple( tuple( x for x in G.neighbors(v)) for v in range(number_of_vertices, total))
161
162
hypergraphs = HyperGraphGenerators()
163
164