passagemath-graphs 10.5.43__cp39-cp39-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 (258) hide show
  1. passagemath_graphs-10.5.43.dist-info/METADATA +293 -0
  2. passagemath_graphs-10.5.43.dist-info/RECORD +258 -0
  3. passagemath_graphs-10.5.43.dist-info/WHEEL +5 -0
  4. passagemath_graphs-10.5.43.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 +2552 -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 +125 -0
  15. sage/combinat/cluster_algebra_quiver/mutation_class.py +625 -0
  16. sage/combinat/cluster_algebra_quiver/mutation_type.py +1556 -0
  17. sage/combinat/cluster_algebra_quiver/quiver.py +2262 -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 +534 -0
  25. sage/combinat/designs/database.py +5614 -0
  26. sage/combinat/designs/design_catalog.py +122 -0
  27. sage/combinat/designs/designs_pyx.cpython-39-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-39-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-39-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 +548 -0
  40. sage/combinat/designs/orthogonal_arrays.py +2243 -0
  41. sage/combinat/designs/orthogonal_arrays_build_recursive.py +1780 -0
  42. sage/combinat/designs/orthogonal_arrays_find_recursive.cpython-39-aarch64-linux-gnu.so +0 -0
  43. sage/combinat/designs/orthogonal_arrays_find_recursive.pyx +966 -0
  44. sage/combinat/designs/resolvable_bibd.py +781 -0
  45. sage/combinat/designs/steiner_quadruple_systems.py +1306 -0
  46. sage/combinat/designs/subhypergraph_search.cpython-39-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/cartesian_product.py +493 -0
  57. sage/combinat/posets/d_complete.py +182 -0
  58. sage/combinat/posets/elements.py +273 -0
  59. sage/combinat/posets/forest.py +30 -0
  60. sage/combinat/posets/hasse_cython.cpython-39-aarch64-linux-gnu.so +0 -0
  61. sage/combinat/posets/hasse_cython.pyx +174 -0
  62. sage/combinat/posets/hasse_diagram.py +3678 -0
  63. sage/combinat/posets/incidence_algebras.py +796 -0
  64. sage/combinat/posets/lattices.py +5119 -0
  65. sage/combinat/posets/linear_extension_iterator.cpython-39-aarch64-linux-gnu.so +0 -0
  66. sage/combinat/posets/linear_extension_iterator.pyx +292 -0
  67. sage/combinat/posets/linear_extensions.py +1039 -0
  68. sage/combinat/posets/mobile.py +275 -0
  69. sage/combinat/posets/moebius_algebra.py +776 -0
  70. sage/combinat/posets/poset_examples.py +2131 -0
  71. sage/combinat/posets/posets.py +9169 -0
  72. sage/combinat/rooted_tree.py +1070 -0
  73. sage/combinat/shard_order.py +239 -0
  74. sage/combinat/tamari_lattices.py +384 -0
  75. sage/combinat/yang_baxter_graph.py +923 -0
  76. sage/databases/all__sagemath_graphs.py +1 -0
  77. sage/databases/knotinfo_db.py +1230 -0
  78. sage/ext_data/all__sagemath_graphs.py +1 -0
  79. sage/ext_data/graphs/graph_plot_js.html +330 -0
  80. sage/ext_data/kenzo/CP2.txt +45 -0
  81. sage/ext_data/kenzo/CP3.txt +349 -0
  82. sage/ext_data/kenzo/CP4.txt +4774 -0
  83. sage/ext_data/kenzo/README.txt +49 -0
  84. sage/ext_data/kenzo/S4.txt +20 -0
  85. sage/graphs/all.py +42 -0
  86. sage/graphs/asteroidal_triples.cpython-39-aarch64-linux-gnu.so +0 -0
  87. sage/graphs/asteroidal_triples.pyx +299 -0
  88. sage/graphs/base/all.py +1 -0
  89. sage/graphs/base/boost_graph.cpython-39-aarch64-linux-gnu.so +0 -0
  90. sage/graphs/base/boost_graph.pxd +106 -0
  91. sage/graphs/base/boost_graph.pyx +3045 -0
  92. sage/graphs/base/c_graph.cpython-39-aarch64-linux-gnu.so +0 -0
  93. sage/graphs/base/c_graph.pxd +106 -0
  94. sage/graphs/base/c_graph.pyx +5096 -0
  95. sage/graphs/base/dense_graph.cpython-39-aarch64-linux-gnu.so +0 -0
  96. sage/graphs/base/dense_graph.pxd +26 -0
  97. sage/graphs/base/dense_graph.pyx +757 -0
  98. sage/graphs/base/graph_backends.cpython-39-aarch64-linux-gnu.so +0 -0
  99. sage/graphs/base/graph_backends.pxd +5 -0
  100. sage/graphs/base/graph_backends.pyx +797 -0
  101. sage/graphs/base/overview.py +85 -0
  102. sage/graphs/base/sparse_graph.cpython-39-aarch64-linux-gnu.so +0 -0
  103. sage/graphs/base/sparse_graph.pxd +90 -0
  104. sage/graphs/base/sparse_graph.pyx +1653 -0
  105. sage/graphs/base/static_dense_graph.cpython-39-aarch64-linux-gnu.so +0 -0
  106. sage/graphs/base/static_dense_graph.pxd +5 -0
  107. sage/graphs/base/static_dense_graph.pyx +1032 -0
  108. sage/graphs/base/static_sparse_backend.cpython-39-aarch64-linux-gnu.so +0 -0
  109. sage/graphs/base/static_sparse_backend.pxd +27 -0
  110. sage/graphs/base/static_sparse_backend.pyx +1580 -0
  111. sage/graphs/base/static_sparse_graph.cpython-39-aarch64-linux-gnu.so +0 -0
  112. sage/graphs/base/static_sparse_graph.pxd +37 -0
  113. sage/graphs/base/static_sparse_graph.pyx +1304 -0
  114. sage/graphs/bipartite_graph.py +2709 -0
  115. sage/graphs/centrality.cpython-39-aarch64-linux-gnu.so +0 -0
  116. sage/graphs/centrality.pyx +965 -0
  117. sage/graphs/cographs.py +519 -0
  118. sage/graphs/comparability.cpython-39-aarch64-linux-gnu.so +0 -0
  119. sage/graphs/comparability.pyx +813 -0
  120. sage/graphs/connectivity.cpython-39-aarch64-linux-gnu.so +0 -0
  121. sage/graphs/connectivity.pxd +157 -0
  122. sage/graphs/connectivity.pyx +4813 -0
  123. sage/graphs/convexity_properties.cpython-39-aarch64-linux-gnu.so +0 -0
  124. sage/graphs/convexity_properties.pxd +16 -0
  125. sage/graphs/convexity_properties.pyx +827 -0
  126. sage/graphs/digraph.py +4410 -0
  127. sage/graphs/digraph_generators.py +1921 -0
  128. sage/graphs/distances_all_pairs.cpython-39-aarch64-linux-gnu.so +0 -0
  129. sage/graphs/distances_all_pairs.pxd +12 -0
  130. sage/graphs/distances_all_pairs.pyx +2938 -0
  131. sage/graphs/domination.py +1363 -0
  132. sage/graphs/dot2tex_utils.py +100 -0
  133. sage/graphs/edge_connectivity.cpython-39-aarch64-linux-gnu.so +0 -0
  134. sage/graphs/edge_connectivity.pyx +1215 -0
  135. sage/graphs/generators/all.py +1 -0
  136. sage/graphs/generators/basic.py +1769 -0
  137. sage/graphs/generators/chessboard.py +538 -0
  138. sage/graphs/generators/classical_geometries.py +1611 -0
  139. sage/graphs/generators/degree_sequence.py +235 -0
  140. sage/graphs/generators/distance_regular.cpython-39-aarch64-linux-gnu.so +0 -0
  141. sage/graphs/generators/distance_regular.pyx +2846 -0
  142. sage/graphs/generators/families.py +4749 -0
  143. sage/graphs/generators/intersection.py +565 -0
  144. sage/graphs/generators/platonic_solids.py +262 -0
  145. sage/graphs/generators/random.py +2623 -0
  146. sage/graphs/generators/smallgraphs.py +5741 -0
  147. sage/graphs/generators/world_map.py +724 -0
  148. sage/graphs/generic_graph.py +26395 -0
  149. sage/graphs/generic_graph_pyx.cpython-39-aarch64-linux-gnu.so +0 -0
  150. sage/graphs/generic_graph_pyx.pxd +34 -0
  151. sage/graphs/generic_graph_pyx.pyx +1626 -0
  152. sage/graphs/genus.cpython-39-aarch64-linux-gnu.so +0 -0
  153. sage/graphs/genus.pyx +623 -0
  154. sage/graphs/graph.py +9362 -0
  155. sage/graphs/graph_coloring.cpython-39-aarch64-linux-gnu.so +0 -0
  156. sage/graphs/graph_coloring.pyx +2284 -0
  157. sage/graphs/graph_database.py +1122 -0
  158. sage/graphs/graph_decompositions/all.py +1 -0
  159. sage/graphs/graph_decompositions/bandwidth.cpython-39-aarch64-linux-gnu.so +0 -0
  160. sage/graphs/graph_decompositions/bandwidth.pyx +428 -0
  161. sage/graphs/graph_decompositions/clique_separators.cpython-39-aarch64-linux-gnu.so +0 -0
  162. sage/graphs/graph_decompositions/clique_separators.pyx +595 -0
  163. sage/graphs/graph_decompositions/cutwidth.cpython-39-aarch64-linux-gnu.so +0 -0
  164. sage/graphs/graph_decompositions/cutwidth.pyx +753 -0
  165. sage/graphs/graph_decompositions/fast_digraph.cpython-39-aarch64-linux-gnu.so +0 -0
  166. sage/graphs/graph_decompositions/fast_digraph.pxd +13 -0
  167. sage/graphs/graph_decompositions/fast_digraph.pyx +212 -0
  168. sage/graphs/graph_decompositions/graph_products.cpython-39-aarch64-linux-gnu.so +0 -0
  169. sage/graphs/graph_decompositions/graph_products.pyx +462 -0
  170. sage/graphs/graph_decompositions/modular_decomposition.cpython-39-aarch64-linux-gnu.so +0 -0
  171. sage/graphs/graph_decompositions/modular_decomposition.pxd +27 -0
  172. sage/graphs/graph_decompositions/modular_decomposition.pyx +1536 -0
  173. sage/graphs/graph_decompositions/slice_decomposition.cpython-39-aarch64-linux-gnu.so +0 -0
  174. sage/graphs/graph_decompositions/slice_decomposition.pxd +18 -0
  175. sage/graphs/graph_decompositions/slice_decomposition.pyx +1080 -0
  176. sage/graphs/graph_decompositions/tree_decomposition.cpython-39-aarch64-linux-gnu.so +0 -0
  177. sage/graphs/graph_decompositions/tree_decomposition.pxd +17 -0
  178. sage/graphs/graph_decompositions/tree_decomposition.pyx +1996 -0
  179. sage/graphs/graph_decompositions/vertex_separation.cpython-39-aarch64-linux-gnu.so +0 -0
  180. sage/graphs/graph_decompositions/vertex_separation.pxd +5 -0
  181. sage/graphs/graph_decompositions/vertex_separation.pyx +1963 -0
  182. sage/graphs/graph_editor.py +82 -0
  183. sage/graphs/graph_generators.py +3301 -0
  184. sage/graphs/graph_generators_pyx.cpython-39-aarch64-linux-gnu.so +0 -0
  185. sage/graphs/graph_generators_pyx.pyx +95 -0
  186. sage/graphs/graph_input.py +812 -0
  187. sage/graphs/graph_latex.py +2064 -0
  188. sage/graphs/graph_list.py +367 -0
  189. sage/graphs/graph_plot.py +1749 -0
  190. sage/graphs/graph_plot_js.py +338 -0
  191. sage/graphs/hyperbolicity.cpython-39-aarch64-linux-gnu.so +0 -0
  192. sage/graphs/hyperbolicity.pyx +1702 -0
  193. sage/graphs/hypergraph_generators.py +364 -0
  194. sage/graphs/independent_sets.cpython-39-aarch64-linux-gnu.so +0 -0
  195. sage/graphs/independent_sets.pxd +13 -0
  196. sage/graphs/independent_sets.pyx +402 -0
  197. sage/graphs/isgci.py +1033 -0
  198. sage/graphs/isoperimetric_inequalities.cpython-39-aarch64-linux-gnu.so +0 -0
  199. sage/graphs/isoperimetric_inequalities.pyx +453 -0
  200. sage/graphs/line_graph.cpython-39-aarch64-linux-gnu.so +0 -0
  201. sage/graphs/line_graph.pyx +627 -0
  202. sage/graphs/lovasz_theta.py +77 -0
  203. sage/graphs/matching.py +1633 -0
  204. sage/graphs/matching_covered_graph.py +3566 -0
  205. sage/graphs/orientations.py +1504 -0
  206. sage/graphs/partial_cube.py +459 -0
  207. sage/graphs/path_enumeration.cpython-39-aarch64-linux-gnu.so +0 -0
  208. sage/graphs/path_enumeration.pyx +2040 -0
  209. sage/graphs/pq_trees.py +1129 -0
  210. sage/graphs/print_graphs.py +201 -0
  211. sage/graphs/schnyder.py +865 -0
  212. sage/graphs/spanning_tree.cpython-39-aarch64-linux-gnu.so +0 -0
  213. sage/graphs/spanning_tree.pyx +1457 -0
  214. sage/graphs/strongly_regular_db.cpython-39-aarch64-linux-gnu.so +0 -0
  215. sage/graphs/strongly_regular_db.pyx +3340 -0
  216. sage/graphs/traversals.cpython-39-aarch64-linux-gnu.so +0 -0
  217. sage/graphs/traversals.pxd +9 -0
  218. sage/graphs/traversals.pyx +1871 -0
  219. sage/graphs/trees.cpython-39-aarch64-linux-gnu.so +0 -0
  220. sage/graphs/trees.pxd +15 -0
  221. sage/graphs/trees.pyx +310 -0
  222. sage/graphs/tutte_polynomial.py +713 -0
  223. sage/graphs/views.cpython-39-aarch64-linux-gnu.so +0 -0
  224. sage/graphs/views.pyx +794 -0
  225. sage/graphs/weakly_chordal.cpython-39-aarch64-linux-gnu.so +0 -0
  226. sage/graphs/weakly_chordal.pyx +562 -0
  227. sage/groups/all__sagemath_graphs.py +1 -0
  228. sage/groups/perm_gps/all__sagemath_graphs.py +1 -0
  229. sage/groups/perm_gps/partn_ref/all__sagemath_graphs.py +1 -0
  230. sage/groups/perm_gps/partn_ref/refinement_graphs.cpython-39-aarch64-linux-gnu.so +0 -0
  231. sage/groups/perm_gps/partn_ref/refinement_graphs.pxd +38 -0
  232. sage/groups/perm_gps/partn_ref/refinement_graphs.pyx +1666 -0
  233. sage/knots/all.py +6 -0
  234. sage/knots/free_knotinfo_monoid.py +507 -0
  235. sage/knots/gauss_code.py +291 -0
  236. sage/knots/knot.py +682 -0
  237. sage/knots/knot_table.py +284 -0
  238. sage/knots/knotinfo.py +2880 -0
  239. sage/knots/link.py +4682 -0
  240. sage/sandpiles/all.py +13 -0
  241. sage/sandpiles/examples.py +225 -0
  242. sage/sandpiles/sandpile.py +6365 -0
  243. sage/topology/all.py +22 -0
  244. sage/topology/cell_complex.py +1214 -0
  245. sage/topology/cubical_complex.py +1977 -0
  246. sage/topology/delta_complex.py +1806 -0
  247. sage/topology/filtered_simplicial_complex.py +744 -0
  248. sage/topology/moment_angle_complex.py +823 -0
  249. sage/topology/simplicial_complex.py +5161 -0
  250. sage/topology/simplicial_complex_catalog.py +86 -0
  251. sage/topology/simplicial_complex_examples.py +1692 -0
  252. sage/topology/simplicial_complex_homset.py +205 -0
  253. sage/topology/simplicial_complex_morphism.py +836 -0
  254. sage/topology/simplicial_set.py +4102 -0
  255. sage/topology/simplicial_set_catalog.py +55 -0
  256. sage/topology/simplicial_set_constructions.py +2954 -0
  257. sage/topology/simplicial_set_examples.py +865 -0
  258. sage/topology/simplicial_set_morphism.py +1464 -0
@@ -0,0 +1,1626 @@
1
+ # sage_setup: distribution = sagemath-graphs
2
+ """
3
+ GenericGraph Cython functions
4
+
5
+ AUTHORS:
6
+
7
+ - Robert L. Miller (2007-02-13): initial version
8
+ - Robert W. Bradshaw (2007-03-31): fast spring layout algorithms
9
+ - Nathann Cohen : exhaustive search
10
+ """
11
+
12
+ # ****************************************************************************
13
+ # Copyright (C) 2007 Robert L. Miller <rlmillster@gmail.com>
14
+ # 2007 Robert W. Bradshaw <robertwb@math.washington.edu>
15
+ #
16
+ # This program is free software: you can redistribute it and/or modify
17
+ # it under the terms of the GNU General Public License as published by
18
+ # the Free Software Foundation, either version 2 of the License, or
19
+ # (at your option) any later version.
20
+ # http://www.gnu.org/licenses/
21
+ # ****************************************************************************
22
+
23
+ from cysignals.memory cimport check_allocarray, check_calloc, sig_free
24
+ from cysignals.signals cimport sig_on, sig_off
25
+
26
+ import cython
27
+
28
+ from sage.data_structures.binary_matrix cimport *
29
+ from libc.math cimport sqrt, fabs
30
+ from libc.string cimport memset
31
+ from memory_allocator cimport MemoryAllocator
32
+
33
+ from sage.cpython.string cimport char_to_str
34
+ from sage.libs.gmp.mpz cimport *
35
+ from sage.misc.prandom import random
36
+ from sage.graphs.base.static_sparse_graph cimport short_digraph
37
+ from sage.graphs.base.static_sparse_graph cimport init_short_digraph
38
+ from sage.graphs.base.static_sparse_graph cimport init_reverse
39
+ from sage.graphs.base.static_sparse_graph cimport free_short_digraph
40
+ from sage.graphs.base.static_sparse_graph cimport out_degree, has_edge
41
+
42
+
43
+ cdef class GenericGraph_pyx(SageObject):
44
+ pass
45
+
46
+
47
+ def layout_split(layout_function, G, **options):
48
+ """
49
+ Graph each component of ``G`` separately with ``layout_function``,
50
+ placing them adjacent to each other.
51
+
52
+ This is done because several layout methods need the input graph to
53
+ be connected. For instance, on a disconnected graph, the spring
54
+ layout will push components further and further from each other
55
+ without bound, resulting in very tight clumps for each component.
56
+
57
+ .. NOTE::
58
+
59
+ If the axis are scaled to fit the plot in a square, the
60
+ horizontal distance may end up being "squished" due to
61
+ the several adjacent components.
62
+
63
+ EXAMPLES::
64
+
65
+ sage: G = graphs.DodecahedralGraph()
66
+ sage: for i in range(10): G.add_cycle(list(range(100*i, 100*i+3)))
67
+ sage: from sage.graphs.generic_graph_pyx import layout_split, spring_layout_fast
68
+ sage: D = layout_split(spring_layout_fast, G); D # random
69
+ {0: [0.77..., 0.06...],
70
+ ...
71
+ 902: [3.13..., 0.22...]}
72
+
73
+ AUTHOR:
74
+
75
+ Robert Bradshaw
76
+ """
77
+ from copy import copy
78
+ Gs = G.connected_components_subgraphs()
79
+ pos = {}
80
+ left = 0
81
+ buffer = 1/sqrt(len(G))
82
+
83
+ on_embedding = options.get('on_embedding', None)
84
+ forest_roots = options.get('forest_roots', None)
85
+ try:
86
+ forest_roots = list(forest_roots) if forest_roots else None
87
+ except TypeError:
88
+ raise TypeError('forest_roots should be an iterable of vertices')
89
+
90
+ if forest_roots or on_embedding:
91
+ options = copy(options)
92
+ options.pop('forest_roots', None)
93
+ options.pop('on_embedding', None)
94
+
95
+ for g in Gs:
96
+ if on_embedding:
97
+ # Restrict ``on_embedding`` to ``g``
98
+ embedding_g = {v: on_embedding[v] for v in g}
99
+ cur_pos = layout_function(g, on_embedding=embedding_g, **options)
100
+ elif forest_roots:
101
+ # Find a root for ``g`` (if any)
102
+ tree_root = next((v for v in forest_roots if v in g), None)
103
+ cur_pos = layout_function(g, tree_root=tree_root, **options)
104
+ else:
105
+ cur_pos = layout_function(g, **options)
106
+
107
+ xmin = min(x[0] for x in cur_pos.values())
108
+ xmax = max(x[0] for x in cur_pos.values())
109
+ if len(g) > 1:
110
+ buffer = max(1, (xmax - xmin)/sqrt(len(g)))
111
+ for v, loc in cur_pos.items():
112
+ loc[0] += left - xmin + buffer
113
+ pos[v] = loc
114
+ left += xmax - xmin + buffer
115
+
116
+ if options.get('set_embedding', None):
117
+ embedding = dict()
118
+ for g in Gs:
119
+ embedding.update(g.get_embedding())
120
+ G.set_embedding(embedding)
121
+ return pos
122
+
123
+
124
+ def spring_layout_fast(G, iterations=50, int dim=2, vpos=None, bint rescale=True,
125
+ bint height=False, by_component=False, **options):
126
+ """
127
+ Spring force model layout.
128
+
129
+ This function primarily acts as a wrapper around :func:`run_spring`,
130
+ converting to and from raw C types.
131
+
132
+ This kind of speed cannot be achieved by naive Cythonification of the
133
+ function alone, especially if we require a function call (let alone
134
+ an object creation) every time we want to add a pair of doubles.
135
+
136
+ INPUT:
137
+
138
+ - ``by_component`` -- boolean
139
+
140
+ EXAMPLES::
141
+
142
+ sage: G = graphs.DodecahedralGraph()
143
+ sage: for i in range(10): G.add_cycle(list(range(100*i, 100*i+3)))
144
+ sage: from sage.graphs.generic_graph_pyx import spring_layout_fast
145
+ sage: pos = spring_layout_fast(G)
146
+ sage: pos[0] # random
147
+ [0.00..., 0.03...]
148
+ sage: sorted(pos.keys()) == sorted(G)
149
+ True
150
+
151
+ With ``split=True``, each component of G is laid out separately,
152
+ placing them adjacent to each other. This is done because on a
153
+ disconnected graph, the spring layout will push components further
154
+ and further from each other without bound, resulting in very tight
155
+ clumps for each component.
156
+
157
+ If the axis are scaled to fit the plot in a square, the
158
+ horizontal distance may end up being "squished" due to
159
+ the several adjacent components. ::
160
+
161
+ sage: G = graphs.DodecahedralGraph()
162
+ sage: for i in range(10): G.add_cycle(list(range(100*i, 100*i+3)))
163
+ sage: from sage.graphs.generic_graph_pyx import spring_layout_fast
164
+ sage: pos = spring_layout_fast(G, by_component = True)
165
+ sage: pos[0] # random
166
+ [2.21..., -0.00...]
167
+ sage: len(pos) == G.order()
168
+ True
169
+ """
170
+ if by_component:
171
+ return layout_split(spring_layout_fast, G, iterations=iterations,
172
+ dim=dim, vpos=vpos, rescale=rescale,
173
+ height=height, **options)
174
+ elif not G:
175
+ return {}
176
+
177
+ G = G.to_undirected()
178
+ cdef list vlist = list(G) # this defines a consistent order
179
+
180
+ cdef int i, j, x
181
+ cdef int n = G.order()
182
+
183
+ cdef double* pos = NULL # position of each vertex (for dim=2: x1,y1,x2,y2,...)
184
+ cdef int* elist = NULL # lexicographically ordered list of edges (u1,v1,u2,v2,...)
185
+ cdef double* cen = NULL # array of 'dim' doubles
186
+ try:
187
+ elist = <int*>check_allocarray(2*G.size() + 2, sizeof(int))
188
+ pos = <double*>check_allocarray(n*dim, sizeof(double))
189
+ cen = <double*>check_calloc(dim, sizeof(double))
190
+ except MemoryError:
191
+ sig_free(pos)
192
+ sig_free(elist)
193
+ sig_free(cen)
194
+ raise
195
+
196
+ # Initialize the starting positions
197
+ if vpos is None:
198
+ for i in range(n*dim):
199
+ pos[i] = random() # random in 1x1 box
200
+ else:
201
+ for i in range(n):
202
+ loc = vpos[vlist[i]]
203
+ for x in range(dim):
204
+ pos[i*dim + x] = loc[x]
205
+
206
+ # Lexicographically ordered list of edges
207
+ cdef int cur_edge = 0
208
+
209
+ for i in range(n):
210
+ for j in range(i+1, n):
211
+ if G.has_edge(vlist[i], vlist[j]):
212
+ elist[cur_edge] = i
213
+ elist[cur_edge+1] = j
214
+ cur_edge += 2
215
+
216
+ # finish the list with -1, -1 which never gets matched
217
+ # but does get compared against when looking for the "next" edge
218
+ elist[cur_edge] = -1
219
+ elist[cur_edge+1] = -1
220
+
221
+ if dim == 2:
222
+ run_spring(<int> iterations, <D_TWO> NULL, <double*> pos, <int*>elist, <int> n, <int> G.size(), <bint> height)
223
+ elif dim == 3:
224
+ run_spring(<int> iterations, <D_THREE> NULL, <double*> pos, <int*>elist, <int> n, <int> G.size(), <bint> height)
225
+ else:
226
+ raise ValueError("'dim' must be equal to 2 or 3")
227
+
228
+ # recenter
229
+ cdef double r, r2, max_r2 = 0
230
+ if rescale:
231
+ for i in range(n):
232
+ for x in range(dim):
233
+ cen[x] += pos[i*dim + x]
234
+ for x in range(dim):
235
+ cen[x] /= n
236
+ for i in range(n):
237
+ r2 = 0
238
+ for x in range(dim):
239
+ pos[i*dim + x] -= cen[x]
240
+ r2 += pos[i*dim + x] * pos[i*dim + x]
241
+ if r2 > max_r2:
242
+ max_r2 = r2
243
+ r = 1 if max_r2 == 0 else sqrt(max_r2)
244
+ for i in range(n):
245
+ for x in range(dim):
246
+ pos[i*dim + x] /= r
247
+
248
+ # put the data back into a position dictionary
249
+ vpos = {}
250
+ for i in range(n):
251
+ vpos[vlist[i]] = [pos[i*dim + x] for x in range(dim)]
252
+
253
+ sig_free(pos)
254
+ sig_free(elist)
255
+ sig_free(cen)
256
+
257
+ return vpos
258
+
259
+
260
+ @cython.cdivision(True)
261
+ cdef run_spring(int iterations, dimension_t _dim, double* pos, int* edges, int n, int m, bint height):
262
+ r"""
263
+ Find a locally optimal layout for this graph, according to the
264
+ constraints that neighboring nodes want to be a fixed distance
265
+ from each other, and non-neighboring nodes always repel.
266
+
267
+ This is not a true physical model of mutually-repulsive particles
268
+ with springs, rather it is more a model of such things traveling,
269
+ without any inertia, through an (ever thickening) fluid.
270
+
271
+ TODO: The inertial model could be incorporated (with F=ma)
272
+ TODO: Are the hard-coded constants here optimal?
273
+
274
+ INPUT:
275
+
276
+ - ``iterations`` -- number of steps to take
277
+ - ``_dim`` -- number of dimensions of freedom. Provide a value of type `D_TWO`
278
+ for 2 dimensions, or type `D_THREE` for three dimensions. The actual
279
+ value does not matter: only its type is important.
280
+ - ``pos`` -- already initialized initial positions. Each vertex is stored as
281
+ [dim] consecutive doubles. These doubles are then placed consecutively
282
+ in the array. For example, if dim=3, we would have
283
+ pos = [x_1, y_1, z_1, x_2, y_2, z_2, ... , x_n, y_n, z_n]
284
+ - ``edges`` -- List of edges, sorted lexicographically by the first (smallest)
285
+ vertex, terminated by -1, -1. The first two entries represent the first
286
+ edge, and so on.
287
+ - ``n`` -- number of vertices in the graph
288
+ - ``height`` -- if ``True``, do not update the last coordinate ever
289
+
290
+ OUTPUT: modifies contents of pos
291
+
292
+ AUTHOR:
293
+
294
+ Robert Bradshaw
295
+ """
296
+ cdef int dim
297
+ cdef int cur_iter, cur_edge
298
+ cdef int i, j, x
299
+
300
+ if dimension_t is D_TWO:
301
+ dim = 2
302
+ else:
303
+ dim = 3
304
+
305
+ # k -- the equilibrium distance between two adjacent nodes
306
+ cdef double t = 1, dt = t/(1e-20 + iterations), k = sqrt(1.0/n)
307
+ cdef double square_dist, dist, force, scale
308
+ cdef double* disp_i
309
+ cdef double* disp_j
310
+ cdef double delta[3]
311
+ cdef double d_tmp
312
+ cdef double xx, yy, zz
313
+
314
+ cdef double* disp = <double*>check_allocarray(n, dim * sizeof(double))
315
+
316
+ if height:
317
+ update_dim = dim - 1
318
+ else:
319
+ update_dim = dim
320
+
321
+ sig_on()
322
+
323
+ for cur_iter in range(iterations):
324
+ cur_edge = 1 # offset by one for fast checking against 2nd element first
325
+ # zero out the disp vectors
326
+ memset(disp, 0, n * dim * sizeof(double))
327
+ for i in range(n):
328
+ disp_i = disp + (i*dim)
329
+ for j in range(i + 1, n):
330
+ disp_j = disp + (j*dim)
331
+
332
+ for x in range(dim):
333
+ delta[x] = pos[i*dim + x] - pos[j*dim + x]
334
+
335
+ xx = delta[0] * delta[0]
336
+ yy = delta[1] * delta[1]
337
+ if dim == 2:
338
+ square_dist = xx+yy
339
+ else:
340
+ zz = delta[2] * delta[2]
341
+ square_dist = xx+yy+zz
342
+
343
+ if square_dist < 0.0001:
344
+ square_dist = 0.0001
345
+
346
+ # they repel according to the (capped) inverse square law
347
+ force = (k*k)/square_dist
348
+
349
+ # and if they are neighbors, attract according Hooke's law
350
+ if edges[cur_edge] == j and edges[cur_edge - 1] == i:
351
+ if dim == 2:
352
+ dist = sqrt_approx(delta[0], delta[1], xx, yy)
353
+ else:
354
+ dist = sqrt(square_dist)
355
+ force -= dist/k
356
+ cur_edge += 2
357
+
358
+ # add this factor into each of the involved points
359
+ for x in range(dim):
360
+ d_tmp = delta[x] * force
361
+ disp_i[x] += d_tmp
362
+ disp_j[x] -= d_tmp
363
+
364
+ # now update the positions
365
+ for i in range(n):
366
+ disp_i = disp + (i*dim)
367
+
368
+ square_dist = disp_i[0] * disp_i[0]
369
+ for x in range(1, dim):
370
+ square_dist += disp_i[x] * disp_i[x]
371
+
372
+ if square_dist < 0.0001:
373
+ scale = 1
374
+ else:
375
+ scale = t/sqrt(square_dist)
376
+
377
+ for x in range(update_dim):
378
+ pos[i*dim+x] += disp_i[x] * scale
379
+
380
+ t -= dt
381
+
382
+ sig_off()
383
+ sig_free(disp)
384
+
385
+
386
+ @cython.cdivision(True)
387
+ cdef inline double sqrt_approx(double x, double y, double xx, double yy) noexcept:
388
+ r"""
389
+ Approximation of `\sqrt(x^2+y^2)`.
390
+
391
+ Assuming that `x > y > 0`, it is a taylor expansion at `x^2`. To see how
392
+ 'bad' the approximation is::
393
+
394
+ sage: def dist(x, y):
395
+ ....: x = abs(x)
396
+ ....: y = abs(y)
397
+ ....: return max(x,y) + min(x,y)**2/(2*max(x,y))
398
+
399
+ sage: polar_plot([1, lambda x: dist(cos(x), sin(x))], (0, 2*math.pi)) # needs sage.plot sage.symbolic
400
+ Graphics object consisting of 2 graphics primitives
401
+ """
402
+ if xx < yy:
403
+ x, y = y, x
404
+ xx, yy = yy, xx
405
+
406
+ x = fabs(x)
407
+
408
+ return x + yy/(2*x)
409
+
410
+
411
+ def int_to_binary_string(n):
412
+ """
413
+ A quick python int to binary string conversion.
414
+
415
+ INPUT:
416
+
417
+ - ``n`` -- integer
418
+
419
+ EXAMPLES::
420
+
421
+ sage: sage.graphs.generic_graph_pyx.int_to_binary_string(389)
422
+ '110000101'
423
+ sage: Integer(389).binary()
424
+ '110000101'
425
+ sage: sage.graphs.generic_graph_pyx.int_to_binary_string(2007)
426
+ '11111010111'
427
+ """
428
+ cdef mpz_t i
429
+ cdef char* s
430
+ mpz_init(i)
431
+ mpz_set_ui(i, n)
432
+ s = mpz_get_str(NULL, 2, i)
433
+ t = char_to_str(s)
434
+ sig_free(s)
435
+ mpz_clear(i)
436
+ return t
437
+
438
+
439
+ def binary_string_to_graph6(x):
440
+ r"""
441
+ Transform a binary string into its graph6 representation.
442
+
443
+ This helper function is named `R` in [McK2015]_.
444
+
445
+ INPUT:
446
+
447
+ - ``x`` -- a binary string
448
+
449
+ EXAMPLES::
450
+
451
+ sage: from sage.graphs.generic_graph_pyx import binary_string_to_graph6
452
+ sage: binary_string_to_graph6('110111010110110010111000001100000001000000001')
453
+ 'vUqwK@?G'
454
+ """
455
+ # The length of x must be a multiple of 6. We extend it with 0s.
456
+ x += '0' * ((6 - (len(x) % 6)) % 6)
457
+
458
+ # Split into groups of 6, and convert numbers to decimal, adding 63
459
+ six_bits = ''
460
+ cdef int i
461
+ for i in range(len(x)/6):
462
+ six_bits += chr(int(x[6*i:6*(i + 1)], 2) + 63)
463
+ return six_bits
464
+
465
+
466
+ def small_integer_to_graph6(n):
467
+ r"""
468
+ Encode a small integer (i.e. a number of vertices) as a graph6 string.
469
+
470
+ This helper function is named `N` [McK2015]_.
471
+
472
+ INPUT:
473
+
474
+ - ``n`` -- integer
475
+
476
+ EXAMPLES::
477
+
478
+ sage: from sage.graphs.generic_graph_pyx import small_integer_to_graph6
479
+ sage: small_integer_to_graph6(13)
480
+ 'L'
481
+ sage: small_integer_to_graph6(136)
482
+ '~?AG'
483
+ """
484
+ if n < 63:
485
+ return chr(n + 63)
486
+ # get 18-bit rep of n
487
+ n = int_to_binary_string(n)
488
+ n = '0'*(18 - len(n)) + n
489
+ return chr(126) + binary_string_to_graph6(n)
490
+
491
+
492
+ def length_and_string_from_graph6(s):
493
+ r"""
494
+ Return a pair ``(length, graph6_string)`` from a graph6 string of unknown length.
495
+
496
+ This helper function is the inverse of `N` from [McK2015]_.
497
+
498
+ INPUT:
499
+
500
+ - ``s`` -- a graph6 string describing a binary vector (and encoding its
501
+ length)
502
+
503
+ EXAMPLES::
504
+
505
+ sage: from sage.graphs.generic_graph_pyx import length_and_string_from_graph6
506
+ sage: g6 = '~??~?????_@?CG??B??@OG?C?G???GO??W@a???CO???OACC?OA?P@G??O?'
507
+ sage: g6 += '?????G??C????c?G?CC?_?@???C_??_?C????PO?C_??AA?OOAHCA___?C'
508
+ sage: g6 += 'C?A?CAOGO??????A??G?GR?C?_o`???g???A_C?OG??O?G_IA????_QO@E'
509
+ sage: g6 += 'G???O??C?_?C@?G???@?_??AC?AO?a???O?????A?_Dw?H???__O@AAOAA'
510
+ sage: g6 += 'Cd?_C??G?G@??GO?_???O@?_O??W??@P???AG??B?????G??GG???A??@?'
511
+ sage: g6 += 'aC_G@A??O??_?A?????O@Z?_@M????GQ@_G@?C?'
512
+ sage: length_and_string_from_graph6(g6)
513
+ (63, '?????_@?CG??B??@OG?C?G???GO??W@a???CO???OACC?OA?P@G??O??????G??C????c?G?CC?_?@???C_??_?C????PO?C_??AA?OOAHCA___?CC?A?CAOGO??????A??G?GR?C?_o`???g???A_C?OG??O?G_IA????_QO@EG???O??C?_?C@?G???@?_??AC?AO?a???O?????A?_Dw?H???__O@AAOAACd?_C??G?G@??GO?_???O@?_O??W??@P???AG??B?????G??GG???A??@?aC_G@A??O??_?A?????O@Z?_@M????GQ@_G@?C?')
514
+ sage: g6 = '_???C?@AA?_?A?O?C??S??O?q_?P?CHD??@?C?GC???C??GG?C_??O?COG?'
515
+ sage: g6 += '???I?J??Q??O?_@@??@??????'
516
+ sage: length_and_string_from_graph6(g6)
517
+ (32, '???C?@AA?_?A?O?C??S??O?q_?P?CHD??@?C?GC???C??GG?C_??O?COG????I?J??Q??O?_@@??@??????')
518
+ """
519
+ if s[0] == chr(126): # first four bytes are N
520
+ a = int_to_binary_string(ord(s[1]) - 63).zfill(6)
521
+ b = int_to_binary_string(ord(s[2]) - 63).zfill(6)
522
+ c = int_to_binary_string(ord(s[3]) - 63).zfill(6)
523
+ n = int(a + b + c, 2)
524
+ s = s[4:]
525
+ else: # only first byte is N
526
+ o = ord(s[0])
527
+ if o > 126 or o < 63:
528
+ raise RuntimeError("the string seems corrupt: valid characters are \n" + ''.join(chr(i) for i in range(63, 127)))
529
+ n = o - 63
530
+ s = s[1:]
531
+ return n, s
532
+
533
+
534
+ def binary_string_from_graph6(s, n):
535
+ r"""
536
+ Decode a binary string from its graph6 representation.
537
+
538
+ This helper function is the inverse of `R` from [McK2015]_.
539
+
540
+ INPUT:
541
+
542
+ - ``s`` -- a graph6 string
543
+
544
+ - ``n`` -- the length of the binary string encoded by ``s``
545
+
546
+ EXAMPLES::
547
+
548
+ sage: from sage.graphs.generic_graph_pyx import binary_string_from_graph6
549
+ sage: g6 = '?????_@?CG??B??@OG?C?G???GO??W@a???CO???OACC?OA?P@G??O?????'
550
+ sage: g6 += '?G??C????c?G?CC?_?@???C_??_?C????PO?C_??AA?OOAHCA___?CC?A?'
551
+ sage: g6 += 'CAOGO??????A??G?GR?C?_o`???g???A_C?OG??O?G_IA????_QO@EG???'
552
+ sage: g6 += 'O??C?_?C@?G???@?_??AC?AO?a???O?????A?_Dw?H???__O@AAOAACd?_'
553
+ sage: g6 += 'C??G?G@??GO?_???O@?_O??W??@P???AG??B?????G??GG???A??@?aC_G'
554
+ sage: g6 += '@A??O??_?A?????O@Z?_@M????GQ@_G@?C?'
555
+ sage: binary_string_from_graph6(g6, 63)
556
+ '0000000000000000000000000000001000000000010000000001000010000000000000000000110000000000000000010100000010000000000001000000000010000000000...10000000000000000000000000000000010000000001011011000000100000000001001110000000000000000000000000001000010010000001100000001000000001000000000100000000'
557
+ sage: g6 = '???C?@AA?_?A?O?C??S??O?q_?P?CHD??@?C?GC???C??GG?C_??O?COG??'
558
+ sage: g6 += '??I?J??Q??O?_@@??@??????'
559
+ sage: binary_string_from_graph6(g6, 32)
560
+ '0000000000000000000001000000000000010000100000100000001000000000000000100000000100000...010000000000000100010000001000000000000000000000000000001010000000001011000000000000010010000000000000010000000000100000000001000001000000000000000001000000000000000000000000000000000000'
561
+ """
562
+ cdef list l = []
563
+ cdef int i
564
+ for i in range(len(s)):
565
+ o = ord(s[i])
566
+ if o > 126 or o < 63:
567
+ raise RuntimeError("the string seems corrupt: valid characters are \n" + ''.join(chr(i) for i in range(63, 127)))
568
+ a = int_to_binary_string(o - 63)
569
+ l.append('0'*(6 - len(a)) + a)
570
+ m = "".join(l)
571
+ return m
572
+
573
+
574
+ def binary_string_from_dig6(s, n):
575
+ """
576
+ A helper function for the dig6 format.
577
+
578
+ INPUT:
579
+
580
+ - ``s`` -- a graph6 string
581
+
582
+ - ``n`` -- the length of the binary string encoded by ``s``
583
+
584
+ EXAMPLES::
585
+
586
+ sage: from sage.graphs.generic_graph_pyx import binary_string_from_dig6
587
+ sage: d6 = '?????_@?CG??B??@OG?C?G???GO??W@a???CO???OACC?OA?P@G??O?????'
588
+ sage: d6 += '?G??C????c?G?CC?_?@???C_??_?C????PO?C_??AA?OOAHCA___?CC?A?'
589
+ sage: d6 += 'CAOGO??????A??G?GR?C?_o`???g???A_C?OG??O?G_IA????_QO@EG???'
590
+ sage: d6 += 'O??C?_?C@?G???@?_??AC?AO?a???O?????A?_Dw?H???__O@AAOAACd?_'
591
+ sage: d6 += 'C??G?G@??GO?_???O@?_O??W??@P???AG??B?????G??GG???A??@?aC_G'
592
+ sage: d6 += '@A??O??_?A?????O@Z?_@M????GQ@_G@?C?'
593
+ sage: binary_string_from_dig6(d6, 63)
594
+ '0000000000000000000000000000001000000000010000000001000010000000000000000000110000000000000000010100000010000000000001000000000010000000000...10000000000000000000000000000000010000000001011011000000100000000001001110000000000000000000000000001000010010000001100000001000000001000000000100000000'
595
+ sage: d6 = '???C?@AA?_?A?O?C??S??O?q_?P?CHD??@?C?GC???C??GG?C_??O?COG??'
596
+ sage: d6 += '??I?J??Q??O?_@@??@??????'
597
+ sage: binary_string_from_dig6(d6, 32)
598
+ '0000000000000000000001000000000000010000100000100000001000000000000000100000000100000...010000000000000100010000001000000000000000000000000000001010000000001011000000000000010010000000000000010000000000100000000001000001000000000000000001000000000000000000000000000000000000'
599
+ """
600
+ cdef list l = []
601
+ cdef int i
602
+ for i in range(len(s)):
603
+ o = ord(s[i])
604
+ if o > 126 or o < 63:
605
+ raise RuntimeError("the string seems corrupt: valid characters are \n" + ''.join(chr(i) for i in range(63, 127)))
606
+ a = int_to_binary_string(o - 63)
607
+ l.append('0'*(6 - len(a)) + a)
608
+ m = "".join(l)
609
+ return m[:n*n]
610
+
611
+
612
+ # Exhaustive search in graphs
613
+
614
+ cdef class SubgraphSearch:
615
+ r"""
616
+ This class implements methods to exhaustively search for
617
+ copies of a graph `H` in a larger graph `G`.
618
+
619
+ It is possible to look for induced subgraphs instead, and to
620
+ iterate or count the number of their occurrences.
621
+
622
+ ALGORITHM:
623
+
624
+ The algorithm is a brute-force search. Let `V(H) =
625
+ \{h_1,\dots,h_k\}`. It first tries to find in `G` a possible
626
+ representative of `h_1`, then a representative of `h_2` compatible
627
+ with `h_1`, then a representative of `h_3` compatible with the first
628
+ two, etc.
629
+
630
+ This way, most of the time we need to test far less than `k!
631
+ \binom{|V(G)|}{k}` subsets, and hope this brute-force technique
632
+ can sometimes be useful.
633
+
634
+ .. NOTE::
635
+
636
+ This algorithm does not take vertex/edge labels into account.
637
+ """
638
+ def __init__(self, G, H, induced=False):
639
+ r"""
640
+ Constructor.
641
+
642
+ This constructor only checks there is no inconsistency in the
643
+ input : `G` and `H` are both graphs or both digraphs and that `H`
644
+ has order at least 2.
645
+
646
+ EXAMPLES::
647
+
648
+ sage: g = graphs.PetersenGraph()
649
+ sage: g.subgraph_search(graphs.CycleGraph(5)) # needs sage.modules
650
+ Subgraph of (Petersen graph): Graph on 5 vertices
651
+
652
+ TESTS:
653
+
654
+ Test proper initialization and deallocation, see :issue:`14067`.
655
+ We intentionally only create the class without doing any
656
+ computations with it::
657
+
658
+ sage: from sage.graphs.generic_graph_pyx import SubgraphSearch
659
+ sage: SubgraphSearch(Graph(5), Graph(1)) # needs sage.modules
660
+ Traceback (most recent call last):
661
+ ...
662
+ ValueError: searched graph should have at least 2 vertices
663
+ sage: SubgraphSearch(Graph(5), Graph(2)) # needs sage.modules
664
+ <sage.graphs.generic_graph_pyx.SubgraphSearch ...>
665
+ """
666
+ if H.order() <= 1:
667
+ raise ValueError("searched graph should have at least 2 vertices")
668
+
669
+ if G.is_directed() != H.is_directed():
670
+ raise ValueError("one graph cannot be directed while the other is not")
671
+
672
+ G._scream_if_not_simple(allow_loops=True)
673
+ H._scream_if_not_simple(allow_loops=True)
674
+
675
+ self._initialization()
676
+
677
+ def __iter__(self):
678
+ r"""
679
+ Return an iterator over all the labeled subgraphs of `G`
680
+ isomorphic to `H`.
681
+
682
+ EXAMPLES:
683
+
684
+ Iterating through all the `P_3` of `P_5`::
685
+
686
+ sage: from sage.graphs.generic_graph_pyx import SubgraphSearch
687
+ sage: g = graphs.PathGraph(5)
688
+ sage: h = graphs.PathGraph(3)
689
+ sage: S = SubgraphSearch(g, h) # needs sage.modules
690
+ sage: for p in S: # needs sage.modules
691
+ ....: print(p)
692
+ [0, 1, 2]
693
+ [1, 2, 3]
694
+ [2, 1, 0]
695
+ [2, 3, 4]
696
+ [3, 2, 1]
697
+ [4, 3, 2]
698
+ """
699
+ self._initialization()
700
+ return self
701
+
702
+ def cardinality(self):
703
+ r"""
704
+ Return the number of labelled subgraphs of `G` isomorphic to
705
+ `H`.
706
+
707
+ .. NOTE::
708
+
709
+ This method counts the subgraphs by enumerating them all !
710
+ Hence it probably is not a good idea to count their number
711
+ before enumerating them :-)
712
+
713
+ EXAMPLES:
714
+
715
+ Counting the number of labelled `P_3` in `P_5`::
716
+
717
+ sage: from sage.graphs.generic_graph_pyx import SubgraphSearch
718
+ sage: g = graphs.PathGraph(5)
719
+ sage: h = graphs.PathGraph(3)
720
+ sage: S = SubgraphSearch(g, h) # needs sage.modules
721
+ sage: S.cardinality() # needs sage.modules
722
+ 6
723
+
724
+ Check that the method is working even when vertices or edges are of
725
+ incomparable types (see :issue:`35904`)::
726
+
727
+ sage: from sage.graphs.generic_graph_pyx import SubgraphSearch
728
+ sage: G = Graph()
729
+ sage: G.add_cycle(['A', 1, 2, 3, ('a', 1)])
730
+ sage: H = Graph()
731
+ sage: H.add_path("xyz")
732
+ sage: S = SubgraphSearch(G, H) # needs sage.modules
733
+ sage: S.cardinality() # needs sage.modules
734
+ 10
735
+ """
736
+ if self.nh > self.ng:
737
+ return 0
738
+
739
+ self._initialization()
740
+ cdef int i
741
+
742
+ i = 0
743
+ for _ in self:
744
+ i += 1
745
+
746
+ from sage.rings.integer import Integer
747
+ return Integer(i)
748
+
749
+ def _initialization(self):
750
+ r"""
751
+ Initialization of the variables.
752
+
753
+ Once the memory allocation is done in :meth:`__cinit__`,
754
+ several variables need to be set to a default value. As this
755
+ operation needs to be performed before any call to
756
+ :meth:`__iter__` or to :meth:`cardinality`, it is cleaner to
757
+ create a dedicated method.
758
+
759
+ EXAMPLES:
760
+
761
+ Finding two times the first occurrence through the
762
+ re-initialization of the instance ::
763
+
764
+ sage: from sage.graphs.generic_graph_pyx import SubgraphSearch
765
+ sage: g = graphs.PathGraph(5)
766
+ sage: h = graphs.PathGraph(3)
767
+ sage: S = SubgraphSearch(g, h) # needs sage.modules
768
+ sage: S.__next__() # needs sage.modules
769
+ [0, 1, 2]
770
+ sage: S._initialization() # needs sage.modules
771
+ sage: S.__next__() # needs sage.modules
772
+ [0, 1, 2]
773
+
774
+ TESTS:
775
+
776
+ Check that :issue:`21828` is fixed::
777
+
778
+ sage: Poset().is_incomparable_chain_free(1,1) # indirect doctest # needs sage.modules
779
+ True
780
+ """
781
+ cdef int i
782
+
783
+ if self.ng > 0:
784
+ # 0 is the first vertex we use, so it is at first busy
785
+ self.busy[0] = 1
786
+ for i in range(1, self.ng):
787
+ self.busy[i] = 0
788
+ # stack -- list of the vertices which are part of the partial copy of H
789
+ # in G.
790
+ #
791
+ # stack[i] -- the integer corresponding to the vertex of G representing
792
+ # the i-th vertex of H.
793
+ #
794
+ # stack[i] = -1 means that i is not represented
795
+ # ... yet!
796
+
797
+ self.stack[0] = 0
798
+ self.stack[1] = -1
799
+
800
+ # Number of representatives we have already found. Set to 1 as vertex 0
801
+ # is already part of the partial copy of H in G.
802
+ self.active = 1
803
+
804
+ def __cinit__(self, G, H, induced=False):
805
+ r"""
806
+ Cython constructor.
807
+
808
+ This method initializes all the C values.
809
+
810
+ EXAMPLES::
811
+
812
+ sage: g = graphs.PetersenGraph()
813
+ sage: g.subgraph_search(graphs.CycleGraph(5)) # needs sage.modules
814
+ Subgraph of (Petersen graph): Graph on 5 vertices
815
+ """
816
+ self.mem = MemoryAllocator()
817
+
818
+ # Storing the number of vertices
819
+ self.ng = G.order()
820
+ self.nh = H.order()
821
+
822
+ # Storing the list of vertices
823
+ self.g_vertices = list(G)
824
+ cdef list h_vertices = list(H)
825
+
826
+ # Are the graphs directed (in __init__(), we check
827
+ # whether both are of the same type)
828
+ self.directed = G.is_directed()
829
+
830
+ cdef int i, j
831
+
832
+ # A vertex is said to be busy if it is already part of the partial copy
833
+ # of H in G.
834
+ self.busy = <int *> self.mem.allocarray(self.ng, sizeof(int))
835
+ self.tmp_array = <int *> self.mem.allocarray(self.ng, sizeof(int))
836
+ self.stack = <int *> self.mem.allocarray(self.nh, sizeof(int))
837
+ self.vertices = <int *> self.mem.allocarray(self.nh, sizeof(int))
838
+ self.line_h_out = <int **> self.mem.allocarray(self.nh, sizeof(int *))
839
+ self.line_h_in = <int **> self.mem.allocarray(self.nh, sizeof(int *)) if self.directed else NULL
840
+
841
+ self.line_h_out[0] = <int *> self.mem.allocarray(self.nh*self.nh,
842
+ sizeof(int))
843
+ if self.directed:
844
+ self.line_h_in[0] = <int *> self.mem.allocarray(self.nh*self.nh,
845
+ sizeof(int))
846
+
847
+ # Should we look for induced subgraphs ?
848
+ if induced:
849
+ self.is_admissible = vectors_equal
850
+ else:
851
+ self.is_admissible = vectors_inferior
852
+
853
+ # static copies of the two graphs for more efficient operations
854
+ self.g = DenseGraph(self.ng)
855
+ self.h = DenseGraph(self.nh)
856
+
857
+ # copying the adjacency relations in both G and H
858
+ cdef dict vertex_to_int = {v: i for i, v in enumerate(self.g_vertices)}
859
+ cdef bint undirected = not G.is_directed()
860
+ for u, v in G.edge_iterator(labels=False):
861
+ i = vertex_to_int[u]
862
+ j = vertex_to_int[v]
863
+ self.g.add_arc(i, j)
864
+ if undirected:
865
+ self.g.add_arc(j, i)
866
+
867
+ vertex_to_int = {v: i for i, v in enumerate(h_vertices)}
868
+ for u, v in H.edge_iterator(labels=False):
869
+ i = vertex_to_int[u]
870
+ j = vertex_to_int[v]
871
+ self.h.add_arc(i, j)
872
+ if undirected:
873
+ self.h.add_arc(j, i)
874
+
875
+ # vertices is equal to range(nh), as an int *variable
876
+ for i in range(self.nh):
877
+ self.vertices[i] = i
878
+
879
+ # line_h_out[i] represents the adjacency sequence of vertex i
880
+ # in h relative to vertices 0, 1, ..., i-1
881
+ for i in range(self.nh):
882
+ self.line_h_out[i] = self.line_h_out[0] + i*self.nh
883
+ self.h.adjacency_sequence_out(i, self.vertices, i, self.line_h_out[i])
884
+
885
+ # Similarly in the opposite direction (only useful if the
886
+ # graphs are directed)
887
+ if self.directed:
888
+ for i in range(self.nh):
889
+ self.line_h_in[i] = self.line_h_in[0] + i*self.nh
890
+ self.h.adjacency_sequence_in(i, self.vertices, i, self.line_h_in[i])
891
+
892
+ def __next__(self):
893
+ r"""
894
+ Return the next isomorphic subgraph if any, and raises a
895
+ ``StopIteration`` otherwise.
896
+
897
+ EXAMPLES::
898
+
899
+ sage: from sage.graphs.generic_graph_pyx import SubgraphSearch
900
+ sage: g = graphs.PathGraph(5)
901
+ sage: h = graphs.PathGraph(3)
902
+ sage: S = SubgraphSearch(g, h) # needs sage.modules
903
+ sage: S.__next__() # needs sage.modules
904
+ [0, 1, 2]
905
+ """
906
+ if not self.ng:
907
+ raise StopIteration
908
+ sig_on()
909
+ cdef bint is_admissible
910
+ cdef int * tmp_array = self.tmp_array
911
+
912
+ # as long as there is a non-void partial copy of H in G
913
+ while self.active >= 0:
914
+ # If we are here and found nothing yet, we try the next possible
915
+ # vertex as a representative of the active i-th vertex of H.
916
+ self.i = self.stack[self.active] + 1
917
+ # Looking for a vertex that is not busy and compatible with the
918
+ # partial copy we have of H.
919
+ while self.i < self.ng:
920
+ if self.busy[self.i]:
921
+ self.i += 1
922
+ else:
923
+ # Testing whether the vertex we picked is a
924
+ # correct extension by checking the edges from the
925
+ # vertices already selected to self.i satisfy the
926
+ # constraints
927
+ self.g.adjacency_sequence_out(self.active, self.stack, self.i, tmp_array)
928
+ is_admissible = self.is_admissible(self.active, tmp_array, self.line_h_out[self.active])
929
+
930
+ # If G and H are digraphs, we also need to ensure
931
+ # the edges going in the opposite direction
932
+ # satisfy the constraints
933
+ if is_admissible and self.directed:
934
+ self.g.adjacency_sequence_in(self.active, self.stack, self.i, tmp_array)
935
+ is_admissible = is_admissible and self.is_admissible(self.active, tmp_array, self.line_h_in[self.active])
936
+
937
+ if is_admissible:
938
+ break
939
+ else:
940
+ self.i += 1
941
+
942
+ # If we have found a good representative of H's i-th vertex in G
943
+ if self.i < self.ng:
944
+
945
+ # updating the last vertex of the stack
946
+ if self.stack[self.active] != -1:
947
+ self.busy[self.stack[self.active]] = 0
948
+ self.stack[self.active] = self.i
949
+
950
+ # We have found our copy !!!
951
+ if self.active == self.nh-1:
952
+ sig_off()
953
+ return [self.g_vertices[self.stack[l]]
954
+ for l in range(self.nh)]
955
+
956
+ # We are still missing several vertices ...
957
+ else:
958
+ self.busy[self.stack[self.active]] = 1
959
+ self.active += 1
960
+
961
+ # we begin the search of the next vertex at 0
962
+ self.stack[self.active] = -1
963
+
964
+ # If we found no representative for the i-th vertex, it
965
+ # means that we cannot extend the current copy of H so we
966
+ # update the status of stack[active] and prepare to change
967
+ # the previous vertex.
968
+
969
+ else:
970
+ if self.stack[self.active] != -1:
971
+ self.busy[self.stack[self.active]] = 0
972
+ self.stack[self.active] = -1
973
+ self.active -= 1
974
+
975
+ sig_off()
976
+ raise StopIteration
977
+
978
+ cdef inline bint vectors_equal(int n, int *a, int *b) noexcept:
979
+ r"""
980
+ Test whether the two given vectors are equal. Two integer vectors
981
+ `a = (a_1, a_2, \dots, a_n)` and `b = (b_1, b_2, \dots, b_n)` are equal
982
+ iff `a_i = b_i` for all `i = 1, 2, \dots, n`. See the function
983
+ ``_test_vectors_equal_inferior()`` for unit tests.
984
+
985
+ INPUT:
986
+
987
+ - ``n`` -- positive integer; length of the vectors
988
+
989
+ - ``a``, ``b`` -- two vectors of integers
990
+
991
+ OUTPUT: ``True`` if ``a`` and ``b`` are the same vector; ``False`` otherwise
992
+ """
993
+ cdef int i
994
+ for i in range(n):
995
+ if a[i] != b[i]:
996
+ return False
997
+ return True
998
+
999
+ cdef inline bint vectors_inferior(int n, int *a, int *b) noexcept:
1000
+ r"""
1001
+ Test whether the second vector of integers is inferior to the first. Let
1002
+ `u = (u_1, u_2, \dots, u_k)` and `v = (v_1, v_2, \dots, v_k)` be two
1003
+ integer vectors of equal length. Then `u` is said to be less than
1004
+ (or inferior to) `v` if `u_i \leq v_i` for all `i = 1, 2, \dots, k`. See
1005
+ the function ``_test_vectors_equal_inferior()`` for unit tests. Given two
1006
+ equal integer vectors `u` and `v`, `u` is inferior to `v` and vice versa.
1007
+ We could also define two vectors `a` and `b` to be equal if `a` is
1008
+ inferior to `b` and `b` is inferior to `a`.
1009
+
1010
+ INPUT:
1011
+
1012
+ - ``n`` -- positive integer; length of the vectors
1013
+
1014
+ - ``a``, ``b`` -- two vectors of integers
1015
+
1016
+ OUTPUT:
1017
+
1018
+ - ``True`` if ``b`` is inferior to (or less than) ``a``; ``False``
1019
+ otherwise.
1020
+ """
1021
+ cdef int i
1022
+ for i in range(n):
1023
+ if a[i] < b[i]:
1024
+ return False
1025
+ return True
1026
+
1027
+
1028
+ ##############################
1029
+ # Further tests. Unit tests for methods, functions, classes defined with cdef.
1030
+ ##############################
1031
+
1032
+ def _test_vectors_equal_inferior():
1033
+ """
1034
+ Unit testing the function ``vectors_equal()``. No output means that no
1035
+ errors were found in the random tests.
1036
+
1037
+ TESTS::
1038
+
1039
+ sage: from sage.graphs.generic_graph_pyx import _test_vectors_equal_inferior
1040
+ sage: _test_vectors_equal_inferior()
1041
+ """
1042
+ from sage.misc.prandom import randint
1043
+ n = randint(500, 10**3)
1044
+ cdef int *u = <int *>check_allocarray(n, sizeof(int))
1045
+ cdef int *v = <int *>check_allocarray(n, sizeof(int))
1046
+ cdef int i
1047
+ # equal vectors: u = v
1048
+ for i in range(n):
1049
+ u[i] = randint(-10**6, 10**6)
1050
+ v[i] = u[i]
1051
+ try:
1052
+ assert vectors_equal(n, u, v)
1053
+ assert vectors_equal(n, v, u)
1054
+ # Since u and v are equal vectors, then u is inferior to v and v is
1055
+ # inferior to u. One could also define u and v as being equal if
1056
+ # u is inferior to v and vice versa.
1057
+ assert vectors_inferior(n, u, v)
1058
+ assert vectors_inferior(n, v, u)
1059
+ except AssertionError:
1060
+ sig_free(u)
1061
+ sig_free(v)
1062
+ raise AssertionError("Vectors u and v should be equal.")
1063
+ # Different vectors: u != v because we have u_j > v_j for some j. Thus,
1064
+ # u_i = v_i for 0 <= i < j and u_j > v_j. For j < k < n - 2, we could have:
1065
+ # (1) u_k = v_k,
1066
+ # (2) u_k < v_k, or
1067
+ # (3) u_k > v_k.
1068
+ # And finally, u_{n-1} < v_{n-1}.
1069
+ cdef int j = randint(1, n//2)
1070
+ cdef int k
1071
+ for i in range(j):
1072
+ u[i] = randint(-10**6, 10**6)
1073
+ v[i] = u[i]
1074
+ u[j] = randint(-10**6, 10**6)
1075
+ v[j] = u[j] - randint(1, 10**6)
1076
+ for k in range(j + 1, n):
1077
+ u[k] = randint(-10**6, 10**6)
1078
+ v[k] = randint(-10**6, 10**6)
1079
+ u[n - 1] = v[n - 1] - randint(1, 10**6)
1080
+ try:
1081
+ assert not vectors_equal(n, u, v)
1082
+ assert not vectors_equal(n, v, u)
1083
+ # u is not inferior to v because at least u_j > v_j
1084
+ assert u[j] > v[j]
1085
+ assert not vectors_inferior(n, v, u)
1086
+ # v is not inferior to u because at least v_{n-1} > u_{n-1}
1087
+ assert v[n - 1] > u[n - 1]
1088
+ assert not vectors_inferior(n, u, v)
1089
+ except AssertionError:
1090
+ sig_free(u)
1091
+ sig_free(v)
1092
+ raise AssertionError("Vectors u and v should not be equal. "
1093
+ "u should not be inferior to v, and vice versa.")
1094
+ # Different vectors: u != v because we have u_j < v_j for some j. Thus,
1095
+ # u_i = v_i for 0 <= i < j and u_j < v_j. For j < k < n - 2, we could have:
1096
+ # (1) u_k = v_k,
1097
+ # (2) u_k < v_k, or
1098
+ # (3) u_k > v_k.
1099
+ # And finally, u_{n-1} > v_{n-1}.
1100
+ j = randint(1, n//2)
1101
+ for i in range(j):
1102
+ u[i] = randint(-10**6, 10**6)
1103
+ v[i] = u[i]
1104
+ u[j] = randint(-10**6, 10**6)
1105
+ v[j] = u[j] + randint(1, 10**6)
1106
+ for j < k < n:
1107
+ u[k] = randint(-10**6, 10**6)
1108
+ v[k] = randint(-10**6, 10**6)
1109
+ u[n - 1] = v[n - 1] + randint(1, 10**6)
1110
+ try:
1111
+ assert not vectors_equal(n, u, v)
1112
+ assert not vectors_equal(n, v, u)
1113
+ # u is not inferior to v because at least u_{n-1} > v_{n-1}
1114
+ assert u[n - 1] > v[n - 1]
1115
+ assert not vectors_inferior(n, v, u)
1116
+ # v is not inferior to u because at least u_j < v_j
1117
+ assert u[j] < v[j]
1118
+ assert not vectors_inferior(n, u, v)
1119
+ except AssertionError:
1120
+ sig_free(u)
1121
+ sig_free(v)
1122
+ raise AssertionError("Vectors u and v should not be equal. "
1123
+ "u should not be inferior to v, and vice versa.")
1124
+ # different vectors u != v
1125
+ # What's the probability of two random vectors being equal?
1126
+ for i in range(n):
1127
+ u[i] = randint(-10**6, 10**6)
1128
+ v[i] = randint(-10**6, 10**6)
1129
+ try:
1130
+ assert not vectors_equal(n, u, v)
1131
+ assert not vectors_equal(n, v, u)
1132
+ except AssertionError:
1133
+ sig_free(u)
1134
+ sig_free(v)
1135
+ raise AssertionError("Vectors u and v should not be equal.")
1136
+ # u is inferior to v, but v is not inferior to u
1137
+ for i in range(n):
1138
+ v[i] = randint(-10**6, 10**6)
1139
+ u[i] = randint(-10**6, 10**6)
1140
+ while u[i] > v[i]:
1141
+ u[i] = randint(-10**6, 10**6)
1142
+ try:
1143
+ assert not vectors_equal(n, u, v)
1144
+ assert not vectors_equal(n, v, u)
1145
+ assert vectors_inferior(n, v, u)
1146
+ assert not vectors_inferior(n, u, v)
1147
+ except AssertionError:
1148
+ raise AssertionError("u should be inferior to v, but v is not inferior to u.")
1149
+ finally:
1150
+ sig_free(u)
1151
+ sig_free(v)
1152
+
1153
+
1154
+ cpdef tuple find_hamiltonian(G, long max_iter=100000, long reset_bound=30000,
1155
+ long backtrack_bound=1000, find_path=False):
1156
+ r"""
1157
+ Randomized backtracking for finding Hamiltonian cycles and paths.
1158
+
1159
+ ALGORITHM:
1160
+
1161
+ A path ``P`` is maintained during the execution of the algorithm.
1162
+ Initially the path will contain an edge of the graph. Every 10
1163
+ iterations the path is reversed. Every ``reset_bound`` iterations
1164
+ the path will be cleared and the procedure is restarted. Every
1165
+ ``backtrack_bound`` steps we discard the last five vertices and
1166
+ continue with the procedure. The total number of steps in the
1167
+ algorithm is controlled by ``max_iter``. If a Hamiltonian cycle or
1168
+ Hamiltonian path is found it is returned. If the number of steps
1169
+ reaches ``max_iter`` then a longest path is returned. See OUTPUT
1170
+ for more details.
1171
+
1172
+ INPUT:
1173
+
1174
+ - ``G`` -- graph
1175
+
1176
+ - ``max_iter`` -- maximum number of iterations
1177
+
1178
+ - ``reset_bound`` -- number of iterations before restarting the
1179
+ procedure
1180
+
1181
+ - ``backtrack_bound`` -- number of iterations to elapse before
1182
+ discarding the last 5 vertices of the path
1183
+
1184
+ - ``find_path`` -- boolean (default: ``False``); if set to ``True``, will
1185
+ search a Hamiltonian path. If ``False``, will search for a Hamiltonian
1186
+ cycle.
1187
+
1188
+ OUTPUT:
1189
+
1190
+ A pair ``(B, P)``, where ``B`` is a Boolean and ``P`` is a list
1191
+ of vertices.
1192
+
1193
+ * If ``B`` is ``True`` and ``find_path`` is ``False``, ``P``
1194
+ represents a Hamiltonian cycle.
1195
+
1196
+ * If ``B`` is ``True`` and ``find_path`` is ``True``, ``P``
1197
+ represents a Hamiltonian path.
1198
+
1199
+ * If ``B`` is ``False``, then ``P`` represents the longest path
1200
+ found during the execution of the algorithm.
1201
+
1202
+ .. WARNING::
1203
+
1204
+ May loop endlessly when run on a graph with vertices of degree 1.
1205
+
1206
+ EXAMPLES:
1207
+
1208
+ For demonstration purposes we fix a random seed::
1209
+
1210
+ sage: set_random_seed(0)
1211
+
1212
+ First we try the algorithm in the Dodecahedral graph, which is
1213
+ Hamiltonian, so we are able to find a Hamiltonian cycle and a
1214
+ Hamiltonian path::
1215
+
1216
+ sage: from sage.graphs.generic_graph_pyx import find_hamiltonian as fh
1217
+ sage: G=graphs.DodecahedralGraph()
1218
+ sage: fh(G)
1219
+ (True, [12, 11, 10, 9, 13, 14, 15, 5, 4, 3, 2, 6, 7, 8, 1, 0, 19, 18, 17, 16])
1220
+ sage: fh(G,find_path=True)
1221
+ (True, [10, 0, 19, 3, 4, 5, 15, 16, 17, 18, 11, 12, 13, 9, 8, 1, 2, 6, 7, 14])
1222
+
1223
+ Another test, now in the Möbius-Kantor graph which is also
1224
+ Hamiltonian, as in our previous example, we are able to find a
1225
+ Hamiltonian cycle and path::
1226
+
1227
+ sage: G=graphs.MoebiusKantorGraph()
1228
+ sage: fh(G)
1229
+ (True, [15, 10, 2, 3, 4, 5, 13, 8, 11, 14, 6, 7, 0, 1, 9, 12])
1230
+ sage: fh(G,find_path=True)
1231
+ (True, [10, 15, 7, 6, 5, 4, 12, 9, 14, 11, 3, 2, 1, 0, 8, 13])
1232
+
1233
+ Now, we try the algorithm on a non Hamiltonian graph, the Petersen
1234
+ graph. This graph is known to be hypohamiltonian, so a
1235
+ Hamiltonian path can be found::
1236
+
1237
+ sage: G=graphs.PetersenGraph()
1238
+ sage: fh(G)
1239
+ (False, [9, 4, 0, 1, 6, 8, 5, 7, 2, 3])
1240
+ sage: fh(G,find_path=True)
1241
+ (True, [7, 2, 1, 0, 5, 8, 6, 9, 4, 3])
1242
+
1243
+ We now show the algorithm working on another known hypohamiltonian
1244
+ graph, the generalized Petersen graph with parameters 11 and 2::
1245
+
1246
+ sage: G=graphs.GeneralizedPetersenGraph(11,2)
1247
+ sage: fh(G)
1248
+ (False, [7, 8, 9, 10, 0, 1, 2, 3, 14, 12, 21, 19, 17, 6, 5, 4, 15, 13, 11, 20, 18, 16])
1249
+ sage: fh(G,find_path=True)
1250
+ (True, [2, 1, 12, 21, 10, 0, 11, 13, 15, 17, 19, 8, 7, 6, 5, 4, 3, 14, 16, 18, 20, 9])
1251
+
1252
+ Finally, an example on a graph which does not have a Hamiltonian
1253
+ path::
1254
+
1255
+ sage: G = graphs.HyperStarGraph(5, 2)
1256
+ sage: G.order()
1257
+ 10
1258
+ sage: b, P = fh(G,find_path=False)
1259
+ sage: b, len(P)
1260
+ (False, 9)
1261
+ sage: b, P = fh(G,find_path=True)
1262
+ sage: b, len(P)
1263
+ (False, 9)
1264
+
1265
+ The method can also be used for directed graphs::
1266
+
1267
+ sage: G = DiGraph([(0, 1), (1, 2), (2, 3)])
1268
+ sage: fh(G)
1269
+ (False, [0, 1, 2, 3])
1270
+ sage: G = G.reverse()
1271
+ sage: fh(G)
1272
+ (False, [3, 2, 1, 0])
1273
+ sage: G = DiGraph()
1274
+ sage: G.add_cycle([0, 1, 2, 3, 4, 5])
1275
+ sage: b, P = fh(G)
1276
+ sage: b, len(P)
1277
+ (True, 6)
1278
+
1279
+ TESTS:
1280
+
1281
+ :issue:`10206` -- Hamiltonian cycle in small (di)graphs::
1282
+
1283
+ sage: for n in range(3): # needs nauty
1284
+ ....: for G in graphs(n):
1285
+ ....: print('order {} and size {}: {}'.format(G.order(),G.size(),fh(G, find_path=False)))
1286
+ order 0 and size 0: (False, [])
1287
+ order 1 and size 0: (False, [0])
1288
+ order 2 and size 0: (False, [0])
1289
+ order 2 and size 1: (False, [0, 1])
1290
+ sage: for n in range(3): # needs nauty
1291
+ ....: for G in digraphs(n):
1292
+ ....: print('order {} and size {}: {}'.format(G.order(),G.size(),fh(G, find_path=False)))
1293
+ order 0 and size 0: (False, [])
1294
+ order 1 and size 0: (False, [0])
1295
+ order 2 and size 0: (False, [0])
1296
+ order 2 and size 1: (False, [0, 1])
1297
+ order 2 and size 2: (False, [0, 1])
1298
+
1299
+ :issue:`10206` -- Hamiltonian path in small (di)graphs::
1300
+
1301
+ sage: for n in range(3): # needs nauty
1302
+ ....: for G in graphs(n):
1303
+ ....: print('order {} and size {}: {}'.format(G.order(),G.size(),fh(G, find_path=True)))
1304
+ order 0 and size 0: (False, [])
1305
+ order 1 and size 0: (False, [0])
1306
+ order 2 and size 0: (False, [0])
1307
+ order 2 and size 1: (True, [0, 1])
1308
+ sage: for n in range(3): # needs nauty
1309
+ ....: for G in digraphs(n):
1310
+ ....: print('order {} and size {}: {}'.format(G.order(),G.size(),fh(G, find_path=True)))
1311
+ order 0 and size 0: (False, [])
1312
+ order 1 and size 0: (False, [0])
1313
+ order 2 and size 0: (False, [0])
1314
+ order 2 and size 1: (True, [0, 1])
1315
+ order 2 and size 2: (True, [0, 1])
1316
+
1317
+ :issue:`10206` -- disconnected graphs::
1318
+
1319
+ sage: G = graphs.CompleteGraph(4) + Graph(1)
1320
+ sage: fh(G, find_path=False)
1321
+ (False, [0, 1, 2, 3])
1322
+ sage: fh(G, find_path=True)
1323
+ (False, [0, 1, 2, 3])
1324
+
1325
+ Check that the method is robust to incomparable vertices::
1326
+
1327
+ sage: G = Graph([(1, 'a'), ('a', 2), (2, 3), (3, 1)])
1328
+ sage: b, C = fh(G, find_path=False)
1329
+ sage: b, len(C)
1330
+ (True, 4)
1331
+ """
1332
+ G._scream_if_not_simple()
1333
+
1334
+ from sage.misc.prandom import randint
1335
+ cdef int n = G.order()
1336
+
1337
+ # Easy cases
1338
+ if n < 2:
1339
+ return False, list(G)
1340
+
1341
+ # To clean the output when find_path is None or a number
1342
+ find_path = (find_path > 0)
1343
+
1344
+ if G.is_clique(induced=False):
1345
+ # We have a hamiltonian path since n >= 2, but we have a hamiltonian
1346
+ # cycle only if n >= 3
1347
+ return find_path or n >= 3, list(G)
1348
+
1349
+ cdef list best_path, p
1350
+ if not G.is_connected():
1351
+ # The (Di)Graph has no hamiltonian path or cycle. We search for the
1352
+ # longest path in its connected components.
1353
+ best_path = []
1354
+ for H in G.connected_components_subgraphs():
1355
+ if H.order() <= len(best_path):
1356
+ continue
1357
+ _, p = find_hamiltonian(H, max_iter=max_iter, reset_bound=reset_bound,
1358
+ backtrack_bound=backtrack_bound, find_path=True)
1359
+ if len(p) > len(best_path):
1360
+ best_path = p
1361
+ return False, best_path
1362
+
1363
+ # Misc variables used below
1364
+ cdef int i, j
1365
+ cdef bint directed = G.is_directed()
1366
+
1367
+ # Initialize the path.
1368
+ cdef MemoryAllocator mem = MemoryAllocator()
1369
+ cdef int *path = <int *>mem.allocarray(n, sizeof(int))
1370
+
1371
+ # Initialize the membership array
1372
+ cdef bint *member = <bint *>mem.allocarray(n, sizeof(int))
1373
+ memset(member, 0, n * sizeof(int))
1374
+
1375
+ # static copy of the graph for more efficient operations
1376
+ cdef list int_to_vertex = list(G)
1377
+ cdef short_digraph sd
1378
+ init_short_digraph(sd, G, edge_labelled=False, vertex_list=int_to_vertex)
1379
+ cdef short_digraph rev_sd
1380
+ cdef bint reverse = False
1381
+ if directed:
1382
+ init_reverse(rev_sd, sd)
1383
+
1384
+ # A list to store the available vertices at each step
1385
+ cdef list available_vertices = []
1386
+
1387
+ # We now work towards picking a random edge
1388
+ # First we pick a random vertex u of (out-)degree at least one
1389
+ cdef int u = randint(0, n - 1)
1390
+ while not out_degree(sd, u):
1391
+ u = randint(0, n - 1)
1392
+ # Then we pick at random a neighbor of u
1393
+ cdef int x = randint(0, out_degree(sd, u) - 1)
1394
+ cdef int v = sd.neighbors[u][x]
1395
+ # This will be the first edge in the path
1396
+ cdef int length = 2
1397
+ path[0] = u
1398
+ path[1] = v
1399
+ member[u] = True
1400
+ member[v] = True
1401
+
1402
+ # Initialize all the variables necessary to start iterating
1403
+ cdef bint done = False
1404
+ cdef long counter = 0
1405
+ cdef long bigcount = 0
1406
+ cdef int longest = length
1407
+
1408
+ # Initialize a path to contain the longest path
1409
+ cdef int *longest_path = <int *>mem.allocarray(n, sizeof(int))
1410
+ for i in range(length):
1411
+ longest_path[i] = path[i]
1412
+
1413
+ cdef bint longer = False
1414
+ cdef bint longest_reversed = False
1415
+ cdef bint flag
1416
+
1417
+ while not done:
1418
+ counter = counter + 1
1419
+ if counter % 10 == 0:
1420
+ # Reverse the path
1421
+ for i in range(length//2):
1422
+ t = path[i]
1423
+ path[i] = path[length - i - 1]
1424
+ path[length - i - 1] = t
1425
+
1426
+ if directed:
1427
+ # We now work on the reverse graph
1428
+ reverse = not reverse
1429
+
1430
+ if counter > reset_bound:
1431
+ bigcount = bigcount + 1
1432
+ counter = 1
1433
+
1434
+ # Time to reset the procedure
1435
+ memset(member, 0, n * sizeof(int))
1436
+ if directed and reverse:
1437
+ # We restore the original orientation
1438
+ reverse = False
1439
+
1440
+ # First we pick a random vertex u of (out-)degree at least one
1441
+ u = randint(0, n - 1)
1442
+ while not out_degree(sd, u):
1443
+ u = randint(0, n - 1)
1444
+ # Then we pick at random a neighbor of u
1445
+ x = randint(0, out_degree(sd, u) - 1)
1446
+ v = sd.neighbors[u][x]
1447
+ # This will be the first edge in the path
1448
+ length = 2
1449
+ path[0] = u
1450
+ path[1] = v
1451
+ member[u] = True
1452
+ member[v] = True
1453
+
1454
+ if length > 5 and counter % backtrack_bound == 0:
1455
+ for i in range(5):
1456
+ member[path[length - i - 1]] = False
1457
+ length = length - 5
1458
+ longer = False
1459
+
1460
+ # We search for a possible extension of the path
1461
+ available_vertices = []
1462
+ u = path[length - 1]
1463
+ if directed and reverse:
1464
+ for i in range(out_degree(rev_sd, u)):
1465
+ v = rev_sd.neighbors[u][i]
1466
+ if not member[v]:
1467
+ available_vertices.append(v)
1468
+ else:
1469
+ for i in range(out_degree(sd, u)):
1470
+ v = sd.neighbors[u][i]
1471
+ if not member[v]:
1472
+ available_vertices.append(v)
1473
+
1474
+ if available_vertices:
1475
+ longer = True
1476
+ x = randint(0, len(available_vertices) - 1)
1477
+ v = available_vertices[x]
1478
+ path[length] = v
1479
+ length = length + 1
1480
+ member[v] = True
1481
+
1482
+ if not longer and length > longest:
1483
+ # Store the current best solution
1484
+ for i in range(length):
1485
+ longest_path[i] = path[i]
1486
+
1487
+ longest = length
1488
+ longest_reversed = reverse
1489
+
1490
+ if not directed and not longer and out_degree(sd, path[length - 1]) > 1:
1491
+ # We revert a cycle to change the extremity of the path
1492
+ degree = out_degree(sd, path[length - 1])
1493
+ while True:
1494
+ x = randint(0, degree - 1)
1495
+ u = sd.neighbors[path[length - 1]][x]
1496
+ if u != path[length - 2]:
1497
+ break
1498
+
1499
+ flag = False
1500
+ j = 0
1501
+ for i in range(length):
1502
+ if i > length - j - 1:
1503
+ break
1504
+ if flag:
1505
+ t = path[i]
1506
+ path[i] = path[length - j - 1]
1507
+ path[length - j - 1] = t
1508
+ j += 1
1509
+ if path[i] == u:
1510
+ flag = True
1511
+
1512
+ if length == n:
1513
+ if find_path:
1514
+ done = True
1515
+ elif directed and reverse:
1516
+ done = has_edge(rev_sd, path[0], path[n - 1]) != NULL
1517
+ else:
1518
+ done = has_edge(sd, path[n - 1], path[0]) != NULL
1519
+
1520
+ if bigcount * reset_bound > max_iter:
1521
+ output = [int_to_vertex[longest_path[i]] for i in range(longest)]
1522
+ free_short_digraph(sd)
1523
+ if directed:
1524
+ free_short_digraph(rev_sd)
1525
+ if longest_reversed:
1526
+ return (False, output[::-1])
1527
+ return (False, output)
1528
+ # #
1529
+ # # Output test
1530
+ # #
1531
+
1532
+ if directed and reverse:
1533
+ # We revert the path to work on sd
1534
+ for i in range(length//2):
1535
+ t = path[i]
1536
+ path[i] = path[length - i - 1]
1537
+ path[length - i - 1] = t
1538
+
1539
+ # Test adjacencies
1540
+ cdef bint good = True
1541
+ for i in range(n - 1):
1542
+ u = path[i]
1543
+ v = path[i + 1]
1544
+ if has_edge(sd, u, v) == NULL:
1545
+ good = False
1546
+ break
1547
+ if good is False:
1548
+ raise RuntimeError(f"vertices {int_to_vertex[u]} and {int_to_vertex[v]}"
1549
+ " are consecutive in the cycle but are not adjacent")
1550
+ if not find_path and has_edge(sd, path[n - 1], path[0]) == NULL:
1551
+ raise RuntimeError(f"vertices {int_to_vertex[path[n - 1]]} and "
1552
+ f"{int_to_vertex[path[0]]} are not adjacent")
1553
+
1554
+ output = [int_to_vertex[path[i]] for i in range(length)]
1555
+ free_short_digraph(sd)
1556
+ if directed:
1557
+ free_short_digraph(rev_sd)
1558
+
1559
+ return (True, output)
1560
+
1561
+
1562
+ def transitive_reduction_acyclic(G):
1563
+ r"""
1564
+ Return the transitive reduction of an acyclic digraph.
1565
+
1566
+ INPUT:
1567
+
1568
+ - ``G`` -- an acyclic digraph
1569
+
1570
+ EXAMPLES::
1571
+
1572
+ sage: from sage.graphs.generic_graph_pyx import transitive_reduction_acyclic
1573
+ sage: G = posets.BooleanLattice(4).hasse_diagram()
1574
+ sage: G == transitive_reduction_acyclic(G.transitive_closure())
1575
+ True
1576
+ """
1577
+ cdef int n = G.order()
1578
+ cdef dict v_to_int = {vv: i for i, vv in enumerate(G)}
1579
+ cdef int u, v, i
1580
+
1581
+ cdef list linear_extension
1582
+
1583
+ is_acyclic, linear_extension = G.is_directed_acyclic(certificate=True)
1584
+ if not is_acyclic:
1585
+ raise ValueError("The graph is not directed acyclic")
1586
+
1587
+ linear_extension.reverse()
1588
+
1589
+ cdef binary_matrix_t closure
1590
+
1591
+ # Build the transitive closure of G
1592
+ #
1593
+ # A point is reachable from u if it is one of its neighbours, or if it is
1594
+ # reachable from one of its neighbours.
1595
+ binary_matrix_init(closure, n, n)
1596
+ for uu in linear_extension:
1597
+ u = v_to_int[uu]
1598
+ for vv in G.neighbors_out(uu):
1599
+ v = v_to_int[vv]
1600
+ binary_matrix_set1(closure, u, v)
1601
+ bitset_or(closure.rows[u], closure.rows[u], closure.rows[v])
1602
+
1603
+ # Build the transitive reduction of G
1604
+ #
1605
+ # An edge uv belongs to the transitive reduction of G if no outneighbor of u
1606
+ # can reach v (except v itself, of course).
1607
+ linear_extension.reverse()
1608
+ cdef list useful_edges = []
1609
+ for uu in linear_extension:
1610
+ u = v_to_int[uu]
1611
+ for vv in G.neighbors_out(uu):
1612
+ v = v_to_int[vv]
1613
+ bitset_difference(closure.rows[u], closure.rows[u], closure.rows[v])
1614
+ for vv in G.neighbors_out(uu):
1615
+ v = v_to_int[vv]
1616
+ if binary_matrix_get(closure, u, v):
1617
+ useful_edges.append((uu, vv))
1618
+
1619
+ from sage.graphs.digraph import DiGraph
1620
+ reduced = DiGraph()
1621
+ reduced.add_edges(useful_edges)
1622
+ reduced.add_vertices(linear_extension)
1623
+
1624
+ binary_matrix_free(closure)
1625
+
1626
+ return reduced