Skip to content

Commit 5324919

Browse files
Matthias Koeppedimpase
Matthias Koeppe
authored andcommitted
sage.graphs: Add # optional - networkx etc.
1 parent 40626c6 commit 5324919

13 files changed

+169
-169
lines changed

src/sage/graphs/base/static_sparse_graph.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -732,8 +732,8 @@ def tarjan_strongly_connected_components(G):
732732
733733
Checking against NetworkX::
734734
735-
sage: import networkx
736-
sage: for i in range(10): # long time
735+
sage: import networkx # optional - networkx
736+
sage: for i in range(10): # long time # optional - networkx
737737
....: g = digraphs.RandomDirectedGNP(100,.05)
738738
....: h = g.networkx_graph()
739739
....: scc1 = g.strongly_connected_components()

src/sage/graphs/bipartite_graph.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,10 @@ class BipartiteGraph(Graph):
302302
303303
#. From a NetworkX bipartite graph::
304304
305-
sage: import networkx
306-
sage: G = graphs.OctahedralGraph()
307-
sage: N = networkx.make_clique_bipartite(G.networkx_graph())
308-
sage: B = BipartiteGraph(N)
305+
sage: import networkx # optional - networkx
306+
sage: G = graphs.OctahedralGraph() # optional - networkx
307+
sage: N = networkx.make_clique_bipartite(G.networkx_graph()) # optional - networkx
308+
sage: B = BipartiteGraph(N) # optional - networkx
309309
310310
TESTS:
311311

src/sage/graphs/centrality.pyx

+5-5
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ def centrality_betweenness(G, bint exact=False, bint normalize=True):
9999
100100
Compare with NetworkX::
101101
102-
sage: import networkx
103-
sage: g = graphs.RandomGNP(100, .2)
104-
sage: nw = networkx.betweenness_centrality(g.networkx_graph())
105-
sage: sg = centrality_betweenness(g)
106-
sage: max(abs(nw[x] - sg[x]) for x in g) # abs tol 1e-10
102+
sage: import networkx # optional - networkx
103+
sage: g = graphs.RandomGNP(100, .2) # optional - networkx
104+
sage: nw = networkx.betweenness_centrality(g.networkx_graph()) # optional - networkx
105+
sage: sg = centrality_betweenness(g) # optional - networkx
106+
sage: max(abs(nw[x] - sg[x]) for x in g) # abs tol 1e-10 # optional - networkx
107107
0
108108
109109
Stupid cases::

src/sage/graphs/digraph.py

+19-19
Original file line numberDiff line numberDiff line change
@@ -422,17 +422,17 @@ class DiGraph(GenericGraph):
422422
423423
#. A NetworkX MultiDiGraph::
424424
425-
sage: import networkx
426-
sage: g = networkx.MultiDiGraph({0: [1, 2, 3], 2: [4]})
427-
sage: DiGraph(g)
425+
sage: import networkx # optional - networkx
426+
sage: g = networkx.MultiDiGraph({0: [1, 2, 3], 2: [4]}) # optional - networkx
427+
sage: DiGraph(g) # optional - networkx
428428
Multi-digraph on 5 vertices
429429
430430
431431
#. A NetworkX digraph::
432432
433-
sage: import networkx
434-
sage: g = networkx.DiGraph({0: [1, 2, 3], 2: [4]})
435-
sage: DiGraph(g)
433+
sage: import networkx # optional - networkx
434+
sage: g = networkx.DiGraph({0: [1, 2, 3], 2: [4]}) # optional - networkx
435+
sage: DiGraph(g) # optional - networkx
436436
Digraph on 5 vertices
437437
438438
#. An igraph directed Graph (see also
@@ -472,18 +472,18 @@ class DiGraph(GenericGraph):
472472
Demonstrate that digraphs using the static backend are equal to mutable
473473
graphs but can be used as dictionary keys::
474474
475-
sage: import networkx
476-
sage: g = networkx.DiGraph({0:[1,2,3], 2:[4]})
477-
sage: G = DiGraph(g)
478-
sage: G_imm = DiGraph(G, data_structure="static_sparse")
479-
sage: H_imm = DiGraph(G, data_structure="static_sparse")
480-
sage: H_imm is G_imm
475+
sage: import networkx # optional - networkx
476+
sage: g = networkx.DiGraph({0:[1,2,3], 2:[4]}) # optional - networkx
477+
sage: G = DiGraph(g) # optional - networkx
478+
sage: G_imm = DiGraph(G, data_structure="static_sparse") # optional - networkx
479+
sage: H_imm = DiGraph(G, data_structure="static_sparse") # optional - networkx
480+
sage: H_imm is G_imm # optional - networkx
481481
False
482-
sage: H_imm == G_imm == G
482+
sage: H_imm == G_imm == G # optional - networkx
483483
True
484-
sage: {G_imm:1}[H_imm]
484+
sage: {G_imm:1}[H_imm] # optional - networkx
485485
1
486-
sage: {G_imm:1}[G]
486+
sage: {G_imm:1}[G] # optional - networkx
487487
Traceback (most recent call last):
488488
...
489489
TypeError: This graph is mutable, and thus not hashable. Create an
@@ -493,10 +493,10 @@ class DiGraph(GenericGraph):
493493
specifying the ``immutable`` optional argument (not only by
494494
``data_structure='static_sparse'`` as above)::
495495
496-
sage: J_imm = DiGraph(G, immutable=True)
497-
sage: J_imm == G_imm
496+
sage: J_imm = DiGraph(G, immutable=True) # optional - networkx
497+
sage: J_imm == G_imm # optional - networkx
498498
True
499-
sage: type(J_imm._backend) == type(G_imm._backend)
499+
sage: type(J_imm._backend) == type(G_imm._backend) # optional - networkx
500500
True
501501
502502
From a list of vertices and a list of edges::
@@ -508,7 +508,7 @@ class DiGraph(GenericGraph):
508508
509509
Check that :trac:`27505` is fixed::
510510
511-
sage: DiGraph(DiGraph().networkx_graph(), weighted=None, format='NX')
511+
sage: DiGraph(DiGraph().networkx_graph(), weighted=None, format='NX') # optional - networkx
512512
Digraph on 0 vertices
513513
"""
514514
_directed = True

src/sage/graphs/generators/basic.py

+38-38
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,12 @@ def CycleGraph(n):
248248
249249
Compare plotting using the predefined layout and networkx::
250250
251-
sage: import networkx
252-
sage: n = networkx.cycle_graph(23)
253-
sage: spring23 = Graph(n)
254-
sage: posdict23 = graphs.CycleGraph(23)
255-
sage: spring23.show() # long time
256-
sage: posdict23.show() # long time
251+
sage: import networkx # optional - networkx
252+
sage: n = networkx.cycle_graph(23) # optional - networkx
253+
sage: spring23 = Graph(n) # optional - networkx
254+
sage: posdict23 = graphs.CycleGraph(23) # optional - networkx
255+
sage: spring23.show() # long time # optional - networkx
256+
sage: posdict23.show() # long time # optional - networkx
257257
258258
We next view many cycle graphs as a Sage graphics array. First we use the
259259
``CycleGraph`` constructor, which fills in the position dictionary::
@@ -275,17 +275,17 @@ def CycleGraph(n):
275275
276276
sage: g = []
277277
sage: j = []
278-
sage: for i in range(9):
278+
sage: for i in range(9): # optional - networkx
279279
....: spr = networkx.cycle_graph(i+3)
280280
....: k = Graph(spr)
281281
....: g.append(k)
282-
sage: for i in range(3):
282+
sage: for i in range(3): # optional - networkx
283283
....: n = []
284284
....: for m in range(3):
285285
....: n.append(g[3*i + m].plot(vertex_size=50, vertex_labels=False))
286286
....: j.append(n)
287-
sage: G = graphics_array(j)
288-
sage: G.show() # long time
287+
sage: G = graphics_array(j) # optional - networkx
288+
sage: G.show() # long time # optional - networkx
289289
290290
TESTS:
291291
@@ -349,27 +349,27 @@ def CompleteGraph(n):
349349
350350
We compare to plotting with the spring-layout algorithm::
351351
352-
sage: import networkx
352+
sage: import networkx # optional - networkx
353353
sage: g = []
354354
sage: j = []
355-
sage: for i in range(9):
355+
sage: for i in range(9): # optional - networkx
356356
....: spr = networkx.complete_graph(i+3)
357357
....: k = Graph(spr)
358358
....: g.append(k)
359-
sage: for i in range(3):
359+
sage: for i in range(3): # optional - networkx
360360
....: n = []
361361
....: for m in range(3):
362362
....: n.append(g[3*i + m].plot(vertex_size=50, vertex_labels=False))
363363
....: j.append(n)
364-
sage: G = graphics_array(j)
365-
sage: G.show() # long time
364+
sage: G = graphics_array(j) # optional - networkx
365+
sage: G.show() # long time # optional - networkx
366366
367367
Compare the constructors (results will vary)::
368368
369-
sage: import networkx
370-
sage: t = cputime()
371-
sage: n = networkx.complete_graph(389); spring389 = Graph(n)
372-
sage: cputime(t) # random
369+
sage: import networkx # optional - networkx
370+
sage: t = cputime() # optional - networkx
371+
sage: n = networkx.complete_graph(389); spring389 = Graph(n) # optional - networkx
372+
sage: cputime(t) # random # optional - networkx
373373
0.59203700000000126
374374
sage: t = cputime()
375375
sage: posdict389 = graphs.CompleteGraph(389)
@@ -378,11 +378,11 @@ def CompleteGraph(n):
378378
379379
We compare plotting::
380380
381-
sage: import networkx
382-
sage: n = networkx.complete_graph(23)
381+
sage: import networkx # optional - networkx
382+
sage: n = networkx.complete_graph(23) # optional - networkx
383383
sage: spring23 = Graph(n)
384384
sage: posdict23 = graphs.CompleteGraph(23)
385-
sage: spring23.show() # long time
385+
sage: spring23.show() # long time # optional - networkx
386386
sage: posdict23.show() # long time
387387
"""
388388
G = Graph(n, name="Complete graph")
@@ -438,19 +438,19 @@ def CompleteBipartiteGraph(p, q, set_position=True):
438438
Two ways of constructing the complete bipartite graph, using different
439439
layout algorithms::
440440
441-
sage: import networkx
442-
sage: n = networkx.complete_bipartite_graph(389, 157); spring_big = Graph(n) # long time
441+
sage: import networkx # optional - networkx
442+
sage: n = networkx.complete_bipartite_graph(389, 157); spring_big = Graph(n) # long time # optional - networkx
443443
sage: posdict_big = graphs.CompleteBipartiteGraph(389, 157) # long time
444444
445445
Compare the plotting::
446446
447-
sage: n = networkx.complete_bipartite_graph(11, 17)
448-
sage: spring_med = Graph(n)
447+
sage: n = networkx.complete_bipartite_graph(11, 17) # optional - networkx
448+
sage: spring_med = Graph(n) # optional - networkx
449449
sage: posdict_med = graphs.CompleteBipartiteGraph(11, 17)
450450
451451
Notice here how the spring-layout tends to center the nodes of `n1`::
452452
453-
sage: spring_med.show() # long time
453+
sage: spring_med.show() # long time # optional - networkx
454454
sage: posdict_med.show() # long time
455455
456456
View many complete bipartite graphs with a Sage Graphics Array, with this
@@ -473,17 +473,17 @@ def CompleteBipartiteGraph(p, q, set_position=True):
473473
474474
sage: g = []
475475
sage: j = []
476-
sage: for i in range(9):
476+
sage: for i in range(9): # optional - networkx
477477
....: spr = networkx.complete_bipartite_graph(i+1,4)
478478
....: k = Graph(spr)
479479
....: g.append(k)
480-
sage: for i in range(3):
480+
sage: for i in range(3): # optional - networkx
481481
....: n = []
482482
....: for m in range(3):
483483
....: n.append(g[3*i + m].plot(vertex_size=50, vertex_labels=False))
484484
....: j.append(n)
485-
sage: G = graphics_array(j)
486-
sage: G.show() # long time
485+
sage: G = graphics_array(j) # optional - networkx
486+
sage: G.show() # long time # optional - networkx
487487
488488
:trac:`12155`::
489489
@@ -933,9 +933,9 @@ def GridGraph(dim_list):
933933
934934
sage: dim = [randint(1,4) for i in range(4)]
935935
sage: g = graphs.GridGraph(dim)
936-
sage: import networkx
937-
sage: h = Graph( networkx.grid_graph(list(dim)) )
938-
sage: g.is_isomorphic(h)
936+
sage: import networkx # optional - networkx
937+
sage: h = Graph(networkx.grid_graph(list(dim))) # optional - networkx
938+
sage: g.is_isomorphic(h) # optional - networkx
939939
True
940940
941941
Trivial cases::
@@ -1223,14 +1223,14 @@ def StarGraph(n):
12231223
12241224
EXAMPLES::
12251225
1226-
sage: import networkx
1226+
sage: import networkx # optional - networkx
12271227
12281228
Compare the plots::
12291229
1230-
sage: n = networkx.star_graph(23)
1231-
sage: spring23 = Graph(n)
1230+
sage: n = networkx.star_graph(23) # optional - networkx
1231+
sage: spring23 = Graph(n) # optional - networkx
12321232
sage: posdict23 = graphs.StarGraph(23)
1233-
sage: spring23.show() # long time
1233+
sage: spring23.show() # long time # optional - networkx
12341234
sage: posdict23.show() # long time
12351235
12361236
View many star graphs as a Sage Graphics Array

src/sage/graphs/generators/families.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -1076,11 +1076,11 @@ def CirculantGraph(n, adjacency):
10761076
EXAMPLES: Compare plotting using the predefined layout and
10771077
networkx::
10781078
1079-
sage: import networkx
1080-
sage: n = networkx.cycle_graph(23)
1081-
sage: spring23 = Graph(n)
1079+
sage: import networkx # optional - networkx
1080+
sage: n = networkx.cycle_graph(23) # optional - networkx
1081+
sage: spring23 = Graph(n) # optional - networkx
10821082
sage: posdict23 = graphs.CirculantGraph(23,2)
1083-
sage: spring23.show() # long time
1083+
sage: spring23.show() # long time # optional - networkx
10841084
sage: posdict23.show() # long time
10851085
10861086
We next view many cycle graphs as a Sage graphics array. First we
@@ -3409,29 +3409,29 @@ def WheelGraph(n):
34093409
34103410
Next, using the spring-layout algorithm::
34113411
3412-
sage: import networkx
3412+
sage: import networkx # optional - sage.groups
34133413
sage: g = []
34143414
sage: j = []
3415-
sage: for i in range(9):
3415+
sage: for i in range(9): # optional - sage.groups
34163416
....: spr = networkx.wheel_graph(i+3)
34173417
....: k = Graph(spr)
34183418
....: g.append(k)
34193419
...
3420-
sage: for i in range(3):
3420+
sage: for i in range(3): # optional - sage.groups
34213421
....: n = []
34223422
....: for m in range(3):
34233423
....: n.append(g[3*i + m].plot(vertex_size=50, vertex_labels=False))
34243424
....: j.append(n)
34253425
...
3426-
sage: G = graphics_array(j)
3427-
sage: G.show() # long time
3426+
sage: G = graphics_array(j) # optional - sage.groups
3427+
sage: G.show() # long time # optional - sage.groups
34283428
34293429
Compare the plotting::
34303430
3431-
sage: n = networkx.wheel_graph(23)
3432-
sage: spring23 = Graph(n)
3431+
sage: n = networkx.wheel_graph(23) # optional - sage.groups
3432+
sage: spring23 = Graph(n) # optional - sage.groups
34333433
sage: posdict23 = graphs.WheelGraph(23)
3434-
sage: spring23.show() # long time
3434+
sage: spring23.show() # long time # optional - sage.groups
34353435
sage: posdict23.show() # long time
34363436
"""
34373437
from sage.graphs.generators.basic import CycleGraph

src/sage/graphs/generators/platonic_solids.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,24 @@ def TetrahedralGraph():
4343
4444
The following example requires networkx::
4545
46-
sage: import networkx as NX
46+
sage: import networkx as NX # optional - networkx
4747
4848
Compare this Tetrahedral, Wheel(4), Complete(4), and the Tetrahedral plotted
4949
with the spring-layout algorithm below in a Sage graphics array::
5050
5151
sage: tetra_pos = graphs.TetrahedralGraph()
52-
sage: tetra_spring = Graph(NX.tetrahedral_graph())
52+
sage: tetra_spring = Graph(NX.tetrahedral_graph()) # optional - networkx
5353
sage: wheel = graphs.WheelGraph(4)
5454
sage: complete = graphs.CompleteGraph(4)
55-
sage: g = [tetra_pos, tetra_spring, wheel, complete]
55+
sage: g = [tetra_pos, tetra_spring, wheel, complete] # optional - networkx
5656
sage: j = []
57-
sage: for i in range(2):
57+
sage: for i in range(2): # optional - networkx
5858
....: n = []
5959
....: for m in range(2):
6060
....: n.append(g[i + m].plot(vertex_size=50, vertex_labels=False))
6161
....: j.append(n)
62-
sage: G = graphics_array(j)
63-
sage: G.show() # long time
62+
sage: G = graphics_array(j) # optional - networkx
63+
sage: G.show() # long time # optional - networkx
6464
"""
6565
edges = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
6666
pos = {0: (0, 0),

0 commit comments

Comments
 (0)