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,1304 @@
1
+ # sage_setup: distribution = sagemath-graphs
2
+ # cython: binding=True
3
+ # distutils: language = c++
4
+ r"""
5
+ Static sparse graphs
6
+
7
+ What is the point ?
8
+ -------------------
9
+
10
+ This class implements a Cython (di)graph structure made for efficiency. The
11
+ graphs are *static*, i.e. no add/remove vertex/edges methods are available, nor
12
+ can they easily or efficiently be implemented within this data structure.
13
+
14
+ The data structure, however, is made to save the maximum amount of computations
15
+ for graph algorithms whose main operation is to *list the out-neighbours of a
16
+ vertex* (which is precisely what BFS, DFS, distance computations and the
17
+ flow-related stuff waste their life on).
18
+
19
+ The code contained in this module is written C-style. The purpose is efficiency
20
+ and simplicity.
21
+
22
+ For an overview of graph data structures in sage, see
23
+ :mod:`~sage.graphs.base.overview`.
24
+
25
+ Author:
26
+
27
+ - Nathann Cohen (2011)
28
+
29
+ Data structure
30
+ --------------
31
+
32
+ .. image:: ../../../media/structure.png
33
+
34
+ The data structure is actually pretty simple and compact. ``short_digraph`` has
35
+ five fields
36
+
37
+ - ``n`` -- integer; the number of vertices in the graph
38
+
39
+ - ``m`` -- integer; the number of edges in the graph
40
+
41
+ - ``edges`` -- ``uint32_t *``; array whose length is the number of edges of the
42
+ graph
43
+
44
+ - ``neighbors`` -- ``uint32_t **``; this array has size `n+1`, and describes how
45
+ the data of ``edges`` should be read : the neighbors of vertex `i` are the
46
+ elements of ``edges`` addressed by ``neighbors[i]...neighbors[i+1]-1``. The
47
+ element ``neighbors[n]``, which corresponds to no vertex (they are numbered
48
+ from `0` to `n-1`) is present so that it remains easy to enumerate the
49
+ neighbors of vertex `n-1` : the last of them is the element addressed by
50
+ ``neighbors[n]-1``.
51
+ The arrays ``neighbors[i]`` are guaranteed to be sorted so the time
52
+ complexity for deciding if ``g`` has edge `(u, v)` is `O(\log{m})` using
53
+ binary search.
54
+
55
+ - ``edge_labels`` -- list; this cython list associates a label to each edge
56
+ of the graph. If a given edge is represented by ``edges[i]``, this its
57
+ associated label can be found at ``edge_labels[i]``. This object is usually
58
+ NULL, unless the call to ``init_short_digraph`` explicitly requires the labels
59
+ to be stored in the data structure.
60
+
61
+ In the example given above, vertex 0 has 2,3,5,7,8 and 9 as out-neighbors, but
62
+ not 4, which is an out-neighbour of vertex 1. Vertex `n-1` has 2, 5, 8 and 9 as
63
+ out-neighbors. ``neighbors[n]`` points toward the cell immediately *after* the
64
+ end of ``edges``, hence *outside of the allocated memory*. It is used to
65
+ indicate the end of the outneighbors of vertex `n-1`
66
+
67
+ **Iterating over the edges**
68
+
69
+ This is *the one thing* to have in mind when working with this data structure::
70
+
71
+ cdef list_edges(short_digraph g):
72
+ cdef int i, j
73
+ for i in range(g.n):
74
+ for j in range(g.neighbors[i+1]-g.neighbors[i]):
75
+ print("There is an edge from {} to {}".format(i, g.neighbors[i][j]))
76
+
77
+ **Advantages**
78
+
79
+ Two great points :
80
+
81
+ - The neighbors of a vertex are C types, and are contiguous in memory.
82
+ - Storing such graphs is incredibly cheaper than storing Python structures.
83
+
84
+ Well, I think it would be hard to have anything more efficient than that to
85
+ enumerate out-neighbors in sparse graphs ! :-)
86
+
87
+ Technical details
88
+ -----------------
89
+
90
+ * When creating a ``short_digraph`` from a ``Graph`` or ``DiGraph`` named ``G``,
91
+ the `i^{\text{th}}` vertex corresponds *by default* to ``list(G)[i]``.
92
+ Using optional parameter ``vertex_list``, you can specify the order of the
93
+ vertices. Then `i^{\text{th}}` vertex will corresponds to ``vertex_list[i]``.
94
+
95
+ * Some methods return ``bitset_t`` objects when lists could be expected. There
96
+ is a very useful ``bitset_list`` function for this kind of problems :-)
97
+
98
+ * When the edges are labelled, most of the space taken by this graph is taken by
99
+ edge labels. If no edge is labelled then this space is not allocated, but if
100
+ *any* edge has a label then a (possibly empty) label is stored for each edge,
101
+ which can double the memory needs.
102
+
103
+ * The data structure stores the number of edges, even though it appears that
104
+ this number can be reconstructed with ``g.neighbors[n]-g.neighbors[0]``. The
105
+ trick is that not all elements of the ``g.edges`` array are necessarily used :
106
+ when an undirected graph contains loops, only one entry of the array of size
107
+ `2m` is used to store it, instead of the expected two. Storing the number of
108
+ edges is the only way to avoid an uselessly costly computation to obtain the
109
+ number of edges of an undirected, looped, AND labelled graph (think of several
110
+ loops on the same vertex with different labels).
111
+
112
+ * The codes of this module are well documented, and many answers can be found
113
+ directly in the code.
114
+
115
+ Cython functions
116
+ ----------------
117
+
118
+ .. csv-table::
119
+ :class: contentstable
120
+ :widths: 30, 70
121
+ :delim: |
122
+
123
+ ``init_short_digraph(short_digraph g, G, edge_labelled, vertex_list)`` | Initialize ``short_digraph g`` from a Sage (Di)Graph.
124
+ ``int n_edges(short_digraph g)`` | Return the number of edges in ``g``
125
+ ``int out_degree(short_digraph g, int i)`` | Return the out-degree of vertex `i` in ``g``
126
+ ``has_edge(short_digraph g, int u, int v)`` | Test the existence of an edge.
127
+ ``edge_label(short_digraph g, int * edge)`` | Return the label associated with a given edge
128
+ ``init_empty_copy(short_digraph dst, short_digraph src)`` | Allocate ``dst`` so that it can contain as many vertices and edges as ``src``.
129
+ ``init_reverse(short_digraph dst, short_digraph src)`` | Initialize ``dst`` to a copy of ``src`` with all edges in the opposite direction.
130
+ ``free_short_digraph(short_digraph g)`` | Free the resources used by ``g``
131
+
132
+ **Connectivity**
133
+
134
+ ``can_be_reached_from(short_digraph g, int src, bitset_t reached)``
135
+
136
+ Assuming ``bitset_t reached`` has size at least ``g.n``, this method updates
137
+ ``reached`` so that it represents the set of vertices that can be reached
138
+ from ``src`` in ``g``.
139
+
140
+ ``strongly_connected_component_containing_vertex(short_digraph g, short_digraph g_reversed, int v, bitset_t scc)``
141
+
142
+ Assuming ``bitset_t reached`` has size at least ``g.n``, this method updates
143
+ ``scc`` so that it represents the vertices of the strongly connected
144
+ component containing ``v`` in ``g``. The variable ``g_reversed`` is assumed
145
+ to represent the reverse of ``g``.
146
+
147
+ ``tarjan_strongly_connected_components_C(short_digraph g, int *scc)``
148
+
149
+ Assuming ``scc`` is already allocated and has size at least ``g.n``, this
150
+ method computes the strongly connected components of ``g``, and outputs in
151
+ ``scc[v]`` the number of the strongly connected component containing ``v``.
152
+ It returns the number of strongly connected components.
153
+
154
+ ``strongly_connected_components_digraph_C(short_digraph g, int nscc, int *scc, short_digraph output):``
155
+
156
+ Assuming ``nscc`` and ``scc`` are the outputs of
157
+ ``tarjan_strongly_connected_components_C`` on ``g``, this routine
158
+ sets ``output`` to the
159
+ strongly connected component digraph of ``g``, that is, the vertices of
160
+ ``output`` are the strongly connected components of ``g`` (numbers are
161
+ provided by ``scc``), and ``output`` contains an arc ``(C1,C2)`` if ``g``
162
+ has an arc from a vertex in ``C1`` to a vertex in ``C2``.
163
+
164
+ What is this module used for ?
165
+ ------------------------------
166
+
167
+ It is for instance used in the :mod:`sage.graphs.distances_all_pairs` module,
168
+ and in the :meth:`~sage.graphs.digraph.DiGraph.strongly_connected_components`
169
+ method.
170
+
171
+ Python functions
172
+ ----------------
173
+
174
+ These functions are available so that Python modules from Sage can call the
175
+ Cython routines this module implements (as they cannot directly call methods
176
+ with C arguments).
177
+ """
178
+ # ****************************************************************************
179
+ # Copyright (C) 2010 Nathann Cohen <nathann.cohen@gmail.com>
180
+ #
181
+ # This program is free software: you can redistribute it and/or modify
182
+ # it under the terms of the GNU General Public License as published by
183
+ # the Free Software Foundation, either version 2 of the License, or
184
+ # (at your option) any later version.
185
+ # https://www.gnu.org/licenses/
186
+ # ****************************************************************************
187
+
188
+ cimport cpython
189
+ from libc.limits cimport INT_MAX
190
+ from libc.math cimport sqrt
191
+ from libcpp.vector cimport vector
192
+ from cysignals.memory cimport check_allocarray, check_calloc, sig_free
193
+ from cysignals.signals cimport sig_on, sig_off
194
+ from cython.operator cimport postincrement
195
+ from memory_allocator cimport MemoryAllocator
196
+
197
+ from sage.data_structures.bitset_base cimport *
198
+ from sage.graphs.base.static_sparse_backend cimport StaticSparseCGraph
199
+ from sage.graphs.base.static_sparse_backend cimport StaticSparseBackend
200
+
201
+
202
+ cdef extern from "fenv.h":
203
+ int FE_TONEAREST
204
+ int FE_UPWARD
205
+ int FE_DOWNWARD
206
+ int FE_TOWARDZERO
207
+ int fegetround ()
208
+ int fesetround (int)
209
+
210
+
211
+ cdef int init_short_digraph(short_digraph g, G, edge_labelled=False,
212
+ vertex_list=None) except -1:
213
+ r"""
214
+ Initialize ``short_digraph g`` from a Sage (Di)Graph.
215
+
216
+ INPUT:
217
+
218
+ - ``g`` -- a short_digraph
219
+
220
+ - ``G`` -- a ``Graph`` or a ``DiGraph``. If ``G`` is a ``Graph`` object,
221
+ then any edge between two vertices `u` and `v` is replaced by two arcs in
222
+ both directions.
223
+
224
+ - ``edge_labelled`` -- boolean (default: ``False``); whether to store the
225
+ label of edges or not
226
+
227
+ - ``vertex_list`` -- list (default: ``None``); list of all vertices of ``G``
228
+ in some order. When given, it is used to map the vertices of the graph to
229
+ consecutive integers. Otherwise, the result of ``list(G)`` is used
230
+ instead. Beware that if ``vertex_list`` is not ``None``, it is not checked
231
+ and this function assumes that it contains a permutation of the vertices
232
+ of the graph ``G``.
233
+
234
+ COMPLEXITY:
235
+
236
+ The time complexity for initializing ``g`` is `O(n + m)` for ``SparseGraph``
237
+ and `O(n^2)` for ``DenseGraph``.
238
+
239
+ TESTS:
240
+
241
+ Indirect doctests for sorted output::
242
+
243
+ sage: from sage.graphs.base.static_sparse_backend import StaticSparseBackend
244
+ sage: G = graphs.CompleteGraph(5).relabel(list('abcde'), inplace=False)
245
+ sage: B = StaticSparseBackend(G, sort=False)
246
+ sage: list(B.iterator_nbrs('a'))
247
+ ['b', 'c', 'd', 'e']
248
+ sage: G = graphs.CompleteGraph(5).relabel(list('badec'), inplace=False)
249
+ sage: B = StaticSparseBackend(G, sort=False)
250
+ sage: list(B.iterator_nbrs('a'))
251
+ ['b', 'd', 'e', 'c']
252
+
253
+ Same with labels::
254
+
255
+ sage: from sage.graphs.base.static_sparse_backend import StaticSparseBackend
256
+ sage: G = graphs.CompleteGraph(5).relabel(list('abcde'), inplace=False)
257
+ sage: for i, (u, v, _) in enumerate(G.edges()):
258
+ ....: G.set_edge_label(u, v, f'{u}{v}')
259
+ sage: B = StaticSparseBackend(G, sort=False)
260
+ sage: list(B.iterator_edges('a', True))
261
+ [('a', 'b', 'ab'), ('a', 'c', 'ac'), ('a', 'd', 'ad'), ('a', 'e', 'ae')]
262
+ sage: G = graphs.CompleteGraph(5).relabel(list('badec'), inplace=False)
263
+ sage: for i, (u, v, _) in enumerate(G.edges()):
264
+ ....: G.set_edge_label(u, v, f'{u}{v}')
265
+ sage: B = StaticSparseBackend(G, sort=False)
266
+ sage: list(B.iterator_edges('a', True))
267
+ [('a', 'b', 'ab'), ('a', 'd', 'ad'), ('a', 'e', 'ae'), ('a', 'c', 'ac')]
268
+ """
269
+ from sage.graphs.graph import Graph
270
+ from sage.graphs.digraph import DiGraph
271
+
272
+ if not isinstance(G, (Graph, DiGraph)):
273
+ raise ValueError("The source graph must be either a DiGraph or a Graph"
274
+ "object !")
275
+
276
+ if G.order() >= INT_MAX:
277
+ raise ValueError(f"short_digraph can handle at most {INT_MAX} vertices")
278
+
279
+ g.edge_labels = NULL
280
+ g.n = G.order()
281
+ g.m = G.size()
282
+
283
+ cdef int isdigraph = G.is_directed()
284
+ cdef uint32_t i, v_id, j
285
+ cdef list vertices = vertex_list if vertex_list is not None else list(G)
286
+ cdef dict v_to_id = {v: i for i, v in enumerate(vertices)}
287
+ cdef list neighbor_label
288
+ cdef list edge_labels
289
+ # Loops are not stored twice for undirected graphs
290
+ cdef int n_edges = g.m if isdigraph else 2*g.m - G.number_of_loops()
291
+
292
+ g.edges = <uint32_t *>check_allocarray(n_edges, sizeof(uint32_t))
293
+ g.neighbors = <uint32_t **>check_allocarray(1 + g.n, sizeof(uint32_t *))
294
+
295
+ # Initializing the value of neighbors
296
+ g.neighbors[0] = g.edges
297
+
298
+ if not G.has_loops():
299
+ # Normal case
300
+ for i, v in enumerate(vertices):
301
+ g.neighbors[i+1] = g.neighbors[i] + <int>(G.out_degree(v) if isdigraph else G.degree(v))
302
+ else:
303
+ # In the presence of loops. For a funny reason, if a vertex v has a loop
304
+ # attached to it and no other incident edge, Sage declares that it has
305
+ # degree 2. This way, the sum of the degrees of the vertices is twice
306
+ # the number of edges, but then the degree of a vertex is not the number
307
+ # of its neighbors anymore. One should never try to think. It never ends
308
+ # well.
309
+ for i, v in enumerate(vertices):
310
+ g.neighbors[i+1] = g.neighbors[i] + <int> len(G.edges_incident(v))
311
+
312
+ if edge_labelled:
313
+ edge_labels = [None] * n_edges
314
+
315
+ # Note that neighbors[i] will be naturally sorted by increasing id,
316
+ # because the arrays will be built by appending vertices in the same
317
+ # order as they appear in ``vertices``
318
+ for i, v in enumerate(vertices):
319
+ if isdigraph:
320
+ edge_iterator = G.incoming_edge_iterator(v,
321
+ labels=edge_labelled)
322
+ else:
323
+ edge_iterator = G.edge_iterator(v, labels=edge_labelled,
324
+ sort_vertices=False)
325
+ for e in edge_iterator:
326
+ u = e[0] if v == e[1] else e[1]
327
+ j = v_to_id[u]
328
+ # Handle the edge u -> v of G (= the edge j -> i of g)
329
+ g.neighbors[j][0] = i
330
+ # Note: cannot use the dereference Cython operator here, do not
331
+ # known why but the following line does not compile
332
+ # dereference(g.neighbors[j]) = i
333
+ if edge_labelled:
334
+ edge_labels[g.neighbors[j] - g.edges] = e[2]
335
+ postincrement(g.neighbors[j]) # increment pointer to next item
336
+
337
+ # Reinitializing the value of neighbors
338
+ for i in range(g.n-1, 0, -1):
339
+ g.neighbors[i] = g.neighbors[i-1]
340
+ g.neighbors[0] = g.edges
341
+
342
+ if edge_labelled:
343
+ g.edge_labels = <PyObject *> <void *> edge_labels
344
+ cpython.Py_XINCREF(g.edge_labels)
345
+
346
+
347
+ cdef inline int n_edges(short_digraph g) noexcept:
348
+ """
349
+ Return the number of edges in ``g``.
350
+
351
+ The number of edges is nothing but a difference of pointers
352
+ """
353
+ return <int> (g.neighbors[g.n] - g.edges)
354
+
355
+
356
+ cdef inline int out_degree(short_digraph g, int i) noexcept:
357
+ """
358
+ Return the out-degree of vertex `i` in ``g``.
359
+
360
+ The out-degree is nothing but a difference of pointers
361
+ """
362
+ return <int> (g.neighbors[i + 1] - g.neighbors[i])
363
+
364
+
365
+ cdef int init_empty_copy(short_digraph dst, short_digraph src) except -1:
366
+ """
367
+ Allocate ``dst`` so that it can contain as many vertices and edges as
368
+ ``src``.
369
+ """
370
+ dst.n = src.n
371
+ dst.m = src.m
372
+ dst.edge_labels = NULL
373
+ cdef list edge_labels
374
+
375
+ dst.edges = <uint32_t *>check_allocarray(n_edges(src), sizeof(uint32_t))
376
+ dst.neighbors = <uint32_t **>check_allocarray(src.n + 1, sizeof(uint32_t *))
377
+
378
+ if src.edge_labels != NULL:
379
+ edge_labels = [None] * n_edges(src)
380
+ dst.edge_labels = <PyObject *> <void *> edge_labels
381
+ cpython.Py_XINCREF(dst.edge_labels)
382
+
383
+
384
+ cdef int init_reverse(short_digraph dst, short_digraph src) except -1:
385
+ """
386
+ Initialize ``dst`` to a copy of ``src`` with all edges in the opposite
387
+ direction.
388
+
389
+ TESTS:
390
+
391
+ Indirect doctests for sorted output::
392
+
393
+ sage: from sage.graphs.base.static_sparse_backend import StaticSparseBackend
394
+ sage: D = digraphs.Complete(5).relabel(list('abcde'), inplace=False)
395
+ sage: for i, (u, v, _) in enumerate(D.edges()):
396
+ ....: D.set_edge_label(u, v, f'{u}{v}')
397
+ sage: B = StaticSparseBackend(D, sort=False)
398
+ sage: list(B.iterator_in_edges('a', True))
399
+ [('b', 'a', 'ba'), ('c', 'a', 'ca'), ('d', 'a', 'da'), ('e', 'a', 'ea')]
400
+ sage: D = digraphs.Complete(5).relabel(list('badec'), inplace=False)
401
+ sage: for i, (u, v, _) in enumerate(D.edges()):
402
+ ....: D.set_edge_label(u, v, f'{u}{v}')
403
+ sage: B = StaticSparseBackend(D, sort=False)
404
+ sage: list(B.iterator_in_edges('a', True))
405
+ [('b', 'a', 'ba'), ('d', 'a', 'da'), ('e', 'a', 'ea'), ('c', 'a', 'ca')]
406
+ """
407
+ cdef int i, j, v
408
+ # Allocates memory for dst
409
+ init_empty_copy(dst, src)
410
+
411
+ # Avoiding a later segfault
412
+ if not dst.n:
413
+ return 0
414
+
415
+ # 1/3
416
+ #
417
+ # In a first pass, we count the in-degrees of each vertex and store it in a
418
+ # vector. With this information, we can initialize dst.neighbors to its
419
+ # correct value. The content of dst.edges is not touched at this level.
420
+ cdef int * in_degree = <int *>check_calloc(src.n, sizeof(int))
421
+
422
+ for i in range(n_edges(src)):
423
+ in_degree[src.edges[i]] += 1
424
+
425
+ # Updating dst.neighbors
426
+ dst.neighbors[0] = dst.edges
427
+ for i in range(1, src.n + 1):
428
+ dst.neighbors[i] = dst.neighbors[i - 1] + in_degree[i - 1]
429
+ sig_free(in_degree)
430
+
431
+ # 2/3
432
+ #
433
+ # Second pass : we list the edges again, and add them in dst.edges. Doing
434
+ # so, we will change the value of dst.neighbors, but that is not so bad as
435
+ # we can fix it afterwards.
436
+ # Note that neighbors[i] will be naturally sorted by increasing id,
437
+ # because the arrays will be built by appending vertices in the same
438
+ # order as they appear in ``vertices``
439
+ for i in range(0, src.n):
440
+ for j in range(out_degree(src, i)):
441
+ v = src.neighbors[i][j]
442
+ dst.neighbors[v][0] = i
443
+
444
+ if dst.edge_labels:
445
+ (<list> dst.edge_labels)[dst.neighbors[v] - dst.edges] = edge_label(src, src.neighbors[i] + j)
446
+
447
+ dst.neighbors[v] += 1
448
+
449
+ # 3/3
450
+ #
451
+ # Third step : set the correct values of dst.neighbors again. It is easy, as
452
+ # the correct value of dst.neighbors[i] is actually dst.neighbors[i-1]
453
+ for i in range(src.n - 1, 0, -1):
454
+ dst.neighbors[i] = dst.neighbors[i - 1]
455
+ dst.neighbors[0] = dst.edges
456
+
457
+ return 0
458
+
459
+
460
+ cdef int compare_uint32_p(const_void *a, const_void *b) noexcept:
461
+ """
462
+ Comparison function needed for ``bsearch``.
463
+ """
464
+ return (<uint32_t *> a)[0] - (<uint32_t *> b)[0]
465
+
466
+
467
+ cdef inline uint32_t * has_edge(short_digraph g, int u, int v) noexcept:
468
+ r"""
469
+ Test the existence of an edge.
470
+
471
+ Return a pointer to ``v`` in the list of neighbors of ``u`` if found and
472
+ ``NULL`` otherwise.
473
+
474
+ .. NOTE::
475
+
476
+ Use the fact that the array ``g.neighbors[u]`` is guaranteed to be sorted.
477
+ """
478
+ # The neighbors of u are sorted by increasing label. We can use binary
479
+ # search to decide if g has edge (u, v)
480
+ return <uint32_t *> bsearch(&v, g.neighbors[u],
481
+ g.neighbors[u+1] - g.neighbors[u],
482
+ sizeof(uint32_t), compare_uint32_p)
483
+
484
+
485
+ cdef inline object edge_label(short_digraph g, uint32_t * edge):
486
+ r"""
487
+ Return the label associated with a given edge
488
+ """
489
+ if not g.edge_labels:
490
+ return None
491
+ return (<list> g.edge_labels)[edge - g.edges]
492
+
493
+
494
+ cdef uint32_t simple_BFS(short_digraph g,
495
+ uint32_t source,
496
+ uint32_t *distances,
497
+ uint32_t *predecessors,
498
+ uint32_t *waiting_list,
499
+ bitset_t seen) noexcept:
500
+ """
501
+ Perform a breadth first search (BFS) using the same method as in
502
+ sage.graphs.distances_all_pairs.all_pairs_shortest_path_BFS
503
+
504
+ Furthermore, the method returns the eccentricity of the source which is
505
+ either the last computed distance when all vertices are seen, or a very
506
+ large number (UINT32_MAX) when the graph is not connected.
507
+
508
+ INPUT:
509
+
510
+ - ``g`` -- a short_digraph
511
+
512
+ - ``source`` -- starting node of the BFS
513
+
514
+ - ``distances`` -- array of size ``n`` to store BFS distances from
515
+ ``source``. This method assumes that this array has already been
516
+ allocated. However, there is no need to initialize it.
517
+
518
+ - ``predecessors`` -- array of size ``n`` to store the first predecessor of
519
+ each vertex during the BFS search from ``source``. The predecessor of the
520
+ ``source`` is itself. This method assumes that this array has already
521
+ been allocated. However, it is possible to pass a ``NULL`` pointer in
522
+ which case the predecessors are not recorded.
523
+
524
+ - ``waiting_list`` -- array of size ``n`` to store the order in which the
525
+ vertices are visited during the BFS search from ``source``. This method
526
+ assumes that this array has already been allocated. However, there is no
527
+ need to initialize it.
528
+
529
+ - ``seen`` -- bitset of size ``n`` that must be initialized before calling
530
+ this method (i.e., bitset_init(seen, n)). However, there is no need to
531
+ clear it.
532
+ """
533
+ cdef uint32_t v, u
534
+ cdef uint32_t waiting_beginning = 0
535
+ cdef uint32_t waiting_end = 0
536
+ cdef uint32_t * p_tmp
537
+ cdef uint32_t * end
538
+ cdef uint32_t n = g.n
539
+ cdef uint32_t ** p_vertices = g.neighbors
540
+
541
+ # the source is seen
542
+ bitset_clear(seen)
543
+ bitset_add(seen, source)
544
+ distances[source] = 0
545
+ if predecessors!=NULL:
546
+ predecessors[source] = source
547
+
548
+ # and added to the queue
549
+ waiting_list[0] = source
550
+ waiting_beginning = 0
551
+ waiting_end = 0
552
+
553
+ # For as long as there are vertices left to explore
554
+ while waiting_beginning <= waiting_end:
555
+
556
+ # We pick the first one
557
+ v = waiting_list[waiting_beginning]
558
+ p_tmp = p_vertices[v]
559
+ end = p_vertices[v + 1]
560
+
561
+ # and we iterate over all the outneighbors u of v
562
+ while p_tmp < end:
563
+ u = p_tmp[0]
564
+
565
+ # If we notice one of these neighbors is not seen yet, we set its
566
+ # parameters and add it to the queue to be explored later.
567
+ if not bitset_in(seen, u):
568
+ distances[u] = distances[v] + 1
569
+ bitset_add(seen, u)
570
+ waiting_end += 1
571
+ waiting_list[waiting_end] = u
572
+ if predecessors:
573
+ predecessors[u] = v
574
+
575
+ p_tmp += 1
576
+
577
+ waiting_beginning += 1
578
+
579
+ # We return the eccentricity of the source
580
+ return distances[waiting_list[waiting_end]] if waiting_end == n - 1 else UINT32_MAX
581
+
582
+
583
+ cdef int can_be_reached_from(short_digraph g, int src, bitset_t reached) except -1:
584
+ """
585
+ Feed ``reached`` with the vertices reachable from ``src``.
586
+ """
587
+ if not g.n:
588
+ return 0
589
+
590
+ # Initializing the set of vertices reached by setting only bit src
591
+ bitset_set_first_n(reached, 0)
592
+ bitset_add(reached, src)
593
+
594
+ # We will be doing a Depth-First Search. We allocate the stack we need for
595
+ # that, and put "src" on top of it.
596
+ cdef int * stack = <int *>check_allocarray(g.n, sizeof(int))
597
+
598
+ stack[0] = src
599
+ cdef int stack_size = 1
600
+
601
+ # What we need to iterate over the edges
602
+ cdef int i
603
+ cdef uint32_t * v
604
+ cdef uint32_t * end
605
+
606
+ # Plain old DFS ...
607
+ #
608
+ # If there is something left on the stack, we remove it consider each of its
609
+ # neighbors. If we find any which has not been reached yet, we set its
610
+ # corresponding bit in the reached bitset, and add it on top of the stack.
611
+
612
+ while stack_size:
613
+ stack_size -= 1
614
+ i = stack[stack_size]
615
+
616
+ v = g.neighbors[i]
617
+ end = g.neighbors[i + 1]
618
+
619
+ while v < end:
620
+ if not bitset_in(reached, v[0]):
621
+ bitset_add(reached, v[0])
622
+ stack[stack_size] = v[0]
623
+ stack_size += 1
624
+
625
+ v += 1
626
+
627
+ sig_free(stack)
628
+
629
+
630
+ cdef int tarjan_strongly_connected_components_C(short_digraph g, int *scc) noexcept:
631
+ r"""
632
+ The Tarjan algorithm to compute strongly connected components (SCCs).
633
+
634
+ This routine returns the number of SCCs `k` and, stores in ``scc[v]`` an
635
+ integer between `0` and `k-1`, corresponding to the SCC containing v. SCCs
636
+ are numbered in reverse topological order, that is, if `(v,w)` is an edge
637
+ in the graph, ``scc[v] <= scc[w]``.
638
+
639
+ The basic idea of the algorithm is this: a depth-first search (DFS) begins
640
+ from an arbitrary start node (and subsequent DFSes are
641
+ conducted on any nodes that have not yet been found). As usual with DFSes,
642
+ the search visits every node of the graph exactly once, declining to revisit
643
+ any node that has already been explored. Thus, the collection of search
644
+ trees is a spanning forest of the graph. The strongly connected components
645
+ are the subtrees of this spanning forest having no edge directed outside the
646
+ subtree.
647
+
648
+ To recover these components, during the DFS, we keep the index of a node,
649
+ that is, the position in the DFS tree, and the lowlink: as soon as the
650
+ subtree rooted at `v` has been fully explored, the lowlink of `v` is the
651
+ smallest index reachable from `v` passing from descendants of `v`. If the
652
+ subtree rooted at `v` has been fully explored, and the index of `v` equals
653
+ the lowlink of `v`, that whole subtree is a new SCC.
654
+ """
655
+ cdef MemoryAllocator mem = MemoryAllocator()
656
+ cdef int u, v, w, n = g.n, current_index = 0, currentscc = 0
657
+ cdef int *index = <int *> mem.malloc(n * sizeof(int))
658
+ cdef int *pred = <int *> mem.malloc(n * sizeof(int))
659
+ cdef int *lowlink = <int *> mem.malloc(n * sizeof(int))
660
+ cdef int *dfs_stack = <int *> mem.malloc((n_edges(g) + 1) * sizeof(int))
661
+ cdef int dfs_stack_end
662
+ cdef int *scc_stack = <int *> mem.malloc(n * sizeof(int)) # Used to keep track of which nodes are in the "current" SCC
663
+ cdef short *in_scc_stack = <short *> mem.calloc(n, sizeof(short))
664
+ cdef uint32_t *p_tmp
665
+ cdef short *visited = <short *> mem.calloc(n, sizeof(short))
666
+ # The variable visited[v] is 0 if the vertex has never been visited, 1 if
667
+ # it is an ancestor of the current vertex, 2 otherwise.
668
+
669
+ for u in range(n):
670
+ if visited[u]:
671
+ continue
672
+
673
+ # Perform a DFS from u
674
+ dfs_stack_end = 1
675
+ scc_stack_end = 0
676
+ dfs_stack[0] = u
677
+ pred[u] = u
678
+
679
+ while dfs_stack_end:
680
+ v = dfs_stack[dfs_stack_end - 1]
681
+ if not visited[v]:
682
+ # It means that this is the first time we visit v.
683
+ # We set the index and the lowlink to be equal: during the
684
+ # algorithm, the lowlink may decrease.
685
+ visited[v] = 1
686
+ index[v] = current_index
687
+ lowlink[v] = current_index
688
+ current_index = current_index + 1
689
+ # We add v to the stack of vertices in the current SCC
690
+ scc_stack[scc_stack_end] = v
691
+ scc_stack_end = scc_stack_end + 1
692
+ in_scc_stack[v] = 1
693
+
694
+ # We iterate over all neighbors of v
695
+ p_tmp = g.neighbors[v]
696
+ while p_tmp < g.neighbors[v + 1]:
697
+ w = p_tmp[0]
698
+ p_tmp += 1
699
+ if not visited[w]:
700
+ # Vertex w is added to the DFS stack
701
+ pred[w] = v
702
+ dfs_stack[dfs_stack_end] = w
703
+ dfs_stack_end += 1
704
+ elif in_scc_stack[w]:
705
+ # We update the lowlink of v (later, we will "pass"
706
+ # this updated value to all ancestors of v.
707
+ lowlink[v] = min(lowlink[v], lowlink[w])
708
+ else:
709
+ # The vertex v has already been visited.
710
+ dfs_stack_end -= 1
711
+
712
+ if visited[v] == 1:
713
+ # It means that we have just processed all the DFS
714
+ # subtree rooted at v. Hence, the lowlink of v is the
715
+ # final value, and we "pass" this value to the
716
+ # predecessor of v.
717
+ lowlink[pred[v]] = min(lowlink[pred[v]], lowlink[v])
718
+
719
+ if lowlink[v] == index[v]:
720
+ # The DFS subtree rooted at v is a new SCC. We
721
+ # recover the SCC from scc_stack.
722
+ w = -1
723
+ while w != v:
724
+ scc_stack_end -= 1
725
+ w = scc_stack[scc_stack_end]
726
+ in_scc_stack[w] = 0
727
+ scc[w] = currentscc
728
+ currentscc += 1
729
+ visited[v] = 2
730
+
731
+ return currentscc
732
+
733
+
734
+ def tarjan_strongly_connected_components(G):
735
+ r"""
736
+ Return the lists of vertices in each strongly connected components (SCCs).
737
+
738
+ This method implements the Tarjan algorithm to compute the strongly
739
+ connected components of the digraph. It returns a list of lists of vertices,
740
+ each list of vertices representing a strongly connected component.
741
+
742
+ The basic idea of the algorithm is this: a depth-first search (DFS) begins
743
+ from an arbitrary start node (and subsequent DFSes are
744
+ conducted on any nodes that have not yet been found). As usual with DFSes,
745
+ the search visits every node of the graph exactly once, declining to revisit
746
+ any node that has already been explored. Thus, the collection of search
747
+ trees is a spanning forest of the graph. The strongly connected components
748
+ correspond to the subtrees of this spanning forest that have no edge
749
+ directed outside the subtree.
750
+
751
+ To recover these components, during the DFS, we keep the index of a node,
752
+ that is, the position in the DFS tree, and the lowlink: as soon as the
753
+ subtree rooted at `v` has been fully explored, the lowlink of `v` is the
754
+ smallest index reachable from `v` passing from descendants of `v`. If the
755
+ subtree rooted at `v` has been fully explored, and the index of `v` equals
756
+ the lowlink of `v`, that whole subtree is a new SCC.
757
+
758
+ For more information, see the
759
+ :wikipedia:`Tarjan%27s_strongly_connected_components_algorithm`.
760
+
761
+ EXAMPLES::
762
+
763
+ sage: from sage.graphs.base.static_sparse_graph import tarjan_strongly_connected_components
764
+ sage: tarjan_strongly_connected_components(digraphs.Path(3))
765
+ [[2], [1], [0]]
766
+ sage: D = DiGraph( { 0 : [1, 3], 1 : [2], 2 : [3], 4 : [5, 6], 5 : [6] } )
767
+ sage: D.connected_components(sort=True)
768
+ [[0, 1, 2, 3], [4, 5, 6]]
769
+ sage: D = DiGraph( { 0 : [1, 3], 1 : [2], 2 : [3], 4 : [5, 6], 5 : [6] } )
770
+ sage: D.strongly_connected_components()
771
+ [[3], [2], [1], [0], [6], [5], [4]]
772
+ sage: D.add_edge([2,0])
773
+ sage: D.strongly_connected_components()
774
+ [[3], [0, 1, 2], [6], [5], [4]]
775
+ sage: D = DiGraph([('a','b'), ('b','c'), ('c', 'd'), ('d', 'b'), ('c', 'e')])
776
+ sage: [sorted(scc) for scc in D.strongly_connected_components()]
777
+ [['e'], ['b', 'c', 'd'], ['a']]
778
+
779
+ TESTS:
780
+
781
+ Checking that the result is correct::
782
+
783
+ sage: from sage.graphs.base.static_sparse_graph import tarjan_strongly_connected_components
784
+ sage: import random
785
+ sage: for i in range(10): # long time
786
+ ....: n = random.randint(2,20)
787
+ ....: m = random.randint(1, n*(n-1))
788
+ ....: g = digraphs.RandomDirectedGNM(n,m)
789
+ ....: sccs = tarjan_strongly_connected_components(g)
790
+ ....: for scc in sccs:
791
+ ....: scc_check = g.strongly_connected_component_containing_vertex(scc[0])
792
+ ....: assert(sorted(scc) == sorted(scc_check))
793
+
794
+ Checking against NetworkX::
795
+
796
+ sage: import networkx # needs networkx
797
+ sage: for i in range(10): # long time # needs networkx
798
+ ....: g = digraphs.RandomDirectedGNP(100,.05)
799
+ ....: h = g.networkx_graph()
800
+ ....: scc1 = g.strongly_connected_components()
801
+ ....: scc2 = networkx.strongly_connected_components(h)
802
+ ....: s1 = Set(map(Set,scc1))
803
+ ....: s2 = Set(map(Set,scc2))
804
+ ....: if s1 != s2:
805
+ ....: print("Ooch !")
806
+ """
807
+ from sage.graphs.digraph import DiGraph
808
+
809
+ if not isinstance(G, DiGraph):
810
+ raise ValueError("G must be a DiGraph.")
811
+
812
+ cdef MemoryAllocator mem = MemoryAllocator()
813
+ cdef list int_to_vertex = list(G)
814
+ cdef short_digraph g
815
+ init_short_digraph(g, G, edge_labelled=False, vertex_list=int_to_vertex)
816
+ cdef int * scc = <int*> mem.malloc(g.n * sizeof(int))
817
+ sig_on()
818
+ cdef int nscc = tarjan_strongly_connected_components_C(g, scc)
819
+ sig_off()
820
+ free_short_digraph(g)
821
+
822
+ cdef int i
823
+ cdef list output = [[] for i in range(nscc)]
824
+
825
+ for i, v in enumerate(int_to_vertex):
826
+ output[scc[i]].append(v)
827
+ return output
828
+
829
+
830
+ cdef void strongly_connected_components_digraph_C(short_digraph g, int nscc, int *scc, short_digraph output) noexcept:
831
+ r"""
832
+ Compute the strongly connected components (SCCs) digraph of `g`.
833
+
834
+ The strongly connected components digraph of `g` is a graph having a vertex
835
+ for each SCC of `g` and an arc from component `C_1` to component `C_2` if
836
+ and only if there is an arc in `g` from a vertex in `C_1` to a vertex in
837
+ `C_2`. The strongly connected components digraph is acyclic by definition.
838
+
839
+ This routine inputs the graph ``g``, the number of SCCs ``nscc``, and an
840
+ array containing in position ``v`` the SCC of vertex ``v`` (these values
841
+ must be already computed). The output is stored in variable ``output``,
842
+ which should be empty at the beginning.
843
+ """
844
+ cdef MemoryAllocator mem = MemoryAllocator()
845
+ cdef size_t v, w, i
846
+ cdef size_t s_nscc = <size_t>nscc
847
+ cdef vector[vector[int]] scc_list = vector[vector[int]](nscc, vector[int]())
848
+ cdef vector[vector[int]] sons = vector[vector[int]](nscc + 1, vector[int]())
849
+ cdef short *neighbors = <short *> mem.calloc(nscc, sizeof(short))
850
+ cdef long m = 0
851
+ cdef uint32_t *p_tmp
852
+
853
+ for v in range(s_nscc):
854
+ scc_list[v] = vector[int]()
855
+ sons[v] = vector[int]()
856
+ sons[nscc] = vector[int]()
857
+
858
+ for i in range(<size_t>g.n):
859
+ scc_list[scc[i]].push_back(i)
860
+
861
+ for v in range(s_nscc):
862
+ for i in range(scc_list[v].size()):
863
+ p_tmp = g.neighbors[scc_list[v][i]]
864
+ while p_tmp<g.neighbors[scc_list[v][i] + 1]:
865
+ w = <int> scc[p_tmp[0]]
866
+ p_tmp += 1
867
+ if not (neighbors[w] or w == v):
868
+ neighbors[w] = 1
869
+ sons[v].push_back(w)
870
+ m += 1
871
+ for w in range(sons[v].size()):
872
+ neighbors[sons[v][w]] = 0
873
+
874
+ output.n = nscc
875
+ output.m = m
876
+
877
+ output.neighbors = <uint32_t **> check_allocarray((1+<int>output.n), sizeof(uint32_t *))
878
+
879
+ if not m:
880
+ output.edges = NULL
881
+ for v in range(1, s_nscc + 1):
882
+ output.neighbors[v] = NULL
883
+
884
+ output.edges = <uint32_t *> check_allocarray(m, sizeof(uint32_t))
885
+ output.neighbors[0] = output.edges
886
+
887
+ for v in range(1, s_nscc + 1):
888
+ output.neighbors[v] = output.neighbors[v - 1] + sons[v - 1].size()
889
+ for i in range(sons[v].size()):
890
+ output.neighbors[v][i] = sons[v][i]
891
+
892
+
893
+ def strongly_connected_components_digraph(G):
894
+ r"""
895
+ Return the digraph of the strongly connected components (SCCs).
896
+
897
+ This routine is used to test ``strongly_connected_components_digraph_C``,
898
+ but it is not used by the Sage digraph. It outputs a pair ``[g_scc,scc]``,
899
+ where ``g_scc`` is the SCC digraph of g, ``scc`` is a dictionary associating
900
+ to each vertex ``v`` the number of the SCC of ``v``, as it appears in
901
+ ``g_scc``.
902
+
903
+ EXAMPLES::
904
+
905
+ sage: from sage.graphs.base.static_sparse_graph import strongly_connected_components_digraph
906
+ sage: strongly_connected_components_digraph(digraphs.Path(3))
907
+ (Digraph on 3 vertices, {0: 2, 1: 1, 2: 0})
908
+ sage: strongly_connected_components_digraph(DiGraph(4))
909
+ (Digraph on 4 vertices, {0: 0, 1: 1, 2: 2, 3: 3})
910
+
911
+ TESTS::
912
+
913
+ sage: from sage.graphs.base.static_sparse_graph import strongly_connected_components_digraph
914
+ sage: import random
915
+ sage: for i in range(100):
916
+ ....: n = random.randint(2,20)
917
+ ....: m = random.randint(1, n*(n-1))
918
+ ....: g = digraphs.RandomDirectedGNM(n,m)
919
+ ....: scc_digraph,sccs = strongly_connected_components_digraph(g)
920
+ ....: assert(scc_digraph.is_directed_acyclic())
921
+ ....: for e in g.edges(sort=False):
922
+ ....: assert(sccs[e[0]]==sccs[e[1]] or scc_digraph.has_edge(sccs[e[0]],sccs[e[1]]))
923
+ ....: assert(sccs[e[0]] >= sccs[e[1]])
924
+ """
925
+ from sage.graphs.digraph import DiGraph
926
+ if not isinstance(G, DiGraph):
927
+ raise ValueError("G must be a DiGraph.")
928
+
929
+ cdef MemoryAllocator mem = MemoryAllocator()
930
+ cdef list int_to_vertex = list(G)
931
+ cdef short_digraph g, scc_g
932
+ init_short_digraph(g, G, edge_labelled=False, vertex_list=int_to_vertex)
933
+ cdef int * scc = <int*> mem.malloc(g.n * sizeof(int))
934
+ cdef int i, j, nscc
935
+ cdef list edges = []
936
+
937
+ sig_on()
938
+ nscc = tarjan_strongly_connected_components_C(g, scc)
939
+ strongly_connected_components_digraph_C(g, nscc, scc, scc_g)
940
+
941
+ output = DiGraph(nscc)
942
+
943
+ for i in range(scc_g.n):
944
+ for j in range(scc_g.neighbors[i + 1] - scc_g.neighbors[i]):
945
+ edges.append((i, scc_g.neighbors[i][j]))
946
+ output.add_edges(edges)
947
+ sig_off()
948
+ free_short_digraph(g)
949
+ return output, {v: scc[i] for i, v in enumerate(int_to_vertex)}
950
+
951
+
952
+ cdef strongly_connected_component_containing_vertex(short_digraph g, short_digraph g_reversed, int v, bitset_t scc):
953
+ """
954
+ Feed ``scc`` with the vertices in the strongly connected component of ``v``.
955
+ """
956
+ # Computing the set of vertices that can be reached from v in g
957
+ can_be_reached_from(g, v, scc)
958
+ # Computing the set of vertices that can be reached from v in g *reversed*
959
+ cdef bitset_t scc_reversed
960
+ bitset_init(scc_reversed, g.n)
961
+ can_be_reached_from(g_reversed, v, scc_reversed)
962
+ # The scc containing v is the intersection of both sets
963
+ bitset_intersection(scc, scc, scc_reversed)
964
+
965
+
966
+ cdef void free_short_digraph(short_digraph g) noexcept:
967
+ """
968
+ Free the resources used by ``g``
969
+ """
970
+ sig_free(g.edges)
971
+ sig_free(g.neighbors)
972
+ cpython.Py_XDECREF(g.edge_labels)
973
+
974
+
975
+ def triangles_count(G):
976
+ r"""
977
+ Return the number of triangles containing `v`, for every `v`.
978
+
979
+ INPUT:
980
+
981
+ - ``G`` -- a graph
982
+
983
+ EXAMPLES::
984
+
985
+ sage: from sage.graphs.base.static_sparse_graph import triangles_count
986
+ sage: triangles_count(graphs.PetersenGraph())
987
+ {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}
988
+ sage: sum(triangles_count(graphs.CompleteGraph(15)).values()) == 3*binomial(15,3) # needs sage.symbolic
989
+ True
990
+ """
991
+ from sage.rings.integer import Integer
992
+ G._scream_if_not_simple()
993
+
994
+ # g is a copy of G. If G is internally a static sparse graph, we use it.
995
+ cdef list int_to_vertex = list(G)
996
+ cdef short_digraph g
997
+ init_short_digraph(g, G, edge_labelled=False, vertex_list=int_to_vertex)
998
+
999
+ cdef uint64_t * count = <uint64_t *> check_calloc(G.order(), sizeof(uint64_t))
1000
+
1001
+ cdef uint64_t tmp_count = 0
1002
+ cdef uint32_t u, v, i
1003
+ cdef uint32_t * p1
1004
+ cdef uint32_t * p2
1005
+
1006
+ for u in range(<uint32_t>g.n):
1007
+ for i in range(<uint32_t>out_degree(g, u)):
1008
+ v = g.neighbors[u][i]
1009
+ if v <= u:
1010
+ continue
1011
+
1012
+ # Size of [N(u) inter N(v)]. Both are sorted lists.
1013
+ p1 = g.neighbors[u]
1014
+ p2 = g.neighbors[v]
1015
+ tmp_count = 0
1016
+ while (p1 < g.neighbors[u + 1] and p2 < g.neighbors[v + 1]):
1017
+ if p1[0] == p2[0]:
1018
+ tmp_count += 1
1019
+ p1 += 1
1020
+ p2 += 1
1021
+ elif p1[0] < p2[0]:
1022
+ p1 += 1
1023
+ else:
1024
+ p2 += 1
1025
+
1026
+ count[u] += tmp_count
1027
+ count[v] += tmp_count
1028
+
1029
+ ans = {w: Integer(count[i] // 2) for i, w in enumerate(int_to_vertex)}
1030
+
1031
+ free_short_digraph(g)
1032
+ sig_free(count)
1033
+ return ans
1034
+
1035
+
1036
+ def spectral_radius(G, prec=1e-10):
1037
+ r"""
1038
+ Return an interval of floating point number that encloses the spectral
1039
+ radius of this graph
1040
+
1041
+ The input graph ``G`` must be *strongly connected*.
1042
+
1043
+ INPUT:
1044
+
1045
+ - ``prec`` -- (default: ``1e-10``) an upper bound for the relative precision
1046
+ of the interval
1047
+
1048
+ The algorithm is iterative and uses an inequality valid for nonnegative
1049
+ matrices. Namely, if `A` is a nonnegative square matrix with
1050
+ Perron-Frobenius eigenvalue `\lambda` then the following inequality is valid
1051
+ for any vector `x`
1052
+
1053
+ .. MATH::
1054
+
1055
+ \min_i \frac{(Ax)_i}{x_i} \leq \lambda \leq \max_i \frac{(Ax)_i}{x_i}
1056
+
1057
+ .. NOTE::
1058
+
1059
+ The speed of convergence of the algorithm is governed by the spectral
1060
+ gap (the distance to the second largest modulus of other eigenvalues).
1061
+ If this gap is small, then this function might not be appropriate.
1062
+
1063
+ The algorithm is not smart and not parallel! It uses basic interval
1064
+ arithmetic and native floating point arithmetic.
1065
+
1066
+ EXAMPLES::
1067
+
1068
+ sage: from sage.graphs.base.static_sparse_graph import spectral_radius
1069
+
1070
+ sage: G = DiGraph([(0,0),(0,1),(1,0)], loops=True)
1071
+ sage: phi = (RR(1) + RR(5).sqrt() ) / 2
1072
+ sage: phi # abs tol 1e-14
1073
+ 1.618033988749895
1074
+ sage: e_min, e_max = spectral_radius(G, 1e-14)
1075
+ sage: e_min, e_max # abs tol 1e-14
1076
+ (1.618033988749894, 1.618033988749896)
1077
+ sage: (e_max - e_min) # abs tol 1e-14
1078
+ 1e-14
1079
+ sage: e_min < phi < e_max
1080
+ True
1081
+
1082
+ This function also works for graphs::
1083
+
1084
+ sage: G = Graph([(0,1),(0,2),(1,2),(1,3),(2,4),(3,4)])
1085
+ sage: e_min, e_max = spectral_radius(G, 1e-14)
1086
+ sage: e = max(G.adjacency_matrix().charpoly().roots(AA, multiplicities=False)) # needs sage.modules sage.rings.number_field
1087
+ sage: e_min < e < e_max # needs sage.modules sage.rings.number_field sage.symbolic
1088
+ True
1089
+
1090
+ sage: G.spectral_radius() # abs tol 1e-9
1091
+ (2.48119430408, 2.4811943041)
1092
+
1093
+ A larger example::
1094
+
1095
+ sage: # needs sage.modules
1096
+ sage: G = DiGraph()
1097
+ sage: G.add_edges((i,i+1) for i in range(200))
1098
+ sage: G.add_edge(200,0)
1099
+ sage: G.add_edge(1,0)
1100
+ sage: e_min, e_max = spectral_radius(G, 0.00001)
1101
+ sage: p = G.adjacency_matrix(sparse=True).charpoly()
1102
+ sage: p
1103
+ x^201 - x^199 - 1
1104
+ sage: r = p.roots(AA, multiplicities=False)[0] # needs sage.rings.number_field
1105
+ sage: e_min < r < e_max # needs sage.rings.number_field
1106
+ True
1107
+
1108
+ A much larger example::
1109
+
1110
+ sage: G = DiGraph(100000)
1111
+ sage: r = list(range(100000))
1112
+ sage: while not G.is_strongly_connected():
1113
+ ....: shuffle(r)
1114
+ ....: G.add_edges(enumerate(r), loops=False)
1115
+ sage: spectral_radius(G, 1e-10) # random
1116
+ (1.9997956006500042, 1.9998043797692782)
1117
+
1118
+ The algorithm takes care of multiple edges::
1119
+
1120
+ sage: G = DiGraph(2,loops=True,multiedges=True)
1121
+ sage: G.add_edges([(0,0),(0,0),(0,1),(1,0)])
1122
+ sage: spectral_radius(G, 1e-14) # abs tol 1e-14
1123
+ (2.414213562373094, 2.414213562373095)
1124
+ sage: max(G.adjacency_matrix().eigenvalues(AA)) # needs sage.modules sage.rings.number_field
1125
+ 2.414213562373095?
1126
+
1127
+ Some bipartite graphs::
1128
+
1129
+ sage: G = Graph([(0,1),(0,3),(2,3)])
1130
+ sage: G.spectral_radius() # abs tol 1e-10
1131
+ (1.6180339887253428, 1.6180339887592732)
1132
+
1133
+ sage: G = DiGraph([(0,1),(0,3),(2,3),(3,0),(1,0),(1,2)])
1134
+ sage: G.spectral_radius() # abs tol 1e-10
1135
+ (1.5537739740270458, 1.553773974033029)
1136
+
1137
+ sage: G = graphs.CompleteBipartiteGraph(1,3)
1138
+ sage: G.spectral_radius() # abs tol 1e-10
1139
+ (1.7320508075688772, 1.7320508075688774)
1140
+
1141
+ TESTS::
1142
+
1143
+ sage: from sage.graphs.base.static_sparse_graph import spectral_radius
1144
+
1145
+ sage: Graph(1).spectral_radius()
1146
+ (0.0, 0.0)
1147
+ sage: Graph([(0,0)], loops=True).spectral_radius()
1148
+ (1.0, 1.0)
1149
+
1150
+ sage: spectral_radius(Graph([(0,1),(0,2)]), 1e-20)
1151
+ Traceback (most recent call last):
1152
+ ...
1153
+ ValueError: precision (=1.00000000000000e-20) is too small
1154
+
1155
+ sage: for _ in range(100): # needs sage.modules sage.rings.number_field
1156
+ ....: G = digraphs.RandomDirectedGNM(10,35)
1157
+ ....: if not G.is_strongly_connected():
1158
+ ....: continue
1159
+ ....: e = max(G.adjacency_matrix().charpoly().roots(AA,multiplicities=False))
1160
+ ....: e_min, e_max = G.spectral_radius(1e-13)
1161
+ ....: assert e_min < e < e_max
1162
+
1163
+ sage: spectral_radius(Graph(), 1e-10)
1164
+ Traceback (most recent call last):
1165
+ ...
1166
+ ValueError: empty graph
1167
+
1168
+ sage: G = DiGraph([(0,1),(1,2),(2,0),(2,0)], multiedges=True)
1169
+ sage: G.spectral_radius()
1170
+ Traceback (most recent call last):
1171
+ ...
1172
+ ValueError: the graph must be aperiodic
1173
+ """
1174
+ if not G:
1175
+ raise ValueError("empty graph")
1176
+ if G.is_directed():
1177
+ if not G.is_strongly_connected():
1178
+ raise ValueError("G must be strongly connected")
1179
+ elif not G.is_connected():
1180
+ raise ValueError("G must be connected")
1181
+
1182
+ cdef double e_min, e_max
1183
+
1184
+ if G.num_verts() == 1:
1185
+ e_min = e_max = G.num_edges()
1186
+ return (e_min, e_max)
1187
+
1188
+ is_bipartite, colors = G.is_bipartite(certificate=True)
1189
+ if is_bipartite:
1190
+ # NOTE: for bipartite graph there are two eigenvalues of maximum modulus
1191
+ # and the iteration is likely to reach a cycle of length 2 and hence the
1192
+ # algorithm never terminate. Here we compute the "square" reduced to
1193
+ # one component of the bipartition.
1194
+ from sage.graphs.digraph import DiGraph
1195
+ H = DiGraph(loops=True, multiedges=True)
1196
+ if G.is_directed():
1197
+ neighbors_iterator = G.neighbor_out_iterator
1198
+ else:
1199
+ neighbors_iterator = G.neighbor_iterator
1200
+ for u0 in G:
1201
+ if colors[u0] == 0:
1202
+ for u1 in neighbors_iterator(u0):
1203
+ for u2 in neighbors_iterator(u1):
1204
+ H.add_edge(u0, u2)
1205
+
1206
+ e_min, e_max = spectral_radius(H, prec)
1207
+ return sqrt(e_min), sqrt(e_max)
1208
+
1209
+ cdef double c_prec = prec
1210
+ if 1+c_prec/2 == 1:
1211
+ raise ValueError("precision (={!r}) is too small".format(prec))
1212
+
1213
+ # test if the graph is aperiodic
1214
+ from sage.graphs.digraph import DiGraph
1215
+ if not DiGraph(G).is_aperiodic():
1216
+ raise ValueError("the graph must be aperiodic")
1217
+
1218
+ # make a copy of G if needed to obtain a static sparse graph
1219
+ # NOTE: the following potentially copies the labels of the graph which is
1220
+ # completely useless for the computation!
1221
+ cdef short_digraph g
1222
+ G = G.copy(immutable=True)
1223
+ g[0] = (<StaticSparseCGraph> (<StaticSparseBackend> G._backend)._cg).g[0]
1224
+
1225
+ cdef size_t n = <size_t>g.n
1226
+ cdef size_t m = <size_t>g.m
1227
+ cdef uint32_t ** neighbors = g.neighbors
1228
+
1229
+ # v1 and v2 are two arrays of length n, allocated as one array
1230
+ # of length 2n for efficiency.
1231
+ cdef double * vmem = <double *>check_allocarray(2*n, sizeof(double))
1232
+ cdef double * v1 = vmem
1233
+ cdef double * v2 = vmem + n
1234
+ cdef double * v3
1235
+
1236
+ cdef size_t i
1237
+ cdef uint32_t *p
1238
+ cdef double s
1239
+
1240
+ for i in range(n):
1241
+ v1[i] = 1
1242
+ s = n
1243
+
1244
+ cdef int old_rounding = fegetround()
1245
+ e_max = m
1246
+ e_min = 0
1247
+ try:
1248
+ sig_on()
1249
+ while (e_max - e_min) > e_max * c_prec:
1250
+ # renormalize
1251
+ s = n/s
1252
+ for i in range(n):
1253
+ v1[i] *= s
1254
+
1255
+ # computing e_max (with upward rounding)
1256
+ e_max = 0
1257
+ fesetround(FE_UPWARD)
1258
+ p = neighbors[0]
1259
+ for i in range(n):
1260
+ v2[i] = 0
1261
+ while p < neighbors[i + 1]:
1262
+ v2[i] += v1[p[0]]
1263
+ p += 1
1264
+ e = v2[i] / v1[i]
1265
+ if e > e_max:
1266
+ e_max = e
1267
+
1268
+ # computing e_min (with downward rounding)
1269
+ e_min = m
1270
+ fesetround(FE_DOWNWARD)
1271
+ p = neighbors[0]
1272
+ for i in range(n):
1273
+ v2[i] = 0
1274
+ while p < neighbors[i + 1]:
1275
+ v2[i] += v1[p[0]]
1276
+ p += 1
1277
+ s += v2[i]
1278
+ e = v2[i] / v1[i]
1279
+ if e < e_min:
1280
+ e_min = e
1281
+
1282
+ # computing the next vector (with nearest rounding)
1283
+ fesetround(FE_TONEAREST)
1284
+ s = 0
1285
+ p = neighbors[0]
1286
+ for i in range(n):
1287
+ v2[i] = 0
1288
+ while p < neighbors[i + 1]:
1289
+ v2[i] += v1[p[0]]
1290
+ p += 1
1291
+ s += v2[i]
1292
+ v3 = v1
1293
+ v1 = v2
1294
+ v2 = v3
1295
+
1296
+ sig_off()
1297
+ finally:
1298
+ # be sure that the rounding is back to default
1299
+ fesetround(old_rounding)
1300
+
1301
+ # and that the memory is freed
1302
+ sig_free(vmem)
1303
+
1304
+ return (e_min, e_max)