passagemath-graphs 10.6.1rc1__cp310-cp310-musllinux_1_2_aarch64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (260) hide show
  1. passagemath_graphs-10.6.1rc1.dist-info/METADATA +292 -0
  2. passagemath_graphs-10.6.1rc1.dist-info/RECORD +260 -0
  3. passagemath_graphs-10.6.1rc1.dist-info/WHEEL +5 -0
  4. passagemath_graphs-10.6.1rc1.dist-info/top_level.txt +2 -0
  5. passagemath_graphs.libs/libgcc_s-69c45f16.so.1 +0 -0
  6. passagemath_graphs.libs/libgmp-8e78bd9b.so.10.5.0 +0 -0
  7. passagemath_graphs.libs/libstdc++-1f1a71be.so.6.0.33 +0 -0
  8. sage/all__sagemath_graphs.py +39 -0
  9. sage/combinat/abstract_tree.py +2723 -0
  10. sage/combinat/all__sagemath_graphs.py +34 -0
  11. sage/combinat/binary_tree.py +5306 -0
  12. sage/combinat/cluster_algebra_quiver/all.py +22 -0
  13. sage/combinat/cluster_algebra_quiver/cluster_seed.py +5208 -0
  14. sage/combinat/cluster_algebra_quiver/interact.py +124 -0
  15. sage/combinat/cluster_algebra_quiver/mutation_class.py +625 -0
  16. sage/combinat/cluster_algebra_quiver/mutation_type.py +1555 -0
  17. sage/combinat/cluster_algebra_quiver/quiver.py +2290 -0
  18. sage/combinat/cluster_algebra_quiver/quiver_mutation_type.py +2468 -0
  19. sage/combinat/designs/MOLS_handbook_data.py +570 -0
  20. sage/combinat/designs/all.py +58 -0
  21. sage/combinat/designs/bibd.py +1655 -0
  22. sage/combinat/designs/block_design.py +1071 -0
  23. sage/combinat/designs/covering_array.py +269 -0
  24. sage/combinat/designs/covering_design.py +530 -0
  25. sage/combinat/designs/database.py +5615 -0
  26. sage/combinat/designs/design_catalog.py +122 -0
  27. sage/combinat/designs/designs_pyx.cpython-310-aarch64-linux-gnu.so +0 -0
  28. sage/combinat/designs/designs_pyx.pxd +21 -0
  29. sage/combinat/designs/designs_pyx.pyx +993 -0
  30. sage/combinat/designs/difference_family.py +3951 -0
  31. sage/combinat/designs/difference_matrices.py +279 -0
  32. sage/combinat/designs/evenly_distributed_sets.cpython-310-aarch64-linux-gnu.so +0 -0
  33. sage/combinat/designs/evenly_distributed_sets.pyx +661 -0
  34. sage/combinat/designs/ext_rep.py +1064 -0
  35. sage/combinat/designs/gen_quadrangles_with_spread.cpython-310-aarch64-linux-gnu.so +0 -0
  36. sage/combinat/designs/gen_quadrangles_with_spread.pyx +339 -0
  37. sage/combinat/designs/group_divisible_designs.py +361 -0
  38. sage/combinat/designs/incidence_structures.py +2357 -0
  39. sage/combinat/designs/latin_squares.py +581 -0
  40. sage/combinat/designs/orthogonal_arrays.py +2244 -0
  41. sage/combinat/designs/orthogonal_arrays_build_recursive.py +1780 -0
  42. sage/combinat/designs/orthogonal_arrays_find_recursive.cpython-310-aarch64-linux-gnu.so +0 -0
  43. sage/combinat/designs/orthogonal_arrays_find_recursive.pyx +967 -0
  44. sage/combinat/designs/resolvable_bibd.py +815 -0
  45. sage/combinat/designs/steiner_quadruple_systems.py +1306 -0
  46. sage/combinat/designs/subhypergraph_search.cpython-310-aarch64-linux-gnu.so +0 -0
  47. sage/combinat/designs/subhypergraph_search.pyx +530 -0
  48. sage/combinat/designs/twographs.py +306 -0
  49. sage/combinat/finite_state_machine.py +14874 -0
  50. sage/combinat/finite_state_machine_generators.py +2006 -0
  51. sage/combinat/graph_path.py +448 -0
  52. sage/combinat/interval_posets.py +3908 -0
  53. sage/combinat/nu_tamari_lattice.py +269 -0
  54. sage/combinat/ordered_tree.py +1446 -0
  55. sage/combinat/posets/all.py +46 -0
  56. sage/combinat/posets/bubble_shuffle.py +247 -0
  57. sage/combinat/posets/cartesian_product.py +493 -0
  58. sage/combinat/posets/d_complete.py +182 -0
  59. sage/combinat/posets/elements.py +273 -0
  60. sage/combinat/posets/forest.py +30 -0
  61. sage/combinat/posets/hasse_cython.cpython-310-aarch64-linux-gnu.so +0 -0
  62. sage/combinat/posets/hasse_cython.pyx +174 -0
  63. sage/combinat/posets/hasse_diagram.py +3672 -0
  64. sage/combinat/posets/hochschild_lattice.py +158 -0
  65. sage/combinat/posets/incidence_algebras.py +794 -0
  66. sage/combinat/posets/lattices.py +5117 -0
  67. sage/combinat/posets/linear_extension_iterator.cpython-310-aarch64-linux-gnu.so +0 -0
  68. sage/combinat/posets/linear_extension_iterator.pyx +292 -0
  69. sage/combinat/posets/linear_extensions.py +1037 -0
  70. sage/combinat/posets/mobile.py +275 -0
  71. sage/combinat/posets/moebius_algebra.py +776 -0
  72. sage/combinat/posets/poset_examples.py +2178 -0
  73. sage/combinat/posets/posets.py +9360 -0
  74. sage/combinat/rooted_tree.py +1070 -0
  75. sage/combinat/shard_order.py +239 -0
  76. sage/combinat/tamari_lattices.py +384 -0
  77. sage/combinat/yang_baxter_graph.py +923 -0
  78. sage/databases/all__sagemath_graphs.py +1 -0
  79. sage/databases/knotinfo_db.py +1231 -0
  80. sage/ext_data/all__sagemath_graphs.py +1 -0
  81. sage/ext_data/graphs/graph_plot_js.html +330 -0
  82. sage/ext_data/kenzo/CP2.txt +45 -0
  83. sage/ext_data/kenzo/CP3.txt +349 -0
  84. sage/ext_data/kenzo/CP4.txt +4774 -0
  85. sage/ext_data/kenzo/README.txt +49 -0
  86. sage/ext_data/kenzo/S4.txt +20 -0
  87. sage/graphs/all.py +42 -0
  88. sage/graphs/asteroidal_triples.cpython-310-aarch64-linux-gnu.so +0 -0
  89. sage/graphs/asteroidal_triples.pyx +320 -0
  90. sage/graphs/base/all.py +1 -0
  91. sage/graphs/base/boost_graph.cpython-310-aarch64-linux-gnu.so +0 -0
  92. sage/graphs/base/boost_graph.pxd +106 -0
  93. sage/graphs/base/boost_graph.pyx +3045 -0
  94. sage/graphs/base/c_graph.cpython-310-aarch64-linux-gnu.so +0 -0
  95. sage/graphs/base/c_graph.pxd +106 -0
  96. sage/graphs/base/c_graph.pyx +5096 -0
  97. sage/graphs/base/dense_graph.cpython-310-aarch64-linux-gnu.so +0 -0
  98. sage/graphs/base/dense_graph.pxd +28 -0
  99. sage/graphs/base/dense_graph.pyx +801 -0
  100. sage/graphs/base/graph_backends.cpython-310-aarch64-linux-gnu.so +0 -0
  101. sage/graphs/base/graph_backends.pxd +5 -0
  102. sage/graphs/base/graph_backends.pyx +797 -0
  103. sage/graphs/base/overview.py +85 -0
  104. sage/graphs/base/sparse_graph.cpython-310-aarch64-linux-gnu.so +0 -0
  105. sage/graphs/base/sparse_graph.pxd +90 -0
  106. sage/graphs/base/sparse_graph.pyx +1653 -0
  107. sage/graphs/base/static_dense_graph.cpython-310-aarch64-linux-gnu.so +0 -0
  108. sage/graphs/base/static_dense_graph.pxd +5 -0
  109. sage/graphs/base/static_dense_graph.pyx +1032 -0
  110. sage/graphs/base/static_sparse_backend.cpython-310-aarch64-linux-gnu.so +0 -0
  111. sage/graphs/base/static_sparse_backend.pxd +27 -0
  112. sage/graphs/base/static_sparse_backend.pyx +1583 -0
  113. sage/graphs/base/static_sparse_graph.cpython-310-aarch64-linux-gnu.so +0 -0
  114. sage/graphs/base/static_sparse_graph.pxd +37 -0
  115. sage/graphs/base/static_sparse_graph.pyx +1375 -0
  116. sage/graphs/bipartite_graph.py +2732 -0
  117. sage/graphs/centrality.cpython-310-aarch64-linux-gnu.so +0 -0
  118. sage/graphs/centrality.pyx +1038 -0
  119. sage/graphs/cographs.py +519 -0
  120. sage/graphs/comparability.cpython-310-aarch64-linux-gnu.so +0 -0
  121. sage/graphs/comparability.pyx +851 -0
  122. sage/graphs/connectivity.cpython-310-aarch64-linux-gnu.so +0 -0
  123. sage/graphs/connectivity.pxd +157 -0
  124. sage/graphs/connectivity.pyx +4813 -0
  125. sage/graphs/convexity_properties.cpython-310-aarch64-linux-gnu.so +0 -0
  126. sage/graphs/convexity_properties.pxd +16 -0
  127. sage/graphs/convexity_properties.pyx +870 -0
  128. sage/graphs/digraph.py +4754 -0
  129. sage/graphs/digraph_generators.py +1993 -0
  130. sage/graphs/distances_all_pairs.cpython-310-aarch64-linux-gnu.so +0 -0
  131. sage/graphs/distances_all_pairs.pxd +12 -0
  132. sage/graphs/distances_all_pairs.pyx +2938 -0
  133. sage/graphs/domination.py +1363 -0
  134. sage/graphs/dot2tex_utils.py +100 -0
  135. sage/graphs/edge_connectivity.cpython-310-aarch64-linux-gnu.so +0 -0
  136. sage/graphs/edge_connectivity.pyx +1215 -0
  137. sage/graphs/generators/all.py +1 -0
  138. sage/graphs/generators/basic.py +1769 -0
  139. sage/graphs/generators/chessboard.py +538 -0
  140. sage/graphs/generators/classical_geometries.py +1611 -0
  141. sage/graphs/generators/degree_sequence.py +235 -0
  142. sage/graphs/generators/distance_regular.cpython-310-aarch64-linux-gnu.so +0 -0
  143. sage/graphs/generators/distance_regular.pyx +2846 -0
  144. sage/graphs/generators/families.py +4759 -0
  145. sage/graphs/generators/intersection.py +565 -0
  146. sage/graphs/generators/platonic_solids.py +262 -0
  147. sage/graphs/generators/random.py +2623 -0
  148. sage/graphs/generators/smallgraphs.py +5741 -0
  149. sage/graphs/generators/world_map.py +724 -0
  150. sage/graphs/generic_graph.py +26867 -0
  151. sage/graphs/generic_graph_pyx.cpython-310-aarch64-linux-gnu.so +0 -0
  152. sage/graphs/generic_graph_pyx.pxd +34 -0
  153. sage/graphs/generic_graph_pyx.pyx +1673 -0
  154. sage/graphs/genus.cpython-310-aarch64-linux-gnu.so +0 -0
  155. sage/graphs/genus.pyx +622 -0
  156. sage/graphs/graph.py +9645 -0
  157. sage/graphs/graph_coloring.cpython-310-aarch64-linux-gnu.so +0 -0
  158. sage/graphs/graph_coloring.pyx +2284 -0
  159. sage/graphs/graph_database.py +1177 -0
  160. sage/graphs/graph_decompositions/all.py +1 -0
  161. sage/graphs/graph_decompositions/bandwidth.cpython-310-aarch64-linux-gnu.so +0 -0
  162. sage/graphs/graph_decompositions/bandwidth.pyx +428 -0
  163. sage/graphs/graph_decompositions/clique_separators.cpython-310-aarch64-linux-gnu.so +0 -0
  164. sage/graphs/graph_decompositions/clique_separators.pyx +616 -0
  165. sage/graphs/graph_decompositions/cutwidth.cpython-310-aarch64-linux-gnu.so +0 -0
  166. sage/graphs/graph_decompositions/cutwidth.pyx +753 -0
  167. sage/graphs/graph_decompositions/fast_digraph.cpython-310-aarch64-linux-gnu.so +0 -0
  168. sage/graphs/graph_decompositions/fast_digraph.pxd +13 -0
  169. sage/graphs/graph_decompositions/fast_digraph.pyx +212 -0
  170. sage/graphs/graph_decompositions/graph_products.cpython-310-aarch64-linux-gnu.so +0 -0
  171. sage/graphs/graph_decompositions/graph_products.pyx +508 -0
  172. sage/graphs/graph_decompositions/modular_decomposition.cpython-310-aarch64-linux-gnu.so +0 -0
  173. sage/graphs/graph_decompositions/modular_decomposition.pxd +27 -0
  174. sage/graphs/graph_decompositions/modular_decomposition.pyx +1536 -0
  175. sage/graphs/graph_decompositions/slice_decomposition.cpython-310-aarch64-linux-gnu.so +0 -0
  176. sage/graphs/graph_decompositions/slice_decomposition.pxd +18 -0
  177. sage/graphs/graph_decompositions/slice_decomposition.pyx +1106 -0
  178. sage/graphs/graph_decompositions/tree_decomposition.cpython-310-aarch64-linux-gnu.so +0 -0
  179. sage/graphs/graph_decompositions/tree_decomposition.pxd +17 -0
  180. sage/graphs/graph_decompositions/tree_decomposition.pyx +1996 -0
  181. sage/graphs/graph_decompositions/vertex_separation.cpython-310-aarch64-linux-gnu.so +0 -0
  182. sage/graphs/graph_decompositions/vertex_separation.pxd +5 -0
  183. sage/graphs/graph_decompositions/vertex_separation.pyx +1963 -0
  184. sage/graphs/graph_editor.py +82 -0
  185. sage/graphs/graph_generators.py +3314 -0
  186. sage/graphs/graph_generators_pyx.cpython-310-aarch64-linux-gnu.so +0 -0
  187. sage/graphs/graph_generators_pyx.pyx +95 -0
  188. sage/graphs/graph_input.py +812 -0
  189. sage/graphs/graph_latex.py +2064 -0
  190. sage/graphs/graph_list.py +410 -0
  191. sage/graphs/graph_plot.py +1756 -0
  192. sage/graphs/graph_plot_js.py +338 -0
  193. sage/graphs/hyperbolicity.cpython-310-aarch64-linux-gnu.so +0 -0
  194. sage/graphs/hyperbolicity.pyx +1704 -0
  195. sage/graphs/hypergraph_generators.py +364 -0
  196. sage/graphs/independent_sets.cpython-310-aarch64-linux-gnu.so +0 -0
  197. sage/graphs/independent_sets.pxd +13 -0
  198. sage/graphs/independent_sets.pyx +402 -0
  199. sage/graphs/isgci.py +1033 -0
  200. sage/graphs/isoperimetric_inequalities.cpython-310-aarch64-linux-gnu.so +0 -0
  201. sage/graphs/isoperimetric_inequalities.pyx +489 -0
  202. sage/graphs/line_graph.cpython-310-aarch64-linux-gnu.so +0 -0
  203. sage/graphs/line_graph.pyx +743 -0
  204. sage/graphs/lovasz_theta.py +77 -0
  205. sage/graphs/matching.py +1633 -0
  206. sage/graphs/matching_covered_graph.py +3590 -0
  207. sage/graphs/orientations.py +1489 -0
  208. sage/graphs/partial_cube.py +459 -0
  209. sage/graphs/path_enumeration.cpython-310-aarch64-linux-gnu.so +0 -0
  210. sage/graphs/path_enumeration.pyx +2040 -0
  211. sage/graphs/pq_trees.py +1129 -0
  212. sage/graphs/print_graphs.py +201 -0
  213. sage/graphs/schnyder.py +865 -0
  214. sage/graphs/spanning_tree.cpython-310-aarch64-linux-gnu.so +0 -0
  215. sage/graphs/spanning_tree.pyx +1457 -0
  216. sage/graphs/strongly_regular_db.cpython-310-aarch64-linux-gnu.so +0 -0
  217. sage/graphs/strongly_regular_db.pyx +3340 -0
  218. sage/graphs/traversals.cpython-310-aarch64-linux-gnu.so +0 -0
  219. sage/graphs/traversals.pxd +9 -0
  220. sage/graphs/traversals.pyx +1872 -0
  221. sage/graphs/trees.cpython-310-aarch64-linux-gnu.so +0 -0
  222. sage/graphs/trees.pxd +15 -0
  223. sage/graphs/trees.pyx +310 -0
  224. sage/graphs/tutte_polynomial.py +713 -0
  225. sage/graphs/views.cpython-310-aarch64-linux-gnu.so +0 -0
  226. sage/graphs/views.pyx +794 -0
  227. sage/graphs/weakly_chordal.cpython-310-aarch64-linux-gnu.so +0 -0
  228. sage/graphs/weakly_chordal.pyx +604 -0
  229. sage/groups/all__sagemath_graphs.py +1 -0
  230. sage/groups/perm_gps/all__sagemath_graphs.py +1 -0
  231. sage/groups/perm_gps/partn_ref/all__sagemath_graphs.py +1 -0
  232. sage/groups/perm_gps/partn_ref/refinement_graphs.cpython-310-aarch64-linux-gnu.so +0 -0
  233. sage/groups/perm_gps/partn_ref/refinement_graphs.pxd +38 -0
  234. sage/groups/perm_gps/partn_ref/refinement_graphs.pyx +1666 -0
  235. sage/knots/all.py +6 -0
  236. sage/knots/free_knotinfo_monoid.py +507 -0
  237. sage/knots/gauss_code.py +291 -0
  238. sage/knots/knot.py +682 -0
  239. sage/knots/knot_table.py +284 -0
  240. sage/knots/knotinfo.py +2900 -0
  241. sage/knots/link.py +4715 -0
  242. sage/sandpiles/all.py +13 -0
  243. sage/sandpiles/examples.py +225 -0
  244. sage/sandpiles/sandpile.py +6365 -0
  245. sage/topology/all.py +22 -0
  246. sage/topology/cell_complex.py +1214 -0
  247. sage/topology/cubical_complex.py +1976 -0
  248. sage/topology/delta_complex.py +1806 -0
  249. sage/topology/filtered_simplicial_complex.py +744 -0
  250. sage/topology/moment_angle_complex.py +823 -0
  251. sage/topology/simplicial_complex.py +5160 -0
  252. sage/topology/simplicial_complex_catalog.py +92 -0
  253. sage/topology/simplicial_complex_examples.py +1680 -0
  254. sage/topology/simplicial_complex_homset.py +205 -0
  255. sage/topology/simplicial_complex_morphism.py +836 -0
  256. sage/topology/simplicial_set.py +4102 -0
  257. sage/topology/simplicial_set_catalog.py +55 -0
  258. sage/topology/simplicial_set_constructions.py +2954 -0
  259. sage/topology/simplicial_set_examples.py +865 -0
  260. sage/topology/simplicial_set_morphism.py +1464 -0
@@ -0,0 +1,402 @@
1
+ # sage_setup: distribution = sagemath-graphs
2
+ r"""
3
+ Independent sets
4
+
5
+ This module implements the :class:`IndependentSets` class which can be used to :
6
+
7
+ - List the independent sets (or cliques) of a graph
8
+ - Count them (which is obviously faster)
9
+ - Test whether a set of vertices is an independent set
10
+
11
+ It can also be restricted to focus on (inclusionwise) maximal independent
12
+ sets. See the documentation of :class:`IndependentSets` for actual examples.
13
+
14
+ Classes and methods
15
+ -------------------
16
+ """
17
+
18
+ from sage.data_structures.binary_matrix cimport *
19
+ from sage.misc.cachefunc import cached_method
20
+ from sage.graphs.base.static_dense_graph cimport dense_graph_init
21
+
22
+
23
+ cdef inline int ismaximal(binary_matrix_t g, int n, bitset_t s) noexcept:
24
+ cdef int i
25
+ for i in range(n):
26
+ if (not bitset_in(s, i)) and bitset_are_disjoint(g.rows[i], s):
27
+ return False
28
+
29
+ return True
30
+
31
+
32
+ cdef class IndependentSets:
33
+ r"""
34
+ The set of independent sets of a graph.
35
+
36
+ For more information on independent sets, see the
37
+ :wikipedia:`Independent_set_(graph_theory)`.
38
+
39
+ INPUT:
40
+
41
+ - ``G`` -- a graph
42
+
43
+ - ``maximal`` -- boolean (default: ``False``); whether to only consider
44
+ (inclusionwise) maximal independent sets
45
+
46
+ - ``complement`` -- boolean (default: ``False``); whether to consider the
47
+ graph's complement (i.e. cliques instead of independent sets)
48
+
49
+ ALGORITHM:
50
+
51
+ The enumeration of independent sets is done naively : given an independent
52
+ set, this implementation considers all ways to add a new vertex to it
53
+ (while keeping it an independent set), and then creates new independent
54
+ sets from all those that were created this way.
55
+
56
+ The implementation, however, is not recursive.
57
+
58
+ .. NOTE::
59
+
60
+ This implementation of the enumeration of *maximal* independent sets is
61
+ not much faster than NetworkX', which is surprising as it is written in
62
+ Cython. This being said, the algorithm from NetworkX appears to be
63
+ slightly different from this one, and that would be a good thing to
64
+ explore if one wants to improve the implementation.
65
+
66
+ A simple generalization can also be done without too much modifications:
67
+ iteration through independent sets with given size bounds
68
+ (minimum and maximum number of vertices allowed).
69
+
70
+ EXAMPLES:
71
+
72
+ Listing all independent sets of the Claw graph::
73
+
74
+ sage: from sage.graphs.independent_sets import IndependentSets
75
+ sage: g = graphs.ClawGraph()
76
+ sage: I = IndependentSets(g)
77
+ sage: list(I)
78
+ [[0], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3], []]
79
+
80
+ Count them::
81
+
82
+ sage: I.cardinality()
83
+ 9
84
+
85
+ List only the maximal independent sets::
86
+
87
+ sage: Im = IndependentSets(g, maximal=True)
88
+ sage: list(Im)
89
+ [[0], [1, 2, 3]]
90
+
91
+ And count them::
92
+
93
+ sage: Im.cardinality()
94
+ 2
95
+
96
+ One can easily count the number of independent sets of each cardinality::
97
+
98
+ sage: g = graphs.PetersenGraph()
99
+ sage: number_of = [0] * g.order()
100
+ sage: for x in IndependentSets(g):
101
+ ....: number_of[len(x)] += 1
102
+ sage: number_of
103
+ [1, 10, 30, 30, 5, 0, 0, 0, 0, 0]
104
+
105
+ It is also possible to define an iterator over all independent sets of a
106
+ given cardinality. Note, however, that Sage will generate them *all*, to
107
+ return only those that satisfy the cardinality constraints. Getting the list
108
+ of independent sets of size 4 in this way can thus take a very long time::
109
+
110
+ sage: is4 = (x for x in IndependentSets(g) if len(x) == 4)
111
+ sage: list(is4)
112
+ [[0, 2, 8, 9], [0, 3, 6, 7], [1, 3, 5, 9], [1, 4, 7, 8], [2, 4, 5, 6]]
113
+
114
+ Given a subset of the vertices, it is possible to test whether it is an
115
+ independent set::
116
+
117
+ sage: g = graphs.DurerGraph()
118
+ sage: I = IndependentSets(g)
119
+ sage: [0, 2] in I
120
+ True
121
+ sage: [0, 3, 5] in I
122
+ False
123
+
124
+ If an element of the subset is not a vertex, then an error is raised::
125
+
126
+ sage: [0, 'a', 'b', 'c'] in I
127
+ Traceback (most recent call last):
128
+ ...
129
+ ValueError: a is not a vertex of the graph
130
+ """
131
+ def __init__(self, G, maximal=False, complement=False):
132
+ r"""
133
+ Constructor for this class.
134
+
135
+ TESTS::
136
+
137
+ sage: from sage.graphs.independent_sets import IndependentSets
138
+ sage: IndependentSets(graphs.PetersenGraph())
139
+ <sage.graphs.independent_sets.IndependentSets object...
140
+
141
+ Compute the number of matchings, and check with Sage's implementation::
142
+
143
+ sage: from sage.graphs.independent_sets import IndependentSets
144
+ sage: from sage.graphs.matchpoly import matching_polynomial # needs sage.libs.flint
145
+ sage: def check_matching(G):
146
+ ....: number_of_matchings = sum(map(abs, matching_polynomial(G).coefficients(sparse=False)))
147
+ ....: if number_of_matchings != IndependentSets(G.line_graph()).cardinality():
148
+ ....: raise ValueError("something goes wrong")
149
+ sage: for i in range(30): # needs sage.libs.flint
150
+ ....: check_matching(graphs.RandomGNP(11, .3))
151
+
152
+ Compare the result with the output of :meth:`subgraph_search`::
153
+
154
+ sage: from sage.sets.set import Set
155
+ sage: def check_with_subgraph_search(G):
156
+ ....: IS = set(map(Set, list(IndependentSets(G))))
157
+ ....: if not all(G.subgraph(l).is_independent_set() for l in IS):
158
+ ....: print("Gloops")
159
+ ....: alpha = max(map(len, IS))
160
+ ....: IS2 = [Set([x]) for x in range(G.order())] + [Set()]
161
+ ....: for n in range(2, alpha + 1):
162
+ ....: IS2.extend(map(Set, list(G.subgraph_search_iterator(Graph(n), induced=True, return_graphs=False))))
163
+ ....: if len(IS) != len(set(IS2)):
164
+ ....: raise ValueError("something goes wrong")
165
+ sage: for i in range(5): # needs sage.modules
166
+ ....: check_with_subgraph_search(graphs.RandomGNP(11, .3))
167
+
168
+ Empty graph::
169
+
170
+ sage: IS0 = IndependentSets(graphs.EmptyGraph())
171
+ sage: list(IS0)
172
+ [[]]
173
+ sage: IS0.cardinality()
174
+ 1
175
+ """
176
+ cdef int i
177
+
178
+ # Map from Vertex to Integer, and from Integer to Vertex
179
+ self.vertices = list(G)
180
+ self.vertex_to_int = {v: i for i, v in enumerate(self.vertices)}
181
+ self.n = G.order()
182
+ self.maximal = maximal
183
+ dense_graph_init(self.g, G, translation=self.vertex_to_int)
184
+
185
+ # If we must consider the graph's complement instead
186
+ if complement:
187
+ binary_matrix_complement(self.g)
188
+ for i in range(self.n):
189
+ binary_matrix_set0(self.g, i, i)
190
+
191
+ self.count_only = 0
192
+
193
+ def __iter__(self):
194
+ r"""
195
+ Return an iterator over the independent sets of ``self``.
196
+
197
+ TESTS::
198
+
199
+ sage: from sage.graphs.independent_sets import IndependentSets
200
+ sage: I = IndependentSets(graphs.PetersenGraph())
201
+ sage: iter1 = iter(I)
202
+ sage: iter2 = iter(I)
203
+ sage: next(iter1) # indirect doctest
204
+ [0]
205
+ sage: next(iter2) # indirect doctest
206
+ [0]
207
+ sage: next(iter2)
208
+ [0, 2]
209
+ sage: next(iter1)
210
+ [0, 2]
211
+ """
212
+ if not self.n:
213
+ yield []
214
+ return
215
+
216
+ cdef int i = 0
217
+
218
+ cdef bitset_t current_set
219
+ cdef bitset_t tmp
220
+ bitset_init(current_set, self.n)
221
+ bitset_set_first_n(current_set, 0)
222
+ bitset_add(current_set, 0)
223
+ bitset_init(tmp, self.n)
224
+
225
+ cdef uint64_t count = 0
226
+ cdef int j
227
+
228
+ try:
229
+ # At every moment of the algorithm current_set represents an independent
230
+ # set, except for the ith bit. All bits >i are zero.
231
+
232
+ while True:
233
+
234
+ # If i is in current_set
235
+ if bitset_in(current_set, i):
236
+
237
+ # We have found an independent set !
238
+ if bitset_are_disjoint(self.g.rows[i], current_set):
239
+
240
+ # Saving that set
241
+ bitset_copy(tmp, current_set)
242
+
243
+ # Preparing for the next set, except if we set the last bit.
244
+ if i < self.n - 1:
245
+
246
+ # Adding (i+1)th bit
247
+ bitset_add(current_set, i + 1)
248
+ i += 1
249
+ else:
250
+ bitset_discard(current_set, i)
251
+
252
+ # Returning the result if necessary ...
253
+ if self.maximal and not ismaximal(self.g, self.n, tmp):
254
+ continue
255
+
256
+ count += 1
257
+
258
+ if not self.count_only:
259
+ yield [self.vertices[j] for j in range(i + 1) if bitset_in(tmp, j)]
260
+ continue
261
+
262
+ else:
263
+ # Removing the ith bit
264
+ bitset_discard(current_set, i)
265
+
266
+ # Preparing for the next set !
267
+ if i < self.n - 1:
268
+ bitset_add(current_set, i + 1)
269
+ i += 1
270
+
271
+ # Not already included in the set
272
+ else:
273
+ if not i:
274
+ break
275
+
276
+ # Going backward, we explored all we could there !
277
+ if bitset_in(current_set, i - 1):
278
+ bitset_discard(current_set, i - 1)
279
+ bitset_add(current_set, i)
280
+ else:
281
+ i -= 1
282
+
283
+ if not self.maximal:
284
+ count += 1
285
+ if not self.count_only:
286
+ yield []
287
+
288
+ if self.count_only:
289
+ yield count
290
+
291
+ finally:
292
+ bitset_free(current_set)
293
+ bitset_free(tmp)
294
+
295
+ def __dealloc__(self):
296
+ r"""
297
+ Frees everything we ever allocated
298
+ """
299
+ if self.g.rows:
300
+ binary_matrix_free(self.g)
301
+
302
+ @cached_method
303
+ def cardinality(self):
304
+ r"""
305
+ Compute and return the number of independent sets.
306
+
307
+ TESTS::
308
+
309
+ sage: from sage.graphs.independent_sets import IndependentSets
310
+ sage: IndependentSets(graphs.PetersenGraph()).cardinality()
311
+ 76
312
+
313
+ Only maximal ones::
314
+
315
+ sage: from sage.graphs.independent_sets import IndependentSets
316
+ sage: IndependentSets(graphs.PetersenGraph(), maximal=True).cardinality()
317
+ 15
318
+ """
319
+ if not self.n:
320
+ return 1
321
+
322
+ self.count_only = 1
323
+
324
+ for i in self:
325
+ pass
326
+
327
+ self.count_only = 0
328
+
329
+ from sage.rings.integer import Integer
330
+ return Integer(i)
331
+
332
+ def __contains__(self, S):
333
+ r"""
334
+ Check whether the set is an independent set (possibly maximal).
335
+
336
+ INPUT:
337
+
338
+ - ``S`` -- set of vertices to be tested
339
+
340
+ TESTS:
341
+
342
+ All independent sets of PetersenGraph are... independent sets::
343
+
344
+ sage: from sage.graphs.independent_sets import IndependentSets
345
+ sage: G = graphs.PetersenGraph()
346
+ sage: IS = IndependentSets(graphs.PetersenGraph())
347
+ sage: all(s in IS for s in IS)
348
+ True
349
+
350
+ And only them are::
351
+
352
+ sage: IS2 = [x for x in subsets(G) if x in IS]
353
+ sage: sorted(IS) == sorted(IS2)
354
+ True
355
+
356
+ Same with maximal independent sets::
357
+
358
+ sage: IS = IndependentSets(graphs.PetersenGraph(), maximal=True)
359
+ sage: S = Subsets(G)
360
+ sage: all(s in IS for s in IS)
361
+ True
362
+ sage: IS2 = [x for x in subsets(G) if x in IS]
363
+ sage: sorted(IS) == sorted(IS2)
364
+ True
365
+
366
+ Check that the empty graph is dealt with correctly::
367
+
368
+ sage: IS = IndependentSets(Graph())
369
+ sage: [] in IS
370
+ True
371
+ """
372
+ if not self.n:
373
+ return not S
374
+
375
+ cdef int i
376
+ # Set of vertices as a bitset
377
+ cdef bitset_t s
378
+ bitset_init(s, self.n)
379
+
380
+ try:
381
+ bitset_set_first_n(s, 0)
382
+
383
+ for v in S:
384
+ try:
385
+ i = self.vertex_to_int[v]
386
+ except KeyError:
387
+ raise ValueError(str(v) + " is not a vertex of the graph")
388
+
389
+ # Adding the new vertex to s
390
+ bitset_add(s, i)
391
+
392
+ # Checking that the set s is independent
393
+ if not bitset_are_disjoint(self.g.rows[i], s):
394
+ return False
395
+
396
+ if self.maximal and not ismaximal(self.g, self.n, s):
397
+ return False
398
+
399
+ return True
400
+
401
+ finally:
402
+ bitset_free(s)