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,813 @@
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 cysignals.memory cimport sig_free
204
+ from sage.graphs.distances_all_pairs cimport c_distances_all_pairs
205
+ from copy import copy
206
+
207
+
208
+ #####################
209
+ # Greedy Algorithms #
210
+ #####################
211
+
212
+ def greedy_is_comparability(g, no_certificate=False, equivalence_class=False):
213
+ r"""
214
+ Check whether the graph is a comparability graph (greedy algorithm).
215
+
216
+ This method only returns no-certificates.
217
+
218
+ To understand how this method works, please consult the documentation of the
219
+ :mod:`comparability module <sage.graphs.comparability>`.
220
+
221
+ INPUT:
222
+
223
+ - ``g`` -- a graph
224
+
225
+ - ``no_certificate`` -- whether to return a *no*-certificate when the graph
226
+ is not a comparability graph. This certificate is an odd cycle of edges,
227
+ each of which implies the next. It is set to ``False`` by default.
228
+
229
+ - ``equivalence_class`` -- whether to return an equivalence class
230
+ if the graph is a comparability graph
231
+
232
+ OUTPUT:
233
+
234
+ - If the graph is a comparability graph and ``no_certificate = False``, this
235
+ method returns ``True`` or ``(True, an_equivalence_class)`` according to
236
+ the value of ``equivalence_class``.
237
+
238
+ - If the graph is *not* a comparability graph, this method returns ``False``
239
+ or ``(False, odd_cycle)`` according to the value of ``no_certificate``.
240
+
241
+ EXAMPLES:
242
+
243
+ The Petersen Graph is not transitively orientable::
244
+
245
+ sage: from sage.graphs.comparability import greedy_is_comparability as is_comparability
246
+ sage: g = graphs.PetersenGraph()
247
+ sage: is_comparability(g)
248
+ False
249
+ sage: is_comparability(g, no_certificate=True)
250
+ (False, [2, 1, 0, 4, 3, 2])
251
+
252
+ But the Bull graph is::
253
+
254
+ sage: g = graphs.BullGraph()
255
+ sage: is_comparability(g)
256
+ True
257
+
258
+ TESTS:
259
+
260
+ Check that the method is working even when vertices are of incomparable
261
+ types::
262
+
263
+ sage: from sage.graphs.comparability import greedy_is_comparability
264
+ sage: G = Graph([('a', 1), (1, 2), (2, 3)])
265
+ sage: greedy_is_comparability(G, equivalence_class=True)
266
+ (True, [('a', 1), (2, 1), (2, 3)])
267
+ """
268
+ cdef int i, j
269
+
270
+ # Each vertex can partition its neighbors into equivalence classes
271
+ cdef dict equivalence_classes = {}
272
+ for v in g:
273
+ equivalence_classes[v] = g.subgraph(vertices=g.neighbors(v)).complement().connected_components(sort=False)
274
+
275
+ # We build a graph h with one vertex per (vertex of g + equivalence class)
276
+ from sage.graphs.graph import Graph
277
+ h = Graph()
278
+ h.add_vertices([(v, i) for v in g for i in range(len(equivalence_classes[v]))])
279
+
280
+ # We add an edge between two vertices of h if they represent
281
+ # opposed equivalence classes
282
+
283
+ for u, v in g.edge_iterator(labels=False):
284
+
285
+ for i, s in enumerate(equivalence_classes[v]):
286
+ if u in s:
287
+ break
288
+
289
+ for j, s in enumerate(equivalence_classes[u]):
290
+ if v in s:
291
+ break
292
+
293
+ h.add_edge((v, i), (u, j))
294
+
295
+ # Is it a comparability graph ?
296
+
297
+ cdef int isit
298
+ isit, certif = h.is_bipartite(certificate=True)
299
+
300
+ if isit:
301
+ if equivalence_class:
302
+ # We use a mapping between vertices and integers to deal with
303
+ # vertices of different types
304
+ int_to_vertex = list(g)
305
+ vertex_to_int = {u: i for i, u in enumerate(int_to_vertex)}
306
+
307
+ # Returning the largest equivalence class
308
+ cc = max(h.connected_components(sort=False), key=len)
309
+
310
+ edges = []
311
+ for v, sid in cc:
312
+ s = equivalence_classes[v][sid]
313
+
314
+ # For each edge we pick the good orientations
315
+ vi = vertex_to_int[v]
316
+ if certif[v, sid] == 1:
317
+ edges.extend((vi, vertex_to_int[vv]) for vv in s)
318
+ else:
319
+ edges.extend((vertex_to_int[vv], vi) for vv in s)
320
+
321
+ # We return the value but take care of removing edges that were
322
+ # added twice.
323
+ edges = [(int_to_vertex[u], int_to_vertex[v]) for u, v in sorted(set(edges))]
324
+ return True, edges
325
+
326
+ return True
327
+
328
+ if no_certificate:
329
+ cycle = [v for v, _ in certif]
330
+ cycle.append(cycle[0])
331
+ return False, cycle
332
+ return False
333
+
334
+
335
+ def greedy_is_comparability_with_certificate(g, certificate=False):
336
+ r"""
337
+ Check whether the graph is a comparability graph and returns
338
+ certificates (greedy algorithm).
339
+
340
+ This method can return certificates of both *yes* and *no* answers.
341
+
342
+ To understand how this method works, please consult the documentation of the
343
+ :mod:`comparability module <sage.graphs.comparability>`.
344
+
345
+ INPUT:
346
+
347
+ - ``g`` -- a graph
348
+
349
+ - ``certificate`` -- boolean; whether to return a
350
+ certificate. *Yes*-answers the certificate is a transitive orientation of
351
+ `G`, and a *no* certificates is an odd cycle of sequentially forcing
352
+ edges.
353
+
354
+ EXAMPLES:
355
+
356
+ The 5-cycle or the Petersen Graph are not transitively orientable::
357
+
358
+ sage: from sage.graphs.comparability import greedy_is_comparability_with_certificate as is_comparability
359
+ sage: is_comparability(graphs.CycleGraph(5), certificate=True)
360
+ (False, [2, 1, 0, 4, 3, 2])
361
+ sage: g = graphs.PetersenGraph()
362
+ sage: is_comparability(g)
363
+ False
364
+ sage: is_comparability(g, certificate=True)
365
+ (False, [2, 1, 0, 4, 3, 2])
366
+
367
+ But the Bull graph is::
368
+
369
+ sage: g = graphs.BullGraph()
370
+ sage: is_comparability(g)
371
+ True
372
+ sage: is_comparability(g, certificate = True)
373
+ (True, Digraph on 5 vertices)
374
+ sage: is_comparability(g, certificate = True)[1].is_transitive()
375
+ True
376
+
377
+ TESTS:
378
+
379
+ Check that the method is working even when vertices are of incomparable
380
+ types::
381
+
382
+ sage: from sage.graphs.comparability import greedy_is_comparability_with_certificate
383
+ sage: G = Graph([('a', 1), (1, 2), (2, 3)])
384
+ sage: greedy_is_comparability_with_certificate(G, certificate=True)
385
+ (True, Digraph on 4 vertices)
386
+ """
387
+ isit, certif = greedy_is_comparability(g, no_certificate=True, equivalence_class=True)
388
+ if not isit:
389
+ if certificate:
390
+ return False, certif
391
+ return False
392
+
393
+ elif not certificate:
394
+ return True
395
+
396
+ gg = copy(g)
397
+ from sage.graphs.digraph import DiGraph
398
+ h = DiGraph()
399
+ h.add_vertices(gg)
400
+
401
+ for u, v in certif:
402
+ gg.delete_edge(u, v)
403
+ h.add_edge(u, v)
404
+
405
+ # While there are some edges left to be oriented
406
+ while gg.size():
407
+
408
+ # We take an equivalence class and orient it
409
+ isit, certif = greedy_is_comparability(gg, no_certificate=True, equivalence_class=True)
410
+
411
+ # Then remove it from the former graph
412
+ for u, v in certif:
413
+ gg.delete_edge(u, v)
414
+ h.add_edge(u, v)
415
+
416
+ return True, h
417
+
418
+
419
+ ###################
420
+ # Integer Program #
421
+ ###################
422
+
423
+ def is_comparability_MILP(g, certificate=False, solver=None, verbose=0):
424
+ r"""
425
+ Check whether the graph is a comparability graph (MILP).
426
+
427
+ INPUT:
428
+
429
+ - ``g`` -- a graph
430
+
431
+ - ``certificate`` -- boolean (default: ``False``); whether to return a
432
+ certificate for yes instances. This method cannot return negative
433
+ certificates.
434
+
435
+ - ``solver`` -- string (default: ``None``); specifies a Mixed Integer Linear
436
+ Programming (MILP) solver to be used. If set to ``None``, the default one
437
+ is used. For more information on MILP solvers and which default solver is
438
+ used, see the method :meth:`solve
439
+ <sage.numerical.mip.MixedIntegerLinearProgram.solve>` of the class
440
+ :class:`MixedIntegerLinearProgram
441
+ <sage.numerical.mip.MixedIntegerLinearProgram>`.
442
+
443
+ - ``verbose`` -- integer (default: 0); sets the level of verbosity. Set
444
+ to 0 by default, which means quiet.
445
+
446
+ EXAMPLES:
447
+
448
+ The 5-cycle or the Petersen Graph are not transitively orientable::
449
+
450
+ sage: from sage.graphs.comparability import is_comparability_MILP as is_comparability
451
+ sage: is_comparability(graphs.CycleGraph(5), certificate=True) # needs sage.numerical.mip
452
+ (False, None)
453
+ sage: g = graphs.PetersenGraph()
454
+ sage: is_comparability(g, certificate=True) # needs sage.numerical.mip
455
+ (False, None)
456
+
457
+ But the Bull graph is::
458
+
459
+ sage: g = graphs.BullGraph()
460
+ sage: is_comparability(g) # needs sage.numerical.mip
461
+ True
462
+ sage: is_comparability(g, certificate=True) # needs sage.numerical.mip
463
+ (True, Digraph on 5 vertices)
464
+ sage: is_comparability(g, certificate=True)[1].is_transitive() # needs sage.numerical.mip
465
+ True
466
+ """
467
+ from sage.numerical.mip import MixedIntegerLinearProgram, MIPSolverException
468
+ p = MixedIntegerLinearProgram(solver=solver)
469
+ o = p.new_variable(binary=True)
470
+
471
+ for u, v in g.edge_iterator(labels=False):
472
+ p.add_constraint(o[u, v] + o[v, u] == 1)
473
+
474
+ from itertools import combinations
475
+ for u in g:
476
+ for v, vv in combinations(g.neighbors(u), 2):
477
+
478
+ # If there is an edge between v and vv, we must be sure it is in the
479
+ # good direction when v-u-vv is a directed path
480
+ if g.has_edge(v, vv):
481
+ p.add_constraint(o[u, v] + o[vv, u] - o[vv, v] <= 1)
482
+ p.add_constraint(o[u, vv] + o[v, u] - o[v, vv] <= 1)
483
+
484
+ # If there is no edge, there are only two orientations possible (see
485
+ # the module's documentation about edges which imply each other)
486
+ else:
487
+ p.add_constraint(o[u, v] + o[vv, u] <= 1)
488
+ p.add_constraint(o[u, vv] + o[v, u] <= 1)
489
+
490
+ try:
491
+ p.solve(log=verbose)
492
+ if not certificate:
493
+ return True
494
+
495
+ # Building the transitive orientation
496
+ from sage.graphs.digraph import DiGraph
497
+ d = DiGraph()
498
+ d.add_vertices(g)
499
+
500
+ tol = 0 if p.base_ring().is_exact() else 1e-6
501
+ o = p.get_values(o, convert=True, tolerance=tol)
502
+ for u, v in g.edge_iterator(labels=False):
503
+ if o[u, v]:
504
+ d.add_edge(u, v)
505
+ else:
506
+ d.add_edge(v, u)
507
+
508
+ return True, d
509
+
510
+ except MIPSolverException:
511
+ if certificate:
512
+ return False, None
513
+ return False
514
+
515
+
516
+ ###############
517
+ # Empty shell #
518
+ ###############
519
+
520
+ def is_comparability(g, algorithm='greedy', certificate=False, check=True,
521
+ solver=None, verbose=0):
522
+ r"""
523
+ Check whether the graph is a comparability graph.
524
+
525
+ INPUT:
526
+
527
+ - ``g`` -- a graph
528
+
529
+ - ``algorithm`` -- string (default: ``'greedy'``); choose the implementation
530
+ used to do the test
531
+
532
+ - ``'greedy'`` -- a greedy algorithm (see the documentation of the
533
+ :mod:`comparability module <sage.graphs.comparability>`)
534
+
535
+ - ``'MILP'`` -- a Mixed Integer Linear Program formulation of the
536
+ problem. Beware, for this implementation is unable to return negative
537
+ certificates ! When ``certificate = True``, negative certificates are
538
+ always equal to ``None``. ``True`` certificates are valid, though.
539
+
540
+ - ``certificate`` -- boolean (default: ``False``); whether to return a
541
+ certificate. *Yes*-answers the certificate is a transitive orientation of
542
+ `G`, and a *no* certificates is an odd cycle of sequentially forcing
543
+ edges.
544
+
545
+ - ``check`` -- boolean (default: ``True``); whether to check that the
546
+ yes-certificates are indeed transitive. As it is very quick
547
+ compared to the rest of the operation, it is enabled by default.
548
+
549
+ - ``solver`` -- string (default: ``None``); specifies a Mixed Integer Linear
550
+ Programming (MILP) solver to be used. If set to ``None``, the default one
551
+ is used. For more information on MILP solvers and which default solver is
552
+ used, see the method :meth:`solve
553
+ <sage.numerical.mip.MixedIntegerLinearProgram.solve>` of the class
554
+ :class:`MixedIntegerLinearProgram
555
+ <sage.numerical.mip.MixedIntegerLinearProgram>`.
556
+
557
+ - ``verbose`` -- integer (default: 0); sets the level of verbosity. Set
558
+ to 0 by default, which means quiet.
559
+
560
+ EXAMPLES::
561
+
562
+ sage: from sage.graphs.comparability import is_comparability
563
+ sage: g = graphs.PetersenGraph()
564
+ sage: is_comparability(g)
565
+ False
566
+ sage: is_comparability(graphs.CompleteGraph(5), certificate=True)
567
+ (True, Digraph on 5 vertices)
568
+
569
+ TESTS:
570
+
571
+ Let us ensure that no exception is raised when we go over all small graphs::
572
+
573
+ sage: from sage.graphs.comparability import is_comparability
574
+ sage: [len([g for g in graphs(i) if is_comparability(g, certificate=True)[0]]) for i in range(7)] # needs nauty
575
+ [1, 1, 2, 4, 11, 33, 144]
576
+ """
577
+ g._scream_if_not_simple()
578
+ if not g.size():
579
+ if certificate:
580
+ from sage.graphs.digraph import DiGraph
581
+ return True, DiGraph(g)
582
+ return True
583
+
584
+ if algorithm == "greedy":
585
+ comparability_test = greedy_is_comparability_with_certificate(g, certificate=certificate)
586
+ elif algorithm == "MILP":
587
+ comparability_test = is_comparability_MILP(g, certificate=certificate,
588
+ solver=solver, verbose=verbose)
589
+
590
+ if not certificate:
591
+ return comparability_test
592
+
593
+ # Checking that the orientation found is indeed transitive. No
594
+ # reason why it should not, but no reason why we should not check
595
+ # anyway :-p
596
+ isit, certif = comparability_test
597
+
598
+ if check and isit and (not certif.is_transitive()):
599
+ raise ValueError("Looks like there is a bug somewhere. The "
600
+ "algorithm thinks that the orientation is "
601
+ "transitive, but we just checked and it is not. "
602
+ "Please report the bug on sage-devel, and give "
603
+ "us the graph that made this method fail !")
604
+
605
+ return isit, certif
606
+
607
+
608
+ def is_permutation(g, algorithm='greedy', certificate=False, check=True,
609
+ solver=None, verbose=0):
610
+ r"""
611
+ Check whether the graph is a permutation graph.
612
+
613
+ For more information on permutation graphs, refer to the documentation of
614
+ the :mod:`comparability module <sage.graphs.comparability>`.
615
+
616
+ INPUT:
617
+
618
+ - ``g`` -- a graph
619
+
620
+ - ``algorithm`` -- string (default: ``'greedy'``); choose the implementation
621
+ used for the subcalls to :meth:`is_comparability`
622
+
623
+ - ``'greedy'`` -- a greedy algorithm (see the documentation of the
624
+ :mod:`comparability module <sage.graphs.comparability>`)
625
+
626
+ - ``'MILP'`` -- a Mixed Integer Linear Program formulation of the
627
+ problem. Beware, for this implementation is unable to return negative
628
+ certificates ! When ``certificate = True``, negative certificates are
629
+ always equal to ``None``. ``True`` certificates are valid, though.
630
+
631
+ - ``certificate`` -- boolean (default: ``False``); whether to return a
632
+ certificate for the answer given. For ``True`` answers the certificate is
633
+ a permutation, for ``False`` answers it is a no-certificate for the test
634
+ of comparability or co-comparability.
635
+
636
+ - ``check`` -- boolean (default: ``True``); whether to check that the
637
+ permutations returned indeed create the expected Permutation graph. Pretty
638
+ cheap compared to the rest, hence a good investment. It is enabled by
639
+ default.
640
+
641
+ - ``solver`` -- string (default: ``None``); specifies a Mixed Integer Linear
642
+ Programming (MILP) solver to be used. If set to ``None``, the default one
643
+ is used. For more information on MILP solvers and which default solver is
644
+ used, see the method :meth:`solve
645
+ <sage.numerical.mip.MixedIntegerLinearProgram.solve>` of the class
646
+ :class:`MixedIntegerLinearProgram
647
+ <sage.numerical.mip.MixedIntegerLinearProgram>`.
648
+
649
+ - ``verbose`` -- integer (default: 0); sets the level of verbosity. Set
650
+ to 0 by default, which means quiet.
651
+
652
+ .. NOTE::
653
+
654
+ As the ``True`` certificate is a :class:`Permutation` object, the
655
+ segment intersection model of the permutation graph can be visualized
656
+ through a call to :meth:`Permutation.show
657
+ <sage.combinat.permutation.Permutation.show>`.
658
+
659
+ EXAMPLES:
660
+
661
+ A permutation realizing the bull graph::
662
+
663
+ sage: from sage.graphs.comparability import is_permutation
664
+ sage: g = graphs.BullGraph()
665
+ sage: _ , certif = is_permutation(g, certificate=True)
666
+ sage: h = graphs.PermutationGraph(*certif)
667
+ sage: h.is_isomorphic(g)
668
+ True
669
+
670
+ Plotting the realization as an intersection graph of segments::
671
+
672
+ sage: true, perm = is_permutation(g, certificate=True)
673
+ sage: p1 = Permutation([nn+1 for nn in perm[0]])
674
+ sage: p2 = Permutation([nn+1 for nn in perm[1]])
675
+ sage: p = p2 * p1.inverse()
676
+ sage: p.show(representation='braid') # needs sage.plot
677
+
678
+ TESTS:
679
+
680
+ Trying random permutations, first with the greedy algorithm::
681
+
682
+ sage: from sage.graphs.comparability import is_permutation
683
+ sage: for i in range(20):
684
+ ....: p = Permutations(10).random_element()
685
+ ....: g1 = graphs.PermutationGraph(p)
686
+ ....: isit, certif = is_permutation(g1, certificate=True)
687
+ ....: if not isit:
688
+ ....: print("Something is wrong here !!")
689
+ ....: break
690
+ ....: g2 = graphs.PermutationGraph(*certif)
691
+ ....: if not g1.is_isomorphic(g2):
692
+ ....: print("Something is wrong here !!")
693
+ ....: break
694
+
695
+ Then with MILP::
696
+
697
+ sage: from sage.graphs.comparability import is_permutation
698
+ sage: for i in range(20): # needs sage.numerical.mip
699
+ ....: p = Permutations(10).random_element()
700
+ ....: g1 = graphs.PermutationGraph(p)
701
+ ....: isit, certif = is_permutation(g1, algorithm='MILP', certificate=True)
702
+ ....: if not isit:
703
+ ....: print("Something is wrong here !!")
704
+ ....: break
705
+ ....: g2 = graphs.PermutationGraph(*certif)
706
+ ....: if not g1.is_isomorphic(g2):
707
+ ....: print("Something is wrong here !!")
708
+ ....: break
709
+ """
710
+ if not certificate:
711
+ # No certificate... A piece of cake
712
+ return (is_comparability(g, algorithm=algorithm, solver=solver, verbose=verbose) and
713
+ is_comparability(g.complement(), algorithm=algorithm, solver=solver, verbose=verbose))
714
+
715
+ # First poset, we stop if it fails
716
+ isit, certif = is_comparability(g, algorithm=algorithm, certificate=True,
717
+ solver=solver, verbose=verbose)
718
+ if not isit:
719
+ return False, certif
720
+
721
+ # Second poset
722
+ isit, co_certif = is_comparability(g.complement(), algorithm=algorithm, certificate=True,
723
+ solver=solver, verbose=verbose)
724
+ if not isit:
725
+ return False, co_certif
726
+
727
+ # Building the two orderings
728
+ tmp = list(co_certif.edges(labels=False, sort=False))
729
+ for u, v in certif.edge_iterator(labels=False):
730
+ co_certif.add_edge(v, u)
731
+ certif.add_edges(tmp)
732
+
733
+ ordering = certif.topological_sort()
734
+ co_ordering = co_certif.topological_sort()
735
+
736
+ # Try to build the Permutation graph from the permutations, just to make
737
+ # sure nothing weird happened !
738
+ if check:
739
+ from sage.graphs.graph_generators import GraphGenerators
740
+ pg = GraphGenerators().PermutationGraph(ordering, co_ordering)
741
+ if not pg.is_isomorphic(g):
742
+ raise ValueError("There is a mistake somewhere ! It looks like "
743
+ "the Permutation Graph model computed does "
744
+ "not match the input graph !")
745
+
746
+ return True, (ordering, co_ordering)
747
+
748
+
749
+ def is_transitive(g, certificate=False):
750
+ r"""
751
+ Test whether the digraph is transitive.
752
+
753
+ A digraph is transitive if for any pair of vertices `u,v\in G` linked by a
754
+ `uv`-path the edge `uv` belongs to `G`.
755
+
756
+ INPUT:
757
+
758
+ - ``certificate`` -- whether to return a certificate for negative answers
759
+
760
+ - If ``certificate = False`` (default), this method returns ``True`` or
761
+ ``False`` according to the graph.
762
+
763
+ - If ``certificate = True``, this method either returns ``True`` answers
764
+ or yield a pair of vertices `uv` such that there exists a `uv`-path in
765
+ `G` but `uv\not\in G`.
766
+
767
+ EXAMPLES::
768
+
769
+ sage: digraphs.Circuit(4).is_transitive()
770
+ False
771
+ sage: digraphs.Circuit(4).is_transitive(certificate=True)
772
+ (0, 2)
773
+ sage: digraphs.RandomDirectedGNP(30,.2).is_transitive()
774
+ False
775
+ sage: D = digraphs.DeBruijn(5, 2) # needs sage.combinat
776
+ sage: D.is_transitive() # needs sage.combinat
777
+ False
778
+ sage: cert = D.is_transitive(certificate=True) # needs sage.combinat
779
+ sage: D.has_edge(*cert) # needs sage.combinat
780
+ False
781
+ sage: bool(D.shortest_path(*cert)) # needs sage.combinat
782
+ True
783
+ sage: digraphs.RandomDirectedGNP(20,.2).transitive_closure().is_transitive() # needs networkx
784
+ True
785
+ """
786
+ cdef int n = g.order()
787
+
788
+ if n <= 2:
789
+ return True
790
+
791
+ cdef list int_to_vertex = list(g)
792
+ cdef unsigned short * distances = c_distances_all_pairs(g, vertex_list=int_to_vertex)
793
+ cdef unsigned short * c_distances = distances
794
+
795
+ cdef int i, j
796
+
797
+ # Only 3 distances can appear in the matrix of all distances : 0, 1, and
798
+ # infinity. Anything else is a proof of nontransitivity !
799
+
800
+ for j in range(n):
801
+ for i in range(n):
802
+ if c_distances[i] != <unsigned short> -1 and c_distances[i] > 1:
803
+ sig_free(distances)
804
+ if certificate:
805
+
806
+ return int_to_vertex[j], int_to_vertex[i]
807
+ else:
808
+ return False
809
+
810
+ c_distances += n
811
+
812
+ sig_free(distances)
813
+ return True