Skip to content

Commit

Permalink
gh-35266: sage.graphs: Add # optional doctest tags for modulariza…
Browse files Browse the repository at this point in the history
…tion

    
<!-- ^^^^^
Please provide a concise, informative and self-explanatory title.
Don't put issue numbers in there, do this in the PR body below.
For example, instead of "Fixes #1234" use "Introduce new method to
calculate 1+1"
-->
### 📚 Description

<!-- Describe your changes here in detail -->
<!-- Why is this change required? What problem does it solve? -->
<!-- If it resolves an open issue, please link to the issue here. For
example "Closes #1337" -->
These tags make it possible/meaningful to doctest `sage.graphs` even
when some "standard" packages (`networkx`, `numpy`, the modularized
distribution packaging providing `sage.groups`) are not installed.

- Part of #29705
- Split out from #34998

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->
<!-- If your change requires a documentation PR, please link it
appropriately -->
<!-- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->

- [x] I have made sure that the title is self-explanatory and the
description concisely explains the PR.
- [x] I have linked an issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation accordingly.

### ⌛ Dependencies
<!-- List all open pull requests that this PR logically depends on -->
<!--
- #xyz: short description why this is a dependency
- #abc: ...
-->
This is just one commit on top of:
- #35237
    
URL: #35266
Reported by: Matthias Köppe
Reviewer(s): David Coudert, Dima Pasechnik, Matthias Köppe
  • Loading branch information
Release Manager committed Mar 31, 2023
2 parents 3142be9 + 8691cb5 commit 428b148
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 169 deletions.
4 changes: 2 additions & 2 deletions src/sage/graphs/base/static_sparse_graph.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,8 @@ def tarjan_strongly_connected_components(G):
Checking against NetworkX::
sage: import networkx
sage: for i in range(10): # long time
sage: import networkx # optional - networkx
sage: for i in range(10): # long time # optional - networkx
....: g = digraphs.RandomDirectedGNP(100,.05)
....: h = g.networkx_graph()
....: scc1 = g.strongly_connected_components()
Expand Down
8 changes: 4 additions & 4 deletions src/sage/graphs/bipartite_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,10 @@ class BipartiteGraph(Graph):
#. From a NetworkX bipartite graph::
sage: import networkx
sage: G = graphs.OctahedralGraph()
sage: N = networkx.make_clique_bipartite(G.networkx_graph())
sage: B = BipartiteGraph(N)
sage: import networkx # optional - networkx
sage: G = graphs.OctahedralGraph() # optional - networkx
sage: N = networkx.make_clique_bipartite(G.networkx_graph()) # optional - networkx
sage: B = BipartiteGraph(N) # optional - networkx
TESTS:
Expand Down
10 changes: 5 additions & 5 deletions src/sage/graphs/centrality.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ def centrality_betweenness(G, bint exact=False, bint normalize=True):
Compare with NetworkX::
sage: import networkx
sage: g = graphs.RandomGNP(100, .2)
sage: nw = networkx.betweenness_centrality(g.networkx_graph())
sage: sg = centrality_betweenness(g)
sage: max(abs(nw[x] - sg[x]) for x in g) # abs tol 1e-10
sage: import networkx # optional - networkx
sage: g = graphs.RandomGNP(100, .2) # optional - networkx
sage: nw = networkx.betweenness_centrality(g.networkx_graph()) # optional - networkx
sage: sg = centrality_betweenness(g) # optional - networkx
sage: max(abs(nw[x] - sg[x]) for x in g) # abs tol 1e-10 # optional - networkx
0
Stupid cases::
Expand Down
38 changes: 19 additions & 19 deletions src/sage/graphs/digraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,17 +427,17 @@ class DiGraph(GenericGraph):
#. A NetworkX MultiDiGraph::
sage: import networkx
sage: g = networkx.MultiDiGraph({0: [1, 2, 3], 2: [4]})
sage: DiGraph(g)
sage: import networkx # optional - networkx
sage: g = networkx.MultiDiGraph({0: [1, 2, 3], 2: [4]}) # optional - networkx
sage: DiGraph(g) # optional - networkx
Multi-digraph on 5 vertices
#. A NetworkX digraph::
sage: import networkx
sage: g = networkx.DiGraph({0: [1, 2, 3], 2: [4]})
sage: DiGraph(g)
sage: import networkx # optional - networkx
sage: g = networkx.DiGraph({0: [1, 2, 3], 2: [4]}) # optional - networkx
sage: DiGraph(g) # optional - networkx
Digraph on 5 vertices
#. An igraph directed Graph (see also
Expand Down Expand Up @@ -477,18 +477,18 @@ class DiGraph(GenericGraph):
Demonstrate that digraphs using the static backend are equal to mutable
graphs but can be used as dictionary keys::
sage: import networkx
sage: g = networkx.DiGraph({0:[1,2,3], 2:[4]})
sage: G = DiGraph(g)
sage: G_imm = DiGraph(G, data_structure="static_sparse")
sage: H_imm = DiGraph(G, data_structure="static_sparse")
sage: H_imm is G_imm
sage: import networkx # optional - networkx
sage: g = networkx.DiGraph({0:[1,2,3], 2:[4]}) # optional - networkx
sage: G = DiGraph(g) # optional - networkx
sage: G_imm = DiGraph(G, data_structure="static_sparse") # optional - networkx
sage: H_imm = DiGraph(G, data_structure="static_sparse") # optional - networkx
sage: H_imm is G_imm # optional - networkx
False
sage: H_imm == G_imm == G
sage: H_imm == G_imm == G # optional - networkx
True
sage: {G_imm:1}[H_imm]
sage: {G_imm:1}[H_imm] # optional - networkx
1
sage: {G_imm:1}[G]
sage: {G_imm:1}[G] # optional - networkx
Traceback (most recent call last):
...
TypeError: This graph is mutable, and thus not hashable. Create an
Expand All @@ -498,10 +498,10 @@ class DiGraph(GenericGraph):
specifying the ``immutable`` optional argument (not only by
``data_structure='static_sparse'`` as above)::
sage: J_imm = DiGraph(G, immutable=True)
sage: J_imm == G_imm
sage: J_imm = DiGraph(G, immutable=True) # optional - networkx
sage: J_imm == G_imm # optional - networkx
True
sage: type(J_imm._backend) == type(G_imm._backend)
sage: type(J_imm._backend) == type(G_imm._backend) # optional - networkx
True
From a list of vertices and a list of edges::
Expand All @@ -513,7 +513,7 @@ class DiGraph(GenericGraph):
Check that :trac:`27505` is fixed::
sage: DiGraph(DiGraph().networkx_graph(), weighted=None, format='NX')
sage: DiGraph(DiGraph().networkx_graph(), weighted=None, format='NX') # optional - networkx
Digraph on 0 vertices
"""
_directed = True
Expand Down
76 changes: 38 additions & 38 deletions src/sage/graphs/generators/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,12 @@ def CycleGraph(n):
Compare plotting using the predefined layout and networkx::
sage: import networkx
sage: n = networkx.cycle_graph(23)
sage: spring23 = Graph(n)
sage: posdict23 = graphs.CycleGraph(23)
sage: spring23.show() # long time
sage: posdict23.show() # long time
sage: import networkx # optional - networkx
sage: n = networkx.cycle_graph(23) # optional - networkx
sage: spring23 = Graph(n) # optional - networkx
sage: posdict23 = graphs.CycleGraph(23) # optional - networkx
sage: spring23.show() # long time # optional - networkx
sage: posdict23.show() # long time # optional - networkx
We next view many cycle graphs as a Sage graphics array. First we use the
``CycleGraph`` constructor, which fills in the position dictionary::
Expand All @@ -275,17 +275,17 @@ def CycleGraph(n):
sage: g = []
sage: j = []
sage: for i in range(9):
sage: for i in range(9): # optional - networkx
....: spr = networkx.cycle_graph(i+3)
....: k = Graph(spr)
....: g.append(k)
sage: for i in range(3):
sage: for i in range(3): # optional - networkx
....: n = []
....: for m in range(3):
....: n.append(g[3*i + m].plot(vertex_size=50, vertex_labels=False))
....: j.append(n)
sage: G = graphics_array(j)
sage: G.show() # long time
sage: G = graphics_array(j) # optional - networkx
sage: G.show() # long time # optional - networkx
TESTS:
Expand Down Expand Up @@ -349,27 +349,27 @@ def CompleteGraph(n):
We compare to plotting with the spring-layout algorithm::
sage: import networkx
sage: import networkx # optional - networkx
sage: g = []
sage: j = []
sage: for i in range(9):
sage: for i in range(9): # optional - networkx
....: spr = networkx.complete_graph(i+3)
....: k = Graph(spr)
....: g.append(k)
sage: for i in range(3):
sage: for i in range(3): # optional - networkx
....: n = []
....: for m in range(3):
....: n.append(g[3*i + m].plot(vertex_size=50, vertex_labels=False))
....: j.append(n)
sage: G = graphics_array(j)
sage: G.show() # long time
sage: G = graphics_array(j) # optional - networkx
sage: G.show() # long time # optional - networkx
Compare the constructors (results will vary)::
sage: import networkx
sage: t = cputime()
sage: n = networkx.complete_graph(389); spring389 = Graph(n)
sage: cputime(t) # random
sage: import networkx # optional - networkx
sage: t = cputime() # optional - networkx
sage: n = networkx.complete_graph(389); spring389 = Graph(n) # optional - networkx
sage: cputime(t) # random # optional - networkx
0.59203700000000126
sage: t = cputime()
sage: posdict389 = graphs.CompleteGraph(389)
Expand All @@ -378,11 +378,11 @@ def CompleteGraph(n):
We compare plotting::
sage: import networkx
sage: n = networkx.complete_graph(23)
sage: import networkx # optional - networkx
sage: n = networkx.complete_graph(23) # optional - networkx
sage: spring23 = Graph(n)
sage: posdict23 = graphs.CompleteGraph(23)
sage: spring23.show() # long time
sage: spring23.show() # long time # optional - networkx
sage: posdict23.show() # long time
"""
G = Graph(n, name="Complete graph")
Expand Down Expand Up @@ -438,19 +438,19 @@ def CompleteBipartiteGraph(p, q, set_position=True):
Two ways of constructing the complete bipartite graph, using different
layout algorithms::
sage: import networkx
sage: n = networkx.complete_bipartite_graph(389, 157); spring_big = Graph(n) # long time
sage: import networkx # optional - networkx
sage: n = networkx.complete_bipartite_graph(389, 157); spring_big = Graph(n) # long time # optional - networkx
sage: posdict_big = graphs.CompleteBipartiteGraph(389, 157) # long time
Compare the plotting::
sage: n = networkx.complete_bipartite_graph(11, 17)
sage: spring_med = Graph(n)
sage: n = networkx.complete_bipartite_graph(11, 17) # optional - networkx
sage: spring_med = Graph(n) # optional - networkx
sage: posdict_med = graphs.CompleteBipartiteGraph(11, 17)
Notice here how the spring-layout tends to center the nodes of `n1`::
sage: spring_med.show() # long time
sage: spring_med.show() # long time # optional - networkx
sage: posdict_med.show() # long time
View many complete bipartite graphs with a Sage Graphics Array, with this
Expand All @@ -473,17 +473,17 @@ def CompleteBipartiteGraph(p, q, set_position=True):
sage: g = []
sage: j = []
sage: for i in range(9):
sage: for i in range(9): # optional - networkx
....: spr = networkx.complete_bipartite_graph(i+1,4)
....: k = Graph(spr)
....: g.append(k)
sage: for i in range(3):
sage: for i in range(3): # optional - networkx
....: n = []
....: for m in range(3):
....: n.append(g[3*i + m].plot(vertex_size=50, vertex_labels=False))
....: j.append(n)
sage: G = graphics_array(j)
sage: G.show() # long time
sage: G = graphics_array(j) # optional - networkx
sage: G.show() # long time # optional - networkx
:trac:`12155`::
Expand Down Expand Up @@ -933,9 +933,9 @@ def GridGraph(dim_list):
sage: dim = [randint(1,4) for i in range(4)]
sage: g = graphs.GridGraph(dim)
sage: import networkx
sage: h = Graph( networkx.grid_graph(list(dim)) )
sage: g.is_isomorphic(h)
sage: import networkx # optional - networkx
sage: h = Graph(networkx.grid_graph(list(dim))) # optional - networkx
sage: g.is_isomorphic(h) # optional - networkx
True
Trivial cases::
Expand Down Expand Up @@ -1223,14 +1223,14 @@ def StarGraph(n):
EXAMPLES::
sage: import networkx
sage: import networkx # optional - networkx
Compare the plots::
sage: n = networkx.star_graph(23)
sage: spring23 = Graph(n)
sage: n = networkx.star_graph(23) # optional - networkx
sage: spring23 = Graph(n) # optional - networkx
sage: posdict23 = graphs.StarGraph(23)
sage: spring23.show() # long time
sage: spring23.show() # long time # optional - networkx
sage: posdict23.show() # long time
View many star graphs as a Sage Graphics Array
Expand Down
24 changes: 12 additions & 12 deletions src/sage/graphs/generators/families.py
Original file line number Diff line number Diff line change
Expand Up @@ -1076,11 +1076,11 @@ def CirculantGraph(n, adjacency):
EXAMPLES: Compare plotting using the predefined layout and
networkx::
sage: import networkx
sage: n = networkx.cycle_graph(23)
sage: spring23 = Graph(n)
sage: import networkx # optional - networkx
sage: n = networkx.cycle_graph(23) # optional - networkx
sage: spring23 = Graph(n) # optional - networkx
sage: posdict23 = graphs.CirculantGraph(23,2)
sage: spring23.show() # long time
sage: spring23.show() # long time # optional - networkx
sage: posdict23.show() # long time
We next view many cycle graphs as a Sage graphics array. First we
Expand Down Expand Up @@ -3409,29 +3409,29 @@ def WheelGraph(n):
Next, using the spring-layout algorithm::
sage: import networkx
sage: import networkx # optional - networkx
sage: g = []
sage: j = []
sage: for i in range(9):
sage: for i in range(9): # optional - networkx
....: spr = networkx.wheel_graph(i+3)
....: k = Graph(spr)
....: g.append(k)
...
sage: for i in range(3):
sage: for i in range(3): # optional - networkx
....: n = []
....: for m in range(3):
....: n.append(g[3*i + m].plot(vertex_size=50, vertex_labels=False))
....: j.append(n)
...
sage: G = graphics_array(j)
sage: G.show() # long time
sage: G = graphics_array(j) # optional - networkx
sage: G.show() # long time # optional - networkx
Compare the plotting::
sage: n = networkx.wheel_graph(23)
sage: spring23 = Graph(n)
sage: n = networkx.wheel_graph(23) # optional - networkx
sage: spring23 = Graph(n) # optional - networkx
sage: posdict23 = graphs.WheelGraph(23)
sage: spring23.show() # long time
sage: spring23.show() # long time # optional - networkx
sage: posdict23.show() # long time
"""
from sage.graphs.generators.basic import CycleGraph
Expand Down
12 changes: 6 additions & 6 deletions src/sage/graphs/generators/platonic_solids.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,24 @@ def TetrahedralGraph():
The following example requires networkx::
sage: import networkx as NX
sage: import networkx as NX # optional - networkx
Compare this Tetrahedral, Wheel(4), Complete(4), and the Tetrahedral plotted
with the spring-layout algorithm below in a Sage graphics array::
sage: tetra_pos = graphs.TetrahedralGraph()
sage: tetra_spring = Graph(NX.tetrahedral_graph())
sage: tetra_spring = Graph(NX.tetrahedral_graph()) # optional - networkx
sage: wheel = graphs.WheelGraph(4)
sage: complete = graphs.CompleteGraph(4)
sage: g = [tetra_pos, tetra_spring, wheel, complete]
sage: g = [tetra_pos, tetra_spring, wheel, complete] # optional - networkx
sage: j = []
sage: for i in range(2):
sage: for i in range(2): # optional - networkx
....: n = []
....: for m in range(2):
....: n.append(g[i + m].plot(vertex_size=50, vertex_labels=False))
....: j.append(n)
sage: G = graphics_array(j)
sage: G.show() # long time
sage: G = graphics_array(j) # optional - networkx
sage: G.show() # long time # optional - networkx
"""
edges = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
pos = {0: (0, 0),
Expand Down
Loading

0 comments on commit 428b148

Please sign in to comment.