Skip to content

Commit d7b2cb2

Browse files
author
Matthias Koeppe
committed
sage.graphs: Use more block # needs
1 parent bc9bb1b commit d7b2cb2

File tree

5 files changed

+187
-159
lines changed

5 files changed

+187
-159
lines changed

src/sage/graphs/base/c_graph.pyx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,12 +1576,13 @@ cdef class CGraphBackend(GenericGraphBackend):
15761576
15771577
We check that the bug described in :trac:`8406` is gone::
15781578
1579+
sage: # needs sage.rings.finite_rings
15791580
sage: G = Graph()
1580-
sage: R.<a> = GF(3**3) # needs sage.rings.finite_rings
1581-
sage: S.<x> = R[] # needs sage.rings.finite_rings
1582-
sage: G.add_vertex(a**2) # needs sage.rings.finite_rings
1583-
sage: G.add_vertex(x) # needs sage.rings.finite_rings
1584-
sage: G.vertices(sort=True) # needs sage.rings.finite_rings
1581+
sage: R.<a> = GF(3**3)
1582+
sage: S.<x> = R[]
1583+
sage: G.add_vertex(a**2)
1584+
sage: G.add_vertex(x)
1585+
sage: G.vertices(sort=True)
15851586
[a^2, x]
15861587
15871588
And that the bug described in :trac:`9610` is gone::

src/sage/graphs/bipartite_graph.py

Lines changed: 68 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,17 @@ class BipartiteGraph(Graph):
198198
199199
#. From a reduced adjacency matrix::
200200
201-
sage: M = Matrix([(1,1,1,0,0,0,0), (1,0,0,1,1,0,0), # needs sage.modules
201+
sage: # needs sage.modules
202+
sage: M = Matrix([(1,1,1,0,0,0,0), (1,0,0,1,1,0,0),
202203
....: (0,1,0,1,0,1,0), (1,1,0,1,0,0,1)])
203-
sage: M # needs sage.modules
204+
sage: M
204205
[1 1 1 0 0 0 0]
205206
[1 0 0 1 1 0 0]
206207
[0 1 0 1 0 1 0]
207208
[1 1 0 1 0 0 1]
208-
sage: H = BipartiteGraph(M); H # needs sage.modules
209+
sage: H = BipartiteGraph(M); H
209210
Bipartite graph on 11 vertices
210-
sage: H.edges(sort=True) # needs sage.modules
211+
sage: H.edges(sort=True)
211212
[(0, 7, None),
212213
(0, 8, None),
213214
(0, 10, None),
@@ -244,13 +245,14 @@ class BipartiteGraph(Graph):
244245
245246
::
246247
247-
sage: F.<a> = GF(4) # needs sage.modules sage.rings.finite_rings
248-
sage: MS = MatrixSpace(F, 2, 3) # needs sage.modules sage.rings.finite_rings
249-
sage: M = MS.matrix([[0, 1, a + 1], [a, 1, 1]]) # needs sage.modules sage.rings.finite_rings
250-
sage: B = BipartiteGraph(M, weighted=True, sparse=True) # needs sage.modules sage.rings.finite_rings
251-
sage: B.edges(sort=True) # needs sage.modules sage.rings.finite_rings
248+
sage: # needs sage.modules sage.rings.finite_rings
249+
sage: F.<a> = GF(4)
250+
sage: MS = MatrixSpace(F, 2, 3)
251+
sage: M = MS.matrix([[0, 1, a + 1], [a, 1, 1]])
252+
sage: B = BipartiteGraph(M, weighted=True, sparse=True)
253+
sage: B.edges(sort=True)
252254
[(0, 4, a), (1, 3, 1), (1, 4, 1), (2, 3, a + 1), (2, 4, 1)]
253-
sage: B.weighted() # needs sage.modules sage.rings.finite_rings
255+
sage: B.weighted()
254256
True
255257
256258
#. From an alist file::
@@ -311,10 +313,11 @@ class BipartiteGraph(Graph):
311313
312314
#. From a NetworkX bipartite graph::
313315
314-
sage: import networkx # needs networkx
315-
sage: G = graphs.OctahedralGraph() # needs networkx
316-
sage: N = networkx.make_clique_bipartite(G.networkx_graph()) # needs networkx
317-
sage: B = BipartiteGraph(N) # needs networkx
316+
sage: # needs networkx
317+
sage: import networkx
318+
sage: G = graphs.OctahedralGraph()
319+
sage: N = networkx.make_clique_bipartite(G.networkx_graph())
320+
sage: B = BipartiteGraph(N)
318321
319322
TESTS:
320323
@@ -328,15 +331,16 @@ class BipartiteGraph(Graph):
328331
Ensure that we can construct a ``BipartiteGraph`` with isolated vertices via
329332
the reduced adjacency matrix (:trac:`10356`)::
330333
331-
sage: a = BipartiteGraph(matrix(2, 2, [1, 0, 1, 0])) # needs sage.modules
332-
sage: a # needs sage.modules
334+
sage: # needs sage.modules
335+
sage: a = BipartiteGraph(matrix(2, 2, [1, 0, 1, 0]))
336+
sage: a
333337
Bipartite graph on 4 vertices
334-
sage: a.vertices(sort=True) # needs sage.modules
338+
sage: a.vertices(sort=True)
335339
[0, 1, 2, 3]
336-
sage: g = BipartiteGraph(matrix(4, 4, [1] * 4 + [0] * 12)) # needs sage.modules
337-
sage: g.vertices(sort=True) # needs sage.modules
340+
sage: g = BipartiteGraph(matrix(4, 4, [1] * 4 + [0] * 12))
341+
sage: g.vertices(sort=True)
338342
[0, 1, 2, 3, 4, 5, 6, 7]
339-
sage: sorted(g.left.union(g.right)) # needs sage.modules
343+
sage: sorted(g.left.union(g.right))
340344
[0, 1, 2, 3, 4, 5, 6, 7]
341345
342346
Make sure that loops are not allowed (:trac:`23275`)::
@@ -1806,19 +1810,20 @@ def save_afile(self, fname):
18061810
18071811
EXAMPLES::
18081812
1809-
sage: M = Matrix([(1,1,1,0,0,0,0), (1,0,0,1,1,0,0), # needs sage.modules
1813+
sage: # needs sage.modules
1814+
sage: M = Matrix([(1,1,1,0,0,0,0), (1,0,0,1,1,0,0),
18101815
....: (0,1,0,1,0,1,0), (1,1,0,1,0,0,1)])
1811-
sage: M # needs sage.modules
1816+
sage: M
18121817
[1 1 1 0 0 0 0]
18131818
[1 0 0 1 1 0 0]
18141819
[0 1 0 1 0 1 0]
18151820
[1 1 0 1 0 0 1]
1816-
sage: b = BipartiteGraph(M) # needs sage.modules
1817-
sage: import tempfile # needs sage.modules
1818-
sage: with tempfile.NamedTemporaryFile() as f: # needs sage.modules
1821+
sage: b = BipartiteGraph(M)
1822+
sage: import tempfile
1823+
sage: with tempfile.NamedTemporaryFile() as f:
18191824
....: b.save_afile(f.name)
18201825
....: b2 = BipartiteGraph(f.name)
1821-
sage: b.is_isomorphic(b2) # needs sage.modules
1826+
sage: b.is_isomorphic(b2)
18221827
True
18231828
18241829
TESTS::
@@ -1923,54 +1928,57 @@ def reduced_adjacency_matrix(self, sparse=True, *, base_ring=None, **kwds):
19231928
Bipartite graphs that are not weighted will return a matrix over ZZ,
19241929
unless a base ring is specified::
19251930
1926-
sage: M = Matrix([(1,1,1,0,0,0,0), (1,0,0,1,1,0,0), # needs sage.modules
1931+
sage: # needs sage.modules
1932+
sage: M = Matrix([(1,1,1,0,0,0,0), (1,0,0,1,1,0,0),
19271933
....: (0,1,0,1,0,1,0), (1,1,0,1,0,0,1)])
1928-
sage: B = BipartiteGraph(M) # needs sage.modules
1929-
sage: N = B.reduced_adjacency_matrix(); N # needs sage.modules
1934+
sage: B = BipartiteGraph(M)
1935+
sage: N = B.reduced_adjacency_matrix(); N
19301936
[1 1 1 0 0 0 0]
19311937
[1 0 0 1 1 0 0]
19321938
[0 1 0 1 0 1 0]
19331939
[1 1 0 1 0 0 1]
1934-
sage: N == M # needs sage.modules
1940+
sage: N == M
19351941
True
1936-
sage: N[0,0].parent() # needs sage.modules
1942+
sage: N[0,0].parent()
19371943
Integer Ring
1938-
sage: N2 = B.reduced_adjacency_matrix(base_ring=RDF); N2 # needs sage.modules
1944+
sage: N2 = B.reduced_adjacency_matrix(base_ring=RDF); N2
19391945
[1.0 1.0 1.0 0.0 0.0 0.0 0.0]
19401946
[1.0 0.0 0.0 1.0 1.0 0.0 0.0]
19411947
[0.0 1.0 0.0 1.0 0.0 1.0 0.0]
19421948
[1.0 1.0 0.0 1.0 0.0 0.0 1.0]
1943-
sage: N2[0, 0].parent() # needs sage.modules
1949+
sage: N2[0, 0].parent()
19441950
Real Double Field
19451951
19461952
Multi-edge graphs also return a matrix over ZZ,
19471953
unless a base ring is specified::
19481954
1949-
sage: M = Matrix([(1,1,2,0,0), (0,2,1,1,1), (0,1,2,1,1)]) # needs sage.modules
1950-
sage: B = BipartiteGraph(M, multiedges=True, sparse=True) # needs sage.modules
1951-
sage: N = B.reduced_adjacency_matrix() # needs sage.modules
1952-
sage: N == M # needs sage.modules
1955+
sage: # needs sage.modules
1956+
sage: M = Matrix([(1,1,2,0,0), (0,2,1,1,1), (0,1,2,1,1)])
1957+
sage: B = BipartiteGraph(M, multiedges=True, sparse=True)
1958+
sage: N = B.reduced_adjacency_matrix()
1959+
sage: N == M
19531960
True
1954-
sage: N[0,0].parent() # needs sage.modules
1961+
sage: N[0,0].parent()
19551962
Integer Ring
1956-
sage: N2 = B.reduced_adjacency_matrix(base_ring=RDF) # needs sage.modules
1957-
sage: N2[0, 0].parent() # needs sage.modules
1963+
sage: N2 = B.reduced_adjacency_matrix(base_ring=RDF)
1964+
sage: N2[0, 0].parent()
19581965
Real Double Field
19591966
19601967
Weighted graphs will return a matrix over the ring given by their
19611968
(first) weights, unless a base ring is specified::
19621969
1963-
sage: F.<a> = GF(4) # needs sage.modules sage.rings.finite_rings
1964-
sage: MS = MatrixSpace(F, 2, 3) # needs sage.modules sage.rings.finite_rings
1965-
sage: M = MS.matrix([[0, 1, a+1], [a, 1, 1]]) # needs sage.modules sage.rings.finite_rings
1966-
sage: B = BipartiteGraph(M, weighted=True, sparse=True) # needs sage.modules sage.rings.finite_rings
1967-
sage: N = B.reduced_adjacency_matrix(sparse=False) # needs sage.modules sage.rings.finite_rings
1968-
sage: N == M # needs sage.modules sage.rings.finite_rings
1970+
sage: # needs sage.modules sage.rings.finite_rings
1971+
sage: F.<a> = GF(4)
1972+
sage: MS = MatrixSpace(F, 2, 3)
1973+
sage: M = MS.matrix([[0, 1, a+1], [a, 1, 1]])
1974+
sage: B = BipartiteGraph(M, weighted=True, sparse=True)
1975+
sage: N = B.reduced_adjacency_matrix(sparse=False)
1976+
sage: N == M
19691977
True
1970-
sage: N[0,0].parent() # needs sage.modules sage.rings.finite_rings
1978+
sage: N[0,0].parent()
19711979
Finite Field in a of size 2^2
1972-
sage: N2 = B.reduced_adjacency_matrix(base_ring=F) # needs sage.modules sage.rings.finite_rings
1973-
sage: N2[0, 0].parent() # needs sage.modules sage.rings.finite_rings
1980+
sage: N2 = B.reduced_adjacency_matrix(base_ring=F)
1981+
sage: N2[0, 0].parent()
19741982
Finite Field in a of size 2^2
19751983
19761984
TESTS::
@@ -1989,11 +1997,12 @@ def reduced_adjacency_matrix(self, sparse=True, *, base_ring=None, **kwds):
19891997
An error is raised if the specified base ring is not compatible with the
19901998
type of the weights of the bipartite graph::
19911999
1992-
sage: F.<a> = GF(4) # needs sage.modules sage.rings.finite_rings
1993-
sage: MS = MatrixSpace(F, 2, 3) # needs sage.modules sage.rings.finite_rings
1994-
sage: M = MS.matrix([[0, 1, a+1], [a, 1, 1]]) # needs sage.modules sage.rings.finite_rings
1995-
sage: B = BipartiteGraph(M, weighted=True, sparse=True) # needs sage.modules sage.rings.finite_rings
1996-
sage: B.reduced_adjacency_matrix(base_ring=RDF) # needs sage.modules sage.rings.finite_rings
2000+
sage: # needs sage.modules sage.rings.finite_rings
2001+
sage: F.<a> = GF(4)
2002+
sage: MS = MatrixSpace(F, 2, 3)
2003+
sage: M = MS.matrix([[0, 1, a+1], [a, 1, 1]])
2004+
sage: B = BipartiteGraph(M, weighted=True, sparse=True)
2005+
sage: B.reduced_adjacency_matrix(base_ring=RDF)
19972006
Traceback (most recent call last):
19982007
...
19992008
TypeError: float() argument must be a string or a ...number, not 'sage.rings.finite_rings.element_givaro.FiniteField_givaroElement'
@@ -2295,10 +2304,11 @@ def vertex_cover(self, algorithm="Konig", value_only=False,
22952304
22962305
The two algorithms should return the same result::
22972306
2298-
sage: g = BipartiteGraph(graphs.RandomBipartite(10, 10, .5)) # needs numpy
2299-
sage: vc1 = g.vertex_cover(algorithm="Konig") # needs numpy
2300-
sage: vc2 = g.vertex_cover(algorithm="Cliquer") # needs numpy
2301-
sage: len(vc1) == len(vc2) # needs numpy
2307+
sage: # needs numpy
2308+
sage: g = BipartiteGraph(graphs.RandomBipartite(10, 10, .5))
2309+
sage: vc1 = g.vertex_cover(algorithm="Konig")
2310+
sage: vc2 = g.vertex_cover(algorithm="Cliquer")
2311+
sage: len(vc1) == len(vc2)
23022312
True
23032313
23042314
TESTS:

src/sage/graphs/centrality.pyx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -638,34 +638,36 @@ def centrality_closeness_top_k(G, int k=1, int verbose=0):
638638
639639
The result is correct::
640640
641+
sage: # needs networkx
641642
sage: from sage.graphs.centrality import centrality_closeness_top_k
642643
sage: import random
643644
sage: n = 20
644645
sage: m = random.randint(1, n * (n - 1) / 2)
645646
sage: k = random.randint(1, n)
646-
sage: g = graphs.RandomGNM(n, m) # needs networkx
647-
sage: topk = centrality_closeness_top_k(g, k) # needs networkx
648-
sage: centr = g.centrality_closeness(algorithm='BFS') # needs networkx
649-
sage: sorted_centr = sorted(centr.values(), reverse=True) # needs networkx
650-
sage: len(topk) == min(k, len(sorted_centr)) # needs networkx
647+
sage: g = graphs.RandomGNM(n, m)
648+
sage: topk = centrality_closeness_top_k(g, k)
649+
sage: centr = g.centrality_closeness(algorithm='BFS')
650+
sage: sorted_centr = sorted(centr.values(), reverse=True)
651+
sage: len(topk) == min(k, len(sorted_centr))
651652
True
652-
sage: all(abs(topk[i][0] - sorted_centr[i]) < 1e-12 for i in range(len(topk))) # needs networkx
653+
sage: all(abs(topk[i][0] - sorted_centr[i]) < 1e-12 for i in range(len(topk)))
653654
True
654655
655656
Directed case::
656657
658+
sage: # needs networkx
657659
sage: from sage.graphs.centrality import centrality_closeness_top_k
658660
sage: import random
659661
sage: n = 20
660662
sage: m = random.randint(1, n * (n - 1))
661663
sage: k = random.randint(1, n)
662-
sage: g = digraphs.RandomDirectedGNM(n, m) # needs networkx
663-
sage: topk = centrality_closeness_top_k(g, k) # needs networkx
664-
sage: centr = g.centrality_closeness(algorithm='BFS') # needs networkx
665-
sage: sorted_centr = sorted(centr.values(), reverse=True) # needs networkx
666-
sage: len(topk) == min(k, len(sorted_centr)) # needs networkx
664+
sage: g = digraphs.RandomDirectedGNM(n, m)
665+
sage: topk = centrality_closeness_top_k(g, k)
666+
sage: centr = g.centrality_closeness(algorithm='BFS')
667+
sage: sorted_centr = sorted(centr.values(), reverse=True)
668+
sage: len(topk) == min(k, len(sorted_centr))
667669
True
668-
sage: all(abs(topk[i][0] - sorted_centr[i]) < 1e-12 for i in range(len(topk))) # needs networkx
670+
sage: all(abs(topk[i][0] - sorted_centr[i]) < 1e-12 for i in range(len(topk)))
669671
True
670672
"""
671673
cdef list res

0 commit comments

Comments
 (0)