passagemath-graphs 10.6.1rc1__cp310-cp310-musllinux_1_2_aarch64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (260) hide show
  1. passagemath_graphs-10.6.1rc1.dist-info/METADATA +292 -0
  2. passagemath_graphs-10.6.1rc1.dist-info/RECORD +260 -0
  3. passagemath_graphs-10.6.1rc1.dist-info/WHEEL +5 -0
  4. passagemath_graphs-10.6.1rc1.dist-info/top_level.txt +2 -0
  5. passagemath_graphs.libs/libgcc_s-69c45f16.so.1 +0 -0
  6. passagemath_graphs.libs/libgmp-8e78bd9b.so.10.5.0 +0 -0
  7. passagemath_graphs.libs/libstdc++-1f1a71be.so.6.0.33 +0 -0
  8. sage/all__sagemath_graphs.py +39 -0
  9. sage/combinat/abstract_tree.py +2723 -0
  10. sage/combinat/all__sagemath_graphs.py +34 -0
  11. sage/combinat/binary_tree.py +5306 -0
  12. sage/combinat/cluster_algebra_quiver/all.py +22 -0
  13. sage/combinat/cluster_algebra_quiver/cluster_seed.py +5208 -0
  14. sage/combinat/cluster_algebra_quiver/interact.py +124 -0
  15. sage/combinat/cluster_algebra_quiver/mutation_class.py +625 -0
  16. sage/combinat/cluster_algebra_quiver/mutation_type.py +1555 -0
  17. sage/combinat/cluster_algebra_quiver/quiver.py +2290 -0
  18. sage/combinat/cluster_algebra_quiver/quiver_mutation_type.py +2468 -0
  19. sage/combinat/designs/MOLS_handbook_data.py +570 -0
  20. sage/combinat/designs/all.py +58 -0
  21. sage/combinat/designs/bibd.py +1655 -0
  22. sage/combinat/designs/block_design.py +1071 -0
  23. sage/combinat/designs/covering_array.py +269 -0
  24. sage/combinat/designs/covering_design.py +530 -0
  25. sage/combinat/designs/database.py +5615 -0
  26. sage/combinat/designs/design_catalog.py +122 -0
  27. sage/combinat/designs/designs_pyx.cpython-310-aarch64-linux-gnu.so +0 -0
  28. sage/combinat/designs/designs_pyx.pxd +21 -0
  29. sage/combinat/designs/designs_pyx.pyx +993 -0
  30. sage/combinat/designs/difference_family.py +3951 -0
  31. sage/combinat/designs/difference_matrices.py +279 -0
  32. sage/combinat/designs/evenly_distributed_sets.cpython-310-aarch64-linux-gnu.so +0 -0
  33. sage/combinat/designs/evenly_distributed_sets.pyx +661 -0
  34. sage/combinat/designs/ext_rep.py +1064 -0
  35. sage/combinat/designs/gen_quadrangles_with_spread.cpython-310-aarch64-linux-gnu.so +0 -0
  36. sage/combinat/designs/gen_quadrangles_with_spread.pyx +339 -0
  37. sage/combinat/designs/group_divisible_designs.py +361 -0
  38. sage/combinat/designs/incidence_structures.py +2357 -0
  39. sage/combinat/designs/latin_squares.py +581 -0
  40. sage/combinat/designs/orthogonal_arrays.py +2244 -0
  41. sage/combinat/designs/orthogonal_arrays_build_recursive.py +1780 -0
  42. sage/combinat/designs/orthogonal_arrays_find_recursive.cpython-310-aarch64-linux-gnu.so +0 -0
  43. sage/combinat/designs/orthogonal_arrays_find_recursive.pyx +967 -0
  44. sage/combinat/designs/resolvable_bibd.py +815 -0
  45. sage/combinat/designs/steiner_quadruple_systems.py +1306 -0
  46. sage/combinat/designs/subhypergraph_search.cpython-310-aarch64-linux-gnu.so +0 -0
  47. sage/combinat/designs/subhypergraph_search.pyx +530 -0
  48. sage/combinat/designs/twographs.py +306 -0
  49. sage/combinat/finite_state_machine.py +14874 -0
  50. sage/combinat/finite_state_machine_generators.py +2006 -0
  51. sage/combinat/graph_path.py +448 -0
  52. sage/combinat/interval_posets.py +3908 -0
  53. sage/combinat/nu_tamari_lattice.py +269 -0
  54. sage/combinat/ordered_tree.py +1446 -0
  55. sage/combinat/posets/all.py +46 -0
  56. sage/combinat/posets/bubble_shuffle.py +247 -0
  57. sage/combinat/posets/cartesian_product.py +493 -0
  58. sage/combinat/posets/d_complete.py +182 -0
  59. sage/combinat/posets/elements.py +273 -0
  60. sage/combinat/posets/forest.py +30 -0
  61. sage/combinat/posets/hasse_cython.cpython-310-aarch64-linux-gnu.so +0 -0
  62. sage/combinat/posets/hasse_cython.pyx +174 -0
  63. sage/combinat/posets/hasse_diagram.py +3672 -0
  64. sage/combinat/posets/hochschild_lattice.py +158 -0
  65. sage/combinat/posets/incidence_algebras.py +794 -0
  66. sage/combinat/posets/lattices.py +5117 -0
  67. sage/combinat/posets/linear_extension_iterator.cpython-310-aarch64-linux-gnu.so +0 -0
  68. sage/combinat/posets/linear_extension_iterator.pyx +292 -0
  69. sage/combinat/posets/linear_extensions.py +1037 -0
  70. sage/combinat/posets/mobile.py +275 -0
  71. sage/combinat/posets/moebius_algebra.py +776 -0
  72. sage/combinat/posets/poset_examples.py +2178 -0
  73. sage/combinat/posets/posets.py +9360 -0
  74. sage/combinat/rooted_tree.py +1070 -0
  75. sage/combinat/shard_order.py +239 -0
  76. sage/combinat/tamari_lattices.py +384 -0
  77. sage/combinat/yang_baxter_graph.py +923 -0
  78. sage/databases/all__sagemath_graphs.py +1 -0
  79. sage/databases/knotinfo_db.py +1231 -0
  80. sage/ext_data/all__sagemath_graphs.py +1 -0
  81. sage/ext_data/graphs/graph_plot_js.html +330 -0
  82. sage/ext_data/kenzo/CP2.txt +45 -0
  83. sage/ext_data/kenzo/CP3.txt +349 -0
  84. sage/ext_data/kenzo/CP4.txt +4774 -0
  85. sage/ext_data/kenzo/README.txt +49 -0
  86. sage/ext_data/kenzo/S4.txt +20 -0
  87. sage/graphs/all.py +42 -0
  88. sage/graphs/asteroidal_triples.cpython-310-aarch64-linux-gnu.so +0 -0
  89. sage/graphs/asteroidal_triples.pyx +320 -0
  90. sage/graphs/base/all.py +1 -0
  91. sage/graphs/base/boost_graph.cpython-310-aarch64-linux-gnu.so +0 -0
  92. sage/graphs/base/boost_graph.pxd +106 -0
  93. sage/graphs/base/boost_graph.pyx +3045 -0
  94. sage/graphs/base/c_graph.cpython-310-aarch64-linux-gnu.so +0 -0
  95. sage/graphs/base/c_graph.pxd +106 -0
  96. sage/graphs/base/c_graph.pyx +5096 -0
  97. sage/graphs/base/dense_graph.cpython-310-aarch64-linux-gnu.so +0 -0
  98. sage/graphs/base/dense_graph.pxd +28 -0
  99. sage/graphs/base/dense_graph.pyx +801 -0
  100. sage/graphs/base/graph_backends.cpython-310-aarch64-linux-gnu.so +0 -0
  101. sage/graphs/base/graph_backends.pxd +5 -0
  102. sage/graphs/base/graph_backends.pyx +797 -0
  103. sage/graphs/base/overview.py +85 -0
  104. sage/graphs/base/sparse_graph.cpython-310-aarch64-linux-gnu.so +0 -0
  105. sage/graphs/base/sparse_graph.pxd +90 -0
  106. sage/graphs/base/sparse_graph.pyx +1653 -0
  107. sage/graphs/base/static_dense_graph.cpython-310-aarch64-linux-gnu.so +0 -0
  108. sage/graphs/base/static_dense_graph.pxd +5 -0
  109. sage/graphs/base/static_dense_graph.pyx +1032 -0
  110. sage/graphs/base/static_sparse_backend.cpython-310-aarch64-linux-gnu.so +0 -0
  111. sage/graphs/base/static_sparse_backend.pxd +27 -0
  112. sage/graphs/base/static_sparse_backend.pyx +1583 -0
  113. sage/graphs/base/static_sparse_graph.cpython-310-aarch64-linux-gnu.so +0 -0
  114. sage/graphs/base/static_sparse_graph.pxd +37 -0
  115. sage/graphs/base/static_sparse_graph.pyx +1375 -0
  116. sage/graphs/bipartite_graph.py +2732 -0
  117. sage/graphs/centrality.cpython-310-aarch64-linux-gnu.so +0 -0
  118. sage/graphs/centrality.pyx +1038 -0
  119. sage/graphs/cographs.py +519 -0
  120. sage/graphs/comparability.cpython-310-aarch64-linux-gnu.so +0 -0
  121. sage/graphs/comparability.pyx +851 -0
  122. sage/graphs/connectivity.cpython-310-aarch64-linux-gnu.so +0 -0
  123. sage/graphs/connectivity.pxd +157 -0
  124. sage/graphs/connectivity.pyx +4813 -0
  125. sage/graphs/convexity_properties.cpython-310-aarch64-linux-gnu.so +0 -0
  126. sage/graphs/convexity_properties.pxd +16 -0
  127. sage/graphs/convexity_properties.pyx +870 -0
  128. sage/graphs/digraph.py +4754 -0
  129. sage/graphs/digraph_generators.py +1993 -0
  130. sage/graphs/distances_all_pairs.cpython-310-aarch64-linux-gnu.so +0 -0
  131. sage/graphs/distances_all_pairs.pxd +12 -0
  132. sage/graphs/distances_all_pairs.pyx +2938 -0
  133. sage/graphs/domination.py +1363 -0
  134. sage/graphs/dot2tex_utils.py +100 -0
  135. sage/graphs/edge_connectivity.cpython-310-aarch64-linux-gnu.so +0 -0
  136. sage/graphs/edge_connectivity.pyx +1215 -0
  137. sage/graphs/generators/all.py +1 -0
  138. sage/graphs/generators/basic.py +1769 -0
  139. sage/graphs/generators/chessboard.py +538 -0
  140. sage/graphs/generators/classical_geometries.py +1611 -0
  141. sage/graphs/generators/degree_sequence.py +235 -0
  142. sage/graphs/generators/distance_regular.cpython-310-aarch64-linux-gnu.so +0 -0
  143. sage/graphs/generators/distance_regular.pyx +2846 -0
  144. sage/graphs/generators/families.py +4759 -0
  145. sage/graphs/generators/intersection.py +565 -0
  146. sage/graphs/generators/platonic_solids.py +262 -0
  147. sage/graphs/generators/random.py +2623 -0
  148. sage/graphs/generators/smallgraphs.py +5741 -0
  149. sage/graphs/generators/world_map.py +724 -0
  150. sage/graphs/generic_graph.py +26867 -0
  151. sage/graphs/generic_graph_pyx.cpython-310-aarch64-linux-gnu.so +0 -0
  152. sage/graphs/generic_graph_pyx.pxd +34 -0
  153. sage/graphs/generic_graph_pyx.pyx +1673 -0
  154. sage/graphs/genus.cpython-310-aarch64-linux-gnu.so +0 -0
  155. sage/graphs/genus.pyx +622 -0
  156. sage/graphs/graph.py +9645 -0
  157. sage/graphs/graph_coloring.cpython-310-aarch64-linux-gnu.so +0 -0
  158. sage/graphs/graph_coloring.pyx +2284 -0
  159. sage/graphs/graph_database.py +1177 -0
  160. sage/graphs/graph_decompositions/all.py +1 -0
  161. sage/graphs/graph_decompositions/bandwidth.cpython-310-aarch64-linux-gnu.so +0 -0
  162. sage/graphs/graph_decompositions/bandwidth.pyx +428 -0
  163. sage/graphs/graph_decompositions/clique_separators.cpython-310-aarch64-linux-gnu.so +0 -0
  164. sage/graphs/graph_decompositions/clique_separators.pyx +616 -0
  165. sage/graphs/graph_decompositions/cutwidth.cpython-310-aarch64-linux-gnu.so +0 -0
  166. sage/graphs/graph_decompositions/cutwidth.pyx +753 -0
  167. sage/graphs/graph_decompositions/fast_digraph.cpython-310-aarch64-linux-gnu.so +0 -0
  168. sage/graphs/graph_decompositions/fast_digraph.pxd +13 -0
  169. sage/graphs/graph_decompositions/fast_digraph.pyx +212 -0
  170. sage/graphs/graph_decompositions/graph_products.cpython-310-aarch64-linux-gnu.so +0 -0
  171. sage/graphs/graph_decompositions/graph_products.pyx +508 -0
  172. sage/graphs/graph_decompositions/modular_decomposition.cpython-310-aarch64-linux-gnu.so +0 -0
  173. sage/graphs/graph_decompositions/modular_decomposition.pxd +27 -0
  174. sage/graphs/graph_decompositions/modular_decomposition.pyx +1536 -0
  175. sage/graphs/graph_decompositions/slice_decomposition.cpython-310-aarch64-linux-gnu.so +0 -0
  176. sage/graphs/graph_decompositions/slice_decomposition.pxd +18 -0
  177. sage/graphs/graph_decompositions/slice_decomposition.pyx +1106 -0
  178. sage/graphs/graph_decompositions/tree_decomposition.cpython-310-aarch64-linux-gnu.so +0 -0
  179. sage/graphs/graph_decompositions/tree_decomposition.pxd +17 -0
  180. sage/graphs/graph_decompositions/tree_decomposition.pyx +1996 -0
  181. sage/graphs/graph_decompositions/vertex_separation.cpython-310-aarch64-linux-gnu.so +0 -0
  182. sage/graphs/graph_decompositions/vertex_separation.pxd +5 -0
  183. sage/graphs/graph_decompositions/vertex_separation.pyx +1963 -0
  184. sage/graphs/graph_editor.py +82 -0
  185. sage/graphs/graph_generators.py +3314 -0
  186. sage/graphs/graph_generators_pyx.cpython-310-aarch64-linux-gnu.so +0 -0
  187. sage/graphs/graph_generators_pyx.pyx +95 -0
  188. sage/graphs/graph_input.py +812 -0
  189. sage/graphs/graph_latex.py +2064 -0
  190. sage/graphs/graph_list.py +410 -0
  191. sage/graphs/graph_plot.py +1756 -0
  192. sage/graphs/graph_plot_js.py +338 -0
  193. sage/graphs/hyperbolicity.cpython-310-aarch64-linux-gnu.so +0 -0
  194. sage/graphs/hyperbolicity.pyx +1704 -0
  195. sage/graphs/hypergraph_generators.py +364 -0
  196. sage/graphs/independent_sets.cpython-310-aarch64-linux-gnu.so +0 -0
  197. sage/graphs/independent_sets.pxd +13 -0
  198. sage/graphs/independent_sets.pyx +402 -0
  199. sage/graphs/isgci.py +1033 -0
  200. sage/graphs/isoperimetric_inequalities.cpython-310-aarch64-linux-gnu.so +0 -0
  201. sage/graphs/isoperimetric_inequalities.pyx +489 -0
  202. sage/graphs/line_graph.cpython-310-aarch64-linux-gnu.so +0 -0
  203. sage/graphs/line_graph.pyx +743 -0
  204. sage/graphs/lovasz_theta.py +77 -0
  205. sage/graphs/matching.py +1633 -0
  206. sage/graphs/matching_covered_graph.py +3590 -0
  207. sage/graphs/orientations.py +1489 -0
  208. sage/graphs/partial_cube.py +459 -0
  209. sage/graphs/path_enumeration.cpython-310-aarch64-linux-gnu.so +0 -0
  210. sage/graphs/path_enumeration.pyx +2040 -0
  211. sage/graphs/pq_trees.py +1129 -0
  212. sage/graphs/print_graphs.py +201 -0
  213. sage/graphs/schnyder.py +865 -0
  214. sage/graphs/spanning_tree.cpython-310-aarch64-linux-gnu.so +0 -0
  215. sage/graphs/spanning_tree.pyx +1457 -0
  216. sage/graphs/strongly_regular_db.cpython-310-aarch64-linux-gnu.so +0 -0
  217. sage/graphs/strongly_regular_db.pyx +3340 -0
  218. sage/graphs/traversals.cpython-310-aarch64-linux-gnu.so +0 -0
  219. sage/graphs/traversals.pxd +9 -0
  220. sage/graphs/traversals.pyx +1872 -0
  221. sage/graphs/trees.cpython-310-aarch64-linux-gnu.so +0 -0
  222. sage/graphs/trees.pxd +15 -0
  223. sage/graphs/trees.pyx +310 -0
  224. sage/graphs/tutte_polynomial.py +713 -0
  225. sage/graphs/views.cpython-310-aarch64-linux-gnu.so +0 -0
  226. sage/graphs/views.pyx +794 -0
  227. sage/graphs/weakly_chordal.cpython-310-aarch64-linux-gnu.so +0 -0
  228. sage/graphs/weakly_chordal.pyx +604 -0
  229. sage/groups/all__sagemath_graphs.py +1 -0
  230. sage/groups/perm_gps/all__sagemath_graphs.py +1 -0
  231. sage/groups/perm_gps/partn_ref/all__sagemath_graphs.py +1 -0
  232. sage/groups/perm_gps/partn_ref/refinement_graphs.cpython-310-aarch64-linux-gnu.so +0 -0
  233. sage/groups/perm_gps/partn_ref/refinement_graphs.pxd +38 -0
  234. sage/groups/perm_gps/partn_ref/refinement_graphs.pyx +1666 -0
  235. sage/knots/all.py +6 -0
  236. sage/knots/free_knotinfo_monoid.py +507 -0
  237. sage/knots/gauss_code.py +291 -0
  238. sage/knots/knot.py +682 -0
  239. sage/knots/knot_table.py +284 -0
  240. sage/knots/knotinfo.py +2900 -0
  241. sage/knots/link.py +4715 -0
  242. sage/sandpiles/all.py +13 -0
  243. sage/sandpiles/examples.py +225 -0
  244. sage/sandpiles/sandpile.py +6365 -0
  245. sage/topology/all.py +22 -0
  246. sage/topology/cell_complex.py +1214 -0
  247. sage/topology/cubical_complex.py +1976 -0
  248. sage/topology/delta_complex.py +1806 -0
  249. sage/topology/filtered_simplicial_complex.py +744 -0
  250. sage/topology/moment_angle_complex.py +823 -0
  251. sage/topology/simplicial_complex.py +5160 -0
  252. sage/topology/simplicial_complex_catalog.py +92 -0
  253. sage/topology/simplicial_complex_examples.py +1680 -0
  254. sage/topology/simplicial_complex_homset.py +205 -0
  255. sage/topology/simplicial_complex_morphism.py +836 -0
  256. sage/topology/simplicial_set.py +4102 -0
  257. sage/topology/simplicial_set_catalog.py +55 -0
  258. sage/topology/simplicial_set_constructions.py +2954 -0
  259. sage/topology/simplicial_set_examples.py +865 -0
  260. sage/topology/simplicial_set_morphism.py +1464 -0
@@ -0,0 +1,851 @@
1
+ # sage_setup: distribution = sagemath-graphs
2
+ # cython: binding=True
3
+ r"""
4
+ Comparability and permutation graphs
5
+
6
+ This module implements method related to :wikipedia:`Comparability_graph` and
7
+ :wikipedia:`Permutation_graph`, that is, for the moment, only recognition
8
+ algorithms.
9
+
10
+ Most of the information found here can also be found in [ST1994]_ or [Sha1997]_.
11
+
12
+ The following methods are implemented in this module
13
+
14
+ .. csv-table::
15
+ :class: contentstable
16
+ :widths: 30, 70
17
+ :delim: |
18
+
19
+ :meth:`~is_comparability_MILP` | Check whether the graph is a comparability graph (MILP)
20
+ :meth:`~greedy_is_comparability` | Check whether the graph is a comparability graph (greedy algorithm)
21
+ :meth:`~greedy_is_comparability_with_certificate` | Check whether the graph is a comparability graph and returns certificates (greedy algorithm)
22
+ :meth:`~is_comparability` | Check whether the graph is a comparability graph
23
+ :meth:`~is_permutation` | Check whether the graph is a permutation graph.
24
+ :meth:`~is_transitive` | Check whether the digraph is transitive.
25
+
26
+ Author:
27
+
28
+ - Nathann Cohen 2012-04
29
+
30
+ Graph classes
31
+ -------------
32
+
33
+ **Comparability graphs**
34
+
35
+ A graph is a comparability graph if it can be obtained from a poset by adding an
36
+ edge between any two elements that are comparable. Co-comparability graph are
37
+ complements of such graphs, i.e. graphs built from a poset by adding an edge
38
+ between any two incomparable elements.
39
+
40
+ For more information on comparability graphs, see the
41
+ :wikipedia:`Comparability_graph`.
42
+
43
+ **Permutation graphs**
44
+
45
+ Definitions:
46
+
47
+ - A permutation `\pi = \pi_1\pi_2\dots\pi_n` defines a graph on `n` vertices
48
+ such that `i\sim j` when `\pi` reverses `i` and `j` (i.e. when `i<j` and
49
+ `\pi_j < \pi_i`. A graph is a permutation graph whenever it can be built
50
+ through this construction.
51
+
52
+ - A graph is a permutation graph if it can be build from two parallel lines are
53
+ the intersection graph of segments intersecting both lines.
54
+
55
+ - A graph is a permutation graph if it is both a comparability graph and a
56
+ co-comparability graph.
57
+
58
+ For more information on permutation graphs, see the
59
+ :wikipedia:`Permutation_graph`.
60
+
61
+
62
+ Recognition algorithm for comparability graphs
63
+ ----------------------------------------------
64
+
65
+ **Greedy algorithm**
66
+
67
+ This algorithm attempts to build a transitive orientation of a given graph `G`,
68
+ that is an orientation `D` such that for any directed `uv`-path of `D` there
69
+ exists in `D` an edge `uv`. This already determines a notion of equivalence
70
+ between some edges of `G` :
71
+
72
+ In `G`, two edges `uv` and `uv'` (incident to a common vertex `u`) such that
73
+ `vv'\not\in G` need necessarily be oriented *the same way* (that is that they
74
+ should either both *leave* or both *enter* `u`). Indeed, if one enters `G`
75
+ while the other leaves it, these two edges form a path of length two, which is
76
+ not possible in any transitive orientation of `G` as `vv'\not\in G`.
77
+
78
+ Hence, we can say that in this case a *directed edge* `uv` is equivalent to a
79
+ *directed edge* `uv'` (to mean that if one belongs to the transitive
80
+ orientation, the other one must be present too) in the same way that `vu` is
81
+ equivalent to `v'u`. We can thus define equivalence classes on oriented edges,
82
+ to represent set of edges that imply each other. We can thus define `C^G_{uv}`
83
+ to be the equivalence class in `G` of the oriented edge `uv`.
84
+
85
+ Of course, if there exists a transitive orientation of a graph `G`, then no edge
86
+ `uv` implies its contrary `vu`, i.e. it is necessary to ensure that `\forall
87
+ uv\in G, vu\not\in C^G_{uv}`. The key result on which the greedy algorithm is
88
+ built is the following (see [ST1994]_):
89
+
90
+ **Theorem** -- The following statements are equivalent :
91
+
92
+ - `G` is a comparability graph
93
+ - `\forall uv\in G, vu\not\in C^G_{uv}`
94
+ - The edges of `G` can be partitioned into `B_1,...,B_k` where `B_i` is the
95
+ equivalence class of some oriented edge in `G-B_1-\dots-B_{i-1}`
96
+
97
+ Hence, ensuring that a graph is a comparability graph can be done by checking
98
+ that no equivalence class is contradictory. Building the orientation, however,
99
+ requires to build equivalence classes step by step until an orientation has been
100
+ found for all of them.
101
+
102
+ **Mixed Integer Linear Program**
103
+
104
+ A MILP formulation is available to check the other methods for correction. It is
105
+ easily built :
106
+
107
+ To each edge are associated two binary variables (one for each possible
108
+ direction). We then ensure that each triangle is transitively oriented, and
109
+ that each pair of incident edges `uv, uv'` such that `vv'\not\in G` do not
110
+ create a 2-path.
111
+
112
+ Here is the formulation:
113
+
114
+ .. MATH::
115
+
116
+ \mbox{Maximize : }&\mbox{Nothing}\\
117
+ \mbox{Such that : }&\\
118
+ &\forall uv\in G\\
119
+ &\cdot o_{uv}+o_{vu} = 1\\
120
+ &\forall u\in G, \forall v,v'\in N(v)\text{ such that }vv'\not\in G\\
121
+ &\cdot o_{uv} + o_{v'u} - o_{v'v} \leq 1\\
122
+ &\cdot o_{uv'} + o_{vu} - o_{vv'} \leq 1\\
123
+ &\forall u\in G, \forall v,v'\in N(v)\text{ such that }vv'\in G\\
124
+ &\cdot o_{uv} + o_{v'u} \leq 1\\
125
+ &\cdot o_{uv'} + o_{vu} \leq 1\\
126
+ &o_{uv}\text{ is a binary variable}\\
127
+
128
+ .. NOTE::
129
+
130
+ The MILP formulation is usually much slower than the greedy algorithm. This
131
+ MILP has been implemented to check the results of the greedy algorithm that
132
+ has been implemented to check the results of a faster algorithm which has not
133
+ been implemented yet.
134
+
135
+ Certificates
136
+ ------------
137
+
138
+ **Comparability graphs**
139
+
140
+ The *yes*-certificates that a graph is a comparability graphs are transitive
141
+ orientations of it. The *no*-certificates, on the other hand, are odd cycles of
142
+ such graph. These odd cycles have the property that around each vertex `v` of
143
+ the cycle its two incident edges must have the same orientation (toward `v`, or
144
+ outward `v`) in any transitive orientation of the graph. This is impossible
145
+ whenever the cycle has odd length. Explanations are given in the "Greedy
146
+ algorithm" part of the previous section.
147
+
148
+ **Permutation graphs**
149
+
150
+ Permutation graphs are precisely the intersection of comparability graphs and
151
+ co-comparability graphs. Hence, negative certificates are precisely negative
152
+ certificates of comparability or co-comparability. Positive certificates are a
153
+ pair of permutations that can be used through
154
+ :meth:`~sage.graphs.graph_generators.GraphGenerators.PermutationGraph` (whose
155
+ documentation says more about what these permutations represent).
156
+
157
+ Implementation details
158
+ ----------------------
159
+
160
+ **Test that the equivalence classes are not self-contradictory**
161
+
162
+ This is done by a call to :meth:`Graph.is_bipartite`, and here is how :
163
+
164
+ Around a vertex `u`, any two edges `uv, uv'` such that `vv'\not\in G` are
165
+ equivalent. Hence, the equivalence class of edges around a vertex are
166
+ precisely the connected components of the complement of the graph induced by
167
+ the neighbors of `u`.
168
+
169
+ In each equivalence class (around a given vertex `u`), the edges should all
170
+ have the same orientation, i.e. all should go toward `u` at the same time, or
171
+ leave it at the same time. To represent this, we create a graph with vertices
172
+ for all equivalent classes around all vertices of `G`, and link `(v, C)` to
173
+ `(u, C')` if `u\in C` and `v\in C'`.
174
+
175
+ A bipartite coloring of this graph with colors 0 and 1 tells us that the
176
+ edges of an equivalence class `C` around `u` should be directed toward `u` if
177
+ `(u, C)` is colored with `0`, and outward if `(u, C)` is colored with `1`.
178
+
179
+ If the graph is not bipartite, this is the proof that some equivalence class
180
+ is self-contradictory !
181
+
182
+
183
+ .. NOTE::
184
+
185
+ The greedy algorithm implemented here is just there to check the correction
186
+ of more complicated ones, and it is reaaaaaaaaaaaalllly bad whenever you
187
+ look at it with performance in mind.
188
+
189
+ Methods
190
+ -------
191
+ """
192
+
193
+ # ****************************************************************************
194
+ # Copyright (C) 2012 Nathann Cohen <nathann.cohen@gmail.com>
195
+ #
196
+ # This program is free software: you can redistribute it and/or modify
197
+ # it under the terms of the GNU General Public License as published by
198
+ # the Free Software Foundation, either version 2 of the License, or
199
+ # (at your option) any later version.
200
+ # https://www.gnu.org/licenses/
201
+ # ****************************************************************************
202
+
203
+ from libc.stdint cimport uint32_t
204
+ from memory_allocator cimport MemoryAllocator
205
+
206
+ from sage.data_structures.bitset_base cimport *
207
+ from sage.graphs.base.static_sparse_backend cimport StaticSparseCGraph
208
+ from sage.graphs.base.static_sparse_backend cimport StaticSparseBackend
209
+ from sage.graphs.base.static_sparse_graph cimport (short_digraph,
210
+ init_short_digraph,
211
+ free_short_digraph,
212
+ simple_BFS,
213
+ out_degree)
214
+
215
+ from copy import copy
216
+
217
+
218
+ #####################
219
+ # Greedy Algorithms #
220
+ #####################
221
+
222
+ def greedy_is_comparability(g, no_certificate=False, equivalence_class=False):
223
+ r"""
224
+ Check whether the graph is a comparability graph (greedy algorithm).
225
+
226
+ This method only returns no-certificates.
227
+
228
+ To understand how this method works, please consult the documentation of the
229
+ :mod:`comparability module <sage.graphs.comparability>`.
230
+
231
+ INPUT:
232
+
233
+ - ``g`` -- a graph
234
+
235
+ - ``no_certificate`` -- whether to return a *no*-certificate when the graph
236
+ is not a comparability graph. This certificate is an odd cycle of edges,
237
+ each of which implies the next. It is set to ``False`` by default.
238
+
239
+ - ``equivalence_class`` -- whether to return an equivalence class
240
+ if the graph is a comparability graph
241
+
242
+ OUTPUT:
243
+
244
+ - If the graph is a comparability graph and ``no_certificate = False``, this
245
+ method returns ``True`` or ``(True, an_equivalence_class)`` according to
246
+ the value of ``equivalence_class``.
247
+
248
+ - If the graph is *not* a comparability graph, this method returns ``False``
249
+ or ``(False, odd_cycle)`` according to the value of ``no_certificate``.
250
+
251
+ EXAMPLES:
252
+
253
+ The Petersen Graph is not transitively orientable::
254
+
255
+ sage: from sage.graphs.comparability import greedy_is_comparability as is_comparability
256
+ sage: g = graphs.PetersenGraph()
257
+ sage: is_comparability(g)
258
+ False
259
+ sage: is_comparability(g, no_certificate=True)
260
+ (False, [2, 1, 0, 4, 3, 2])
261
+
262
+ But the Bull graph is::
263
+
264
+ sage: g = graphs.BullGraph()
265
+ sage: is_comparability(g)
266
+ True
267
+
268
+ TESTS:
269
+
270
+ Check that the method is working even when vertices are of incomparable
271
+ types::
272
+
273
+ sage: from sage.graphs.comparability import greedy_is_comparability
274
+ sage: G = Graph([('a', 1), (1, 2), (2, 3)])
275
+ sage: greedy_is_comparability(G, equivalence_class=True)
276
+ (True, [('a', 1), (2, 1), (2, 3)])
277
+ """
278
+ cdef int i, j
279
+
280
+ # Each vertex can partition its neighbors into equivalence classes
281
+ cdef dict equivalence_classes = {}
282
+ for v in g:
283
+ equivalence_classes[v] = g.subgraph(vertices=g.neighbors(v)).complement().connected_components(sort=False)
284
+
285
+ # We build a graph h with one vertex per (vertex of g + equivalence class)
286
+ from sage.graphs.graph import Graph
287
+ h = Graph()
288
+ h.add_vertices([(v, i) for v in g for i in range(len(equivalence_classes[v]))])
289
+
290
+ # We add an edge between two vertices of h if they represent
291
+ # opposed equivalence classes
292
+
293
+ for u, v in g.edge_iterator(labels=False):
294
+
295
+ for i, s in enumerate(equivalence_classes[v]):
296
+ if u in s:
297
+ break
298
+
299
+ for j, s in enumerate(equivalence_classes[u]):
300
+ if v in s:
301
+ break
302
+
303
+ h.add_edge((v, i), (u, j))
304
+
305
+ # Is it a comparability graph ?
306
+
307
+ cdef int isit
308
+ isit, certif = h.is_bipartite(certificate=True)
309
+
310
+ if isit:
311
+ if equivalence_class:
312
+ # We use a mapping between vertices and integers to deal with
313
+ # vertices of different types
314
+ int_to_vertex = list(g)
315
+ vertex_to_int = {u: i for i, u in enumerate(int_to_vertex)}
316
+
317
+ # Returning the largest equivalence class
318
+ cc = max(h.connected_components(sort=False), key=len)
319
+
320
+ edges = []
321
+ for v, sid in cc:
322
+ s = equivalence_classes[v][sid]
323
+
324
+ # For each edge we pick the good orientations
325
+ vi = vertex_to_int[v]
326
+ if certif[v, sid] == 1:
327
+ edges.extend((vi, vertex_to_int[vv]) for vv in s)
328
+ else:
329
+ edges.extend((vertex_to_int[vv], vi) for vv in s)
330
+
331
+ # We return the value but take care of removing edges that were
332
+ # added twice.
333
+ edges = [(int_to_vertex[u], int_to_vertex[v]) for u, v in sorted(set(edges))]
334
+ return True, edges
335
+
336
+ return True
337
+
338
+ if no_certificate:
339
+ cycle = [v for v, _ in certif]
340
+ cycle.append(cycle[0])
341
+ return False, cycle
342
+ return False
343
+
344
+
345
+ def greedy_is_comparability_with_certificate(g, certificate=False):
346
+ r"""
347
+ Check whether the graph is a comparability graph and returns
348
+ certificates (greedy algorithm).
349
+
350
+ This method can return certificates of both *yes* and *no* answers.
351
+
352
+ To understand how this method works, please consult the documentation of the
353
+ :mod:`comparability module <sage.graphs.comparability>`.
354
+
355
+ INPUT:
356
+
357
+ - ``g`` -- a graph
358
+
359
+ - ``certificate`` -- boolean; whether to return a
360
+ certificate. *Yes*-answers the certificate is a transitive orientation of
361
+ `G`, and a *no* certificates is an odd cycle of sequentially forcing
362
+ edges.
363
+
364
+ EXAMPLES:
365
+
366
+ The 5-cycle or the Petersen Graph are not transitively orientable::
367
+
368
+ sage: from sage.graphs.comparability import greedy_is_comparability_with_certificate as is_comparability
369
+ sage: is_comparability(graphs.CycleGraph(5), certificate=True)
370
+ (False, [2, 1, 0, 4, 3, 2])
371
+ sage: g = graphs.PetersenGraph()
372
+ sage: is_comparability(g)
373
+ False
374
+ sage: is_comparability(g, certificate=True)
375
+ (False, [2, 1, 0, 4, 3, 2])
376
+
377
+ But the Bull graph is::
378
+
379
+ sage: g = graphs.BullGraph()
380
+ sage: is_comparability(g)
381
+ True
382
+ sage: is_comparability(g, certificate = True)
383
+ (True, Digraph on 5 vertices)
384
+ sage: is_comparability(g, certificate = True)[1].is_transitive()
385
+ True
386
+
387
+ TESTS:
388
+
389
+ Check that the method is working even when vertices are of incomparable
390
+ types::
391
+
392
+ sage: from sage.graphs.comparability import greedy_is_comparability_with_certificate
393
+ sage: G = Graph([('a', 1), (1, 2), (2, 3)])
394
+ sage: greedy_is_comparability_with_certificate(G, certificate=True)
395
+ (True, Digraph on 4 vertices)
396
+ """
397
+ isit, certif = greedy_is_comparability(g, no_certificate=True, equivalence_class=True)
398
+ if not isit:
399
+ if certificate:
400
+ return False, certif
401
+ return False
402
+
403
+ elif not certificate:
404
+ return True
405
+
406
+ gg = copy(g)
407
+ from sage.graphs.digraph import DiGraph
408
+ h = DiGraph()
409
+ h.add_vertices(gg)
410
+
411
+ for u, v in certif:
412
+ gg.delete_edge(u, v)
413
+ h.add_edge(u, v)
414
+
415
+ # While there are some edges left to be oriented
416
+ while gg.size():
417
+
418
+ # We take an equivalence class and orient it
419
+ isit, certif = greedy_is_comparability(gg, no_certificate=True, equivalence_class=True)
420
+
421
+ # Then remove it from the former graph
422
+ for u, v in certif:
423
+ gg.delete_edge(u, v)
424
+ h.add_edge(u, v)
425
+
426
+ return True, h
427
+
428
+
429
+ ###################
430
+ # Integer Program #
431
+ ###################
432
+
433
+ def is_comparability_MILP(g, certificate=False, solver=None, verbose=0):
434
+ r"""
435
+ Check whether the graph is a comparability graph (MILP).
436
+
437
+ INPUT:
438
+
439
+ - ``g`` -- a graph
440
+
441
+ - ``certificate`` -- boolean (default: ``False``); whether to return a
442
+ certificate for yes instances. This method cannot return negative
443
+ certificates.
444
+
445
+ - ``solver`` -- string (default: ``None``); specifies a Mixed Integer Linear
446
+ Programming (MILP) solver to be used. If set to ``None``, the default one
447
+ is used. For more information on MILP solvers and which default solver is
448
+ used, see the method :meth:`solve
449
+ <sage.numerical.mip.MixedIntegerLinearProgram.solve>` of the class
450
+ :class:`MixedIntegerLinearProgram
451
+ <sage.numerical.mip.MixedIntegerLinearProgram>`.
452
+
453
+ - ``verbose`` -- integer (default: 0); sets the level of verbosity. Set
454
+ to 0 by default, which means quiet.
455
+
456
+ EXAMPLES:
457
+
458
+ The 5-cycle or the Petersen Graph are not transitively orientable::
459
+
460
+ sage: from sage.graphs.comparability import is_comparability_MILP as is_comparability
461
+ sage: is_comparability(graphs.CycleGraph(5), certificate=True) # needs sage.numerical.mip
462
+ (False, None)
463
+ sage: g = graphs.PetersenGraph()
464
+ sage: is_comparability(g, certificate=True) # needs sage.numerical.mip
465
+ (False, None)
466
+
467
+ But the Bull graph is::
468
+
469
+ sage: g = graphs.BullGraph()
470
+ sage: is_comparability(g) # needs sage.numerical.mip
471
+ True
472
+ sage: is_comparability(g, certificate=True) # needs sage.numerical.mip
473
+ (True, Digraph on 5 vertices)
474
+ sage: is_comparability(g, certificate=True)[1].is_transitive() # needs sage.numerical.mip
475
+ True
476
+ """
477
+ from sage.numerical.mip import MixedIntegerLinearProgram, MIPSolverException
478
+ p = MixedIntegerLinearProgram(solver=solver)
479
+ o = p.new_variable(binary=True)
480
+
481
+ for u, v in g.edge_iterator(labels=False):
482
+ p.add_constraint(o[u, v] + o[v, u] == 1)
483
+
484
+ from itertools import combinations
485
+ for u in g:
486
+ for v, vv in combinations(g.neighbors(u), 2):
487
+
488
+ # If there is an edge between v and vv, we must be sure it is in the
489
+ # good direction when v-u-vv is a directed path
490
+ if g.has_edge(v, vv):
491
+ p.add_constraint(o[u, v] + o[vv, u] - o[vv, v] <= 1)
492
+ p.add_constraint(o[u, vv] + o[v, u] - o[v, vv] <= 1)
493
+
494
+ # If there is no edge, there are only two orientations possible (see
495
+ # the module's documentation about edges which imply each other)
496
+ else:
497
+ p.add_constraint(o[u, v] + o[vv, u] <= 1)
498
+ p.add_constraint(o[u, vv] + o[v, u] <= 1)
499
+
500
+ try:
501
+ p.solve(log=verbose)
502
+ if not certificate:
503
+ return True
504
+
505
+ # Building the transitive orientation
506
+ from sage.graphs.digraph import DiGraph
507
+ d = DiGraph()
508
+ d.add_vertices(g)
509
+
510
+ tol = 0 if p.base_ring().is_exact() else 1e-6
511
+ o = p.get_values(o, convert=True, tolerance=tol)
512
+ for u, v in g.edge_iterator(labels=False):
513
+ if o[u, v]:
514
+ d.add_edge(u, v)
515
+ else:
516
+ d.add_edge(v, u)
517
+
518
+ return True, d
519
+
520
+ except MIPSolverException:
521
+ if certificate:
522
+ return False, None
523
+ return False
524
+
525
+
526
+ ###############
527
+ # Empty shell #
528
+ ###############
529
+
530
+ def is_comparability(g, algorithm='greedy', certificate=False, check=True,
531
+ solver=None, verbose=0):
532
+ r"""
533
+ Check whether the graph is a comparability graph.
534
+
535
+ INPUT:
536
+
537
+ - ``g`` -- a graph
538
+
539
+ - ``algorithm`` -- string (default: ``'greedy'``); choose the implementation
540
+ used to do the test
541
+
542
+ - ``'greedy'`` -- a greedy algorithm (see the documentation of the
543
+ :mod:`comparability module <sage.graphs.comparability>`)
544
+
545
+ - ``'MILP'`` -- a Mixed Integer Linear Program formulation of the
546
+ problem. Beware, for this implementation is unable to return negative
547
+ certificates ! When ``certificate = True``, negative certificates are
548
+ always equal to ``None``. ``True`` certificates are valid, though.
549
+
550
+ - ``certificate`` -- boolean (default: ``False``); whether to return a
551
+ certificate. *Yes*-answers the certificate is a transitive orientation of
552
+ `G`, and a *no* certificates is an odd cycle of sequentially forcing
553
+ edges.
554
+
555
+ - ``check`` -- boolean (default: ``True``); whether to check that the
556
+ yes-certificates are indeed transitive. As it is very quick
557
+ compared to the rest of the operation, it is enabled by default.
558
+
559
+ - ``solver`` -- string (default: ``None``); specifies a Mixed Integer Linear
560
+ Programming (MILP) solver to be used. If set to ``None``, the default one
561
+ is used. For more information on MILP solvers and which default solver is
562
+ used, see the method :meth:`solve
563
+ <sage.numerical.mip.MixedIntegerLinearProgram.solve>` of the class
564
+ :class:`MixedIntegerLinearProgram
565
+ <sage.numerical.mip.MixedIntegerLinearProgram>`.
566
+
567
+ - ``verbose`` -- integer (default: 0); sets the level of verbosity. Set
568
+ to 0 by default, which means quiet.
569
+
570
+ EXAMPLES::
571
+
572
+ sage: from sage.graphs.comparability import is_comparability
573
+ sage: g = graphs.PetersenGraph()
574
+ sage: is_comparability(g)
575
+ False
576
+ sage: is_comparability(graphs.CompleteGraph(5), certificate=True)
577
+ (True, Digraph on 5 vertices)
578
+
579
+ TESTS:
580
+
581
+ Let us ensure that no exception is raised when we go over all small graphs::
582
+
583
+ sage: from sage.graphs.comparability import is_comparability
584
+ sage: [len([g for g in graphs(i) if is_comparability(g, certificate=True)[0]]) for i in range(7)] # needs nauty
585
+ [1, 1, 2, 4, 11, 33, 144]
586
+ """
587
+ g._scream_if_not_simple()
588
+ if not g.size():
589
+ if certificate:
590
+ from sage.graphs.digraph import DiGraph
591
+ return True, DiGraph(g)
592
+ return True
593
+
594
+ if algorithm == "greedy":
595
+ comparability_test = greedy_is_comparability_with_certificate(g, certificate=certificate)
596
+ elif algorithm == "MILP":
597
+ comparability_test = is_comparability_MILP(g, certificate=certificate,
598
+ solver=solver, verbose=verbose)
599
+
600
+ if not certificate:
601
+ return comparability_test
602
+
603
+ # Checking that the orientation found is indeed transitive. No
604
+ # reason why it should not, but no reason why we should not check
605
+ # anyway :-p
606
+ isit, certif = comparability_test
607
+
608
+ if check and isit and (not certif.is_transitive()):
609
+ raise ValueError("Looks like there is a bug somewhere. The "
610
+ "algorithm thinks that the orientation is "
611
+ "transitive, but we just checked and it is not. "
612
+ "Please report the bug on sage-devel, and give "
613
+ "us the graph that made this method fail !")
614
+
615
+ return isit, certif
616
+
617
+
618
+ def is_permutation(g, algorithm='greedy', certificate=False, check=True,
619
+ solver=None, verbose=0):
620
+ r"""
621
+ Check whether the graph is a permutation graph.
622
+
623
+ For more information on permutation graphs, refer to the documentation of
624
+ the :mod:`comparability module <sage.graphs.comparability>`.
625
+
626
+ INPUT:
627
+
628
+ - ``g`` -- a graph
629
+
630
+ - ``algorithm`` -- string (default: ``'greedy'``); choose the implementation
631
+ used for the subcalls to :meth:`is_comparability`
632
+
633
+ - ``'greedy'`` -- a greedy algorithm (see the documentation of the
634
+ :mod:`comparability module <sage.graphs.comparability>`)
635
+
636
+ - ``'MILP'`` -- a Mixed Integer Linear Program formulation of the
637
+ problem. Beware, for this implementation is unable to return negative
638
+ certificates ! When ``certificate = True``, negative certificates are
639
+ always equal to ``None``. ``True`` certificates are valid, though.
640
+
641
+ - ``certificate`` -- boolean (default: ``False``); whether to return a
642
+ certificate for the answer given. For ``True`` answers the certificate is
643
+ a permutation, for ``False`` answers it is a no-certificate for the test
644
+ of comparability or co-comparability.
645
+
646
+ - ``check`` -- boolean (default: ``True``); whether to check that the
647
+ permutations returned indeed create the expected Permutation graph. Pretty
648
+ cheap compared to the rest, hence a good investment. It is enabled by
649
+ default.
650
+
651
+ - ``solver`` -- string (default: ``None``); specifies a Mixed Integer Linear
652
+ Programming (MILP) solver to be used. If set to ``None``, the default one
653
+ is used. For more information on MILP solvers and which default solver is
654
+ used, see the method :meth:`solve
655
+ <sage.numerical.mip.MixedIntegerLinearProgram.solve>` of the class
656
+ :class:`MixedIntegerLinearProgram
657
+ <sage.numerical.mip.MixedIntegerLinearProgram>`.
658
+
659
+ - ``verbose`` -- integer (default: 0); sets the level of verbosity. Set
660
+ to 0 by default, which means quiet.
661
+
662
+ .. NOTE::
663
+
664
+ As the ``True`` certificate is a :class:`Permutation` object, the
665
+ segment intersection model of the permutation graph can be visualized
666
+ through a call to :meth:`Permutation.show
667
+ <sage.combinat.permutation.Permutation.show>`.
668
+
669
+ EXAMPLES:
670
+
671
+ A permutation realizing the bull graph::
672
+
673
+ sage: from sage.graphs.comparability import is_permutation
674
+ sage: g = graphs.BullGraph()
675
+ sage: _ , certif = is_permutation(g, certificate=True)
676
+ sage: h = graphs.PermutationGraph(*certif)
677
+ sage: h.is_isomorphic(g)
678
+ True
679
+
680
+ Plotting the realization as an intersection graph of segments::
681
+
682
+ sage: true, perm = is_permutation(g, certificate=True)
683
+ sage: p1 = Permutation([nn+1 for nn in perm[0]])
684
+ sage: p2 = Permutation([nn+1 for nn in perm[1]])
685
+ sage: p = p2 * p1.inverse()
686
+ sage: p.show(representation='braid') # needs sage.plot
687
+
688
+ TESTS:
689
+
690
+ Trying random permutations, first with the greedy algorithm::
691
+
692
+ sage: from sage.graphs.comparability import is_permutation
693
+ sage: for i in range(20):
694
+ ....: p = Permutations(10).random_element()
695
+ ....: g1 = graphs.PermutationGraph(p)
696
+ ....: isit, certif = is_permutation(g1, certificate=True)
697
+ ....: if not isit:
698
+ ....: print("Something is wrong here !!")
699
+ ....: break
700
+ ....: g2 = graphs.PermutationGraph(*certif)
701
+ ....: if not g1.is_isomorphic(g2):
702
+ ....: print("Something is wrong here !!")
703
+ ....: break
704
+
705
+ Then with MILP::
706
+
707
+ sage: from sage.graphs.comparability import is_permutation
708
+ sage: for i in range(20): # needs sage.numerical.mip
709
+ ....: p = Permutations(10).random_element()
710
+ ....: g1 = graphs.PermutationGraph(p)
711
+ ....: isit, certif = is_permutation(g1, algorithm='MILP', certificate=True)
712
+ ....: if not isit:
713
+ ....: print("Something is wrong here !!")
714
+ ....: break
715
+ ....: g2 = graphs.PermutationGraph(*certif)
716
+ ....: if not g1.is_isomorphic(g2):
717
+ ....: print("Something is wrong here !!")
718
+ ....: break
719
+ """
720
+ if not certificate:
721
+ # No certificate... A piece of cake
722
+ return (is_comparability(g, algorithm=algorithm, solver=solver, verbose=verbose) and
723
+ is_comparability(g.complement(), algorithm=algorithm, solver=solver, verbose=verbose))
724
+
725
+ # First poset, we stop if it fails
726
+ isit, certif = is_comparability(g, algorithm=algorithm, certificate=True,
727
+ solver=solver, verbose=verbose)
728
+ if not isit:
729
+ return False, certif
730
+
731
+ # Second poset
732
+ isit, co_certif = is_comparability(g.complement(), algorithm=algorithm, certificate=True,
733
+ solver=solver, verbose=verbose)
734
+ if not isit:
735
+ return False, co_certif
736
+
737
+ # Building the two orderings
738
+ tmp = list(co_certif.edges(labels=False, sort=False))
739
+ for u, v in certif.edge_iterator(labels=False):
740
+ co_certif.add_edge(v, u)
741
+ certif.add_edges(tmp)
742
+
743
+ ordering = certif.topological_sort()
744
+ co_ordering = co_certif.topological_sort()
745
+
746
+ # Try to build the Permutation graph from the permutations, just to make
747
+ # sure nothing weird happened !
748
+ if check:
749
+ from sage.graphs.graph_generators import GraphGenerators
750
+ pg = GraphGenerators().PermutationGraph(ordering, co_ordering)
751
+ if not pg.is_isomorphic(g):
752
+ raise ValueError("There is a mistake somewhere ! It looks like "
753
+ "the Permutation Graph model computed does "
754
+ "not match the input graph !")
755
+
756
+ return True, (ordering, co_ordering)
757
+
758
+
759
+ def is_transitive(g, certificate=False):
760
+ r"""
761
+ Check whether the digraph is transitive.
762
+
763
+ A digraph is transitive if for any pair of vertices `u,v\in G` linked by a
764
+ `uv`-path the edge `uv` belongs to `G`.
765
+
766
+ INPUT:
767
+
768
+ - ``g`` -- a digraph
769
+
770
+ - ``certificate`` -- boolean (default: ``False``); whether to return a
771
+ certificate for negative answers
772
+
773
+ - If ``certificate = False`` (default), this method returns ``True`` or
774
+ ``False`` according to the graph.
775
+
776
+ - If ``certificate = True``, this method either returns ``True`` answers
777
+ or yield a pair of vertices `uv` such that there exists a `uv`-path in
778
+ `G` but `uv\not\in G`.
779
+
780
+ EXAMPLES::
781
+
782
+ sage: digraphs.Circuit(4).is_transitive()
783
+ False
784
+ sage: digraphs.Circuit(4).is_transitive(certificate=True)
785
+ (0, 2)
786
+ sage: digraphs.RandomDirectedGNP(30,.2).is_transitive()
787
+ False
788
+ sage: D = digraphs.DeBruijn(5, 2) # needs sage.combinat
789
+ sage: D.is_transitive() # needs sage.combinat
790
+ False
791
+ sage: cert = D.is_transitive(certificate=True) # needs sage.combinat
792
+ sage: D.has_edge(*cert) # needs sage.combinat
793
+ False
794
+ sage: bool(D.shortest_path(*cert)) # needs sage.combinat
795
+ True
796
+ sage: digraphs.RandomDirectedGNP(20,.2).transitive_closure().is_transitive() # needs networkx
797
+ True
798
+ """
799
+ cdef int n = g.order()
800
+ if n <= 2:
801
+ return True
802
+
803
+ # Copying the whole graph to obtain the list of neighbors quicker than by
804
+ # calling out_neighbors. This data structure is well documented in the
805
+ # module sage.graphs.base.static_sparse_graph
806
+ cdef list int_to_vertex
807
+ cdef StaticSparseCGraph cg
808
+ cdef short_digraph sd
809
+ if isinstance(g, StaticSparseBackend):
810
+ cg = <StaticSparseCGraph> g._cg
811
+ sd = <short_digraph> cg.g
812
+ int_to_vertex = cg._vertex_to_labels
813
+ else:
814
+ int_to_vertex = list(g)
815
+ init_short_digraph(sd, g, edge_labelled=False, vertex_list=int_to_vertex)
816
+
817
+ cdef MemoryAllocator mem = MemoryAllocator()
818
+ cdef uint32_t * distances = <uint32_t *> mem.malloc(n * sizeof(uint32_t))
819
+ cdef uint32_t * waiting_list = <uint32_t *> mem.malloc(n * sizeof(uint32_t))
820
+ cdef bitset_t seen
821
+ bitset_init(seen, n)
822
+
823
+ cdef uint32_t u, v
824
+ cdef int i
825
+
826
+ for u in range(n):
827
+
828
+ # 1. perform a breadth first search from u
829
+ _ = simple_BFS(sd, u, distances, NULL, waiting_list, seen)
830
+
831
+ # 2. Check whether the BFS reaches vertices that are not in the closed
832
+ # neighborhood of u.
833
+ if bitset_len(seen) != out_degree(sd, u) + 1:
834
+ if certificate:
835
+ bitset_discard(seen, u)
836
+ for i in range(out_degree(sd, u)):
837
+ bitset_discard(seen, sd.neighbors[u][i])
838
+ v = bitset_first(seen)
839
+
840
+ bitset_free(seen)
841
+ if not isinstance(g, StaticSparseBackend):
842
+ free_short_digraph(sd)
843
+ if certificate:
844
+ return (int_to_vertex[u], int_to_vertex[v])
845
+ return False
846
+
847
+ bitset_free(seen)
848
+ if not isinstance(g, StaticSparseBackend):
849
+ free_short_digraph(sd)
850
+
851
+ return True