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,827 @@
1
+ # sage_setup: distribution = sagemath-graphs
2
+ # cython: binding=True
3
+ r"""
4
+ Convexity properties of graphs
5
+
6
+ This class gathers the algorithms related to convexity in a graph. It implements
7
+ the following methods:
8
+
9
+ .. csv-table::
10
+ :class: contentstable
11
+ :widths: 30, 70
12
+ :delim: |
13
+
14
+ :meth:`ConvexityProperties.hull` | Return the convex hull of a set of vertices
15
+ :meth:`ConvexityProperties.hull_number` | Compute the hull number of a graph and a corresponding generating set
16
+ :meth:`geodetic_closure`| Return the geodetic closure of a set of vertices
17
+ :meth:`is_geodetic` | Check whether the input (di)graph is geodetic
18
+
19
+ Some of these methods can be used through the :class:`ConvexityProperties`
20
+ object returned by :meth:`Graph.convexity_properties`.
21
+
22
+ Methods
23
+ -------
24
+ """
25
+
26
+ # ****************************************************************************
27
+ # Copyright (C) 2011 Nathann Cohen <nathann.cohen@gmail.com>
28
+ # 2021 David Coudert <david.coudert@inria.fr>
29
+ #
30
+ # This program is free software: you can redistribute it and/or modify
31
+ # it under the terms of the GNU General Public License as published by
32
+ # the Free Software Foundation, either version 2 of the License, or
33
+ # (at your option) any later version.
34
+ # https://www.gnu.org/licenses/
35
+ # ****************************************************************************
36
+
37
+ from sage.data_structures.binary_matrix cimport *
38
+ from sage.numerical.backends.generic_backend cimport GenericBackend
39
+ from sage.numerical.backends.generic_backend import get_solver
40
+ from sage.graphs.distances_all_pairs cimport c_distances_all_pairs
41
+ from cysignals.memory cimport sig_free
42
+ from memory_allocator cimport MemoryAllocator
43
+ from libc.stdint cimport uint32_t
44
+ from sage.graphs.base.static_sparse_graph cimport (short_digraph,
45
+ init_short_digraph,
46
+ free_short_digraph,
47
+ out_degree,
48
+ simple_BFS)
49
+
50
+
51
+ cdef class ConvexityProperties:
52
+ r"""
53
+ This class gathers the algorithms related to convexity in a graph.
54
+
55
+ **Definitions**
56
+
57
+ A set `S \subseteq V(G)` of vertices is said to be convex if for all `u,v\in
58
+ S` the set `S` contains all the vertices located on a shortest path between
59
+ `u` and `v`. Alternatively, a set `S` is said to be convex if the distances
60
+ satisfy `\forall u,v\in S, \forall w\in V\backslash S : d_{G}(u,w) +
61
+ d_{G}(w,v) > d_{G}(u,v)`.
62
+
63
+ The convex hull `h(S)` of a set `S` of vertices is defined as the smallest
64
+ convex set containing `S`.
65
+
66
+ It is a closure operator, as trivially `S\subseteq h(S)` and `h(h(S)) =
67
+ h(S)`.
68
+
69
+ **What this class contains**
70
+
71
+ As operations on convex sets generally involve the computation of distances
72
+ between vertices, this class' purpose is to cache that information so that
73
+ computing the convex hulls of several different sets of vertices does not
74
+ imply recomputing several times the distances between the vertices.
75
+
76
+ In order to compute the convex hull of a set `S` it is possible to write the
77
+ following algorithm:
78
+
79
+ For any pair `u,v` of elements in the set `S`, and for any vertex `w`
80
+ outside of it, add `w` to `S` if `d_{G}(u,w) + d_{G}(w,v) =
81
+ d_{G}(u,v)`. When no vertex can be added anymore, the set `S` is convex
82
+
83
+ The distances are not actually that relevant. The same algorithm can be
84
+ implemented by remembering for each pair `u, v` of vertices the list of
85
+ elements `w` satisfying the condition, and this is precisely what this class
86
+ remembers, encoded as bitsets to make storage and union operations more
87
+ efficient.
88
+
89
+ .. NOTE::
90
+
91
+ * This class is useful if you compute the convex hulls of many sets in
92
+ the same graph, or if you want to compute the hull number itself as it
93
+ involves many calls to :meth:`hull`
94
+
95
+ * Using this class on non-connected graphs is a waste of space and
96
+ efficiency ! If your graph is disconnected, the best for you is to
97
+ deal independently with each connected component, whatever you are
98
+ doing.
99
+
100
+ **Possible improvements**
101
+
102
+ When computing a convex set, all the pairs of elements belonging to the set
103
+ `S` are enumerated several times.
104
+
105
+ * There should be a smart way to avoid enumerating pairs of vertices which
106
+ have already been tested. The cost of each of them is not very high, so
107
+ keeping track of those which have been tested already may be too expensive
108
+ to gain any efficiency.
109
+
110
+ * The ordering in which they are visited is currently purely lexicographic,
111
+ while there is a Poset structure to exploit. In particular, when two
112
+ vertices `u, v` are far apart and generate a set `h(\{u,v\})` of vertices,
113
+ all the pairs of vertices `u', v'\in h(\{u,v\})` satisfy `h(\{u',v'\})
114
+ \subseteq h(\{u,v\})`, and so it is useless to test the pair `u', v'` when
115
+ both `u` and `v` where present.
116
+
117
+ * The information cached is for any pair `u,v` of vertices the list of
118
+ elements `z` with `d_{G}(u,w) + d_{G}(w,v) = d_{G}(u,v)`. This is not in
119
+ general equal to `h(\{u,v\})` !
120
+
121
+ Nothing says these recommendations will actually lead to any actual
122
+ improvements. There are just some ideas remembered while writing this
123
+ code. Trying to optimize may well lead to lost in efficiency on many
124
+ instances.
125
+
126
+ EXAMPLES::
127
+
128
+ sage: from sage.graphs.convexity_properties import ConvexityProperties
129
+ sage: g = graphs.PetersenGraph()
130
+ sage: CP = ConvexityProperties(g)
131
+ sage: CP.hull([1, 3])
132
+ [1, 2, 3]
133
+ sage: CP.hull_number() # needs sage.numerical.mip
134
+ 3
135
+
136
+ TESTS::
137
+
138
+ sage: ConvexityProperties(digraphs.Circuit(5))
139
+ Traceback (most recent call last):
140
+ ...
141
+ NotImplementedError: this is currently implemented for Graphs only, but only minor updates are needed if you want to make it support DiGraphs too
142
+ """
143
+
144
+ def __init__(self, G):
145
+ r"""
146
+ Constructor.
147
+
148
+ EXAMPLES::
149
+
150
+ sage: from sage.graphs.convexity_properties import ConvexityProperties
151
+ sage: g = graphs.PetersenGraph()
152
+ sage: ConvexityProperties(g)
153
+ <sage.graphs.convexity_properties.ConvexityProperties object at ...>
154
+ """
155
+ from sage.graphs.digraph import DiGraph
156
+ if isinstance(G, DiGraph):
157
+ raise NotImplementedError("this is currently implemented for Graphs only, "
158
+ "but only minor updates are needed if you want "
159
+ "to make it support DiGraphs too")
160
+
161
+ # Cached number of vertices
162
+ cdef int n = G.order()
163
+ self._n = n
164
+
165
+ cdef int i = 0
166
+ cdef int j, k
167
+
168
+ # Build mappings integer <-> vertices.
169
+ # Must be consistent with the mappings used in c_distances_all_pairs
170
+ self._list_integers_to_vertices = list(G)
171
+ self._dict_vertices_to_integers = {v: i for i, v in enumerate(self._list_integers_to_vertices)}
172
+
173
+ # Computation of distances between all pairs. Costly.
174
+ cdef unsigned short* c_distances = c_distances_all_pairs(G, vertex_list=self._list_integers_to_vertices)
175
+ # Temporary variables
176
+ cdef unsigned short* d_i
177
+ cdef unsigned short* d_j
178
+ cdef int d_ij
179
+
180
+ # We use a binary matrix with one row per pair of vertices,
181
+ # so n * (n - 1) / 2 rows. Row u * n + v is a bitset whose 1 bits are
182
+ # the vertices located on a shortest path from vertex u to v
183
+ #
184
+ # Note that u < v
185
+ binary_matrix_init(self._cache_hull_pairs, n * (n - 1) / 2, n)
186
+ binary_matrix_fill(self._cache_hull_pairs, 0)
187
+ cdef bitset_t * p_bitset = self._cache_hull_pairs.rows
188
+
189
+ # Filling the cache
190
+ #
191
+ # The p_bitset variable iterates over the successive elements of the cache
192
+ #
193
+ # For any pair i, j of vertices (i < j), we built the bitset of all the
194
+ # elements k which are on a shortest path from i to j
195
+
196
+ for i in range(n):
197
+ # Caching the distances from i to the other vertices
198
+ d_i = c_distances + n * i
199
+
200
+ for j in range(i + 1, n):
201
+ # Caching the distances from j to the other vertices
202
+ d_j = c_distances + n * j
203
+
204
+ # Caching the distance between i and j
205
+ d_ij = d_i[j]
206
+
207
+ # Filling it
208
+ for k in range(n):
209
+ if d_i[k] + d_j[k] == d_ij:
210
+ bitset_add(p_bitset[0], k)
211
+
212
+ # Next bitset !
213
+ p_bitset = p_bitset + 1
214
+
215
+ sig_free(c_distances)
216
+
217
+ def __dealloc__(self):
218
+ r"""
219
+ Destructor.
220
+
221
+ EXAMPLES::
222
+
223
+ sage: from sage.graphs.convexity_properties import ConvexityProperties
224
+ sage: g = graphs.PetersenGraph()
225
+ sage: ConvexityProperties(g)
226
+ <sage.graphs.convexity_properties.ConvexityProperties object at ...>
227
+ """
228
+ binary_matrix_free(self._cache_hull_pairs)
229
+
230
+ cdef list _vertices_to_integers(self, vertices):
231
+ r"""
232
+ Convert a list of vertices to a list of integers with the cached data.
233
+ """
234
+ return [self._dict_vertices_to_integers[v] for v in vertices]
235
+
236
+ cdef list _integers_to_vertices(self, list integers):
237
+ r"""
238
+ Convert a list of integers to a list of vertices with the cached data.
239
+ """
240
+ cdef int i
241
+ return [self._list_integers_to_vertices[i] for i in integers]
242
+
243
+ cdef _bitset_convex_hull(self, bitset_t hull):
244
+ r"""
245
+ Compute the convex hull of a list of vertices given as a bitset.
246
+
247
+ (this method returns nothing and modifies the input)
248
+ """
249
+ cdef int count
250
+ cdef int tmp_count
251
+ cdef int i, j
252
+
253
+ cdef bitset_t * p_bitset
254
+
255
+ # Current size of the set
256
+ count = bitset_len(hull)
257
+
258
+ while True:
259
+
260
+ # Iterating over all the elements in the cache
261
+ p_bitset = self._cache_hull_pairs.rows
262
+
263
+ # For any vertex i
264
+ for i in range(self._n - 1):
265
+
266
+ # If i is not in the current set, we skip it !
267
+ if not bitset_in(hull, i):
268
+ p_bitset = p_bitset + (self._n - 1 - i)
269
+ continue
270
+
271
+ # If it is, we iterate over all the elements j
272
+ for j in range(i + 1, self._n):
273
+
274
+ # If both i and j are inside, we add all the (cached)
275
+ # vertices on a shortest ij-path
276
+
277
+ if bitset_in(hull, j):
278
+ bitset_union(hull, hull, p_bitset[0])
279
+
280
+ # Next bitset !
281
+ p_bitset = p_bitset + 1
282
+
283
+ tmp_count = bitset_len(hull)
284
+
285
+ # If we added nothing new during the previous loop, our set is
286
+ # convex !
287
+ if tmp_count == count:
288
+ return
289
+
290
+ # Otherwise, update and back to the loop
291
+ count = tmp_count
292
+
293
+ cpdef hull(self, list vertices):
294
+ r"""
295
+ Return the convex hull of a set of vertices.
296
+
297
+ INPUT:
298
+
299
+ - ``vertices`` -- list of vertices
300
+
301
+ EXAMPLES::
302
+
303
+ sage: from sage.graphs.convexity_properties import ConvexityProperties
304
+ sage: g = graphs.PetersenGraph()
305
+ sage: CP = ConvexityProperties(g)
306
+ sage: CP.hull([1, 3])
307
+ [1, 2, 3]
308
+ """
309
+ cdef bitset_t bs
310
+ bitset_init(bs, self._n)
311
+ bitset_set_first_n(bs, 0)
312
+
313
+ for v in vertices:
314
+ bitset_add(bs, <mp_bitcnt_t> self._dict_vertices_to_integers[v])
315
+
316
+ self._bitset_convex_hull(bs)
317
+
318
+ cdef list answer = self._integers_to_vertices(bitset_list(bs))
319
+
320
+ bitset_free(bs)
321
+
322
+ return answer
323
+
324
+ cdef _greedy_increase(self, bitset_t bs):
325
+ r"""
326
+ Given a bitset whose hull is not the whole set, greedily add vertices
327
+ and stop before its hull is the whole set.
328
+
329
+ .. NOTE::
330
+
331
+ * Counting the bits at each turn is not the best way...
332
+ """
333
+ cdef bitset_t tmp
334
+ bitset_init(tmp, self._n)
335
+
336
+ for i in range(self._n):
337
+ if not bitset_in(bs, i):
338
+ bitset_copy(tmp, bs)
339
+ bitset_add(tmp, i)
340
+ self._bitset_convex_hull(tmp)
341
+ if bitset_len(tmp) < self._n:
342
+ bitset_add(bs, i)
343
+
344
+ bitset_free(tmp)
345
+
346
+ cpdef hull_number(self, value_only=True, verbose=False):
347
+ r"""
348
+ Compute the hull number and a corresponding generating set.
349
+
350
+ The hull number `hn(G)` of a graph `G` is the cardinality of a smallest
351
+ set of vertices `S` such that `h(S)=V(G)`.
352
+
353
+ INPUT:
354
+
355
+ - ``value_only`` -- boolean (default: ``True``); whether to return only
356
+ the hull number (default) or a minimum set whose convex hull is the
357
+ whole graph
358
+
359
+ - ``verbose`` -- boolean (default: ``False``); whether to display
360
+ information on the LP
361
+
362
+ **COMPLEXITY:**
363
+
364
+ This problem is NP-Hard [HLT1993]_, but seems to be of the "nice" kind.
365
+ Update this comment if you fall on hard instances `:-)`
366
+
367
+ **ALGORITHM:**
368
+
369
+ This is solved by linear programming.
370
+
371
+ As the function `h(S)` associating to each set `S` its convex hull is a
372
+ closure operator, it is clear that any set `S_G` of vertices such that
373
+ `h(S_G)=V(G)` must satisfy `S_G \not \subseteq C` for any *proper*
374
+ convex set `C \subsetneq V(G)`. The following formulation is hence
375
+ correct
376
+
377
+ .. MATH::
378
+
379
+ \text{Minimize :}& \sum_{v\in G}b_v\\
380
+ \text{Such that :}&\\
381
+ &\forall C\subsetneq V(G)\text{ a proper convex set }\\
382
+ &\sum_{v\in V(G)\backslash C} b_v \geq 1
383
+
384
+ Of course, the number of convex sets -- and so the number of constraints
385
+ -- can be huge, and hard to enumerate, so at first an incomplete
386
+ formulation is solved (it is missing some constraints). If the answer
387
+ returned by the LP solver is a set `S` generating the whole graph, then
388
+ it is optimal and so is returned. Otherwise, the constraint
389
+ corresponding to the set `h(S)` can be added to the LP, which makes the
390
+ answer `S` infeasible, and another solution computed.
391
+
392
+ This being said, simply adding the constraint corresponding to `h(S)` is
393
+ a bit slow, as these sets can be large (and the corresponding constraint
394
+ a bit weak). To improve it a bit, before being added, the set `h(S)` is
395
+ "greedily enriched" to a set `S'` with vertices for as long as
396
+ `h(S')\neq V(G)`. This way, we obtain a set `S'` with `h(S)\subseteq
397
+ h(S')\subsetneq V(G)`, and the constraint corresponding to `h(S')` --
398
+ which is stronger than the one corresponding to `h(S)` -- is added.
399
+
400
+ This can actually be seen as a hitting set problem on the complement of
401
+ convex sets.
402
+
403
+ EXAMPLES:
404
+
405
+ The Hull number of Petersen's graph::
406
+
407
+ sage: from sage.graphs.convexity_properties import ConvexityProperties
408
+ sage: g = graphs.PetersenGraph()
409
+ sage: CP = ConvexityProperties(g)
410
+ sage: CP.hull_number() # needs sage.numerical.mip
411
+ 3
412
+ sage: generating_set = CP.hull_number(value_only=False) # needs sage.numerical.mip
413
+ sage: CP.hull(generating_set) # needs sage.numerical.mip
414
+ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
415
+ """
416
+ cdef int i
417
+ cdef list constraint # temporary variable to add constraints to the LP
418
+
419
+ if self._n <= 2:
420
+ if value_only:
421
+ return self._n
422
+ else:
423
+ return self._list_integers_to_vertices
424
+
425
+ cdef GenericBackend p = <GenericBackend> get_solver(constraint_generation=True)
426
+
427
+ # Minimization
428
+ p.set_sense(False)
429
+
430
+ # We have exactly n binary variables, all of them with a coefficient of
431
+ # 1 in the objective function
432
+ p.add_variables(self._n, 0, None, True, False, False, 1, None)
433
+
434
+ # We know that at least 2 vertices are required to cover the whole graph
435
+ p.add_linear_constraint([(i, 1) for i in range(self._n)], 2, None)
436
+
437
+ # The set of vertices generated by the current LP solution
438
+ cdef bitset_t current_hull
439
+ bitset_init(current_hull, self._n)
440
+
441
+ # Which is at first empty
442
+ bitset_set_first_n(current_hull, 1)
443
+
444
+ while True:
445
+
446
+ # Greedily increase it to obtain a better constraint
447
+ self._greedy_increase(current_hull)
448
+
449
+ if verbose:
450
+ print("Adding a constraint corresponding to convex set ",
451
+ end="")
452
+ print(bitset_list(current_hull))
453
+
454
+ # Building the corresponding constraint
455
+ constraint = []
456
+ for i in range(self._n):
457
+ if not bitset_in(current_hull, i):
458
+ constraint.append((i, 1))
459
+
460
+ p.add_linear_constraint(constraint, 1, None)
461
+
462
+ p.solve()
463
+
464
+ # Computing the current solution's convex hull
465
+ bitset_set_first_n(current_hull, 0)
466
+
467
+ for i in range(self._n):
468
+ if p.get_variable_value(i) > .5:
469
+ bitset_add(current_hull, i)
470
+
471
+ self._bitset_convex_hull(current_hull)
472
+
473
+ # Are we done ?
474
+ if bitset_len(current_hull) == self._n:
475
+ break
476
+
477
+ bitset_free(current_hull)
478
+
479
+ if value_only:
480
+ return <int> p.get_objective_value()
481
+
482
+ constraint = []
483
+ for i in range(self._n):
484
+ if p.get_variable_value(i) > .5:
485
+ constraint.append(i)
486
+
487
+ return self._integers_to_vertices(constraint)
488
+
489
+
490
+ def geodetic_closure(G, S):
491
+ r"""
492
+ Return the geodetic closure of the set of vertices `S` in `G`.
493
+
494
+ The geodetic closure `g(S)` of a subset of vertices `S` of a graph `G` is in
495
+ [HLT1993]_ as the set of all vertices that lie on a shortest `u-v` path for
496
+ any pair of vertices `u,v \in S`. We assume that `g(\emptyset) = \emptyset`
497
+ and that `g(\{u\}) = \{u\}` for any `u` in `G`.
498
+
499
+ .. WARNING::
500
+
501
+ This operation is **not** a closure function. Indeed, a closure function
502
+ must satisfy the property that `f(f(X))` should be equal to `f(X)`,
503
+ which is not always the case here. The term ``closure`` is used here to
504
+ follow the terminology of the domain. See for instance [HLT1993]_.
505
+
506
+ Here, we implement a simple algorithm to determine this set. Roughly, for
507
+ each vertex `u \in S`, the algorithm first performs a breadth first search
508
+ from `u` to get distances, and then identifies the vertices of `G` lying on
509
+ a shortest path from `u` to any `v\in S` using a reversal traversal from
510
+ vertices in `S`. This algorithm has time complexity in `O(|S|(n + m))` for
511
+ ``SparseGraph``, `O(|S|(n + m) + n^2)` for ``DenseGraph`` and
512
+ space complexity in `O(n + m)`.
513
+
514
+ INPUT:
515
+
516
+ - ``G`` -- a Sage graph
517
+
518
+ - ``S`` -- a subset of vertices of `G`
519
+
520
+ EXAMPLES:
521
+
522
+ The vertices of the Petersen graph can be obtained by a geodetic closure of
523
+ four of its vertices::
524
+
525
+ sage: from sage.graphs.convexity_properties import geodetic_closure
526
+ sage: G = graphs.PetersenGraph()
527
+ sage: geodetic_closure(G, [0, 2, 8, 9])
528
+ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
529
+
530
+ The vertices of a 2D grid can be obtained by a geodetic closure of
531
+ two vertices::
532
+
533
+ sage: G = graphs.Grid2dGraph(4, 4)
534
+ sage: c = G.geodetic_closure([(0, 0), (3, 3)])
535
+ sage: len(c) == G.order()
536
+ True
537
+
538
+ If two vertices belong to different connected components of a graph, their
539
+ geodetic closure is trivial::
540
+
541
+ sage: G = Graph([(0, 1), (2, 3)])
542
+ sage: geodetic_closure(G, [0, 2])
543
+ [0, 2]
544
+
545
+ The geodetic closure does not satisfy the closure function property that
546
+ `f(f(X))` should be equal to `f(X)`::
547
+
548
+ sage: G = graphs.DiamondGraph()
549
+ sage: G.subdivide_edge((1, 2), 1)
550
+ sage: geodetic_closure(G, [0, 3])
551
+ [0, 1, 2, 3]
552
+ sage: geodetic_closure(G, geodetic_closure(G, [0, 3]))
553
+ [0, 1, 2, 3, 4]
554
+
555
+ TESTS::
556
+
557
+ sage: G = graphs.DiamondGraph()
558
+ sage: geodetic_closure(G, [])
559
+ []
560
+ sage: geodetic_closure(G, [1])
561
+ [1]
562
+ sage: S = geodetic_closure(G, list(G))
563
+ sage: all(u in G for u in S) and len(S) == G.order()
564
+ True
565
+ sage: S = geodetic_closure(G, G)
566
+ sage: all(u in G for u in S) and len(S) == G.order()
567
+ True
568
+ sage: geodetic_closure(G, [1, 'foo'])
569
+ Traceback (most recent call last):
570
+ ...
571
+ ValueError: S is not a subset of vertices of the graph
572
+ sage: geodetic_closure(digraphs.Path(3), [0, 1])
573
+ Traceback (most recent call last):
574
+ ...
575
+ NotImplementedError: the geodetic closure of digraphs has not been implemented yet
576
+ """
577
+ if G.is_directed():
578
+ raise NotImplementedError("the geodetic closure of digraphs has not been implemented yet")
579
+ S = list(S)
580
+ if not S:
581
+ return []
582
+ elif any(v not in G for v in S):
583
+ raise ValueError("S is not a subset of vertices of the graph")
584
+ elif len(S) == 1 or len(S) == G.order():
585
+ return S
586
+ elif not G.is_connected():
587
+ L = []
588
+ for g in G.connected_components_subgraphs():
589
+ Sg = [u for u in S if u in g]
590
+ L.extend(geodetic_closure(g, Sg))
591
+ return L
592
+
593
+ cdef int n = G.order()
594
+ cdef int nS = len(S)
595
+ cdef list int_to_vertex = list(G)
596
+ cdef dict vertex_to_int = {u: i for i, u in enumerate(int_to_vertex)}
597
+ cdef list S_int = [vertex_to_int[u] for u in S]
598
+
599
+ # Copy the graph as a short digraph
600
+ cdef short_digraph sd
601
+ init_short_digraph(sd, G, edge_labelled=False, vertex_list=int_to_vertex)
602
+
603
+ # Allocate some data structures
604
+ cdef MemoryAllocator mem = MemoryAllocator()
605
+ cdef uint32_t * distances = <uint32_t *> mem.malloc(n * sizeof(uint32_t))
606
+ cdef uint32_t * waiting_list = <uint32_t *> mem.malloc(n * sizeof(uint32_t))
607
+ if not distances or not waiting_list:
608
+ free_short_digraph(sd)
609
+ raise MemoryError()
610
+ cdef bitset_t seen
611
+ cdef bitset_t visited
612
+ cdef bitset_t closure
613
+ bitset_init(seen, n)
614
+ bitset_init(visited, n)
615
+ bitset_init(closure, n)
616
+
617
+ cdef int ui, vi, xi, yi, i_begin, i_end, i, j, k
618
+
619
+ # Vertices in S are in the closure
620
+ bitset_clear(closure)
621
+ for ui in S_int:
622
+ bitset_add(closure, ui)
623
+
624
+ # We now explore geodesics between vertices in S, and we avoid visiting
625
+ # twice the geodesics between u and v
626
+ for i in range(nS - 1):
627
+ ui = S_int[i]
628
+
629
+ # Compute distances from ui using BFS
630
+ _ = simple_BFS(sd, ui, distances, NULL, waiting_list, seen)
631
+
632
+ # Perform a reverse BFS from vertices in S, following only vertices
633
+ # along a shortest path from ui to identify vertices of the geodetic
634
+ # closure.
635
+ bitset_clear(visited)
636
+ i_begin = 0
637
+ i_end = 0
638
+ for j in range(i + 1, nS):
639
+ vi = S_int[j]
640
+ if not bitset_in(seen, vi) or bitset_in(visited, vi):
641
+ # vi is not reachable from ui using BFS or has already been
642
+ # considered for the geodetic closure from ui
643
+ continue
644
+
645
+ # We explore all vertices on a shortest path from ui to vi
646
+ waiting_list[i_end] = vi
647
+ i_end += 1
648
+ bitset_add(visited, vi)
649
+
650
+ while i_begin < i_end:
651
+ xi = waiting_list[i_begin]
652
+ i_begin += 1
653
+
654
+ for k in range(out_degree(sd, xi)):
655
+ yi = sd.neighbors[xi][k]
656
+ if distances[xi] == distances[yi] + 1:
657
+ # yi in on a shortest path to ui
658
+ if not bitset_in(visited, yi):
659
+ waiting_list[i_end] = yi
660
+ i_end += 1
661
+ bitset_add(visited, yi)
662
+
663
+ # Now, visited contains all vertices on geodesics from ui to any other
664
+ # vertex in S
665
+ bitset_union(closure, closure, visited)
666
+
667
+ cdef list ret = [int_to_vertex[ui] for ui in range(n) if bitset_in(closure, ui)]
668
+
669
+ bitset_free(seen)
670
+ bitset_free(visited)
671
+ bitset_free(closure)
672
+ free_short_digraph(sd)
673
+
674
+ return ret
675
+
676
+
677
+ def is_geodetic(G):
678
+ r"""
679
+ Check whether the input (di)graph is geodetic.
680
+
681
+ A graph `G` is *geodetic* if there exists only one shortest path between
682
+ every pair of its vertices. This can be checked in time `O(nm)` for
683
+ ``SparseGraph`` and `O(nm+n^2)` for ``DenseGraph`` in unweighted (di)graphs
684
+ with `n` nodes and `m` edges. Examples of geodetic graphs are trees, cliques
685
+ and odd cycles. See the :wikipedia:`Geodetic_graph` for more details.
686
+
687
+ (Di)graphs with multiple edges are not considered geodetic.
688
+
689
+ INPUT:
690
+
691
+ - ``G`` -- a graph or a digraph
692
+
693
+ EXAMPLES:
694
+
695
+ Trees, cliques and odd cycles are geodetic::
696
+
697
+ sage: T = graphs.RandomTree(20)
698
+ sage: T.is_geodetic()
699
+ True
700
+ sage: all(graphs.CompleteGraph(n).is_geodetic() for n in range(8))
701
+ True
702
+ sage: all(graphs.CycleGraph(n).is_geodetic() for n in range(3, 16, 2))
703
+ True
704
+
705
+ Even cycles of order at least 4 are not geodetic::
706
+
707
+ sage: all(graphs.CycleGraph(n).is_geodetic() for n in range(4, 17, 2))
708
+ False
709
+
710
+ The Petersen graph is geodetic::
711
+
712
+ sage: P = graphs.PetersenGraph()
713
+ sage: P.is_geodetic()
714
+ True
715
+
716
+ Grid graphs are not geodetic::
717
+
718
+ sage: G = graphs.Grid2dGraph(2, 3)
719
+ sage: G.is_geodetic()
720
+ False
721
+
722
+ This method is also valid for digraphs::
723
+
724
+ sage: G = DiGraph(graphs.PetersenGraph())
725
+ sage: G.is_geodetic()
726
+ True
727
+ sage: G = digraphs.Path(5)
728
+ sage: G.add_path([0, 'a', 'b', 'c', 4])
729
+ sage: G.is_geodetic()
730
+ False
731
+
732
+ TESTS::
733
+
734
+ sage: all(g.is_geodetic() for g in graphs(3)) # needs nauty
735
+ True
736
+ sage: all((2*g).is_geodetic() for g in graphs(3)) # needs nauty
737
+ True
738
+ sage: G = graphs.CycleGraph(5)
739
+ sage: G.allow_loops(True)
740
+ sage: G.add_edges([(u, u) for u in G])
741
+ sage: G.is_geodetic()
742
+ True
743
+ sage: G.allow_multiple_edges(True)
744
+ sage: G.is_geodetic()
745
+ True
746
+ sage: G.add_edge(G.random_edge())
747
+ sage: G.is_geodetic()
748
+ False
749
+ """
750
+ if G.has_multiple_edges():
751
+ return False
752
+
753
+ if G.order() < 4:
754
+ return True
755
+
756
+ # Copy the graph as a short digraph
757
+ cdef int n = G.order()
758
+ cdef short_digraph sd
759
+ init_short_digraph(sd, G, edge_labelled=False, vertex_list=list(G))
760
+
761
+ # Allocate some data structures
762
+ cdef MemoryAllocator mem = MemoryAllocator()
763
+ cdef uint32_t * distances = <uint32_t *> mem.malloc(n * sizeof(uint32_t))
764
+ cdef uint32_t * waiting_list = <uint32_t *> mem.malloc(n * sizeof(uint32_t))
765
+ if not distances or not waiting_list:
766
+ free_short_digraph(sd)
767
+ raise MemoryError()
768
+ cdef bitset_t seen
769
+ bitset_init(seen, n)
770
+
771
+ # We now explore geodesics between vertices in S, and we avoid visiting
772
+ # twice the geodesics between u and v
773
+
774
+ cdef uint32_t source, u, v
775
+ cdef uint32_t waiting_beginning
776
+ cdef uint32_t waiting_end
777
+ cdef uint32_t * p_tmp
778
+ cdef uint32_t * end
779
+ cdef uint32_t ** p_vertices = sd.neighbors
780
+
781
+ for source in range(n):
782
+
783
+ # Compute distances from source using BFS
784
+ bitset_clear(seen)
785
+ bitset_add(seen, source)
786
+ distances[source] = 0
787
+ waiting_beginning = 0
788
+ waiting_end = 0
789
+ waiting_list[waiting_beginning] = source
790
+
791
+ # For as long as there are vertices left to explore
792
+ while waiting_beginning <= waiting_end:
793
+
794
+ # We pick the first one
795
+ v = waiting_list[waiting_beginning]
796
+ p_tmp = p_vertices[v]
797
+ end = p_vertices[v + 1]
798
+
799
+ # and we iterate over all the outneighbors u of v
800
+ while p_tmp < end:
801
+ u = p_tmp[0]
802
+
803
+ # If we notice one of these neighbors is not seen yet, we set
804
+ # its parameters and add it to the queue to be explored later.
805
+ # Otherwise, we check whether we have detected a second shortest
806
+ # path between source and v.
807
+ if not bitset_in(seen, u):
808
+ distances[u] = distances[v] + 1
809
+ bitset_add(seen, u)
810
+ waiting_end += 1
811
+ waiting_list[waiting_end] = u
812
+ elif distances[u] == distances[v] + 1:
813
+ # G is not geodetic
814
+ bitset_free(seen)
815
+ free_short_digraph(sd)
816
+ return False
817
+
818
+ p_tmp += 1
819
+
820
+ # We go to the next vertex in the queue
821
+ waiting_beginning += 1
822
+
823
+ bitset_free(seen)
824
+ free_short_digraph(sd)
825
+
826
+ # The graph is geodetic
827
+ return True