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,3566 @@
1
+ # sage_setup: distribution = sagemath-graphs
2
+ # sage.doctest: needs networkx
3
+ r"""
4
+ Matching covered graphs
5
+
6
+ This module implements functions and operations pertaining to matching covered
7
+ graphs.
8
+
9
+ A *matching* in a graph is a set of pairwise nonadjacent links
10
+ (nonloop edges). In other words, a matching in a graph is the edge set of an
11
+ 1-regular subgraph. A matching is called a *perfect* *matching* if it the
12
+ subgraph generated by a set of matching edges spans the graph, i.e. it's the
13
+ edge set of an 1-regular spanning subgraph. A connected nontrivial graph is
14
+ called *matching* *covered* if each edge participates in some perfect matching.
15
+
16
+ {INDEX_OF_METHODS}
17
+
18
+ REFERENCES:
19
+
20
+ - This methods of this module has been adopted and inspired by the book of
21
+ Lucchesi and Murty --- *Perfect Matchings: a theory of matching covered
22
+ graphs* [LM2024]_.
23
+
24
+ AUTHORS:
25
+
26
+ - Janmenjaya Panda (2024-06-14): initial version
27
+
28
+ .. TODO::
29
+
30
+ The following methods are to be incorporated in
31
+ :class:`~MatchingCoveredGraph`:
32
+
33
+ .. csv-table::
34
+ :class: contentstable
35
+ :widths: 30, 70
36
+ :delim: |
37
+
38
+ ``__hash__()`` | Compute a hash for ``self``, if ``self`` is immutable.
39
+ ``_subgraph_by_deleting()`` | Return the matching covered subgraph containing the provided vertices and edges.
40
+
41
+ **Overwritten Methods**
42
+
43
+ .. csv-table::
44
+ :class: contentstable
45
+ :widths: 30, 70
46
+ :delim: |
47
+
48
+ ``add_clique()`` | Add a clique to the graph with the provided vertices.
49
+ ``add_cycle()`` | Add a cycle to the graph with the provided vertices.
50
+ ``add_path()`` | Add a path to the graph with the provided vertices.
51
+ ``cartesian_product()`` | Return the Cartesian product of ``self`` and ``other``.
52
+ ``clear()`` | Empties the graph of vertices and edges and removes name, associated objects, and position information.
53
+ ``complement()`` | Return the complement of the graph.
54
+ ``contract_edge()`` | Contract an edge from ``u`` to ``v``.
55
+ ``contract_edges()`` | Contract edges from an iterable container.
56
+ ``degree_constrained_subgraph()`` | Return a degree-constrained matching covered subgraph.
57
+ ``delete_edge()`` | Delete the edge from ``u`` to ``v``.
58
+ ``delete_edges()`` | Delete edges from an iterable container.
59
+ ``delete_multiedge()`` | Delete all edges from ``u`` to ``v``.
60
+ ``disjoint_union()`` | Return the disjoint union of ``self`` and ``other``.
61
+ ``disjunctive_product()`` | Return the disjunctive product of ``self`` and ``other``.
62
+ ``is_biconnected()`` | Check if the matching covered graph is biconnected.
63
+ ``is_block_graph()`` | Check whether the matching covered graph is a block graph.
64
+ ``is_cograph()`` | Check whether the matching covered graph is cograph.
65
+ ``is_forest()`` | Check if the matching covered graph is a forest, i.e. a disjoint union of trees.
66
+ ``is_matching_covered()`` | Check if the graph is matching covered.
67
+ ``is_path()`` | Check whether the graph is a path.
68
+ ``is_subgraph()`` | Check whether the matching covered graph is a subgraph of ``other``.
69
+ ``is_tree()`` | Check whether the matching covered graph is a tree.
70
+ ``join()`` | Return the join of ``self`` and ``other``.
71
+ ``lexicographic_product()`` | Return the lexicographic product of ``self`` and ``other``.
72
+ ``load_afile()`` | Load the matching covered graph specified in the given file into the current object.
73
+ ``merge_vertices()`` | Merge vertices.
74
+ ``minor()`` | Return the vertices of a minor isomorphic to `H` in the current graph.
75
+ ``random_subgraph()`` | Return a random matching covered subgraph containing each vertex with probability ``p``.
76
+ ``save_afile()`` | Save the graph to file in alist format.
77
+ ``strong_product()`` | Return the strong product of ``self`` and ``other``.
78
+ ``subdivide_edge()`` | Subdivide an edge `k` times.
79
+ ``subdivide_edges()`` | Subdivide `k` times edges from an iterable container.
80
+ ``subgraph()`` | Return the matching covered subgraph containing the given vertices and edges.
81
+ ``subgraph_search()`` | Return a copy of (matching covered) ``G`` in ``self``.
82
+ ``subgraph_search_count()`` | Return the number of labelled occurrences of (matching covered) ``G`` in ``self``.
83
+ ``subgraph_search_iterator()`` | Return an iterator over the labelled copies of (matching covered) ``G`` in ``self``.
84
+ ``tensor_product()`` | Return the tensor product of ``self`` and ``other``.
85
+ ``to_undirected()`` | Return an undirected Graph instance of the matching covered graph.
86
+ ``topological_minor()`` | Return a topological `H`-minor from ``self`` if one exists.
87
+ ``transitive_closure()`` | Return the transitive closure of the matching covered graph.
88
+ ``transitive_reduction()`` | Return a transitive reduction of the matching covered graph.
89
+ ``union()`` | Return the union of ``self`` and ``other``.
90
+
91
+ **Bricks, braces and tight cut decomposition**
92
+
93
+ .. csv-table::
94
+ :class: contentstable
95
+ :widths: 30, 70
96
+ :delim: |
97
+
98
+ ``bricks_and_braces()`` | Return the list of (underlying simple graph of) the bricks and braces of the (matching covered) graph.
99
+ ``number_of_braces()`` | Return the number of braces.
100
+ ``number_of_bricks()`` | Return the number of bricks.
101
+ ``number_of_petersen_bricks()`` | Return the number of Petersen bricks.
102
+ ``tight_cut_decomposition()`` | Return a maximal set of laminar nontrivial tight cuts and a corresponding vertex set partition.
103
+
104
+ **Removability and ear decomposition**
105
+
106
+ .. csv-table::
107
+ :class: contentstable
108
+ :widths: 30, 70
109
+ :delim: |
110
+
111
+ ``add_ear()`` | Add an ear to the graph with the provided end vertices number of internal vertices.
112
+ ``bisubdivide_edge()`` | Bisubdivide an edge `k` times.
113
+ ``bisubdivide_edges()`` | Bisubdivide `k` times edges from an iterable container.
114
+ ``efficient_ear_decomposition()`` | Return a matching covered ear decomposition computed at the fastest possible time.
115
+ ``is_removable_double_ear()`` | Check whether the pair of ears form a removable double ear.
116
+ ``is_removable_doubleton()`` | Check whether the pair of edges constitute a removable doubleton.
117
+ ``is_removable_ear()`` | Check whether the ear is removable.
118
+ ``is_removable_edge()`` | Check whether the edge is removable.
119
+ ``optimal_ear_decomposition()`` | Return an optimal ear decomposition.
120
+ ``removable_double_ears()`` | Return a list of removable double ears.
121
+ ``removable_doubletons()`` | Return a list of removable doubletons.
122
+ ``removable_ears()`` | Return a list of removable ears.
123
+ ``removable_edges()`` | Return a :class:`~EdgesView` of removable edges.
124
+ ``retract()`` | Compute the retract of the (matching covered) graph.
125
+
126
+ **Generating bricks and braces**
127
+
128
+ .. csv-table::
129
+ :class: contentstable
130
+ :widths: 30, 70
131
+ :delim: |
132
+
133
+ ``brace_generation_sequence()`` | Return a McCuaig brace generation sequence of the (provided) brace.
134
+ ``brick_generation_sequence()`` | Return a Norine-Thomas brick generation sequence of the (provided) brick.
135
+ ``is_mccuaig_brace()`` | Check if the brace is a McCuaig brace.
136
+ ``is_norine_thomas_brick()`` | Check if the brick is a Norine-Thomas brick.
137
+
138
+
139
+ Methods
140
+ -------
141
+ """
142
+
143
+ # ****************************************************************************
144
+ # Copyright (C) 2024 Janmenjaya Panda <janmenjaya.panda.22@gmail.com>
145
+ #
146
+ # This program is free software: you can redistribute it and/or modify
147
+ # it under the terms of the GNU General Public License as published by
148
+ # the Free Software Foundation, either version 2 of the License, or
149
+ # (at your option) any later version.
150
+ # https://www.gnu.org/licenses/
151
+ # ****************************************************************************
152
+ from sage.graphs.graph import Graph
153
+ from sage.misc.rest_index_of_methods import doc_index, gen_thematic_rest_table_index
154
+
155
+
156
+ class MatchingCoveredGraph(Graph):
157
+ r"""
158
+ Matching covered graph
159
+
160
+ INPUT:
161
+
162
+ - ``data`` -- can be any of the following:
163
+
164
+ - Empty or ``None`` (throws a :exc:`ValueError` as the graph must be
165
+ nontrival).
166
+
167
+ - An arbitrary graph.
168
+
169
+ - ``matching`` -- (default: ``None``); a perfect matching of the
170
+ graph, that can be given using any valid input format of
171
+ :class:`~sage.graphs.graph.Graph`.
172
+
173
+ If set to ``None``, a matching is computed using the other parameters.
174
+
175
+ - ``algorithm`` -- string (default: ``'Edmonds'``); the algorithm to be
176
+ used to compute a maximum matching of the graph among
177
+
178
+ - ``'Edmonds'`` selects Edmonds' algorithm as implemented in NetworkX,
179
+
180
+ - ``'LP'`` uses a Linear Program formulation of the matching problem.
181
+
182
+ - ``solver`` -- string (default: ``None``); specify a Mixed Integer
183
+ Linear Programming (MILP) solver to be used. If set to ``None``, the
184
+ default one is used. For more information on MILP solvers and which
185
+ default solver is used, see the method :meth:`solve
186
+ <sage.numerical.mip.MixedIntegerLinearProgram.solve>` of the class
187
+ :class:`MixedIntegerLinearProgram
188
+ <sage.numerical.mip.MixedIntegerLinearProgram>`.
189
+
190
+ - ``verbose`` -- integer (default: ``0``); sets the level of verbosity:
191
+ set to 0 by default, which means quiet (only useful when ``algorithm
192
+ == 'LP'``).
193
+
194
+ - ``integrality_tolerance`` -- float; parameter for use with MILP
195
+ solvers over an inexact base ring; see
196
+ :meth:`MixedIntegerLinearProgram.get_values`.
197
+
198
+ OUTPUT:
199
+
200
+ - An object of the class :class:`~MatchingCoveredGraph` if the input is
201
+ valid and the graph is matching covered, or otherwise an error is thrown.
202
+
203
+ .. NOTE::
204
+
205
+ All remaining arguments are passed to the ``Graph`` constructor
206
+
207
+ EXAMPLES:
208
+
209
+ Generating an object of the class ``MatchingCoveredGraph`` from the
210
+ provided instance of ``Graph`` without providing any other information::
211
+
212
+ sage: G = MatchingCoveredGraph(graphs.PetersenGraph())
213
+ sage: G
214
+ Matching covered petersen graph: graph on 10 vertices
215
+ sage: sorted(G.get_matching())
216
+ [(0, 5, None), (1, 6, None), (2, 7, None), (3, 8, None), (4, 9, None)]
217
+
218
+ sage: G = graphs.StaircaseGraph(4)
219
+ sage: H = MatchingCoveredGraph(G)
220
+ sage: H
221
+ Matching covered staircase graph: graph on 8 vertices
222
+ sage: H == G
223
+ True
224
+ sage: sorted(H.get_matching())
225
+ [(0, 1, None), (2, 7, None), (3, 6, None), (4, 5, None)]
226
+
227
+ sage: G = Graph({0: [1, 2, 3, 4], 1: [2, 5],
228
+ ....: 2: [5], 3: [4, 5], 4: [5]})
229
+ sage: H = MatchingCoveredGraph(G)
230
+ sage: H
231
+ Matching covered graph on 6 vertices
232
+ sage: H == G
233
+ True
234
+ sage: sorted(H.get_matching())
235
+ [(0, 4, None), (1, 2, None), (3, 5, None)]
236
+
237
+ sage: # needs networkx
238
+ sage: import networkx
239
+ sage: G = Graph(networkx.complete_bipartite_graph(12, 12))
240
+ sage: H = MatchingCoveredGraph(G)
241
+ sage: H
242
+ Matching covered graph on 24 vertices
243
+ sage: H == G
244
+ True
245
+ sage: sorted(H.get_matching())
246
+ [(0, 15, None), (1, 14, None), (2, 13, None), (3, 12, None),
247
+ (4, 23, None), (5, 22, None), (6, 21, None), (7, 20, None),
248
+ (8, 19, None), (9, 18, None), (10, 17, None), (11, 16, None)]
249
+
250
+ sage: G = Graph('E|fG', sparse=True)
251
+ sage: H = MatchingCoveredGraph(G)
252
+ sage: H
253
+ Matching covered graph on 6 vertices
254
+ sage: H == G
255
+ True
256
+ sage: sorted(H.get_matching())
257
+ [(0, 5, None), (1, 2, None), (3, 4, None)]
258
+
259
+ sage: # needs sage.modules
260
+ sage: M = Matrix([(0,1,0,0,1,1,0,0,0,0),
261
+ ....: (1,0,1,0,0,0,1,0,0,0),
262
+ ....: (0,1,0,1,0,0,0,1,0,0),
263
+ ....: (0,0,1,0,1,0,0,0,1,0),
264
+ ....: (1,0,0,1,0,0,0,0,0,1),
265
+ ....: (1,0,0,0,0,0,0,1,1,0),
266
+ ....: (0,1,0,0,0,0,0,0,1,1),
267
+ ....: (0,0,1,0,0,1,0,0,0,1),
268
+ ....: (0,0,0,1,0,1,1,0,0,0),
269
+ ....: (0,0,0,0,1,0,1,1,0,0)])
270
+ sage: M
271
+ [0 1 0 0 1 1 0 0 0 0]
272
+ [1 0 1 0 0 0 1 0 0 0]
273
+ [0 1 0 1 0 0 0 1 0 0]
274
+ [0 0 1 0 1 0 0 0 1 0]
275
+ [1 0 0 1 0 0 0 0 0 1]
276
+ [1 0 0 0 0 0 0 1 1 0]
277
+ [0 1 0 0 0 0 0 0 1 1]
278
+ [0 0 1 0 0 1 0 0 0 1]
279
+ [0 0 0 1 0 1 1 0 0 0]
280
+ [0 0 0 0 1 0 1 1 0 0]
281
+ sage: G = Graph(M)
282
+ sage: H = MatchingCoveredGraph(G)
283
+ sage: H == G
284
+ True
285
+ sage: sorted(H.get_matching())
286
+ [(0, 5, None), (1, 6, None), (2, 7, None), (3, 8, None), (4, 9, None)]
287
+
288
+ sage: # needs sage.modules
289
+ sage: M = Matrix([(-1, 0, 0, 0, 1, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0),
290
+ ....: ( 1,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0),
291
+ ....: ( 0, 1,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0),
292
+ ....: ( 0, 0, 1,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0),
293
+ ....: ( 0, 0, 0, 1,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1),
294
+ ....: ( 0, 0, 0, 0, 0,-1, 0, 0, 0, 1, 1, 0, 0, 0, 0),
295
+ ....: ( 0, 0, 0, 0, 0, 0, 0, 1,-1, 0, 0, 1, 0, 0, 0),
296
+ ....: ( 0, 0, 0, 0, 0, 1,-1, 0, 0, 0, 0, 0, 1, 0, 0),
297
+ ....: ( 0, 0, 0, 0, 0, 0, 0, 0, 1,-1, 0, 0, 0, 1, 0),
298
+ ....: ( 0, 0, 0, 0, 0, 0, 1,-1, 0, 0, 0, 0, 0, 0, 1)])
299
+ sage: M
300
+ [-1 0 0 0 1 0 0 0 0 0 -1 0 0 0 0]
301
+ [ 1 -1 0 0 0 0 0 0 0 0 0 -1 0 0 0]
302
+ [ 0 1 -1 0 0 0 0 0 0 0 0 0 -1 0 0]
303
+ [ 0 0 1 -1 0 0 0 0 0 0 0 0 0 -1 0]
304
+ [ 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 -1]
305
+ [ 0 0 0 0 0 -1 0 0 0 1 1 0 0 0 0]
306
+ [ 0 0 0 0 0 0 0 1 -1 0 0 1 0 0 0]
307
+ [ 0 0 0 0 0 1 -1 0 0 0 0 0 1 0 0]
308
+ [ 0 0 0 0 0 0 0 0 1 -1 0 0 0 1 0]
309
+ [ 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 1]
310
+ sage: G = Graph(M)
311
+ sage: H = MatchingCoveredGraph(G)
312
+ sage: H == G
313
+ True
314
+ sage: sorted(H.get_matching())
315
+ [(0, 5, None), (1, 6, None), (2, 7, None), (3, 8, None), (4, 9, None)]
316
+
317
+ sage: G = Graph([(0, 1), (0, 3), (0, 4), (1, 2), (1, 5), (2, 3),
318
+ ....: (2, 6), (3, 7), (4, 5), (4, 7), (5, 6), (6, 7)])
319
+ sage: H = MatchingCoveredGraph(G)
320
+ sage: H == G
321
+ True
322
+ sage: sorted(H.get_matching())
323
+ [(0, 4, None), (1, 5, None), (2, 6, None), (3, 7, None)]
324
+
325
+ sage: # optional - python_igraph
326
+ sage: import igraph
327
+ sage: G = Graph(igraph.Graph([(0, 1), (0, 3), (1, 2), (2, 3)]))
328
+ sage: H = MatchingCoveredGraph(G)
329
+ sage: H
330
+ Matching covered graph on 4 vertices
331
+ sage: sorted(H.get_matching())
332
+ [(0, 3, {}), (1, 2, {})]
333
+
334
+ One may specify a perfect matching::
335
+
336
+ sage: P = graphs.PetersenGraph()
337
+ sage: M = P.matching()
338
+ sage: sorted(M)
339
+ [(0, 5, None), (1, 6, None), (2, 7, None), (3, 8, None), (4, 9, None)]
340
+ sage: G = MatchingCoveredGraph(P, matching=M)
341
+ sage: G
342
+ Matching covered petersen graph: graph on 10 vertices
343
+ sage: P == G
344
+ True
345
+ sage: sorted(G.get_matching())
346
+ [(0, 5, None), (1, 6, None), (2, 7, None), (3, 8, None), (4, 9, None)]
347
+ sage: sorted(G.get_matching()) == sorted(M)
348
+ True
349
+
350
+ sage: G = graphs.TruncatedBiwheelGraph(14)
351
+ sage: M = G.matching()
352
+ sage: sorted(M)
353
+ [(0, 27, None), (1, 26, None), (2, 3, None), (4, 5, None),
354
+ (6, 7, None), (8, 9, None), (10, 11, None), (12, 13, None),
355
+ (14, 15, None), (16, 17, None), (18, 19, None), (20, 21, None),
356
+ (22, 23, None), (24, 25, None)]
357
+ sage: H = MatchingCoveredGraph(G, M)
358
+ sage: H
359
+ Matching covered truncated biwheel graph: graph on 28 vertices
360
+ sage: H == G
361
+ True
362
+ sage: sorted(H.get_matching()) == sorted(M)
363
+ True
364
+
365
+ One may specify some keyword arguments::
366
+
367
+ sage: G = Graph([(0, 1, 5)], {'weighted': True})
368
+ sage: kwds = {
369
+ ....: 'loops': False,
370
+ ....: 'multiedges': True,
371
+ ....: 'pos': {0: (0, 0), 1: (1, 1)}
372
+ ....: }
373
+ sage: H = MatchingCoveredGraph(G, **kwds)
374
+ sage: H
375
+ Matching covered multi-graph on 2 vertices
376
+ sage: H.add_edge(0, 1)
377
+ sage: H.edges()
378
+ [(0, 1, None), (0, 1, 5)]
379
+
380
+ TESTS:
381
+
382
+ An empty graph is not matching covered::
383
+
384
+ sage: G = Graph()
385
+ sage: H = MatchingCoveredGraph(G)
386
+ Traceback (most recent call last):
387
+ ...
388
+ ValueError: the graph is trivial
389
+ sage: G = MatchingCoveredGraph()
390
+ Traceback (most recent call last):
391
+ ...
392
+ ValueError: the graph is trivial
393
+
394
+ Providing with a graph that is not connected::
395
+
396
+ sage: G = graphs.CycleGraph(4)
397
+ sage: G += graphs.CycleGraph(6)
398
+ sage: G.connected_components_number()
399
+ 2
400
+ sage: H = MatchingCoveredGraph(G)
401
+ Traceback (most recent call last):
402
+ ...
403
+ ValueError: the graph is not connected
404
+
405
+ Make sure that self-loops are not allowed for a matching covered graph::
406
+
407
+ sage: P = graphs.PetersenGraph()
408
+ sage: kwds = {'loops': True}
409
+ sage: G = MatchingCoveredGraph(P, **kwds)
410
+ Traceback (most recent call last):
411
+ ...
412
+ ValueError: loops are not allowed in matching covered graphs
413
+ sage: G = MatchingCoveredGraph(P)
414
+ sage: G.allows_loops()
415
+ False
416
+ sage: G.allow_loops(True)
417
+ Traceback (most recent call last):
418
+ ...
419
+ ValueError: loops are not allowed in matching covered graphs
420
+ sage: G.add_edge(0, 0)
421
+ Traceback (most recent call last):
422
+ ...
423
+ ValueError: loops are not allowed in matching covered graphs
424
+ sage: H = MatchingCoveredGraph(P, loops=True)
425
+ Traceback (most recent call last):
426
+ ...
427
+ ValueError: loops are not allowed in matching covered graphs
428
+
429
+ Make sure that multiple edges are allowed for a matching covered graph (by
430
+ default it is off and can be modified to be allowed)::
431
+
432
+ sage: P = graphs.PetersenGraph()
433
+ sage: G = MatchingCoveredGraph(P)
434
+ sage: G
435
+ Matching covered petersen graph: graph on 10 vertices
436
+ sage: G.allows_multiple_edges()
437
+ False
438
+ sage: G.size()
439
+ 15
440
+ sage: G.allow_multiple_edges(True)
441
+ sage: G.allows_multiple_edges()
442
+ True
443
+ sage: G.add_edge(next(P.edge_iterator()))
444
+ sage: G.size()
445
+ 16
446
+ sage: G
447
+ Matching covered petersen graph: multi-graph on 10 vertices
448
+ sage: H = MatchingCoveredGraph(P, multiedges=True)
449
+ sage: H.allows_multiple_edges()
450
+ True
451
+ sage: H.add_edge(next(P.edge_iterator()))
452
+ sage: H.size()
453
+ 16
454
+ sage: H
455
+ Matching covered petersen graph: multi-graph on 10 vertices
456
+
457
+ Providing with a connected nontrivial graph free of self-loops that is
458
+ not matching covered::
459
+
460
+ sage: G = graphs.CompleteGraph(11)
461
+ sage: H = MatchingCoveredGraph(G)
462
+ Traceback (most recent call last):
463
+ ...
464
+ ValueError: input graph is not matching covered
465
+ sage: G = Graph({0: [1, 6, 11], 1: [2, 4], 2: [3, 5], 3: [4, 5],
466
+ ....: 4: [5], 6: [7, 9], 7: [8, 10], 8: [9, 10], 9: [10],
467
+ ....: 11: [12, 14], 12: [13, 15], 13: [14, 15], 14: [15]})
468
+ sage: H = MatchingCoveredGraph(G)
469
+ Traceback (most recent call last):
470
+ ...
471
+ ValueError: input graph is not matching covered
472
+
473
+ sage: # needs networkx
474
+ sage: import networkx
475
+ sage: G = Graph(networkx.complete_bipartite_graph(2, 12))
476
+ sage: H = MatchingCoveredGraph(G)
477
+ Traceback (most recent call last):
478
+ ...
479
+ ValueError: input graph is not matching covered
480
+ sage: G = Graph('F~~~w')
481
+ sage: H = MatchingCoveredGraph(G)
482
+ Traceback (most recent call last):
483
+ ...
484
+ ValueError: input graph is not matching covered
485
+
486
+ sage: # needs sage.modules
487
+ sage: M = Matrix([(0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0),
488
+ ....: (1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
489
+ ....: (0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
490
+ ....: (0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
491
+ ....: (0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
492
+ ....: (0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
493
+ ....: (1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0),
494
+ ....: (0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0),
495
+ ....: (0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0),
496
+ ....: (0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0),
497
+ ....: (0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0),
498
+ ....: (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0),
499
+ ....: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1),
500
+ ....: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1),
501
+ ....: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1),
502
+ ....: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0)])
503
+ sage: M
504
+ [0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0]
505
+ [1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0]
506
+ [0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0]
507
+ [0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0]
508
+ [0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0]
509
+ [0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0]
510
+ [1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0]
511
+ [0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0]
512
+ [0 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0]
513
+ [0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0]
514
+ [0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0]
515
+ [1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0]
516
+ [0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1]
517
+ [0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1]
518
+ [0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1]
519
+ [0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0]
520
+ sage: G = Graph(M)
521
+ sage: H = MatchingCoveredGraph(G)
522
+ Traceback (most recent call last):
523
+ ...
524
+ ValueError: input graph is not matching covered
525
+
526
+ sage: # needs sage.modules
527
+ sage: M = Matrix([(1, 1, 0, 0, 0, 0),
528
+ ....: (0, 0, 1, 1, 0, 0),
529
+ ....: (0, 0, 1, 0, 1, 0),
530
+ ....: (1, 0, 0, 0, 0, 1),
531
+ ....: (0, 1, 0, 1, 1, 1)])
532
+ sage: G = Graph(M)
533
+ sage: H = MatchingCoveredGraph(G)
534
+ Traceback (most recent call last):
535
+ ...
536
+ ValueError: input graph is not matching covered
537
+ sage: G = Graph([(11, 12), (11, 14), (0, 1), (0, 11), (0, 6), (1, 2),
538
+ ....: (1, 4), (2, 3), (2, 5), (3, 4), (3, 5), (4, 5),
539
+ ....: (6, 7), (6, 9), (7, 8), (7, 10), (8, 9), (8, 10),
540
+ ....: (9, 10), (12, 13), (12, 15), (13, 14), (13, 15),
541
+ ....: (14, 15)])
542
+ sage: H = MatchingCoveredGraph(G)
543
+ Traceback (most recent call last):
544
+ ...
545
+ ValueError: input graph is not matching covered
546
+
547
+ sage: # optional - python_igraph
548
+ sage: import igraph
549
+ sage: G = Graph(igraph.Graph([(0, 1), (0, 2), (0, 3), (1, 2), (2, 3)]))
550
+ sage: H = MatchingCoveredGraph(G)
551
+ Traceback (most recent call last):
552
+ ...
553
+ ValueError: input graph is not matching covered
554
+
555
+ Providing with a wrong matching::
556
+
557
+ sage: P = graphs.PetersenGraph()
558
+ sage: M = str('0')
559
+ sage: H = MatchingCoveredGraph(P, matching=M)
560
+ Traceback (most recent call last):
561
+ ...
562
+ RuntimeError: the string seems corrupt: valid characters are
563
+ ?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
564
+ sage: N = str('graph')
565
+ sage: J = MatchingCoveredGraph(P, matching=N)
566
+ Traceback (most recent call last):
567
+ ...
568
+ RuntimeError: the string (graph) seems corrupt: for n = 40,
569
+ the string is too short
570
+
571
+ sage: G = graphs.CompleteGraph(6)
572
+ sage: M = Graph(G.matching())
573
+ sage: M.add_edges([(0, 1), (0, 2)])
574
+ sage: H = MatchingCoveredGraph(G, matching=M)
575
+ Traceback (most recent call last):
576
+ ...
577
+ ValueError: the input is not a matching
578
+ sage: N = Graph(G.matching())
579
+ sage: N.add_edge(6, 7)
580
+ sage: H = MatchingCoveredGraph(G, matching=N)
581
+ Traceback (most recent call last):
582
+ ...
583
+ ValueError: the input is not a matching of the graph
584
+ sage: J = Graph()
585
+ sage: J.add_edges([(0, 1), (2, 3)])
586
+ sage: H = MatchingCoveredGraph(G, matching=J)
587
+ Traceback (most recent call last):
588
+ ...
589
+ ValueError: the input is not a perfect matching of the graph
590
+
591
+ Note that data shall be one of empty or ``None`` or an instance of
592
+ ``Graph`` or an instance of ``MatchingCoveredGraph``. Otherwise a
593
+ :exc:`ValueError` is returned::
594
+
595
+ sage: D = digraphs.Complete(10)
596
+ sage: D
597
+ Complete digraph: Digraph on 10 vertices
598
+ sage: G = MatchingCoveredGraph(D)
599
+ Traceback (most recent call last):
600
+ ...
601
+ TypeError: input data is of unknown type
602
+ """
603
+
604
+ def __init__(self, data=None, matching=None, algorithm='Edmonds',
605
+ solver=None, verbose=0, integrality_tolerance=0.001,
606
+ *args, **kwds):
607
+ r"""
608
+ Create a matching covered graph, that is a connected nontrivial graph
609
+ wherein each edge participates in some perfect matching.
610
+
611
+ See documentation ``MatchingCoveredGraph?`` for detailed information.
612
+ """
613
+ success = False
614
+
615
+ if not kwds:
616
+ kwds = {'loops': False}
617
+ else:
618
+ if 'loops' in kwds and kwds['loops']:
619
+ raise ValueError('loops are not allowed in '
620
+ 'matching covered graphs')
621
+ kwds['loops'] = False
622
+
623
+ if data is None:
624
+ raise ValueError('the graph is trivial')
625
+
626
+ elif isinstance(data, MatchingCoveredGraph):
627
+ Graph.__init__(self, data, *args, **kwds)
628
+ success = True
629
+
630
+ elif isinstance(data, Graph):
631
+ try:
632
+ self._upgrade_from_graph(data=data, matching=matching,
633
+ algorithm=algorithm,
634
+ solver=solver, verbose=verbose,
635
+ integrality_tolerance=integrality_tolerance,
636
+ *args, **kwds)
637
+ success = True
638
+
639
+ except Exception as exception:
640
+ raise exception
641
+
642
+ if success:
643
+ if matching:
644
+ # The input matching is a valid perfect matching of the graph
645
+ self._matching = matching
646
+
647
+ else:
648
+ self._matching = Graph(self).matching()
649
+
650
+ else:
651
+ raise TypeError('input data is of unknown type')
652
+
653
+ def __repr__(self):
654
+ r"""
655
+ Return a short string representation of the (matching covered) graph.
656
+
657
+ EXAMPLES:
658
+
659
+ If the string representation of the (matching covered) graph does not
660
+ contain the term 'matching covered', it's used as the prefix::
661
+
662
+ sage: G = graphs.CompleteGraph(10)
663
+ sage: H = MatchingCoveredGraph(G)
664
+ sage: H
665
+ Matching covered complete graph: graph on 10 vertices
666
+
667
+ sage: G = graphs.HexahedralGraph()
668
+ sage: H = MatchingCoveredGraph(BipartiteGraph(G))
669
+ sage: H # An object of the class MatchingCoveredGraph
670
+ Matching covered hexahedron: graph on 8 vertices
671
+
672
+ In case the string representation of the (matching covered) graph
673
+ contains the term 'matching covered', the representation remains as it
674
+ is::
675
+
676
+ sage: G = graphs.CompleteGraph(10)
677
+ sage: H = MatchingCoveredGraph(G)
678
+ sage: H
679
+ Matching covered complete graph: graph on 10 vertices
680
+ sage: J = MatchingCoveredGraph(H)
681
+ sage: J
682
+ Matching covered complete graph: graph on 10 vertices
683
+ sage: G = graphs.HexahedralGraph()
684
+ sage: H = BipartiteGraph(MatchingCoveredGraph(G))
685
+ sage: H # An object of the class BipartiteGraph
686
+ Bipartite hexahedron: graph on 8 vertices
687
+ sage: J = MatchingCoveredGraph(H)
688
+ sage: J # An object of the class MatchingCoveredGraph
689
+ Matching covered hexahedron: graph on 8 vertices
690
+ """
691
+ s = Graph._repr_(self).lower()
692
+ if "matching covered" in s:
693
+ return s.capitalize()
694
+ return "".join(["Matching covered ", s])
695
+
696
+ def _subgraph_by_adding(self, vertices=None, edges=None, edge_property=None, immutable=None):
697
+ r"""
698
+ Return the matching covered subgraph containing the given vertices and edges.
699
+
700
+ The edges also satisfy the edge_property, if it is not None. The
701
+ subgraph is created by creating a new empty graph and adding the
702
+ necessary vertices, edges, and other properties.
703
+
704
+ .. NOTE::
705
+
706
+ This method overwrites the
707
+ :meth:`~sage.graphs.generic_graph.GenericGraph._subgraph_by_adding`
708
+ method to ensure that resultant subgraph is also matching covered.
709
+
710
+ INPUT:
711
+
712
+ - ``vertices`` -- (default: ``None``) an iterable container of
713
+ vertices, e.g. a list, set, graph, file or numeric array. If not
714
+ passed (i.e., ``None``), defaults to the entire graph.
715
+
716
+ - ``edges`` -- a single edge or an iterable container of edges (e.g., a
717
+ list, set, file, numeric array, etc.). By default (``edges=None``),
718
+ all edges are assumed and the returned graph is an induced
719
+ subgraph. In the case of multiple edges, specifying an edge as `(u,v)`
720
+ means to keep all edges `(u,v)`, regardless of the label.
721
+
722
+ - ``edge_property`` -- function (default: ``None``); a function that
723
+ inputs an edge and outputs a boolean value, i.e., a edge ``e`` in
724
+ ``edges`` is kept if ``edge_property(e) == True``
725
+
726
+ - ``immutable`` -- boolean (default: ``None``); whether to create a
727
+ mutable/immutable subgraph. ``immutable=None`` (default) means that
728
+ the graph and its subgraph will behave the same way.
729
+
730
+ OUTPUT:
731
+
732
+ - An instance of :class:`~MatchingCoveredGraph` is returned if the
733
+ subgraph obtained is matching covered, otherwise a :exc:`ValueError`
734
+ is thrown.
735
+
736
+ EXAMPLES:
737
+
738
+ Ladder graphs are matching covered subgraphs of a staircase graph,
739
+ which is also matching covered::
740
+
741
+ sage: G = MatchingCoveredGraph(graphs.StaircaseGraph(4))
742
+ sage: H = G._subgraph_by_adding(vertices=[0..5])
743
+ sage: H.order(), H.size()
744
+ (6, 7)
745
+ sage: H
746
+ Matching covered subgraph of (staircase graph): graph on 6 vertices
747
+ sage: H.is_isomorphic(graphs.LadderGraph(3))
748
+ True
749
+
750
+ Cycle graphs are matching covered subgraphs of a biwheel graph, which
751
+ is also matching covered::
752
+
753
+ sage: G = MatchingCoveredGraph(graphs.BiwheelGraph(5))
754
+ sage: H = G._subgraph_by_adding(vertices=[0..7],
755
+ ....: edges=[(u, (u+1) % 8) for u in range(8)])
756
+ sage: H.order(), H.size()
757
+ (8, 8)
758
+ sage: H
759
+ Matching covered subgraph of (biwheel graph): graph on 8 vertices
760
+ sage: H.is_isomorphic(graphs.CycleGraph(8))
761
+ True
762
+
763
+ One may pass no value for any of the input arguments; in such a case,
764
+ the whole matching covered graph will be returned::
765
+
766
+ sage: T = graphs.TwinplexGraph()
767
+ sage: G = MatchingCoveredGraph(T)
768
+ sage: J = G._subgraph_by_adding()
769
+ sage: G == J
770
+ True
771
+
772
+ One may use the ``edge_property`` argument::
773
+
774
+ sage: G = Graph(multiedges=True)
775
+ sage: G.add_edges([
776
+ ....: (0, 1, 'label'), (0, 2), (0, 3), (0, 4),
777
+ ....: (0, 5), (1, 2, 'label'), (1, 2), (1, 5),
778
+ ....: (2, 5), (3, 4), (3, 5), (4, 5)
779
+ ....: ])
780
+ sage: H = MatchingCoveredGraph(G)
781
+ sage: J = H._subgraph_by_adding(vertices=[0, 1, 2, 5], edge_property=
782
+ ....: (lambda edge:
783
+ ....: (edge[0] in [1, 2]) != (edge[1] in [1, 2]))
784
+ ....: )
785
+ sage: J.order(), J.size()
786
+ (4, 4)
787
+ sage: J
788
+ Matching covered subgraph of (): multi-graph on 4 vertices
789
+ sage: J.is_isomorphic(graphs.CompleteBipartiteGraph(2, 2))
790
+ True
791
+
792
+ We may specify the subgraph to be immutable::
793
+
794
+ sage: M = graphs.MoebiusLadderGraph(4)
795
+ sage: G = MatchingCoveredGraph(M)
796
+ sage: H = G._subgraph_by_adding(edge_property=
797
+ ....: (lambda edge: abs(edge[0] - edge[1]) != 4),
798
+ ....: immutable=True)
799
+ sage: H.order(), H.size()
800
+ (8, 8)
801
+ sage: H
802
+ Matching covered subgraph of (moebius ladder graph): graph on 8 vertices
803
+ sage: H.is_isomorphic(graphs.CycleGraph(8))
804
+ True
805
+ sage: H.is_immutable()
806
+ True
807
+ sage: C = graphs.CubeplexGraph()
808
+ sage: D = MatchingCoveredGraph(C)
809
+ sage: I = D._subgraph_by_adding(immutable=True)
810
+ sage: (I == D) and (I.is_immutable())
811
+ True
812
+ sage: J = D._subgraph_by_adding(vertices=D.vertices(), immutable=True)
813
+ sage: (J == D) and (J.is_immutable())
814
+ True
815
+
816
+ An error is thrown if the subgraph is not matching covered::
817
+
818
+ sage: P = graphs.PetersenGraph()
819
+ sage: G = MatchingCoveredGraph(P)
820
+ sage: H = G._subgraph_by_adding(vertices=[])
821
+ Traceback (most recent call last):
822
+ ...
823
+ ValueError: the graph is trivial
824
+ sage: H = G._subgraph_by_adding(edge_property=
825
+ ....: (lambda edge: edge[0] == 0)
826
+ ....: )
827
+ Traceback (most recent call last):
828
+ ...
829
+ ValueError: the graph is not connected
830
+ sage: H = G._subgraph_by_adding(vertices=[1, 2, 3])
831
+ Traceback (most recent call last):
832
+ ...
833
+ ValueError: input graph is not matching covered
834
+ """
835
+ if immutable is None:
836
+ immutable = self.is_immutable()
837
+
838
+ if edges is None and edge_property is None:
839
+ if vertices is None:
840
+ G = self.copy()
841
+ G.name('Matching covered subgraph of ({})'.format(self.name()))
842
+ if immutable:
843
+ G = G.copy(immutable=True)
844
+
845
+ return G
846
+
847
+ else:
848
+ # Check if all existent vertices are there
849
+ all_existent_vertices = True
850
+ for vertex in self:
851
+ if vertex not in vertices:
852
+ all_existent_vertices = False
853
+ break
854
+
855
+ if all_existent_vertices:
856
+ G = self.copy()
857
+ G.name('Matching covered subgraph of ({})'.format(self.name()))
858
+ if immutable:
859
+ G = G.copy(immutable=True)
860
+
861
+ return G
862
+
863
+ G = Graph(self, weighted=self._weighted, loops=self.allows_loops(),
864
+ multiedges=self.allows_multiple_edges())
865
+
866
+ H = G._subgraph_by_adding(vertices=vertices, edges=edges,
867
+ edge_property=edge_property,
868
+ immutable=False)
869
+
870
+ try:
871
+ H = MatchingCoveredGraph(H)
872
+ H.name('Matching covered subgraph of ({})'.format(self.name()))
873
+ if immutable:
874
+ H = H.copy(immutable=True)
875
+
876
+ return H
877
+
878
+ except Exception as exception:
879
+ raise exception
880
+
881
+ def _upgrade_from_graph(self, data=None, matching=None, algorithm='Edmonds',
882
+ solver=None, verbose=0, integrality_tolerance=0.001,
883
+ *args, **kwds):
884
+ r"""
885
+ Upgrade the given graph to a matching covered graph if eligible.
886
+
887
+ See documentation ``MatchingCoveredGraph?`` for detailed information.
888
+ """
889
+ try:
890
+ check = Graph.is_matching_covered(G=data, matching=matching,
891
+ algorithm=algorithm,
892
+ coNP_certificate=False,
893
+ solver=solver, verbose=verbose,
894
+ integrality_tolerance=integrality_tolerance)
895
+
896
+ if check:
897
+ Graph.__init__(self, data, *args, **kwds)
898
+ else:
899
+ raise ValueError("input graph is not matching covered")
900
+
901
+ except Exception as exception:
902
+ raise exception
903
+
904
+ @doc_index('Overwritten methods')
905
+ def add_edge(self, u, v=None, label=None):
906
+ r"""
907
+ Add an edge from vertex ``u`` to vertex ``v``.
908
+
909
+ .. NOTE::
910
+
911
+ This method overwrites the
912
+ :meth:`~sage.graphs.generic_graph.GenericGraph.add_edge` method
913
+ to ensure that resultant graph is also matching covered.
914
+
915
+ INPUT:
916
+
917
+ The following forms are all accepted:
918
+
919
+ - G.add_edge(1, 2)
920
+ - G.add_edge((1, 2))
921
+ - G.add_edges([(1, 2)])
922
+ - G.add_edge(1, 2, 'label')
923
+ - G.add_edge((1, 2, 'label'))
924
+ - G.add_edges([(1, 2, 'label')])
925
+
926
+ OUTPUT:
927
+
928
+ - If an edge is provided with a valid format, but addition of the edge
929
+ leaves the resulting graph not being matching covered, a
930
+ :exc:`ValueError` is returned without any alteration to the existing
931
+ matching covered graph. If the addition of the edge preserves the
932
+ property of matching covered, then the graph is updated and nothing
933
+ is returned.
934
+
935
+ - If the edge is provided with an invalid format, a :exc:`ValueError`
936
+ is returned.
937
+
938
+ WARNING:
939
+
940
+ The following intuitive input results in nonintuitive output,
941
+ even though the resulting graph behind the intuition might be matching
942
+ covered::
943
+
944
+ sage: P = graphs.WheelGraph(6)
945
+ sage: G = MatchingCoveredGraph(P)
946
+ sage: G.add_edge((1, 4), 'label')
947
+ Traceback (most recent call last):
948
+ ...
949
+ ValueError: the graph obtained after the addition of edge
950
+ (((1, 4), 'label', None)) is not matching covered
951
+ sage: G.edges(sort=False)
952
+ [(0, 1, None), (0, 2, None), (0, 3, None), (0, 4, None),
953
+ (0, 5, None), (1, 2, None), (1, 5, None), (2, 3, None),
954
+ (3, 4, None), (4, 5, None)]
955
+
956
+ The key word ``label`` must be used::
957
+
958
+ sage: W = graphs.WheelGraph(6)
959
+ sage: G = MatchingCoveredGraph(W)
960
+ sage: G.add_edge((1, 4), label='label')
961
+ sage: G.edges(sort=False) # No alteration to the existing graph
962
+ [(0, 1, None), (0, 2, None), (0, 3, None), (0, 4, None),
963
+ (0, 5, None), (1, 2, None), (1, 4, 'label'), (1, 5, None),
964
+ (2, 3, None), (3, 4, None), (4, 5, None)]
965
+
966
+ An expression, analogous to the syntax mentioned above may be used::
967
+
968
+ sage: S = graphs.StaircaseGraph(4)
969
+ sage: G = MatchingCoveredGraph(S)
970
+ sage: G.add_edge(0, 5)
971
+ sage: G.edges(sort=False)
972
+ [(0, 1, None), (0, 3, None), (0, 5, None), (0, 6, None),
973
+ (1, 2, None), (1, 4, None), (2, 5, None), (2, 7, None),
974
+ (3, 4, None), (3, 6, None), (4, 5, None), (5, 7, None),
975
+ (6, 7, None)]
976
+ sage: G.add_edge((2, 3))
977
+ sage: G.edges(sort=False)
978
+ [(0, 1, None), (0, 3, None), (0, 5, None), (0, 6, None),
979
+ (1, 2, None), (1, 4, None), (2, 3, None), (2, 5, None),
980
+ (2, 7, None), (3, 4, None), (3, 6, None), (4, 5, None),
981
+ (5, 7, None), (6, 7, None)]
982
+ sage: G.add_edges([(0, 4)])
983
+ sage: G.edges(sort=False)
984
+ [(0, 1, None), (0, 3, None), (0, 4, None), (0, 5, None),
985
+ (0, 6, None), (1, 2, None), (1, 4, None), (2, 3, None),
986
+ (2, 5, None), (2, 7, None), (3, 4, None), (3, 6, None),
987
+ (4, 5, None), (5, 7, None), (6, 7, None)]
988
+ sage: G.add_edge(2, 4, 'label')
989
+ sage: G.edges(sort=False)
990
+ [(0, 1, None), (0, 3, None), (0, 4, None), (0, 5, None),
991
+ (0, 6, None), (1, 2, None), (1, 4, None), (2, 3, None),
992
+ (2, 4, 'label'), (2, 5, None), (2, 7, None), (3, 4, None),
993
+ (3, 6, None), (4, 5, None), (5, 7, None), (6, 7, None)]
994
+ sage: G.add_edge((4, 6, 'label'))
995
+ sage: G.edges(sort=False)
996
+ [(0, 1, None), (0, 3, None), (0, 4, None), (0, 5, None),
997
+ (0, 6, None), (1, 2, None), (1, 4, None), (2, 3, None),
998
+ (2, 4, 'label'), (2, 5, None), (2, 7, None), (3, 4, None),
999
+ (3, 6, None), (4, 5, None), (4, 6, 'label'), (5, 7, None),
1000
+ (6, 7, None)]
1001
+ sage: G.add_edges([(4, 7, 'label')])
1002
+ sage: G.edges(sort=False)
1003
+ [(0, 1, None), (0, 3, None), (0, 4, None), (0, 5, None),
1004
+ (0, 6, None), (1, 2, None), (1, 4, None), (2, 3, None),
1005
+ (2, 4, 'label'), (2, 5, None), (2, 7, None), (3, 4, None),
1006
+ (3, 6, None), (4, 5, None), (4, 6, 'label'), (4, 7, 'label'),
1007
+ (5, 7, None), (6, 7, None)]
1008
+
1009
+ Note that the ``weight`` of the edge shall be input as the ``label``::
1010
+
1011
+ sage: G.add_edge((1, 3), label=5)
1012
+ sage: G.edges()
1013
+ [(0, 1, None), (0, 3, None), (0, 4, None), (0, 5, None),
1014
+ (0, 6, None), (1, 2, None), (1, 3, 5), (1, 4, None),
1015
+ (2, 3, None), (2, 4, 'label'), (2, 5, None), (2, 7, None),
1016
+ (3, 4, None), (3, 6, None), (4, 5, None), (4, 6, 'label'),
1017
+ (4, 7, 'label'), (5, 7, None), (6, 7, None)]
1018
+ sage: G.add_edge((2, 4, 6), label=6)
1019
+ Traceback (most recent call last):
1020
+ ...
1021
+ ValueError: the graph obtained after the addition of edge
1022
+ (((2, 4, 6), None, 6)) is not matching covered
1023
+
1024
+ Vertex name cannot be ``None``, so::
1025
+
1026
+ sage: W = graphs.WheelGraph(6)
1027
+ sage: H = MatchingCoveredGraph(W)
1028
+ sage: H.add_edge(None, 1)
1029
+ Traceback (most recent call last):
1030
+ ...
1031
+ ValueError: the graph obtained after the addition of edge
1032
+ ((None, 1, None)) is not matching covered
1033
+ sage: H.edges(sort=False) # No alteration to the existing graph
1034
+ [(0, 1, None), (0, 2, None), (0, 3, None), (0, 4, None),
1035
+ (0, 5, None), (1, 2, None), (1, 5, None), (2, 3, None),
1036
+ (3, 4, None), (4, 5, None)]
1037
+ sage: H.add_edge(None, None)
1038
+ Traceback (most recent call last):
1039
+ ...
1040
+ ValueError: the graph obtained after the addition of edge
1041
+ ((None, None, None)) is not matching covered
1042
+ sage: H.edges(sort=False) # No alteration to the existing graph
1043
+ [(0, 1, None), (0, 2, None), (0, 3, None), (0, 4, None),
1044
+ (0, 5, None), (1, 2, None), (1, 5, None), (2, 3, None),
1045
+ (3, 4, None), (4, 5, None)]
1046
+
1047
+ EXAMPLES:
1048
+
1049
+ Adding an already existing edge::
1050
+
1051
+ sage: P = graphs.PetersenGraph()
1052
+ sage: G = MatchingCoveredGraph(P)
1053
+ sage: G.add_edge(next(G.edge_iterator()))
1054
+ sage: P == G
1055
+ True
1056
+ sage: G.size()
1057
+ 15
1058
+ sage: G.allow_multiple_edges(True)
1059
+ sage: G.add_edge(0, 1)
1060
+ sage: G.size()
1061
+ 16
1062
+
1063
+ Adding an edge such that the resulting graph is matching covered::
1064
+
1065
+ sage: P = graphs.PetersenGraph()
1066
+ sage: G = MatchingCoveredGraph(P)
1067
+ sage: G.add_edge(1, 4)
1068
+ sage: G.edges(sort=False)
1069
+ [(0, 1, None), (0, 4, None), (0, 5, None), (1, 2, None),
1070
+ (1, 4, None), (1, 6, None), (2, 3, None), (2, 7, None),
1071
+ (3, 4, None), (3, 8, None), (4, 9, None), (5, 7, None),
1072
+ (5, 8, None), (6, 8, None), (6, 9, None), (7, 9, None)]
1073
+
1074
+ Adding an edge with both the incident vertices being existent such
1075
+ that the resulting graph is not matching covered::
1076
+
1077
+ sage: C = graphs.CycleGraph(4)
1078
+ sage: G = MatchingCoveredGraph(C)
1079
+ sage: G.add_edge(0, 2)
1080
+ Traceback (most recent call last):
1081
+ ...
1082
+ ValueError: the graph obtained after the addition of edge
1083
+ ((0, 2, None)) is not matching covered
1084
+ sage: G.edges(sort=False) # No alteration to the existing graph
1085
+ [(0, 1, None), (0, 3, None), (1, 2, None), (2, 3, None)]
1086
+
1087
+ Adding an edge with exactly one incident vertex that is nonexistent
1088
+ throws a :exc:`ValueError` exception, as the resulting graph would
1089
+ have an odd order::
1090
+
1091
+ sage: C = graphs.CycleGraph(4)
1092
+ sage: G = MatchingCoveredGraph(C)
1093
+ sage: G.add_edge(0, 4)
1094
+ Traceback (most recent call last):
1095
+ ...
1096
+ ValueError: the graph obtained after the addition of edge
1097
+ ((0, 4, None)) is not matching covered
1098
+ sage: G.edges(sort=False) # No alteration to the existing graph
1099
+ [(0, 1, None), (0, 3, None), (1, 2, None), (2, 3, None)]
1100
+
1101
+ Adding an edge with both the incident vertices that is nonexistent
1102
+ throws a :exc:`ValueError` exception, as the resulting graph would
1103
+ have been disconnected::
1104
+
1105
+ sage: C = graphs.CycleGraph(4)
1106
+ sage: G = MatchingCoveredGraph(C)
1107
+ sage: G.add_edge(4, 5)
1108
+ Traceback (most recent call last):
1109
+ ...
1110
+ ValueError: the graph obtained after the addition of edge
1111
+ ((4, 5, None)) is not matching covered
1112
+ sage: G.edges(sort=False) # No alteration to the existing graph
1113
+ [(0, 1, None), (0, 3, None), (1, 2, None), (2, 3, None)]
1114
+
1115
+ Adding a self-loop::
1116
+
1117
+ sage: H = graphs.HeawoodGraph()
1118
+ sage: G = MatchingCoveredGraph(H)
1119
+ sage: v = next(G.vertex_iterator())
1120
+ sage: G.add_edge(v, v)
1121
+ Traceback (most recent call last):
1122
+ ...
1123
+ ValueError: loops are not allowed in matching covered graphs
1124
+ """
1125
+ if label is None:
1126
+ if v is None:
1127
+ try:
1128
+ u, v, label = u
1129
+ except Exception:
1130
+ try:
1131
+ u, v = u
1132
+ except Exception:
1133
+ pass
1134
+
1135
+ else:
1136
+ if v is None:
1137
+ try:
1138
+ u, v = u
1139
+ except Exception:
1140
+ pass
1141
+
1142
+ if u in self and v in self:
1143
+ if u == v:
1144
+ raise ValueError('loops are not allowed in '
1145
+ 'matching covered graphs')
1146
+
1147
+ # TODO: A ligher incremental method to check whether the new graph
1148
+ # is matching covered instead of creating a new graph and checking
1149
+
1150
+ G = Graph(self, multiedges=self.allows_multiple_edges())
1151
+ G.add_edge(u, v, label=label)
1152
+
1153
+ try:
1154
+ self.__init__(data=G, matching=self.get_matching())
1155
+
1156
+ except Exception:
1157
+ raise ValueError('the graph obtained after the addition of '
1158
+ 'edge (%s) is not matching covered'
1159
+ % str((u, v, label)))
1160
+
1161
+ else:
1162
+ # At least one of u or v is a nonexistent vertex.
1163
+ # Thus, the resulting graph is either disconnected
1164
+ # or has an odd order, hence not matching covered
1165
+ raise ValueError('the graph obtained after the addition of edge '
1166
+ '(%s) is not matching covered'
1167
+ % str((u, v, label)))
1168
+
1169
+ @doc_index('Overwritten methods')
1170
+ def add_edges(self, edges, loops=False):
1171
+ r"""
1172
+ Add edges from an iterable container.
1173
+
1174
+ .. NOTE::
1175
+
1176
+ This method overwrites the
1177
+ :meth:`~sage.graphs.generic_graph.GenericGraph.add_edges` method
1178
+ to ensure that resultant graph is also matching covered.
1179
+
1180
+ INPUT:
1181
+
1182
+ - ``edges`` -- an iterable of edges, given either as ``(u, v)``
1183
+ or ``(u, v, 'label')``
1184
+
1185
+ - ``loops`` -- boolean (default: ``False``); note that this shall
1186
+ always be set to either ``False`` or ``None`` (since matching covered
1187
+ graphs are free of loops), in which case all the loops
1188
+ ``(v, v, 'label')`` are removed from the iterator. If ``loops`` is
1189
+ set to ``True``, a :exc:`ValueError` is thrown.
1190
+
1191
+ OUTPUT:
1192
+
1193
+ - If ``loops`` is set to ``True``, a :exc:`ValueError` is returned.
1194
+
1195
+ - If ``edges`` is provided with a valid format, but addition of the
1196
+ edges leave the resulting graph not being matching covered, a
1197
+ :exc:`ValueError` is returned without any alteration to the existing
1198
+ matching covered graph. If the addition of the edges preserves the
1199
+ property of matching covered, then the graph is updated and nothing
1200
+ is returned.
1201
+
1202
+ - If ``edges`` is provided with an invalid format, a :exc:`ValueError`
1203
+ is returned.
1204
+
1205
+ EXAMPLES:
1206
+
1207
+ Providing with an empty list of edges::
1208
+
1209
+ sage: C = graphs.CycleGraph(6)
1210
+ sage: G = MatchingCoveredGraph(C)
1211
+ sage: G.add_edges([])
1212
+ sage: G == C
1213
+ True
1214
+
1215
+ Adding some edges, the incident vertices of each of which are existent,
1216
+ such that the resulting graph is matching covered::
1217
+
1218
+ sage: S = graphs.StaircaseGraph(4)
1219
+ sage: G = MatchingCoveredGraph(S)
1220
+ sage: F = [(0, 4), (2, 4), (4, 6), (4, 7)]
1221
+ sage: G.add_edges(F)
1222
+ sage: G.edges(sort=True)
1223
+ [(0, 1, None), (0, 3, None), (0, 4, None), (0, 6, None),
1224
+ (1, 2, None), (1, 4, None), (2, 4, None), (2, 5, None),
1225
+ (2, 7, None), (3, 4, None), (3, 6, None), (4, 5, None),
1226
+ (4, 6, None), (4, 7, None), (5, 7, None), (6, 7, None)]
1227
+
1228
+ Adding some edges, at least one of the incident vertices of some of
1229
+ which are nonexistent such that the resulting graph is matching
1230
+ covered::
1231
+
1232
+ sage: C = graphs.CycleGraph(8)
1233
+ sage: G = MatchingCoveredGraph(C)
1234
+ sage: F = [(0, 9), (1, 8), (2, 9), (3, 8),
1235
+ ....: (4, 9), (5, 8), (6, 9), (7, 8)]
1236
+ sage: G.add_edges(F)
1237
+ sage: G.edges(sort=True)
1238
+ [(0, 1, None), (0, 7, None), (0, 9, None), (1, 2, None),
1239
+ (1, 8, None), (2, 3, None), (2, 9, None), (3, 4, None),
1240
+ (3, 8, None), (4, 5, None), (4, 9, None), (5, 6, None),
1241
+ (5, 8, None), (6, 7, None), (6, 9, None), (7, 8, None)]
1242
+ sage: G.is_isomorphic(graphs.BiwheelGraph(5))
1243
+ True
1244
+
1245
+ Adding a removable double ear to a matching covered graph::
1246
+
1247
+ sage: H = graphs.HexahedralGraph()
1248
+ sage: G = MatchingCoveredGraph(H)
1249
+ sage: F = {(0, 8, None), (1, 10), (4, 11, 'label'),
1250
+ ....: (5, 9), (8, 9), (10, 11)}
1251
+ sage: G.add_edges(F)
1252
+ sage: G.edges(sort=True)
1253
+ [(0, 1, None), (0, 3, None), (0, 4, None), (0, 8, None),
1254
+ (1, 2, None), (1, 5, None), (1, 10, None), (2, 3, None),
1255
+ (2, 6, None), (3, 7, None), (4, 5, None), (4, 7, None),
1256
+ (4, 11, 'label'), (5, 6, None), (5, 9, None), (6, 7, None),
1257
+ (8, 9, None), (10, 11, None)]
1258
+
1259
+ Adding some edges, the incident vertices of each of which are existent,
1260
+ such that the resulting graph is NOT matching covered::
1261
+
1262
+ sage: C = graphs.CycleGraph(6)
1263
+ sage: G = MatchingCoveredGraph(C)
1264
+ sage: F = [(0, 2), (3, 5)]
1265
+ sage: G.add_edges(F)
1266
+ Traceback (most recent call last):
1267
+ ...
1268
+ ValueError: the resulting graph after the addition ofthe edges is not matching covered
1269
+
1270
+ Adding some edges, at least one of the incident vertices of some of
1271
+ which are nonexistent such that the resulting graph is NOT matching
1272
+ covered::
1273
+
1274
+ sage: H = graphs.HexahedralGraph()
1275
+ sage: G = MatchingCoveredGraph(H)
1276
+ sage: F = [(3, 8), (6, 9), (8, 9)]
1277
+ sage: G.add_edges(F)
1278
+ Traceback (most recent call last):
1279
+ ...
1280
+ ValueError: the resulting graph after the addition ofthe edges is not matching covered
1281
+ sage: I = [(0, 8), (1, 9)]
1282
+ sage: G.add_edges(I)
1283
+ Traceback (most recent call last):
1284
+ ...
1285
+ ValueError: the resulting graph after the addition ofthe edges is not matching covered
1286
+ sage: J = [(u, 8) for u in range(8)]
1287
+ sage: G.add_edges(J)
1288
+ Traceback (most recent call last):
1289
+ ...
1290
+ ValueError: odd order is not allowed for matching covered graphs
1291
+
1292
+ Setting the parameter ``loops`` to either ``False`` or ``None``::
1293
+
1294
+ sage: W = graphs.WheelGraph(6)
1295
+ sage: G = MatchingCoveredGraph(W)
1296
+ sage: F = [(0, 0), (1, 3), (2, 4)]
1297
+ sage: G.add_edges(edges=F, loops=False)
1298
+ sage: G.edges(sort=True)
1299
+ [(0, 1, None), (0, 2, None), (0, 3, None), (0, 4, None),
1300
+ (0, 5, None), (1, 2, None), (1, 3, None), (1, 5, None),
1301
+ (2, 3, None), (2, 4, None), (3, 4, None), (4, 5, None)]
1302
+ sage: J = [(1, 1), (3, 5)]
1303
+ sage: G.add_edges(edges=J, loops=True)
1304
+ Traceback (most recent call last):
1305
+ ...
1306
+ ValueError: loops are not allowed in matching covered graphs
1307
+ sage: G.edges(sort=True)
1308
+ [(0, 1, None), (0, 2, None), (0, 3, None), (0, 4, None),
1309
+ (0, 5, None), (1, 2, None), (1, 3, None), (1, 5, None),
1310
+ (2, 3, None), (2, 4, None), (3, 4, None), (4, 5, None)]
1311
+
1312
+ Setting the parameter ``loops`` to ``True``::
1313
+
1314
+ sage: P = graphs.PetersenGraph()
1315
+ sage: G = MatchingCoveredGraph(P)
1316
+ sage: F = [(0, 0), (0, 2), (0, 3)]
1317
+ sage: G.add_edges(edges=F, loops=True)
1318
+ Traceback (most recent call last):
1319
+ ...
1320
+ ValueError: loops are not allowed in matching covered graphs
1321
+
1322
+ Adding a multiple edge::
1323
+
1324
+ sage: S = graphs.StaircaseGraph(4)
1325
+ sage: G = MatchingCoveredGraph(S)
1326
+ sage: G.allow_multiple_edges(True)
1327
+ sage: F = [(0, 1, 'label'), (0, 4), (1, 2)]
1328
+ sage: G.add_edges(F)
1329
+ sage: G.edges(sort=False)
1330
+ [(0, 1, None), (0, 1, 'label'), (0, 3, None), (0, 4, None),
1331
+ (0, 6, None), (1, 2, None), (1, 2, None), (1, 4, None),
1332
+ (2, 5, None), (2, 7, None), (3, 4, None), (3, 6, None),
1333
+ (4, 5, None), (5, 7, None), (6, 7, None)]
1334
+
1335
+ TESTS:
1336
+
1337
+ Providing with an edge in ``edges`` that has 0 values to unpack::
1338
+
1339
+ sage: W = graphs.WagnerGraph()
1340
+ sage: G = MatchingCoveredGraph(W)
1341
+ sage: G.add_edges([()])
1342
+ Traceback (most recent call last):
1343
+ ...
1344
+ ValueError: need more than 0 values to unpack
1345
+
1346
+ Providing with an edge in ``edges`` that has precisely one value to unpack::
1347
+
1348
+ sage: T = graphs.TruncatedBiwheelGraph(10)
1349
+ sage: G = MatchingCoveredGraph(T)
1350
+ sage: G.add_edges([(0, )])
1351
+ Traceback (most recent call last):
1352
+ ...
1353
+ ValueError: need more than 1 value to unpack
1354
+
1355
+ Providing with an edge in ``edges`` that has more than 3 values to unpack::
1356
+
1357
+ sage: B = graphs.BiwheelGraph(5)
1358
+ sage: G = MatchingCoveredGraph(B)
1359
+ sage: G.add_edges([(0, 1, 2, 3, 4)])
1360
+ Traceback (most recent call last):
1361
+ ...
1362
+ ValueError: too many values to unpack (expected 2)
1363
+
1364
+ Providing with an edge of unknown data type::
1365
+
1366
+ sage: M = graphs.MurtyGraph()
1367
+ sage: G = MatchingCoveredGraph(M)
1368
+ sage: F = ['', 'edge', None, 1234]
1369
+ sage: G.add_edges(F)
1370
+ Traceback (most recent call last):
1371
+ ...
1372
+ TypeError: input edges is of unknown type
1373
+ """
1374
+ if loops:
1375
+ raise ValueError('loops are not allowed in '
1376
+ 'matching covered graphs')
1377
+
1378
+ if not edges: # do nothing
1379
+ return
1380
+
1381
+ for edge in edges:
1382
+ if isinstance(edge, tuple):
1383
+ if len(edge) == 0:
1384
+ raise ValueError('need more than 0 values to unpack')
1385
+
1386
+ elif len(edge) == 1:
1387
+ raise ValueError('need more than 1 value to unpack')
1388
+
1389
+ elif len(edge) > 3:
1390
+ raise ValueError('too many values to unpack (expected 2)')
1391
+
1392
+ else:
1393
+ raise TypeError('input edges is of unknown type')
1394
+
1395
+ # Remove potentially duplicated edges
1396
+ edges = list(set(edges))
1397
+
1398
+ # Remove all the loops from edges
1399
+ for edge in edges:
1400
+ if edge[0] == edge[1]:
1401
+ edges.remove(edge)
1402
+
1403
+ # Check if all the incident vertices of the input edges are existent
1404
+ new_vertices = list(set([x for u, v, *_ in edges for x in [u, v]]))
1405
+
1406
+ for vertex in new_vertices[:]:
1407
+ if vertex in self:
1408
+ new_vertices.remove(vertex)
1409
+
1410
+ # Throw error if the no. of new vertices is odd
1411
+ if len(new_vertices) % 2:
1412
+ raise ValueError('odd order is not allowed for '
1413
+ 'matching covered graphs')
1414
+
1415
+ try:
1416
+ G = Graph(self, multiedges=self.allows_multiple_edges())
1417
+ G.add_edges(edges=edges, loops=loops)
1418
+
1419
+ # Check if G has a vertex with at most 1 neighbor
1420
+ if any(len(G.neighbors(v)) <= 1 for v in G):
1421
+ raise ValueError('the resulting graph after the addition of'
1422
+ 'the edges is not matching covered')
1423
+
1424
+ # If all the vertices are existent, the existing perfect matching
1425
+ # can be used.
1426
+ if not new_vertices:
1427
+ self.__init__(data=G, matching=self.get_matching())
1428
+
1429
+ else:
1430
+ # Check if the existing perfect matching may be extended to a
1431
+ # perfect matching of the new graph
1432
+ edges_with_two_new_vertices = []
1433
+
1434
+ for edge in edges:
1435
+ if edge[0] in new_vertices and edge[1] in new_vertices:
1436
+ edges_with_two_new_vertices.append(edge)
1437
+
1438
+ H = Graph(data=edges_with_two_new_vertices, format='list_of_edges')
1439
+ M = Graph(self.get_matching()).union(Graph(H.matching()))
1440
+
1441
+ # Check if M is a perfect matching of the resulting graph
1442
+ if (G.order() != 2*M.size()):
1443
+ M = None
1444
+
1445
+ self.__init__(data=G, matching=M)
1446
+
1447
+ except Exception:
1448
+ raise ValueError('the resulting graph after the addition of'
1449
+ 'the edges is not matching covered')
1450
+
1451
+ @doc_index('Overwritten methods')
1452
+ def add_vertex(self, name=None):
1453
+ r"""
1454
+ Add a vertex to the (matching covered) graph.
1455
+
1456
+ .. NOTE::
1457
+
1458
+ This method overwrites the
1459
+ :meth:`~sage.graphs.generic_graph.GenericGraph.add_vertex` method
1460
+ to ensure that isolated vertices are forbidden in
1461
+ :class:`~MatchingCoveredGraph`.
1462
+
1463
+ INPUT:
1464
+
1465
+ - ``name`` -- an immutable object (default: ``None``); when no name is
1466
+ specified (default), then the new vertex will be represented by the
1467
+ least integer not already representing a vertex. ``name`` must be an
1468
+ immutable object (e.g., an integer, a tuple, etc.).
1469
+
1470
+ OUTPUT:
1471
+
1472
+ - If ``name`` specifies an existing vertex, then nothing is done.
1473
+ Otherwise a :exc:`ValueError` is returned with no change to the
1474
+ existing (matching covered) graph is returned since matching covered
1475
+ graphs are free of isolated vertices.
1476
+
1477
+ EXAMPLES:
1478
+
1479
+ Adding an existing vertex::
1480
+
1481
+ sage: P = graphs.PetersenGraph()
1482
+ sage: P
1483
+ Petersen graph: Graph on 10 vertices
1484
+ sage: G = MatchingCoveredGraph(P)
1485
+ sage: G
1486
+ Matching covered petersen graph: graph on 10 vertices
1487
+ sage: u = next(G.vertex_iterator())
1488
+ sage: G.add_vertex(u)
1489
+ sage: G
1490
+ Matching covered petersen graph: graph on 10 vertices
1491
+
1492
+ Adding a new/ non-existing vertex::
1493
+
1494
+ sage: G.add_vertex()
1495
+ Traceback (most recent call last):
1496
+ ...
1497
+ ValueError: isolated vertices are not allowed in matching covered graphs
1498
+ sage: u = 100
1499
+ sage: G.add_vertex(u)
1500
+ Traceback (most recent call last):
1501
+ ...
1502
+ ValueError: isolated vertices are not allowed in matching covered graphs
1503
+ """
1504
+ if name not in self:
1505
+ raise ValueError('isolated vertices are not allowed in '
1506
+ 'matching covered graphs')
1507
+
1508
+ @doc_index('Overwritten methods')
1509
+ def add_vertices(self, vertices):
1510
+ r"""
1511
+ Add vertices to the (matching covered) graph from an iterable container
1512
+ of vertices.
1513
+
1514
+ .. NOTE::
1515
+
1516
+ This method overwrites the
1517
+ :meth:`~sage.graphs.generic_graph.GenericGraph.add_vertices` method
1518
+ to ensure that isolated vertices are forbidden in
1519
+ :class:`~MatchingCoveredGraph`.
1520
+
1521
+ INPUT:
1522
+
1523
+ - ``vertices`` -- iterator container of vertex labels. A new label is
1524
+ created, used and returned in the output list for all ``None`` values
1525
+ in ``vertices``.
1526
+
1527
+ OUTPUT:
1528
+
1529
+ - If all of the vertices are existing vertices of the (matching
1530
+ covered) graph, then nothing is done; otherwise a :exc:`ValueError`
1531
+ is returned with no change to the existing (matching covered) graph
1532
+ since matching covered graphs are free of isolated vertices.
1533
+
1534
+ EXAMPLES:
1535
+
1536
+ Adding a list of already existing vertices::
1537
+
1538
+ sage: T = graphs.TruncatedBiwheelGraph(15)
1539
+ sage: T
1540
+ Truncated biwheel graph: Graph on 30 vertices
1541
+ sage: G = MatchingCoveredGraph(T)
1542
+ sage: G
1543
+ Matching covered truncated biwheel graph: graph on 30 vertices
1544
+ sage: S = [0, 1, 2, 3] # We choose 4 existing vertices
1545
+ sage: G.add_vertices(S)
1546
+ sage: G
1547
+ Matching covered truncated biwheel graph: graph on 30 vertices
1548
+
1549
+ Adding a list of vertices in which at least one is non-existent or
1550
+ ``None`` or possibly both::
1551
+
1552
+ sage: T = graphs.CompleteGraph(2)
1553
+ sage: T
1554
+ Complete graph: Graph on 2 vertices
1555
+ sage: G = MatchingCoveredGraph(T)
1556
+ sage: G
1557
+ Matching covered complete graph: graph on 2 vertices
1558
+ sage: S1 = [2, 3, 4]
1559
+ sage: G.add_vertices(S1)
1560
+ Traceback (most recent call last):
1561
+ ...
1562
+ ValueError: isolated vertices are not allowed in matching covered graphs
1563
+ sage: S2 = [None, None]
1564
+ sage: G.add_vertices(S2)
1565
+ Traceback (most recent call last):
1566
+ ...
1567
+ ValueError: isolated vertices are not allowed in matching covered graphs
1568
+ sage: S3 = [2, None, None, 5]
1569
+ sage: G.add_vertices(S3)
1570
+ Traceback (most recent call last):
1571
+ ...
1572
+ ValueError: isolated vertices are not allowed in matching covered graphs
1573
+ """
1574
+ if any(vertex not in self for vertex in vertices):
1575
+ raise ValueError('isolated vertices are not allowed in '
1576
+ 'matching covered graphs')
1577
+
1578
+ @doc_index('Overwritten methods')
1579
+ def allow_loops(self, new, check=True):
1580
+ r"""
1581
+ Change whether loops are allowed in (matching covered) graphs.
1582
+
1583
+ .. NOTE::
1584
+
1585
+ This method overwrites the
1586
+ :meth:`~sage.graphs.generic_graph.GenericGraph.allow_loops` method
1587
+ to ensure that loops are forbidden in :class:`~MatchingCoveredGraph`.
1588
+
1589
+ INPUT:
1590
+
1591
+ - ``new`` -- boolean
1592
+
1593
+ - ``check`` -- boolean (default: ``True``); whether to remove existing
1594
+ loops from the graph when the new status is ``False``. It is an
1595
+ argument in
1596
+ :meth:`~sage.graphs.generic_graph.GenericGraph.allow_loops` method
1597
+ and is not used in this overwritten one.
1598
+
1599
+ OUTPUT:
1600
+
1601
+ - A :exc:`ValueError` is returned with no change to the existing
1602
+ (matching covered) graph if ``new`` is ``True`` since a matching
1603
+ covered graph, by definition, is free of self-loops. If ``new`` is
1604
+ set to ``False``, there is no output.
1605
+
1606
+ EXAMPLES:
1607
+
1608
+ Petersen graph is matching covered::
1609
+
1610
+ sage: P = graphs.PetersenGraph()
1611
+ sage: P.is_matching_covered()
1612
+ True
1613
+ sage: G = MatchingCoveredGraph(P)
1614
+ sage: G.allow_loops(True)
1615
+ Traceback (most recent call last):
1616
+ ...
1617
+ ValueError: loops are not allowed in matching covered graphs
1618
+
1619
+ .. SEEALSO::
1620
+
1621
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.allows_loops`
1622
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.has_loops`
1623
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.loop_edges`
1624
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.loop_vertices`
1625
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.loops`
1626
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.number_of_loops`
1627
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.remove_loops`
1628
+ """
1629
+ if new:
1630
+ raise ValueError('loops are not allowed in '
1631
+ 'matching covered graphs')
1632
+
1633
+ @doc_index('Overwritten methods')
1634
+ def allows_loops(self):
1635
+ r"""
1636
+ Return whether loops are permitted in (matching covered) graphs.
1637
+
1638
+ .. NOTE::
1639
+
1640
+ This method overwrites the
1641
+ :meth:`~sage.graphs.generic_graph.GenericGraph.allows_loops` method
1642
+ to show that loops are forbidden in :class:`~MatchingCoveredGraph`.
1643
+
1644
+ OUTPUT:
1645
+
1646
+ - A boolean value ``False`` is returned, since matching covered graphs,
1647
+ by definition, are free of loops.
1648
+
1649
+ EXAMPLES:
1650
+
1651
+ Petersen graph is matching covered::
1652
+
1653
+ sage: P = graphs.PetersenGraph()
1654
+ sage: P.is_matching_covered()
1655
+ True
1656
+ sage: G = MatchingCoveredGraph(P)
1657
+ sage: G.allows_loops()
1658
+ False
1659
+
1660
+ .. SEEALSO::
1661
+
1662
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.allow_loops`
1663
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.has_loops`
1664
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.loop_edges`
1665
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.loop_vertices`
1666
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.loops`
1667
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.number_of_loops`
1668
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.remove_loops`
1669
+ """
1670
+ return False
1671
+
1672
+ @doc_index('Barriers and canonical partition')
1673
+ def canonical_partition(self):
1674
+ r"""
1675
+ Return the canonical partition of the (matching covered) graph.
1676
+
1677
+ For a matching covered graph `G`, a subset `B` of the vertex set `V` is
1678
+ a barrier if `|B| = o(G - B)`, where `|B|` denotes the cardinality of
1679
+ the set `B` and `o(G - B)` denotes the number of odd components in the
1680
+ graph `G - B`. And a barrier `B` is a maximal barrier if `C` is not a
1681
+ barrier for each `C` such that `B \subset C \subseteq V`.
1682
+
1683
+ Note that in a matching covered graph, each vertex belongs to a unique
1684
+ maximal barrier. The maximal barriers of a matching covered graph
1685
+ partitions its vertex set and the partition of the vertex set of a
1686
+ matching covered graph into its maximal barriers is called as its
1687
+ *canonical* *partition*.
1688
+
1689
+ OUTPUT:
1690
+
1691
+ - A list of sets that constitute a (canonical) partition of the vertex
1692
+ set, wherein each set is a (unique) maximal barrier of the (matching
1693
+ covered) graph.
1694
+
1695
+ EXAMPLES:
1696
+
1697
+ Show the maximal barrier of the graph `K_4 \odot K_{3, 3}`::
1698
+
1699
+ sage: G = Graph([
1700
+ ....: (0, 2), (0, 3), (0, 4), (1, 2),
1701
+ ....: (1, 3), (1, 4), (2, 5), (3, 6),
1702
+ ....: (4, 7), (5, 6), (5, 7), (6, 7)
1703
+ ....: ])
1704
+ sage: H = MatchingCoveredGraph(G)
1705
+ sage: H.canonical_partition()
1706
+ [{0}, {1}, {2, 3, 4}, {5}, {6}, {7}]
1707
+
1708
+ For a bicritical graph (for instance, the Petersen graph), the
1709
+ canonical parition constitutes of only singleton sets each containing
1710
+ an individual vertex::
1711
+
1712
+ sage: P = graphs.PetersenGraph()
1713
+ sage: G = MatchingCoveredGraph(P)
1714
+ sage: G.canonical_partition()
1715
+ [{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}]
1716
+
1717
+ For a bipartite matching covered graph (for instance, the Hexahedral
1718
+ graph), the canonical partition consists of two sets each of which
1719
+ corresponds to the individual color class::
1720
+
1721
+ sage: H = graphs.HexahedralGraph()
1722
+ sage: G = MatchingCoveredGraph(H)
1723
+ sage: G.canonical_partition()
1724
+ [{0, 2, 5, 7}, {1, 3, 4, 6}]
1725
+ sage: B = BipartiteGraph(H)
1726
+ sage: list(B.bipartition()) == G.canonical_partition()
1727
+ True
1728
+
1729
+ REFERENCES:
1730
+
1731
+ - [LM2024]_
1732
+
1733
+ .. SEEALSO::
1734
+
1735
+ - :meth:`~sage.graphs.graph.Graph.is_bicritical`
1736
+ - :meth:`~sage.graphs.graph.Graph.is_matching_covered`
1737
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.maximal_barrier`
1738
+ """
1739
+ visited = set()
1740
+
1741
+ maximal_barriers = []
1742
+ for v in self:
1743
+ if v not in visited:
1744
+ B = self.maximal_barrier(v)
1745
+ visited.update(B)
1746
+ maximal_barriers.append(B)
1747
+
1748
+ return maximal_barriers
1749
+
1750
+ @doc_index('Overwritten methods')
1751
+ def delete_vertex(self, vertex, in_order=False):
1752
+ r"""
1753
+ Delete a vertex, removing all incident edges.
1754
+
1755
+ .. NOTE::
1756
+
1757
+ This method overwrites the
1758
+ :meth:`~sage.graphs.generic_graph.GenericGraph.delete_vertex`
1759
+ method to ensure that an odd order is forbidden in
1760
+ :class:`~MatchingCoveredGraph`.
1761
+
1762
+ INPUT:
1763
+
1764
+ - ``vertex`` -- a vertex that is to be deleted.
1765
+
1766
+ - ``in_order`` -- boolean (default: ``False``); if ``True``, this
1767
+ deletes the `i`-th vertex in the sorted list of vertices, i.e.
1768
+ ``G.vertices(sort=True)[i]``
1769
+
1770
+ OUTPUT:
1771
+
1772
+ - Deleting a non-existent vertex raises a :exc:`ValueError` exception;
1773
+ also a (different) :exc:`ValueError` is returned on deleting an
1774
+ existing vertex since matching covered graphs are of even order. In
1775
+ both cases no modifications are made to the existing (matching
1776
+ covered) graph.
1777
+
1778
+ EXAMPLES:
1779
+
1780
+ Deleting a non-existent vertex::
1781
+
1782
+ sage: W = graphs.WheelGraph(12)
1783
+ sage: G = MatchingCoveredGraph(W)
1784
+ sage: u = 100
1785
+ sage: G.delete_vertex(u)
1786
+ Traceback (most recent call last):
1787
+ ...
1788
+ ValueError: vertex (100) not in the graph
1789
+ sage: G.delete_vertex(vertex=u, in_order=True)
1790
+ Traceback (most recent call last):
1791
+ ...
1792
+ ValueError: vertex (100) not in the graph
1793
+
1794
+ Deleting an existing vertex::
1795
+
1796
+ sage: W = graphs.WheelGraph(12)
1797
+ sage: G = MatchingCoveredGraph(W)
1798
+ sage: u = next(G.vertex_iterator())
1799
+ sage: G.delete_vertex(u)
1800
+ Traceback (most recent call last):
1801
+ ...
1802
+ ValueError: odd order is not allowed for matching covered graphs
1803
+ sage: G.delete_vertex(vertex=u, in_order=True)
1804
+ Traceback (most recent call last):
1805
+ ...
1806
+ ValueError: odd order is not allowed for matching covered graphs
1807
+ """
1808
+ if vertex not in self:
1809
+ raise ValueError('vertex (%s) not in the graph' % str(vertex))
1810
+
1811
+ if in_order:
1812
+ vertex = self.vertices(sort=True)[vertex]
1813
+
1814
+ raise ValueError('odd order is not allowed for '
1815
+ 'matching covered graphs')
1816
+
1817
+ @doc_index('Overwritten methods')
1818
+ def delete_vertices(self, vertices):
1819
+ r"""
1820
+ Delete specified vertices form ``self``.
1821
+
1822
+ This method deletes the vertices from the iterable container
1823
+ ``vertices`` from ``self`` along with incident edges. An error is
1824
+ raised if the resulting graph is not matching covered.
1825
+
1826
+ .. NOTE::
1827
+
1828
+ This method overwrites the
1829
+ :meth:`~sage.graphs.generic_graph.GenericGraph.delete_vertices`
1830
+ method to ensure that an odd order is forbidden in
1831
+ :class:`~MatchingCoveredGraph`.
1832
+
1833
+ INPUT:
1834
+
1835
+ - ``vertices`` -- a list/ set of vertices that are to be deleted.
1836
+
1837
+ OUTPUT:
1838
+
1839
+ - Deleting a non-existent vertex will raise a :exc:`ValueError`
1840
+ exception, in which case none of the vertices in ``vertices``
1841
+ is deleted.
1842
+
1843
+ - If all of the vertices in the list/ set provided exist in the graph,
1844
+ but the resulting graph after deletion of all of those is not
1845
+ matching covered, then a :exc:`ValueError` exception is raised
1846
+ without any alterations to the existing (matching covered) graph,
1847
+ otherwise the vertices are deleted and nothing is returned.
1848
+
1849
+ EXAMPLES:
1850
+
1851
+ Providing with an empty list of vertices::
1852
+
1853
+ sage: C = graphs.CycleGraph(6)
1854
+ sage: G = MatchingCoveredGraph(C)
1855
+ sage: G.delete_vertices([])
1856
+ sage: G == C
1857
+ True
1858
+
1859
+ Removing all the existent vertices::
1860
+
1861
+ sage: M = graphs.MoebiusLadderGraph(10)
1862
+ sage: G = MatchingCoveredGraph(M)
1863
+ sage: S = list(G.vertices())
1864
+ sage: G.delete_vertices(S)
1865
+ Traceback (most recent call last):
1866
+ ...
1867
+ ValueError: the resulting graph after the removal of the vertices
1868
+ is trivial, therefore is not matching covered
1869
+
1870
+ Providing with a list of vertices with at least one non-existent
1871
+ vertex::
1872
+
1873
+ sage: S = graphs.StaircaseGraph(4)
1874
+ sage: S
1875
+ Staircase graph: Graph on 8 vertices
1876
+ sage: G = MatchingCoveredGraph(S)
1877
+ sage: G
1878
+ Matching covered staircase graph: graph on 8 vertices
1879
+ sage: T = list(range(5, 20, 2))
1880
+ sage: G.delete_vertices(T)
1881
+ Traceback (most recent call last):
1882
+ ...
1883
+ ValueError: vertex (9) not in the graph
1884
+
1885
+ Removing an odd no. of distinct vertices from
1886
+ a matching covered graph::
1887
+
1888
+ sage: P = graphs.PetersenGraph()
1889
+ sage: G = MatchingCoveredGraph(P)
1890
+ sage: S = [0, 1, 2, 10, 10, 100]
1891
+ sage: G.delete_vertices(S)
1892
+ Traceback (most recent call last):
1893
+ ...
1894
+ ValueError: an odd no. of distinct vertices can not be
1895
+ removed from a matching covered graph
1896
+
1897
+ Providing with a list of existent vertices whose deletion results in a
1898
+ graph which is not matching covered::
1899
+
1900
+ sage: S = graphs.StaircaseGraph(4)
1901
+ sage: S
1902
+ Staircase graph: Graph on 8 vertices
1903
+ sage: G = MatchingCoveredGraph(S)
1904
+ sage: G
1905
+ Matching covered staircase graph: graph on 8 vertices
1906
+ sage: T = [1, 4]
1907
+ sage: G.delete_vertices(T)
1908
+ Traceback (most recent call last):
1909
+ ...
1910
+ ValueError: the resulting graph after the removal of
1911
+ the vertices is not matching covered
1912
+
1913
+ Providing with a list of existent vertices after the deletion of which
1914
+ the resulting graph is still matching covered; note that in the
1915
+ following example, after the deletion of two vertices from a staircase
1916
+ graph, the resulting graph is NOT a staircase graph
1917
+ (see :issue:`38768`)::
1918
+
1919
+ sage: S = graphs.StaircaseGraph(4)
1920
+ sage: S
1921
+ Staircase graph: Graph on 8 vertices
1922
+ sage: G = MatchingCoveredGraph(S)
1923
+ sage: G
1924
+ Matching covered staircase graph: graph on 8 vertices
1925
+ sage: T = [6, 7]
1926
+ sage: G.delete_vertices(T)
1927
+ sage: G # Matching covered graph on 6 vertices
1928
+ Matching covered staircase graph: graph on 6 vertices
1929
+ """
1930
+ if not vertices: # do nothing
1931
+ return
1932
+
1933
+ # Remove potentially duplicated vertices
1934
+ vertices = set(vertices)
1935
+
1936
+ if len(vertices) % 2: # try to remove an odd number of vertices
1937
+ raise ValueError('an odd no. of distinct vertices can not be '
1938
+ 'removed from a matching covered graph')
1939
+
1940
+ for vertex in vertices:
1941
+ if vertex not in self:
1942
+ raise ValueError('vertex (%s) not in the graph' % str(vertex))
1943
+
1944
+ if self.order() == len(vertices):
1945
+ raise ValueError('the resulting graph after the removal of the '
1946
+ 'vertices is trivial, therefore is not '
1947
+ 'matching covered')
1948
+
1949
+ try:
1950
+ G = Graph(self, multiedges=self.allows_multiple_edges())
1951
+ G.delete_vertices(vertices)
1952
+
1953
+ M = Graph(self.get_matching())
1954
+
1955
+ M.delete_vertices(vertices)
1956
+ # The resulting matching after the removal of the input vertices
1957
+ # must be a valid perfect matching of the resulting graph obtained
1958
+ # after the removal of the vertices
1959
+
1960
+ if (G.order() != 2*M.size()):
1961
+ M = None
1962
+
1963
+ self.__init__(data=G, matching=M)
1964
+
1965
+ except Exception:
1966
+ raise ValueError('the resulting graph after the removal of '
1967
+ 'the vertices is not matching covered')
1968
+
1969
+ @doc_index('Miscellaneous methods')
1970
+ def get_matching(self):
1971
+ r"""
1972
+ Return an :class:`~EdgesView` of ``self._matching``.
1973
+
1974
+ OUTPUT:
1975
+
1976
+ - This method returns :class:`EdgesView` of the edges of a
1977
+ perfect matching of the (matching covered) graph.
1978
+
1979
+ EXAMPLES:
1980
+
1981
+ If one specifies a perfect matching while initializing the object, the
1982
+ value of ``self._matching`` is captures the same matching::
1983
+
1984
+ sage: P = graphs.PetersenGraph()
1985
+ sage: M = [(0, 1), (2, 3), (4, 9), (5, 7), (6, 8)]
1986
+ sage: G = MatchingCoveredGraph(P, M)
1987
+ sage: sorted(G.get_matching())
1988
+ [(0, 1), (2, 3), (4, 9), (5, 7), (6, 8)]
1989
+ sage: M == sorted(G.get_matching())
1990
+ True
1991
+
1992
+ If no matching is specified while initilizing a matching covered graph,
1993
+ a perfect is computed
1994
+ :meth:`~sage.graphs.graph.Graph.matching` and that is captured as
1995
+ ``self._matching``::
1996
+
1997
+ sage: P = graphs.PetersenGraph()
1998
+ sage: M = P.matching()
1999
+ sage: G = MatchingCoveredGraph(P)
2000
+ sage: sorted(G.get_matching())
2001
+ [(0, 5, None), (1, 6, None), (2, 7, None), (3, 8, None), (4, 9, None)]
2002
+ sage: sorted(M) == sorted(G.get_matching())
2003
+ True
2004
+ """
2005
+ return self._matching
2006
+
2007
+ @doc_index('Barriers and canonical partition')
2008
+ def maximal_barrier(self, vertex):
2009
+ r"""
2010
+ Return the (unique) maximal barrier containing the vertex.
2011
+
2012
+ For a matching covered graph `G`, a subset `B` of the vertex set `V` is
2013
+ a barrier if `|B| = o(G - B)`, where `|B|` denotes the cardinality of
2014
+ the set `B` and `o(G - B)` denotes the number of odd components in the
2015
+ graph `G - B`. And a barrier `B` is a maximal barrier if `C` is not a
2016
+ barrier for each `C` such that `B \subset C \subseteq V`.
2017
+
2018
+ In a matching covered graph, each vertex belongs to a unique maximal
2019
+ barrier, which is a consequence of the following theorem.
2020
+
2021
+ .. RUBRIC:: Theorem [LM2024]_:
2022
+
2023
+ Let `u` and `v` be any two vertices in a matchable graph `G`. Then the
2024
+ graph `G - u - v` is matchable if and only if there is no barrier of
2025
+ `G` which contains both `u` and `v`.
2026
+
2027
+ And in order to find the vertices that do not lie in the maximal
2028
+ barrier containing the provided vertex in linear time we take
2029
+ inspiration of the `M` alternating tree seach method [LR2004]_.
2030
+
2031
+ INPUT:
2032
+
2033
+ - ``vertex`` -- a vertex of the graph
2034
+
2035
+ OUTPUT:
2036
+
2037
+ - A :exc:`~ValueError` is returned if ``vertex`` is not a vertex of the
2038
+ graph, otherwise a set of vertices that constitute the (unique)
2039
+ maximal barrier containing the vertex is returned.
2040
+
2041
+ EXAMPLES:
2042
+
2043
+ The graph `K_4 \odot K_{3, 3}` is matching covered. Show the set of
2044
+ vertices in the (unique) maximal barrier containing the vertex `2`::
2045
+
2046
+ sage: G = Graph([
2047
+ ....: (0, 2), (0, 3), (0, 4), (1, 2),
2048
+ ....: (1, 3), (1, 4), (2, 5), (3, 6),
2049
+ ....: (4, 7), (5, 6), (5, 7), (6, 7)
2050
+ ....: ])
2051
+ sage: H = MatchingCoveredGraph(G)
2052
+ sage: B = H.maximal_barrier(2)
2053
+ sage: B
2054
+ {2, 3, 4}
2055
+
2056
+ Let `B` be a maximal barrier of a matching covered graph `G` (which is,
2057
+ of course, a matchable graph). The graph, `J := G - B` has no even
2058
+ component::
2059
+
2060
+ sage: J = G.copy()
2061
+ sage: J.delete_vertices(B)
2062
+ sage: all(len(K)%2 != 0 for K in J.connected_components(sort=True))
2063
+ True
2064
+
2065
+ Let `B` be a maximal barrier in a matching covered graph `G` and let
2066
+ `M` be a perfect matching of `G`. If `K` is an odd component of
2067
+ `J := G - B`, then `M \cap \partial_G(K)` has precisely one edge and if
2068
+ `v` is the end of that edge in `V(K)`, then `M \cap E(K)` is a perfect
2069
+ matching of `K - v`::
2070
+
2071
+ sage: K = J.subgraph(vertices=(J.connected_components(sort=True))[0])
2072
+ sage: # Let F := \partial_G(K) and T := M \cap F
2073
+ sage: F = [edge for edge in G.edge_iterator()
2074
+ ....: if (edge[0] in K and edge[1] not in K)
2075
+ ....: or (edge[0] not in K and edge[1] in K)
2076
+ ....: ]
2077
+ sage: M = H.get_matching()
2078
+ sage: T = [edge for edge in F if edge in M]
2079
+ sage: len(T) == 1
2080
+ True
2081
+ sage: v = T[0][0] if T[0][0] in K else T[0][1]
2082
+ sage: # Let N := M \cap E(K) and L := K - v
2083
+ sage: N = Graph([edge for edge in K.edge_iterator() if edge in M])
2084
+ sage: L = K.copy()
2085
+ sage: L.delete_vertex(v)
2086
+ sage: # Check if N is a perfect matching of L
2087
+ sage: L.order() == 2*N.size()
2088
+ True
2089
+
2090
+ Let `B` be a maximal barrier of a matching covered graph `G` (which is,
2091
+ of course, a matchable graph). The graph induced by each component of
2092
+ `G - B` is factor critical::
2093
+
2094
+ sage: all((K.subgraph(vertices=connected_component)).is_factor_critical()
2095
+ ....: for connected_component in K.connected_components(sort=True)
2096
+ ....: )
2097
+ True
2098
+
2099
+ For a bicritical graph (for instance, the Petersen graph), for each
2100
+ vertex the maximal barrier is a singleton set containing only that
2101
+ vertex::
2102
+
2103
+ sage: P = graphs.PetersenGraph()
2104
+ sage: G = MatchingCoveredGraph(P)
2105
+ sage: u = 0
2106
+ sage: set([u]) == G.maximal_barrier(u)
2107
+ True
2108
+
2109
+ In a bipartite matching covered graph (for instance, the Hexahedral
2110
+ graph), for a vertex, the maximal barrier is the set of vertices of
2111
+ the color class that the particular vertex belongs to. In other words,
2112
+ there are precisely two maximal barriers in a bipartite matching
2113
+ covered graph, that is, the vertex sets of the individual color class::
2114
+
2115
+ sage: G = graphs.HexahedralGraph()
2116
+ sage: H = MatchingCoveredGraph(G)
2117
+ sage: A, _ = H.bipartite_sets()
2118
+ sage: # needs random
2119
+ sage: import random
2120
+ sage: a = random.choice(list(A))
2121
+ sage: A == H.maximal_barrier(a)
2122
+ True
2123
+
2124
+ Maximal barriers of matching covered graph constitute a partition of
2125
+ its vertex set::
2126
+
2127
+ sage: S = set()
2128
+ sage: for v in H:
2129
+ ....: B = tuple(sorted(list(H.maximal_barrier(v))))
2130
+ ....: S.add(B)
2131
+ sage: S = list(S)
2132
+ sage: # Check that S is a partition of the vertex set of H
2133
+ sage: # Part 1: Check if S spans the vertex set of H
2134
+ sage: sorted([u for B in S for u in B]) == sorted(list(H))
2135
+ True
2136
+ sage: # Part 2: Check if each maximal barrier in S is disjoint
2137
+ sage: is_disjoint = True
2138
+ sage: for i in range(len(S)):
2139
+ ....: for j in range(i+1, len(S)):
2140
+ ....: c = [v for v in S[i] if v in S[j]]
2141
+ ....: is_disjoint = (len(c) == 0)
2142
+ sage: is_disjoint
2143
+ True
2144
+
2145
+ TESTS:
2146
+
2147
+ Providing with a nonexistent vertex::
2148
+
2149
+ sage: P = graphs.PetersenGraph()
2150
+ sage: G = MatchingCoveredGraph(P)
2151
+ sage: G.maximal_barrier('')
2152
+ Traceback (most recent call last):
2153
+ ...
2154
+ ValueError: vertex not in the graph
2155
+ sage: G.maximal_barrier(100)
2156
+ Traceback (most recent call last):
2157
+ ...
2158
+ ValueError: vertex 100 not in the graph
2159
+
2160
+ REFERENCES:
2161
+
2162
+ - [LZ2004]_
2163
+ - [LM2024]_
2164
+
2165
+ .. SEEALSO::
2166
+
2167
+ - :meth:`~sage.graphs.graph.Graph.is_bicritical`
2168
+ - :meth:`~sage.graphs.graph.Graph.is_matching_covered`
2169
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.canonical_partition`
2170
+ """
2171
+ if vertex not in self:
2172
+ raise ValueError('vertex {} not in the graph'.format(vertex))
2173
+
2174
+ # u: The M neighbor of vertex
2175
+ matching = self.get_matching()
2176
+ u = next((a if b == vertex else b) for a, b, *_ in matching if vertex in [a, b])
2177
+
2178
+ # Goal: Find the vertices w such that G - w - vertex is matchable.
2179
+ # In other words, there exists an odd length M-alternating vertex-w
2180
+ # path in G, starting and ending with edges in M. Alternatively, there
2181
+ # exists an even length M-alternating u-w path in the graph G - vertex
2182
+ # starting with an edge not in M and ending with and edge in M.
2183
+
2184
+ # even: The set of all such vertex w
2185
+ from sage.graphs.matching import M_alternating_even_mark
2186
+ even = M_alternating_even_mark(G=self, matching=matching,
2187
+ vertex=u)
2188
+
2189
+ B = set([vertex])
2190
+ B.update(v for v in self if v not in even)
2191
+
2192
+ return B
2193
+
2194
+ @doc_index('Overwritten methods')
2195
+ def has_loops(self):
2196
+ r"""
2197
+ Check whether there are loops in the (matching covered) graph.
2198
+
2199
+ .. NOTE::
2200
+
2201
+ This method overwrites the
2202
+ :meth:`~sage.graphs.generic_graph.GenericGraph.has_loops` method in
2203
+ order to return ``False`` as matching covered graphs are always
2204
+ free of looped edges.
2205
+
2206
+ OUTPUT:
2207
+
2208
+ - A boolean ``False`` is returned since matching covered graphs, by
2209
+ definition, are free of self-loops.
2210
+
2211
+ EXAMPLES:
2212
+
2213
+ A matching covered graph, for instance the Petersen graph, is always free
2214
+ of loops::
2215
+
2216
+ sage: P = graphs.PetersenGraph()
2217
+ sage: G = MatchingCoveredGraph(P)
2218
+ sage: G
2219
+ Matching covered petersen graph: graph on 10 vertices
2220
+ sage: G.has_loops()
2221
+ False
2222
+ sage: G.allows_loops()
2223
+ False
2224
+ sage: G.add_edge(0, 0)
2225
+ Traceback (most recent call last):
2226
+ ...
2227
+ ValueError: loops are not allowed in matching covered graphs
2228
+
2229
+ A matching covered graph may support multiple edges, still no
2230
+ loops are allowed::
2231
+
2232
+ sage: K = graphs.CompleteGraph(2)
2233
+ sage: G = MatchingCoveredGraph(K)
2234
+ sage: G.allow_multiple_edges(True)
2235
+ sage: G
2236
+ Matching covered complete graph: multi-graph on 2 vertices
2237
+ sage: G.add_edge(0, 1, 'label')
2238
+ sage: G.add_edge(0, 0)
2239
+ Traceback (most recent call last):
2240
+ ...
2241
+ ValueError: loops are not allowed in matching covered graphs
2242
+ sage: G.edges(sort=False)
2243
+ [(0, 1, None), (0, 1, 'label')]
2244
+ sage: G.allows_loops()
2245
+ False
2246
+ sage: G.has_loops()
2247
+ False
2248
+ sage: G.allow_loops(True)
2249
+ Traceback (most recent call last):
2250
+ ...
2251
+ ValueError: loops are not allowed in matching covered graphs
2252
+
2253
+ .. SEEALSO::
2254
+
2255
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.allow_loops`
2256
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.allows_loops`
2257
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.loop_edges`
2258
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.loop_vertices`
2259
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.loops`
2260
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.number_of_loops`
2261
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.remove_loops`
2262
+ """
2263
+ return False
2264
+
2265
+ @doc_index('Overwritten methods')
2266
+ def has_perfect_matching(G, algorithm='Edmonds', solver=None, verbose=0,
2267
+ *, integrality_tolerance=1e-3):
2268
+ r"""
2269
+ Check whether the graph has a perfect matching.
2270
+
2271
+ .. NOTE::
2272
+
2273
+ This method overwrites the
2274
+ :meth:`~sage.graphs.graph.Graph.has_perfect_matching` method in
2275
+ order to return ``True`` (provided the input arguments are valid)
2276
+ as matching covered graphs always admit a perfect matching.
2277
+
2278
+ INPUT:
2279
+
2280
+ - ``algorithm`` -- string (default: ``'Edmonds'``)
2281
+
2282
+ - ``'Edmonds'`` uses Edmonds' algorithm as implemented in NetworkX to
2283
+ find a matching of maximal cardinality, then check whether this
2284
+ cardinality is half the number of vertices of the graph.
2285
+
2286
+ - ``'LP_matching'`` uses a Linear Program to find a matching of
2287
+ maximal cardinality, then check whether this cardinality is half the
2288
+ number of vertices of the graph.
2289
+
2290
+ - ``'LP'`` uses a Linear Program formulation of the perfect matching
2291
+ problem: put a binary variable ``b[e]`` on each edge `e`, and for
2292
+ each vertex `v`, require that the sum of the values of the edges
2293
+ incident to `v` is 1.
2294
+
2295
+ - ``solver`` -- string (default: ``None``); specifies a Mixed Integer
2296
+ Linear Programming (MILP) solver to be used. If set to ``None``, the
2297
+ default one is used. For more information on MILP solvers and which
2298
+ default solver is used, see the method :meth:`solve
2299
+ <sage.numerical.mip.MixedIntegerLinearProgram.solve>` of the class
2300
+ :class:`MixedIntegerLinearProgram
2301
+ <sage.numerical.mip.MixedIntegerLinearProgram>`.
2302
+
2303
+ - ``verbose`` -- integer (default: 0); sets the level of verbosity:
2304
+ set to 0 by default, which means quiet (only useful when
2305
+ ``algorithm == "LP_matching"`` or ``algorithm == "LP"``)
2306
+
2307
+ - ``integrality_tolerance`` -- float; parameter for use with MILP
2308
+ solvers over an inexact base ring; see
2309
+ :meth:`MixedIntegerLinearProgram.get_values`.
2310
+
2311
+ OUTPUT:
2312
+
2313
+ - If the input arguments are valid, a boolean (``True``) is returned as
2314
+ a maximum matching of a matching covered graph is always a perfect
2315
+ matching, otherwise a :exc:`~ValueError` is raised.
2316
+
2317
+ EXAMPLES:
2318
+
2319
+ Note that regardless of the algorithm (as long as the input arguments
2320
+ are in valid format), the method always returns the boolean ``True``::
2321
+
2322
+ sage: P = graphs.PetersenGraph()
2323
+ sage: P.has_perfect_matching() # Calls Graph.has_perfect_matching()
2324
+ True
2325
+ sage: G = MatchingCoveredGraph(P)
2326
+ sage: G.has_perfect_matching() # Calls MatchingCoveredGraph.has_perfect_matching()
2327
+ True
2328
+ sage: W = graphs.WheelGraph(6)
2329
+ sage: H = MatchingCoveredGraph(W)
2330
+ sage: H.has_perfect_matching(algorithm='LP_matching')
2331
+ True
2332
+
2333
+ Providing with an algorithm, that is not one of ``'Edmonds'``,
2334
+ ``'LP_matching'`` or ``'LP'``::
2335
+
2336
+ sage: S = graphs.StaircaseGraph(4)
2337
+ sage: J = MatchingCoveredGraph(S)
2338
+ sage: J.has_perfect_matching(algorithm='algorithm')
2339
+ Traceback (most recent call last):
2340
+ ...
2341
+ ValueError: algorithm must be set to 'Edmonds',
2342
+ 'LP_matching' or 'LP'
2343
+ """
2344
+ if algorithm in ['Edmonds', 'LP_matching', 'LP']:
2345
+ return True
2346
+
2347
+ raise ValueError('algorithm must be set to \'Edmonds\', '
2348
+ '\'LP_matching\' or \'LP\'')
2349
+
2350
+ @doc_index('Bricks, braces and tight cut decomposition')
2351
+ def is_brace(self, coNP_certificate=False):
2352
+ r"""
2353
+ Check if the (matching covered) graph is a brace.
2354
+
2355
+ A matching covered graph which is free of nontrivial tight cuts is
2356
+ called a *brace* if it is bipartite. Let `G := (A \cup B, E)` be a
2357
+ bipartite matching covered graph on six or more vertices. The
2358
+ following statements are equivalent [LM2024]_:
2359
+
2360
+ 1. `G` is a brace (aka free of nontrivial tight cuts).
2361
+ 2. `G - a_1 - a_2 - b_1 - b_2` has a perfect matching for any two
2362
+ distinct vertices `a_1` and `a_2` in `A` and any two distinct
2363
+ vertices `b_1` and `b_2` in `B`.
2364
+ 3. `G` is two extendable (any two nonadjacent distinct edges can be
2365
+ extended to some perfect matching of `G`).
2366
+ 4. `|N(X)| \geq |X| + 2`, for all `X ⊂ A` such that `0 < |X| <
2367
+ |A| - 1`, where `N(S) := \{b \mid (a, b) \in E ∧ a \in S\}` is called
2368
+ the neighboring set of `S`.
2369
+ 5. `G - a - b` is matching covered, for some perfect matching `M` of
2370
+ `G` and for each edge `ab` in `M`.
2371
+
2372
+ We shall be using the 5th characterization mentioned above in order
2373
+ to determine whether the provided bipartite matching covered graph
2374
+ is a brace or not using *M*-alternating tree search [LZ2001]_.
2375
+
2376
+ INPUT:
2377
+
2378
+ - ``coNP_certificate`` -- boolean (default: ``False``)
2379
+
2380
+ OUTPUT:
2381
+
2382
+ - If the input matching covered graph is not bipartite, a
2383
+ :exc:`ValueError` is returned.
2384
+
2385
+ - If the input bipartite matching covered graph is a brace, a boolean
2386
+ ``True`` is returned if ``coNP_certificate`` is set to ``False``
2387
+ otherwise a 5-tuple ``(True, None, None, None, None)`` is returned.
2388
+
2389
+ - If the input bipartite matching covered graph is not a brace, a
2390
+ boolean ``False`` is returned if ``coNP_certificate`` is set to
2391
+ ``False`` otherwise a 5-tuple of
2392
+
2393
+ 1. a boolean ``False``,
2394
+
2395
+ 2. a list of edges constituting a nontrivial tight cut (which is a
2396
+ nontrivial barrier cut)
2397
+
2398
+ 3. a set of vertices of one of the shores of the nontrivial tight cut
2399
+
2400
+ 4. a string 'nontrivial tight cut'
2401
+
2402
+ 5. a set of vertices showing the respective barrier
2403
+
2404
+ is returned.
2405
+
2406
+ EXAMPLES:
2407
+
2408
+ The complete graph on two vertices `K_2` is the smallest brace::
2409
+
2410
+ sage: K = graphs.CompleteGraph(2)
2411
+ sage: G = MatchingCoveredGraph(K)
2412
+ sage: G.is_brace()
2413
+ True
2414
+
2415
+ The cycle graph on four vertices `C_4` is a brace::
2416
+
2417
+ sage: C = graphs.CycleGraph(4)
2418
+ sage: G = MatchingCoveredGraph(C)
2419
+ sage: G.is_brace()
2420
+ True
2421
+
2422
+ Each graph that is isomorphic to a biwheel is a brace::
2423
+
2424
+ sage: B = graphs.BiwheelGraph(15)
2425
+ sage: G = MatchingCoveredGraph(B)
2426
+ sage: G.is_brace()
2427
+ True
2428
+
2429
+ A circular ladder graph of order eight or more on `2n` vertices for
2430
+ an even `n` is a brace::
2431
+
2432
+ sage: n = 10
2433
+ sage: CL = graphs.CircularLadderGraph(n)
2434
+ sage: G = MatchingCoveredGraph(CL)
2435
+ sage: G.is_brace()
2436
+ True
2437
+
2438
+ A moebius ladder graph of order six or more on `2n` vertices for an odd
2439
+ `n` is a brace::
2440
+
2441
+ sage: n = 11
2442
+ sage: ML = graphs.MoebiusLadderGraph(n)
2443
+ sage: G = MatchingCoveredGraph(ML)
2444
+ sage: G.is_brace()
2445
+ True
2446
+
2447
+ Note that the union of the above mentioned four families of braces,
2448
+ that are:
2449
+
2450
+ 1. the biwheel graph ``BiwheelGraph(n)``,
2451
+ 2. the circular ladder graph ``CircularLadderGraph(n)`` for even ``n``,
2452
+ 3. the moebius ladder graph ``MoebiusLadderGraph(n)`` for odd ``n``,
2453
+
2454
+ is referred to as the *McCuaig* *family* *of* *braces.*
2455
+
2456
+ The only simple brace of order six is the complete graph of the same
2457
+ order, that is `K_{3, 3}`::
2458
+
2459
+ sage: L = list(graphs(6,
2460
+ ....: lambda G: G.size() <= 15 and
2461
+ ....: G.is_bipartite())
2462
+ ....: )
2463
+ sage: L = list(G for G in L if G.is_connected() and
2464
+ ....: G.is_matching_covered()
2465
+ ....: )
2466
+ sage: M = list(MatchingCoveredGraph(G) for G in L)
2467
+ sage: B = list(G for G in M if G.is_brace())
2468
+ sage: K = graphs.CompleteBipartiteGraph(3, 3)
2469
+ sage: G = MatchingCoveredGraph(K)
2470
+ sage: next(iter(B)).is_isomorphic(G)
2471
+ True
2472
+
2473
+ The nonplanar `K_{3, 3}`-free brace Heawood graph is the unique cubic
2474
+ graph of girth six with the fewest number of vertices (that is 14).
2475
+ Note that by `K_{3, 3}`-free, it shows that the Heawood graph does not
2476
+ contain a subgraph that is isomophic to a graph obtained by
2477
+ bisubdivision of `K_{3, 3}`::
2478
+
2479
+ sage: K = graphs.CompleteBipartiteGraph(3, 3)
2480
+ sage: J = graphs.HeawoodGraph()
2481
+ sage: H = MatchingCoveredGraph(J)
2482
+ sage: H.is_brace() and not H.is_planar() and \
2483
+ ....: H.is_regular(k=3) and H.girth() == 6
2484
+ True
2485
+
2486
+ Braces of order six or more are 3-connected::
2487
+
2488
+ sage: H = graphs.HexahedralGraph()
2489
+ sage: G = MatchingCoveredGraph(H)
2490
+ sage: G.is_brace() and G.is_triconnected()
2491
+ True
2492
+
2493
+ Braces of order four or more are 2-extendable::
2494
+
2495
+ sage: H = graphs.EllinghamHorton54Graph()
2496
+ sage: G = MatchingCoveredGraph(H)
2497
+ sage: G.is_brace()
2498
+ True
2499
+ sage: e = next(G.edge_iterator(labels=False)); f = None
2500
+ sage: for f in G.edge_iterator(labels=False):
2501
+ ....: if not (set(e) & set(f)):
2502
+ ....: break
2503
+ sage: S = [u for x in [e, f] for u in set(x)]
2504
+ sage: J = H.copy(); J.delete_vertices(S)
2505
+ sage: M = Graph(J.matching())
2506
+ sage: M.add_edges([e, f])
2507
+ sage: if all(d == 1 for d in M.degree()) and \
2508
+ ....: G.order() == M.order() and \
2509
+ ....: G.order() == 2*M.size():
2510
+ ....: print(f'graph {G} is 2-extendable')
2511
+ graph Ellingham-Horton 54-graph is 2-extendable
2512
+
2513
+ Every edge in a brace of order at least six is removable::
2514
+
2515
+ sage: H = graphs.CircularLadderGraph(8)
2516
+ sage: G = MatchingCoveredGraph(H)
2517
+ sage: # len(G.removble_edges()) == G.size()
2518
+ # True
2519
+
2520
+ Every brace of order eight has the hexahedral graph as a spanning
2521
+ subgraph::
2522
+
2523
+ sage: H = graphs.HexahedralGraph()
2524
+ sage: L = list(graphs(8,
2525
+ ....: lambda G: G.size() <= 28 and
2526
+ ....: G.is_bipartite())
2527
+ ....: )
2528
+ sage: L = list(G for G in L if G.is_connected() and
2529
+ ....: G.is_matching_covered()
2530
+ ....: )
2531
+ sage: M = list(MatchingCoveredGraph(G) for G in L)
2532
+ sage: B = list(G for G in M if G.is_brace())
2533
+ sage: C = list(G for G in M if Graph(G).subgraph_search(H) is not None)
2534
+ sage: B == C
2535
+ True
2536
+
2537
+ For every brace `G[A, B]` of order at least six, the graph
2538
+ `G - a_1 - a_2 - b_1 - b_2` has a perfect matching for any two distinct
2539
+ vertices `a_1` and `a_2` in `A` and any two distinct vertices `b_1` and
2540
+ `b_2` in `B`::
2541
+
2542
+ sage: H = graphs.CompleteBipartiteGraph(10, 10)
2543
+ sage: G = MatchingCoveredGraph(H)
2544
+ sage: G.is_brace()
2545
+ True
2546
+ sage: S = [0, 1, 10, 12]
2547
+ sage: G.delete_vertices(S)
2548
+ sage: G.has_perfect_matching()
2549
+ True
2550
+
2551
+ For a brace `G[A, B]` of order six or more, `|N(X)| \geq |X| + 2`, for
2552
+ all `X \subset A` such that `0 < |X| <|A| - 1`, where
2553
+ `N(S) := \{b | (a, b) \in E \^ a \in S\}` is called the neighboring set
2554
+ of `S`::
2555
+
2556
+ sage: H = graphs.MoebiusLadderGraph(15)
2557
+ sage: G = MatchingCoveredGraph(H)
2558
+ sage: G.is_brace()
2559
+ True
2560
+ sage: A, _ = G.bipartite_sets()
2561
+ sage: # needs random
2562
+ sage: X = random.sample(list(A), random.randint(1, len(A) - 1))
2563
+ sage: N = {v for u in X for v in G.neighbor_iterator(u)}
2564
+ sage: len(N) >= len(X) + 2
2565
+ True
2566
+
2567
+ For a brace `G` of order four or more with a perfect matching `M`, the
2568
+ graph `G - a - b` is matching covered for each edge `(a, b)` in `M`::
2569
+
2570
+ sage: H = graphs.HeawoodGraph()
2571
+ sage: G = MatchingCoveredGraph(H)
2572
+ sage: G.is_brace()
2573
+ True
2574
+ sage: M = G.get_matching()
2575
+ sage: L = []
2576
+ sage: for a, b, *_ in M:
2577
+ ....: J = G.copy(); J.delete_vertices([a, b])
2578
+ ....: if J.is_matching_covered():
2579
+ ....: L.append(J)
2580
+ sage: len(L) == len(M)
2581
+ True
2582
+
2583
+ A cycle graph of order six or more is a bipartite matching covered
2584
+ graph, but is not a brace::
2585
+
2586
+ sage: C = graphs.CycleGraph(10)
2587
+ sage: G = MatchingCoveredGraph(C)
2588
+ sage: G.is_brace()
2589
+ False
2590
+
2591
+ A ladder graph of order six or more is a bipartite matching covered
2592
+ graph, that is not a brace. The tight cut decomposition of a ladder
2593
+ graph produces a list graphs the underlying graph of each of which
2594
+ is isomorphic to a 4-cycle::
2595
+
2596
+ sage: L = graphs.LadderGraph(10)
2597
+ sage: G = MatchingCoveredGraph(L)
2598
+ sage: G.is_brace()
2599
+ False
2600
+
2601
+ One may set the ``coNP_certificate`` to be ``True``::
2602
+
2603
+ sage: H = graphs.HexahedralGraph()
2604
+ sage: G = MatchingCoveredGraph(H)
2605
+ sage: G.is_brace(coNP_certificate=True)
2606
+ (True, None, None, None, None)
2607
+ sage: C = graphs.CycleGraph(6)
2608
+ sage: D = MatchingCoveredGraph(C)
2609
+ sage: is_brace, nontrivial_tight_cut, nontrivial_odd_component, \
2610
+ ....: nontrivial_tight_cut_variant, cut_identifier = \
2611
+ ....: D.is_brace(coNP_certificate=True)
2612
+ sage: is_brace is False
2613
+ True
2614
+ sage: J = C.subgraph(vertices=nontrivial_odd_component)
2615
+ sage: J.is_isomorphic(graphs.PathGraph(3))
2616
+ True
2617
+ sage: len(nontrivial_tight_cut) == 2
2618
+ True
2619
+ sage: nontrivial_tight_cut_variant
2620
+ 'nontrivial barrier cut'
2621
+ sage: # Corresponding barrier
2622
+ sage: cut_identifier == {a for u, v, *_ in nontrivial_tight_cut for a in [u, v] \
2623
+ ....: if a not in nontrivial_odd_component}
2624
+ True
2625
+ sage: for u, v, *_ in nontrivial_tight_cut:
2626
+ ....: assert (u in nontrivial_odd_component and v not in nontrivial_odd_component)
2627
+ sage: L = graphs.LadderGraph(3) # A ladder graph with two constituent braces
2628
+ sage: G = MatchingCoveredGraph(L)
2629
+ sage: is_brace, nontrivial_tight_cut, nontrivial_odd_component, cut_variant, cut_identifier = \
2630
+ ....: G.is_brace(coNP_certificate=True)
2631
+ sage: is_brace is False
2632
+ True
2633
+ sage: G1 = L.copy()
2634
+ sage: G1.merge_vertices(list(nontrivial_odd_component))
2635
+ sage: G1.to_simple().is_isomorphic(graphs.CycleGraph(4))
2636
+ True
2637
+ sage: G2 = L.copy()
2638
+ sage: G2.merge_vertices([v for v in G if v not in nontrivial_odd_component])
2639
+ sage: G2.to_simple().is_isomorphic(graphs.CycleGraph(4))
2640
+ True
2641
+ sage: cut_variant
2642
+ 'nontrivial barrier cut'
2643
+ sage: cut_identifier == {a for u, v, *_ in nontrivial_tight_cut for a in [u, v] \
2644
+ ....: if a not in nontrivial_odd_component}
2645
+ True
2646
+ sage: H = graphs.CompleteBipartiteGraph(3, 3)
2647
+ sage: H.delete_edge(0, 3)
2648
+ sage: G = MatchingCoveredGraph(H)
2649
+ sage: G.is_brace(coNP_certificate=True)
2650
+ (False,
2651
+ [(4, 1, None), (5, 1, None), (4, 2, None), (5, 2, None)],
2652
+ {0, 4, 5},
2653
+ 'nontrivial barrier cut',
2654
+ {1, 2})
2655
+
2656
+ If the input matching covered graph is nonbipartite, a
2657
+ :exc:`ValueError` is thrown::
2658
+
2659
+ sage: K4 = graphs.CompleteGraph(4)
2660
+ sage: G = MatchingCoveredGraph(K4)
2661
+ sage: G.is_brace()
2662
+ Traceback (most recent call last):
2663
+ ...
2664
+ ValueError: the input graph is not bipartite
2665
+ sage: P = graphs.PetersenGraph()
2666
+ sage: H = MatchingCoveredGraph(P)
2667
+ sage: H.is_brace(coNP_certificate=True)
2668
+ Traceback (most recent call last):
2669
+ ...
2670
+ ValueError: the input graph is not bipartite
2671
+
2672
+ .. SEEALSO::
2673
+
2674
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.is_brick`
2675
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.bricks_and_braces`
2676
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.number_of_braces`
2677
+ """
2678
+ if not self.is_bipartite():
2679
+ raise ValueError('the input graph is not bipartite')
2680
+
2681
+ if self.order() < 6:
2682
+ return (True, None, None, None, None) if coNP_certificate else True
2683
+
2684
+ A, B = self.bipartite_sets()
2685
+ matching = set(self.get_matching())
2686
+ matching_neighbor = {x: y for u, v, *_ in matching for x, y in [(u, v), (v, u)]}
2687
+
2688
+ for e in matching:
2689
+ u, v, *_ = e
2690
+
2691
+ # Let G denote the undirected graph self, and
2692
+ # let the graph H(e) := G — u — v
2693
+ H = Graph(self, multiedges=False)
2694
+ H.delete_vertices([u, v])
2695
+
2696
+ if not H.is_connected() or not H.is_matching_covered(list(matching - set([e]))):
2697
+ if not coNP_certificate:
2698
+ return False
2699
+
2700
+ # Construct the digraph D(e)(A ∪ B, F) defined as follows:
2701
+ from sage.graphs.digraph import DiGraph
2702
+ D = DiGraph()
2703
+
2704
+ # For each edge (a, b) in E(H(e)) ∩ M with a in A, b —> a in D(e).
2705
+ # For each edge (a, b) in E(H(e)) with a in A, a —> b in D(e).
2706
+ for a, b in H.edge_iterator(labels=False, sort_vertices=True):
2707
+
2708
+ if a in B:
2709
+ a, b = b, a
2710
+
2711
+ D.add_edge((a, b))
2712
+ if matching_neighbor[a] == b:
2713
+ D.add_edge((b, a))
2714
+
2715
+ # H(e) is matching covered iff D(e) is strongly connected.
2716
+ # Check if D(e) is strongly connected using Kosaraju's algorithm
2717
+ def dfs(x, visited, neighbor_iterator):
2718
+ stack = [x] # a stack of xertices
2719
+
2720
+ while stack:
2721
+ x = stack.pop()
2722
+ visited.add(x)
2723
+
2724
+ for y in neighbor_iterator(x):
2725
+ if y not in visited:
2726
+ stack.append(y)
2727
+
2728
+ root = next(D.vertex_iterator())
2729
+
2730
+ visited_in = set()
2731
+ dfs(root, visited_in, D.neighbor_in_iterator)
2732
+
2733
+ # Since D(e) is not strongly connected, it has a directed cut T(e).
2734
+ # Note that by definition of D(e), it follows that T(e) ⊆ E(H(e)) — M.
2735
+ # Thus, T(e) is a cut of H(e), which has a shore X such that every edge of T(e) is
2736
+ # incident with a vertex in X ∩ B.
2737
+
2738
+ # Moreover, M — e is a perfect matching of H(e), and thus, |X ∩ A| = |X ∩ B|
2739
+ # Consequently, Y := X + v is a shore of a nontrivial tight cut T of G
2740
+
2741
+ if len(visited_in) != D.order():
2742
+ X = visited_in
2743
+ else:
2744
+ X = set()
2745
+ dfs(root, X, D.neighbor_out_iterator)
2746
+
2747
+ color_class = None
2748
+
2749
+ for a, b in H.edge_iterator(labels=False, sort_vertices=True):
2750
+ if (a in X) ^ (b in X):
2751
+ x = a if a in A else b
2752
+ color_class = x not in X
2753
+ break
2754
+
2755
+ # Obtain the color class Z ∈ {A, B} such that X ∩ Z is a vertex cover for T(e)
2756
+ # Thus, obtain Y := X + v
2757
+ X.add(u if (not color_class and u in A) or (color_class and u in B) or (color_class is None) else v)
2758
+
2759
+ # Compute the nontrivial tight cut C := ∂(Y)
2760
+ C = [(x, y, w) if x in X else (y, x, w)
2761
+ for x, y, w in self.edge_iterator(sort_vertices=True)
2762
+ if (x in X) ^ (y in X)]
2763
+
2764
+ # Obtain the barrier Z
2765
+ Z = None
2766
+
2767
+ if (u in X and u in A) or (v in X and v in A):
2768
+ Z = {b for b in B if b not in X}
2769
+ else:
2770
+ Z = {a for a in A if a not in X}
2771
+
2772
+ return (False, C, set(X), 'nontrivial barrier cut', Z)
2773
+
2774
+ return (True, None, None, None, None) if coNP_certificate else True
2775
+
2776
+ @doc_index('Bricks, braces and tight cut decomposition')
2777
+ def is_brick(self, coNP_certificate=False):
2778
+ r"""
2779
+ Check if the (matching covered) graph is a brick.
2780
+
2781
+ A matching covered graph which is free of nontrivial tight cuts is
2782
+ called a *brick* if it is nonbipartite. A nonbipartite matching covered
2783
+ graph is a brick if and only if it is 3-connected and bicritical
2784
+ [LM2024]_.
2785
+
2786
+ INPUT:
2787
+
2788
+ - ``coNP_certificate`` -- boolean (default: ``False``)
2789
+
2790
+ OUTPUT:
2791
+
2792
+ - If the input matching covered graph is bipartite, a :exc:`ValueError`
2793
+ is returned.
2794
+
2795
+ - If the input nonbipartite matching covered graph is a brick, a
2796
+ boolean ``True`` is returned if ``coNP_certificate`` is set to
2797
+ ``False``, otherwise a 5-tuple ``(True, None, None, None, None)`` is
2798
+ returned.
2799
+
2800
+ - If the input nonbipartite matching covered graph is not a brick, a
2801
+ boolean ``False`` is returned if ``coNP_certificate`` is set to
2802
+ ``False``.
2803
+
2804
+ - If ``coNP_certificate`` is set to ``True`` and the input nonbipartite
2805
+ graph is not a brick, a 5-tuple of
2806
+
2807
+ 1. a boolean ``False``,
2808
+
2809
+ 2. a list of lists of edges, each list constituting a nontrivial
2810
+ tight cut collectively representing a laminar tight cut,
2811
+
2812
+ 3. a list of set of vertices of one of the shores of those respective
2813
+ nontrivial tight cuts:
2814
+
2815
+ #. In case of nontrivial barrier cuts, each of the shores is a
2816
+ nontrivial odd component with respect to a nontrivial barrier,
2817
+ thus the returned list forms mutually exclusive collection of
2818
+ (odd) sets.
2819
+
2820
+ #. Otherwise each of the nontrivial tight cuts is a 2-separation
2821
+ cut, each of the shores form a subset sequence, with the
2822
+ `i` th shore being a proper subset of the `i + 1` th shore.
2823
+
2824
+ 4. a string showing whether the nontrivial tight cuts are barrier
2825
+ cuts (if the string is 'nontrivial barrier cut'), or 2-separation
2826
+ cuts (if the string is 'nontrivial 2-separation cut')
2827
+
2828
+ 5. a set of vertices showing the respective barrier if the
2829
+ nontrivial tight cuts are barrier cuts, or otherwise
2830
+ a set of two vertices constituting the corresponding
2831
+ two vertex cut (in this case the nontrivial tight cuts are
2832
+ 2-separation cuts)
2833
+
2834
+ is returned.
2835
+
2836
+ EXAMPLES:
2837
+
2838
+ The complete graph on four vertices `K_4` is the smallest brick::
2839
+
2840
+ sage: K = graphs.CompleteGraph(4)
2841
+ sage: G = MatchingCoveredGraph(K)
2842
+ sage: G.is_brick()
2843
+ True
2844
+
2845
+ The triangular cicular ladder (a graph on six vertices), aka
2846
+ `\overline{C_6}` is a brick::
2847
+
2848
+ sage: C6Bar = graphs.CircularLadderGraph(3)
2849
+ sage: G = MatchingCoveredGraph(C6Bar)
2850
+ sage: G.is_brick()
2851
+ True
2852
+
2853
+ Each of Petersen graph, Bicorn graph, Tricorn graph, Cubeplex graph,
2854
+ Twinplex graph, Wagner graph is a brick::
2855
+
2856
+ sage: MatchingCoveredGraph(graphs.PetersenGraph()).is_brick() and \
2857
+ ....: MatchingCoveredGraph(graphs.StaircaseGraph(4)).is_brick() and \
2858
+ ....: MatchingCoveredGraph(graphs.TricornGraph()).is_brick() and \
2859
+ ....: MatchingCoveredGraph(graphs.CubeplexGraph()).is_brick() and \
2860
+ ....: MatchingCoveredGraph(graphs.TwinplexGraph()).is_brick() and \
2861
+ ....: MatchingCoveredGraph(graphs.WagnerGraph()).is_brick()
2862
+ True
2863
+
2864
+ The Murty graph is the smallest simple brick that is not odd-intercyclic::
2865
+
2866
+ sage: M = graphs.MurtyGraph()
2867
+ sage: G = MatchingCoveredGraph(M)
2868
+ sage: G.is_brick()
2869
+ True
2870
+
2871
+ A circular ladder graph of order six or more on `2n` vertices for an
2872
+ odd `n` is a brick::
2873
+
2874
+ sage: n = 11
2875
+ sage: CL = graphs.CircularLadderGraph(n)
2876
+ sage: G = MatchingCoveredGraph(CL)
2877
+ sage: G.is_brick()
2878
+ True
2879
+
2880
+ A moebius ladder graph of order eight or more on `2n` vertices for an
2881
+ even `n` is a brick::
2882
+
2883
+ sage: n = 10
2884
+ sage: ML = graphs.MoebiusLadderGraph(n)
2885
+ sage: G = MatchingCoveredGraph(ML)
2886
+ sage: G.is_brick()
2887
+ True
2888
+
2889
+ A wheel graph of an even order is a brick::
2890
+
2891
+ sage: W = graphs.WheelGraph(10)
2892
+ sage: G = MatchingCoveredGraph(W)
2893
+ sage: G.is_brick()
2894
+ True
2895
+
2896
+ A graph that is isomorphic to a truncated biwheel graph is a brick::
2897
+
2898
+ sage: TB = graphs.TruncatedBiwheelGraph(15)
2899
+ sage: G = MatchingCoveredGraph(TB)
2900
+ sage: G.is_brick()
2901
+ True
2902
+
2903
+ Each of the graphs in the staircase graph family with order eight or
2904
+ more is a brick::
2905
+
2906
+ sage: ST = graphs.StaircaseGraph(9)
2907
+ sage: G = MatchingCoveredGraph(ST)
2908
+ sage: G.is_brick()
2909
+ True
2910
+
2911
+ Bricks are 3-connected::
2912
+
2913
+ sage: P = graphs.PetersenGraph()
2914
+ sage: G = MatchingCoveredGraph(P)
2915
+ sage: G.is_brick()
2916
+ True
2917
+ sage: G.is_triconnected()
2918
+ True
2919
+
2920
+ Bricks are bicritical::
2921
+
2922
+ sage: P = graphs.PetersenGraph()
2923
+ sage: G = MatchingCoveredGraph(P)
2924
+ sage: G.is_brick()
2925
+ True
2926
+ sage: G.is_bicritical()
2927
+ True
2928
+
2929
+ Examples of nonbipartite matching covered graphs that are not
2930
+ bricks::
2931
+
2932
+ sage: H = Graph([
2933
+ ....: (0, 3), (0, 4), (0, 7),
2934
+ ....: (1, 3), (1, 5), (1, 7),
2935
+ ....: (2, 3), (2, 6), (2, 7),
2936
+ ....: (4, 5), (4, 6), (5, 6)
2937
+ ....: ])
2938
+ sage: G = MatchingCoveredGraph(H)
2939
+ sage: G.is_bipartite()
2940
+ False
2941
+ sage: G.is_bicritical()
2942
+ False
2943
+ sage: G.is_triconnected()
2944
+ True
2945
+ sage: G.is_brick()
2946
+ False
2947
+ sage: H = Graph([
2948
+ ....: (0, 1), (0, 2), (0, 3), (0, 4), (1, 2),
2949
+ ....: (1, 5), (2, 5), (3, 4), (3, 5), (4, 5)
2950
+ ....: ])
2951
+ sage: G = MatchingCoveredGraph(H)
2952
+ sage: G.is_bipartite()
2953
+ False
2954
+ sage: G.is_bicritical()
2955
+ True
2956
+ sage: G.is_triconnected()
2957
+ False
2958
+ sage: G.is_brick()
2959
+ False
2960
+
2961
+ One may set the ``coNP_certificate`` to be ``True``::
2962
+
2963
+ sage: K4 = graphs.CompleteGraph(4)
2964
+ sage: G = MatchingCoveredGraph(K4)
2965
+ sage: G.is_brick(coNP_certificate=True)
2966
+ (True, None, None, None, None)
2967
+ sage: # K(4) ⊙ K(3, 3) is nonbipartite but not a brick
2968
+ sage: H = graphs.MurtyGraph(); H.delete_edge(0, 1)
2969
+ sage: G = MatchingCoveredGraph(H)
2970
+ sage: G.is_brick(coNP_certificate=True)
2971
+ (False, [[(5, 2, None), (6, 3, None), (7, 4, None)]], [{5, 6, 7}],
2972
+ 'nontrivial barrier cut', {2, 3, 4})
2973
+ sage: H = Graph([
2974
+ ....: (0, 12), (0, 13), (0, 15), (1, 4), (1, 13), (1, 14),
2975
+ ....: (1, 19), (2, 4), (2, 13), (2, 14), (2, 17), (3, 9),
2976
+ ....: (3, 13), (3, 16), (3, 21), (4, 6), (4, 7), (5, 7),
2977
+ ....: (5, 8), (5, 12), (6, 8), (6, 11), (7, 10), (8, 9),
2978
+ ....: (9, 10), (10, 11), (11, 12), (14, 15), (14, 16), (15, 16),
2979
+ ....: (17, 18), (17, 21), (18, 19), (18, 20), (19, 20), (20, 21)
2980
+ ....: ])
2981
+ sage: G = MatchingCoveredGraph(H)
2982
+ sage: G.is_brick(coNP_certificate=True)
2983
+ (False,
2984
+ [[(12, 0, None), (4, 1, None), (4, 2, None), (9, 3, None)],
2985
+ [(19, 1, None), (17, 2, None), (21, 3, None)],
2986
+ [(15, 0, None), (14, 1, None), (14, 2, None), (16, 3, None)]],
2987
+ [{4, 5, 6, 7, 8, 9, 10, 11, 12}, {17, 18, 19, 20, 21}, {14, 15, 16}],
2988
+ 'nontrivial barrier cut', {0, 1, 2, 3})
2989
+ sage: J = Graph([
2990
+ ....: (0, 1), (0, 2), (0, 3), (0, 4), (0, 5),
2991
+ ....: (0, 6), (0, 7), (0, 8), (0, 9), (0, 10),
2992
+ ....: (1, 2), (1, 11), (2, 11), (3, 4), (3, 11),
2993
+ ....: (4, 11), (5, 6), (5, 11), (6, 11), (7, 8),
2994
+ ....: (7, 11), (8, 11), (9, 10), (9, 11), (10, 11)
2995
+ ....: ])
2996
+ sage: G = MatchingCoveredGraph(J)
2997
+ sage: G.is_brick(coNP_certificate=True)
2998
+ (False,
2999
+ [[(0, 3, None),
3000
+ (0, 4, None),
3001
+ (0, 5, None),
3002
+ (0, 6, None),
3003
+ (0, 7, None),
3004
+ (0, 8, None),
3005
+ (0, 9, None),
3006
+ (0, 10, None),
3007
+ (1, 11, None),
3008
+ (2, 11, None)],
3009
+ [(0, 5, None),
3010
+ (0, 6, None),
3011
+ (0, 7, None),
3012
+ (0, 8, None),
3013
+ (0, 9, None),
3014
+ (0, 10, None),
3015
+ (1, 11, None),
3016
+ (2, 11, None),
3017
+ (3, 11, None),
3018
+ (4, 11, None)],
3019
+ [(0, 7, None),
3020
+ (0, 8, None),
3021
+ (0, 9, None),
3022
+ (0, 10, None),
3023
+ (1, 11, None),
3024
+ (2, 11, None),
3025
+ (3, 11, None),
3026
+ (4, 11, None),
3027
+ (5, 11, None),
3028
+ (6, 11, None)],
3029
+ [(0, 9, None),
3030
+ (0, 10, None),
3031
+ (1, 11, None),
3032
+ (2, 11, None),
3033
+ (3, 11, None),
3034
+ (4, 11, None),
3035
+ (5, 11, None),
3036
+ (6, 11, None),
3037
+ (7, 11, None),
3038
+ (8, 11, None)]],
3039
+ [{0, 1, 2},
3040
+ {0, 1, 2, 3, 4},
3041
+ {0, 1, 2, 3, 4, 5, 6},
3042
+ {0, 1, 2, 3, 4, 5, 6, 7, 8}],
3043
+ 'nontrivial 2-separation cut',
3044
+ {0, 11})
3045
+
3046
+ If the input matching covered graph is bipartite, a
3047
+ :exc:`ValueError` is thrown::
3048
+
3049
+ sage: H = graphs.HexahedralGraph()
3050
+ sage: G = MatchingCoveredGraph(H)
3051
+ sage: G.is_brick()
3052
+ Traceback (most recent call last):
3053
+ ...
3054
+ ValueError: the input graph is bipartite
3055
+ sage: J = graphs.HeawoodGraph()
3056
+ sage: G = MatchingCoveredGraph(J)
3057
+ sage: G.is_brick(coNP_certificate=True)
3058
+ Traceback (most recent call last):
3059
+ ...
3060
+ ValueError: the input graph is bipartite
3061
+
3062
+ .. SEEALSO::
3063
+
3064
+ - :meth:`~sage.graphs.graph.Graph.is_bicritical`
3065
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.is_brace`
3066
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.bricks_and_braces`
3067
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.number_of_bricks`
3068
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.number_of_petersen_bricks`
3069
+ """
3070
+ if self.is_bipartite():
3071
+ raise ValueError('the input graph is bipartite')
3072
+
3073
+ # Check if G is bicritical
3074
+ bicritical, certificate = self.is_bicritical(coNP_certificate=True)
3075
+
3076
+ if not bicritical:
3077
+ if not coNP_certificate:
3078
+ return False
3079
+
3080
+ # G has a pair of vertices u, v such that G - u - v is not matching
3081
+ # covered, thus has a nontrivial barrier B containing both u and v.
3082
+ u, _ = certificate
3083
+ B = self.maximal_barrier(u)
3084
+
3085
+ H = Graph(self)
3086
+ H.delete_vertices(B)
3087
+
3088
+ # Let K be a nontrivial odd component of H := G - B. Note that
3089
+ # there exists at least one such K since G is nonbipartite
3090
+ nontrivial_odd_components = [
3091
+ set(component) for component in H.connected_components(sort=True)
3092
+ if len(component) % 2 and len(component) > 1
3093
+ ]
3094
+
3095
+ # Find a laminar set of nontrivial barrier cuts
3096
+ C = [[(u, v, w) if u in nontrivial_odd_component else (v, u, w)
3097
+ for u, v, w in self.edge_iterator()
3098
+ if (u in nontrivial_odd_component) ^ (v in nontrivial_odd_component)]
3099
+ for nontrivial_odd_component in nontrivial_odd_components]
3100
+
3101
+ return (False, C, nontrivial_odd_components, 'nontrivial barrier cut', B)
3102
+
3103
+ # Check if G is 3-connected
3104
+ if self.is_triconnected():
3105
+ return (True, None, None, None, None) if coNP_certificate else True
3106
+
3107
+ # G has a 2-vertex cut
3108
+ # Compute the SPQR-tree decomposition
3109
+ spqr_tree = self.spqr_tree()
3110
+ two_vertex_cut = []
3111
+
3112
+ # Check for 2-vertex cuts in a P node
3113
+ # Since the graph is matching covered, it is free of cut vertices
3114
+ # It can be shown using counting arguments that the spqr tree
3115
+ # decomposition for a bicritical graph, that is 2-connected but not
3116
+ # 3-connected, is free of 'S' nodes
3117
+ for u in spqr_tree:
3118
+ if u[0] == 'P':
3119
+ two_vertex_cut.extend(u[1])
3120
+ break
3121
+
3122
+ # If no 2-vertex cut found, look for R nodes
3123
+ if not two_vertex_cut:
3124
+ from collections import Counter
3125
+ R_frequency = Counter()
3126
+
3127
+ for t, g in spqr_tree:
3128
+ if t == 'R':
3129
+ R_frequency.update(g)
3130
+
3131
+ # R frequency must be at least 2,
3132
+ # since the graph is 2-connected but not 3-connected
3133
+ two_vertex_cut = [u for u, f in R_frequency.items() if f >= 2][:2]
3134
+
3135
+ # We obtain a 2-vertex cut (u, v)
3136
+ H = Graph(self)
3137
+ H.delete_vertices(two_vertex_cut)
3138
+
3139
+ # Check if all components of H are odd
3140
+ components = H.connected_components(sort=True)
3141
+
3142
+ # Find a nontrivial odd component
3143
+ nontrivial_tight_cut_variation = 'nontrivial 2-separation cut'
3144
+ nontrivial_odd_components = []
3145
+
3146
+ for index, component in enumerate(components):
3147
+ if index == len(components) - 1:
3148
+ continue
3149
+ elif not index:
3150
+ nontrivial_odd_components.append(set(components[0] + [two_vertex_cut[0]]))
3151
+ else:
3152
+ nontrivial_odd_component = nontrivial_odd_components[-1].copy()
3153
+ nontrivial_odd_component.update(component)
3154
+ nontrivial_odd_components.append(nontrivial_odd_component)
3155
+
3156
+ C = [[(u, v, w) if u in nontrivial_odd_component else (v, u, w)
3157
+ for u, v, w in self.edge_iterator()
3158
+ if (u in nontrivial_odd_component) ^ (v in nontrivial_odd_component)]
3159
+ for nontrivial_odd_component in nontrivial_odd_components]
3160
+
3161
+ # Edge (u, v, w) in C are formatted so that u is in a nontrivial odd component
3162
+ return (False, C, nontrivial_odd_components, nontrivial_tight_cut_variation, set(two_vertex_cut)) if coNP_certificate else False
3163
+
3164
+ @doc_index('Overwritten methods')
3165
+ def loop_edges(self, labels=True):
3166
+ r"""
3167
+ Return a list of all loops in the (matching covered) graph.
3168
+
3169
+ .. NOTE::
3170
+
3171
+ This method overwrites the
3172
+ :meth:`~sage.graphs.generic_graph.GenericGraph.loop_edges` method
3173
+ in order to return an empty list as matching covered graphs are
3174
+ free of looped edges.
3175
+
3176
+ INPUT:
3177
+
3178
+ - ``labels`` -- boolean (default: ``True``); whether returned edges
3179
+ have labels (``(u,v,l)``) or not (``(u,v)``).
3180
+
3181
+ OUTPUT:
3182
+
3183
+ - A list capturing the edges that are loops in the matching covered
3184
+ graph; note that, the list is empty since matching covered graphs do
3185
+ not contain any looped edges.
3186
+
3187
+ EXAMPLES:
3188
+
3189
+ A matching covered graph, for instance the Heawood graph, by
3190
+ definition, is always free of loops::
3191
+
3192
+ sage: H = graphs.HeawoodGraph()
3193
+ sage: G = MatchingCoveredGraph(H)
3194
+ sage: G
3195
+ Matching covered heawood graph: graph on 14 vertices
3196
+ sage: G.add_edge(0, 0)
3197
+ Traceback (most recent call last):
3198
+ ...
3199
+ ValueError: loops are not allowed in matching covered graphs
3200
+ sage: G.loops()
3201
+ []
3202
+ sage: G.loop_edges()
3203
+ []
3204
+
3205
+ A matching covered graph may support multiple edges, still no
3206
+ loops are allowed::
3207
+
3208
+ sage: C = graphs.CycleGraph(4)
3209
+ sage: G = MatchingCoveredGraph(C)
3210
+ sage: G.allow_multiple_edges(True)
3211
+ sage: G
3212
+ Matching covered cycle graph: multi-graph on 4 vertices
3213
+ sage: G.add_edge(0, 1, 'label')
3214
+ sage: G.add_edge(0, 0)
3215
+ Traceback (most recent call last):
3216
+ ...
3217
+ ValueError: loops are not allowed in matching covered graphs
3218
+ sage: G.edges(sort=False)
3219
+ [(0, 1, None), (0, 1, 'label'), (0, 3, None), (1, 2, None), (2, 3, None)]
3220
+ sage: G.loops()
3221
+ []
3222
+ sage: G.loop_edges()
3223
+ []
3224
+
3225
+ One may set the ``label`` to either ``True`` or ``False``::
3226
+
3227
+ sage: G.loop_edges(labels=False)
3228
+ []
3229
+ sage: G.loops(labels=True)
3230
+ []
3231
+
3232
+ .. SEEALSO::
3233
+
3234
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.allow_loops`
3235
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.allows_loops`
3236
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.has_loops`
3237
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.loop_vertices`
3238
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.loops`
3239
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.number_of_loops`
3240
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.remove_loops`
3241
+ """
3242
+ return []
3243
+
3244
+ @doc_index('Overwritten methods')
3245
+ def loop_vertices(self):
3246
+ r"""
3247
+ Return a list of vertices with loops.
3248
+
3249
+ .. NOTE::
3250
+
3251
+ This method overwrites the
3252
+ :meth:`~sage.graphs.generic_graph.GenericGraph.loop_vertices`
3253
+ method in order to return an empty list as matching covered graphs
3254
+ are free of vertices that have looped edges.
3255
+
3256
+ OUTPUT:
3257
+
3258
+ - A list capturing the vertices that have loops in the matching covered
3259
+ graph; note that, the list is empty since matching covered graphs do
3260
+ not contain any looped edges.
3261
+
3262
+ EXAMPLES:
3263
+
3264
+ A matching covered graph, for instance the Möbius graph of order 8, by
3265
+ definition, is always free of loops::
3266
+
3267
+ sage: M = graphs.MoebiusLadderGraph(4)
3268
+ sage: G = MatchingCoveredGraph(M)
3269
+ sage: G
3270
+ Matching covered moebius ladder graph: graph on 8 vertices
3271
+ sage: G.add_edge(0, 0)
3272
+ Traceback (most recent call last):
3273
+ ...
3274
+ ValueError: loops are not allowed in matching covered graphs
3275
+ sage: G.loop_vertices()
3276
+ []
3277
+
3278
+ A matching covered graph may support multiple edges, still no
3279
+ loops are allowed::
3280
+
3281
+ sage: S = graphs.StaircaseGraph(4)
3282
+ sage: G = MatchingCoveredGraph(S)
3283
+ sage: G.allow_multiple_edges(True)
3284
+ sage: G
3285
+ Matching covered staircase graph: multi-graph on 8 vertices
3286
+ sage: G.add_edge(0, 1, 'label')
3287
+ sage: G.add_edge(0, 0)
3288
+ Traceback (most recent call last):
3289
+ ...
3290
+ ValueError: loops are not allowed in matching covered graphs
3291
+ sage: G.edges(sort=False)
3292
+ [(0, 1, None), (0, 1, 'label'), (0, 3, None), (0, 6, None),
3293
+ (1, 2, None), (1, 4, None), (2, 5, None), (2, 7, None),
3294
+ (3, 4, None), (3, 6, None), (4, 5, None), (5, 7, None),
3295
+ (6, 7, None)]
3296
+ sage: G.loop_vertices()
3297
+ []
3298
+
3299
+ .. SEEALSO::
3300
+
3301
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.allow_loops`
3302
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.allows_loops`
3303
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.has_loops`
3304
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.loop_edges`
3305
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.loops`
3306
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.number_of_loops`
3307
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.remove_loops`
3308
+ """
3309
+ return []
3310
+
3311
+ loops = loop_edges
3312
+
3313
+ @doc_index('Overwritten methods')
3314
+ def number_of_loops(self):
3315
+ r"""
3316
+ Return the number of edges that are loops.
3317
+
3318
+ .. NOTE::
3319
+
3320
+ This method overwrites the
3321
+ :meth:`~sage.graphs.generic_graph.GenericGraph.number_of_loops`
3322
+ method in order to return 0 as matching covered graphs are free
3323
+ of looped edges.
3324
+
3325
+ OUTPUT:
3326
+
3327
+ - An integer, 0 is returned, since matching covered graphs do not
3328
+ contain zero loops.
3329
+
3330
+ EXAMPLES:
3331
+
3332
+ A matching covered graph, for instance the Truncated biwheel graph,
3333
+ by definition, is always free of loops::
3334
+
3335
+ sage: T = graphs.TruncatedBiwheelGraph(5)
3336
+ sage: G = MatchingCoveredGraph(T)
3337
+ sage: G
3338
+ Matching covered truncated biwheel graph: graph on 10 vertices
3339
+ sage: G.add_edge(0, 0)
3340
+ Traceback (most recent call last):
3341
+ ...
3342
+ ValueError: loops are not allowed in matching covered graphs
3343
+ sage: G.loop_vertices()
3344
+ []
3345
+ sage: G.number_of_loops()
3346
+ 0
3347
+
3348
+ A matching covered graph may support multiple edges, still no
3349
+ loops are allowed::
3350
+
3351
+ sage: B = graphs.BiwheelGraph(4)
3352
+ sage: G = MatchingCoveredGraph(B)
3353
+ sage: G.allow_multiple_edges(True)
3354
+ sage: G
3355
+ Matching covered biwheel graph: multi-graph on 8 vertices
3356
+ sage: G.add_edge(0, 1, 'label')
3357
+ sage: G.add_edge(0, 0)
3358
+ Traceback (most recent call last):
3359
+ ...
3360
+ ValueError: loops are not allowed in matching covered graphs
3361
+ sage: G.edges(sort=False)
3362
+ [(0, 1, None), (0, 1, 'label'), (0, 5, None), (0, 7, None),
3363
+ (1, 2, None), (1, 6, None), (2, 3, None), (2, 7, None),
3364
+ (3, 4, None), (3, 6, None), (4, 5, None), (4, 7, None),
3365
+ (5, 6, None)]
3366
+ sage: G.loop_vertices()
3367
+ []
3368
+ sage: G.number_of_loops()
3369
+ 0
3370
+
3371
+ .. SEEALSO::
3372
+
3373
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.allow_loops`
3374
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.allows_loops`
3375
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.has_loops`
3376
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.loop_edges`
3377
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.loop_vertices`
3378
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.loops`
3379
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.remove_loops`
3380
+ """
3381
+ return 0
3382
+
3383
+ @doc_index('Overwritten methods')
3384
+ def remove_loops(self, vertices=None):
3385
+ r"""
3386
+ Remove loops on vertices in ``vertices``.
3387
+
3388
+ .. NOTE::
3389
+
3390
+ This method overwrites the
3391
+ :meth:`~sage.graphs.generic_graph.GenericGraph.remove_loops` method
3392
+ in order to return without any alteration as matching covered
3393
+ graphs are free of looped edges.
3394
+
3395
+ INPUT:
3396
+
3397
+ - ``vertices`` -- (default: ``None``) iterator container of vertex
3398
+ labels corresponding to which the looped edges are to be removed. If
3399
+ ``vertices`` is ``None``, remove all loops.
3400
+
3401
+ OUTPUT:
3402
+
3403
+ - Nothing is returned, as a matching covered graph is already devoid of
3404
+ any loops.
3405
+
3406
+ EXAMPLES:
3407
+
3408
+ A matching covered graph, for instance the Wheel graph of order six, is
3409
+ always free of loops::
3410
+
3411
+ sage: W = graphs.WheelGraph(6)
3412
+ sage: G = MatchingCoveredGraph(W)
3413
+ sage: G
3414
+ Matching covered wheel graph: graph on 6 vertices
3415
+ sage: G.add_edge(0, 0)
3416
+ Traceback (most recent call last):
3417
+ ...
3418
+ ValueError: loops are not allowed in matching covered graphs
3419
+ sage: G.remove_loops()
3420
+ sage: G.edges(sort=True)
3421
+ [(0, 1, None), (0, 2, None), (0, 3, None), (0, 4, None),
3422
+ (0, 5, None), (1, 2, None), (1, 5, None), (2, 3, None),
3423
+ (3, 4, None), (4, 5, None)]
3424
+
3425
+ A matching covered graph may support multiple edges, still no
3426
+ loops are allowed::
3427
+
3428
+ sage: K = graphs.CompleteGraph(2)
3429
+ sage: G = MatchingCoveredGraph(K)
3430
+ sage: G.allow_multiple_edges(True)
3431
+ sage: G
3432
+ Matching covered complete graph: multi-graph on 2 vertices
3433
+ sage: G.add_edge(0, 1, 'label')
3434
+ sage: G.add_edge(0, 0)
3435
+ Traceback (most recent call last):
3436
+ ...
3437
+ ValueError: loops are not allowed in matching covered graphs
3438
+ sage: G.edges(sort=False)
3439
+ [(0, 1, None), (0, 1, 'label')]
3440
+ sage: G.remove_loops(vertices=[0, 1])
3441
+ sage: G.edges(sort=False)
3442
+ [(0, 1, None), (0, 1, 'label')]
3443
+ sage: G.remove_loops(vertices=[0..100])
3444
+
3445
+ Note that the parameter ``vertices`` must be either ``None`` or an
3446
+ iterable::
3447
+
3448
+ sage: G.remove_loops(vertices='')
3449
+ sage: G.edges(sort=False)
3450
+ [(0, 1, None), (0, 1, 'label')]
3451
+ sage: G.remove_loops(vertices=None)
3452
+ sage: G.edges(sort=False)
3453
+ [(0, 1, None), (0, 1, 'label')]
3454
+ sage: G.remove_loops(vertices=0)
3455
+ Traceback (most recent call last):
3456
+ ...
3457
+ TypeError: 'Integer' object is not iterable
3458
+ sage: G.remove_loops(vertices=False)
3459
+ Traceback (most recent call last):
3460
+ ...
3461
+ TypeError: 'bool' object is not iterable
3462
+
3463
+ .. SEEALSO::
3464
+
3465
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.allow_loops`
3466
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.allows_loops`
3467
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.has_loops`
3468
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.loop_edges`
3469
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.loop_vertices`
3470
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.loops`
3471
+ - :meth:`~sage.graphs.matching_covered_graph.MatchingCoveredGraph.number_of_loops`
3472
+ """
3473
+ from collections.abc import Iterable
3474
+
3475
+ if vertices is not None and not isinstance(vertices, Iterable):
3476
+ raise TypeError(f'\'{vertices.__class__.__name__}\' '
3477
+ 'object is not iterable')
3478
+
3479
+ @doc_index('Miscellaneous methods')
3480
+ def update_matching(self, matching):
3481
+ r"""
3482
+ Update the perfect matching captured in ``self._matching``.
3483
+
3484
+ INPUT:
3485
+
3486
+ - ``matching`` -- a perfect matching of the graph, that can be given
3487
+ using any valid input format of :class:`~sage.graphs.graph.Graph`.
3488
+
3489
+ OUTPUT:
3490
+
3491
+ - If ``matching`` is a valid perfect matching of the graph, then
3492
+ ``self._matching`` gets updated to this provided matching, or
3493
+ otherwise an exception is returned without any alterations to
3494
+ ``self._matching``.
3495
+
3496
+ EXAMPLES:
3497
+
3498
+ Providing with a valid perfect matching of the graph::
3499
+
3500
+ sage: P = graphs.PetersenGraph()
3501
+ sage: G = MatchingCoveredGraph(P)
3502
+ sage: sorted(G.get_matching())
3503
+ [(0, 5, None), (1, 6, None), (2, 7, None), (3, 8, None), (4, 9, None)]
3504
+ sage: M = [(0, 1), (2, 3), (4, 9), (5, 7), (6, 8)]
3505
+ sage: G.update_matching(M)
3506
+ sage: sorted(G.get_matching())
3507
+ [(0, 1, None), (2, 3, None), (4, 9, None), (5, 7, None), (6, 8, None)]
3508
+
3509
+ TESTS:
3510
+
3511
+ Providing with a wrong matching::
3512
+
3513
+ sage: P = graphs.PetersenGraph()
3514
+ sage: G = MatchingCoveredGraph(P)
3515
+ sage: sorted(G.get_matching())
3516
+ [(0, 5, None), (1, 6, None), (2, 7, None), (3, 8, None), (4, 9, None)]
3517
+ sage: S = str('0')
3518
+ sage: G.update_matching(S)
3519
+ Traceback (most recent call last):
3520
+ ...
3521
+ RuntimeError: the string seems corrupt: valid characters are
3522
+ ?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
3523
+ sage: T = str('graph')
3524
+ sage: G.update_matching(T)
3525
+ Traceback (most recent call last):
3526
+ ...
3527
+ RuntimeError: the string (graph) seems corrupt: for n = 40,
3528
+ the string is too short
3529
+ sage: M = Graph(G.matching())
3530
+ sage: M.add_edges([(0, 1), (0, 2)])
3531
+ sage: G.update_matching(M)
3532
+ Traceback (most recent call last):
3533
+ ...
3534
+ ValueError: the input is not a matching
3535
+ sage: N = Graph(G.matching())
3536
+ sage: N.add_edge(10, 11)
3537
+ sage: G.update_matching(N)
3538
+ Traceback (most recent call last):
3539
+ ...
3540
+ ValueError: the input is not a matching of the graph
3541
+ sage: J = Graph()
3542
+ sage: J.add_edges([(0, 1), (2, 3)])
3543
+ sage: G.update_matching(J)
3544
+ Traceback (most recent call last):
3545
+ ...
3546
+ ValueError: the input is not a perfect matching of the graph
3547
+ """
3548
+ try:
3549
+ M = Graph(matching)
3550
+
3551
+ if any(d != 1 for d in M.degree()):
3552
+ raise ValueError("the input is not a matching")
3553
+
3554
+ if any(not self.has_edge(edge) for edge in M.edge_iterator()):
3555
+ raise ValueError("the input is not a matching of the graph")
3556
+
3557
+ if (self.order() != M.order()):
3558
+ raise ValueError("the input is not a perfect matching of the graph")
3559
+
3560
+ self._matching = M.edges()
3561
+
3562
+ except Exception as exception:
3563
+ raise exception
3564
+
3565
+
3566
+ __doc__ = __doc__.replace('{INDEX_OF_METHODS}', gen_thematic_rest_table_index(MatchingCoveredGraph, only_local_functions=False))