@@ -17605,7 +17605,7 @@ def wiener_index(self, by_weight=False, algorithm=None,
17605
17605
G = networkx.Graph(list(self.edges(labels=False, sort=False)))
17606
17606
G.add_nodes_from(self)
17607
17607
total = sum(sum(networkx.single_source_dijkstra_path_length(G, u).values())
17608
- for u in G)
17608
+ for u in G)
17609
17609
WI = total if self.is_directed() else (total / 2)
17610
17610
17611
17611
else:
@@ -17687,14 +17687,14 @@ def average_distance(self, by_weight=False, algorithm=None,
17687
17687
"""
17688
17688
if self.order() < 2:
17689
17689
raise ValueError("average distance is not defined for empty or one-element graph")
17690
- WI = self.wiener_index(by_weight=by_weight, algorithm=algorithm,
17691
- weight_function=weight_function, check_weight=check_weight)
17690
+ WI = self.wiener_index(by_weight=by_weight, algorithm=algorithm,
17691
+ weight_function=weight_function, check_weight=check_weight)
17692
17692
f = 1 if self.is_directed() else 2
17693
17693
if WI in ZZ:
17694
17694
return QQ((f * WI, self.order() * (self.order() - 1)))
17695
17695
return f * WI / (self.order() * (self.order() - 1))
17696
17696
17697
- ### Searches
17697
+ # Searches
17698
17698
17699
17699
def breadth_first_search(self, start, ignore_direction=False,
17700
17700
distance=None, neighbors=None,
@@ -18038,7 +18038,7 @@ def depth_first_search(self, start, ignore_direction=False,
18038
18038
if x not in seen:
18039
18039
queue.append((w, x, d + 1))
18040
18040
18041
- ### Constructors
18041
+ # Constructors
18042
18042
18043
18043
def add_clique(self, vertices, loops=False):
18044
18044
"""
@@ -18543,7 +18543,9 @@ def cartesian_product(self, other):
18543
18543
sage: H = Graph([('a', 'b')])
18544
18544
sage: C1 = G.cartesian_product(H)
18545
18545
sage: C1.edges(sort=True, labels=None)
18546
- [((0, 'a'), (0, 'b')), ((0, 'a'), (1, 'a')), ((0, 'b'), (1, 'b')), ((1, 'a'), (1, 'b')), ((1, 'a'), (2, 'a')), ((1, 'b'), (2, 'b')), ((2, 'a'), (2, 'b'))]
18546
+ [((0, 'a'), (0, 'b')), ((0, 'a'), (1, 'a')), ((0, 'b'), (1, 'b')),
18547
+ ((1, 'a'), (1, 'b')), ((1, 'a'), (2, 'a')), ((1, 'b'), (2, 'b')),
18548
+ ((2, 'a'), (2, 'b'))]
18547
18549
sage: C2 = H.cartesian_product(G)
18548
18550
sage: C1.is_isomorphic(C2)
18549
18551
True
@@ -18562,7 +18564,16 @@ def cartesian_product(self, other):
18562
18564
sage: B = digraphs.DeBruijn(['a', 'b'], 2)
18563
18565
sage: Q = P.cartesian_product(B)
18564
18566
sage: Q.edges(sort=True, labels=None)
18565
- [((0, 'aa'), (0, 'aa')), ((0, 'aa'), (0, 'ab')), ((0, 'aa'), (1, 'aa')), ((0, 'ab'), (0, 'ba')), ((0, 'ab'), (0, 'bb')), ((0, 'ab'), (1, 'ab')), ((0, 'ba'), (0, 'aa')), ((0, 'ba'), (0, 'ab')), ((0, 'ba'), (1, 'ba')), ((0, 'bb'), (0, 'ba')), ((0, 'bb'), (0, 'bb')), ((0, 'bb'), (1, 'bb')), ((1, 'aa'), (1, 'aa')), ((1, 'aa'), (1, 'ab')), ((1, 'ab'), (1, 'ba')), ((1, 'ab'), (1, 'bb')), ((1, 'ba'), (1, 'aa')), ((1, 'ba'), (1, 'ab')), ((1, 'bb'), (1, 'ba')), ((1, 'bb'), (1, 'bb'))]
18567
+ [((0, 'aa'), (0, 'aa')), ((0, 'aa'), (0, 'ab')),
18568
+ ((0, 'aa'), (1, 'aa')), ((0, 'ab'), (0, 'ba')),
18569
+ ((0, 'ab'), (0, 'bb')), ((0, 'ab'), (1, 'ab')),
18570
+ ((0, 'ba'), (0, 'aa')), ((0, 'ba'), (0, 'ab')),
18571
+ ((0, 'ba'), (1, 'ba')), ((0, 'bb'), (0, 'ba')),
18572
+ ((0, 'bb'), (0, 'bb')), ((0, 'bb'), (1, 'bb')),
18573
+ ((1, 'aa'), (1, 'aa')), ((1, 'aa'), (1, 'ab')),
18574
+ ((1, 'ab'), (1, 'ba')), ((1, 'ab'), (1, 'bb')),
18575
+ ((1, 'ba'), (1, 'aa')), ((1, 'ba'), (1, 'ab')),
18576
+ ((1, 'bb'), (1, 'ba')), ((1, 'bb'), (1, 'bb'))]
18566
18577
sage: Q.strongly_connected_components_digraph().num_verts()
18567
18578
2
18568
18579
sage: V = Q.strongly_connected_component_containing_vertex((0, 'aa'))
@@ -18709,7 +18720,10 @@ def lexicographic_product(self, other):
18709
18720
sage: H = Graph([('a', 'b')])
18710
18721
sage: T = G.lexicographic_product(H)
18711
18722
sage: T.edges(sort=True, labels=None)
18712
- [((0, 'a'), (0, 'b')), ((0, 'a'), (1, 'a')), ((0, 'a'), (1, 'b')), ((0, 'b'), (1, 'a')), ((0, 'b'), (1, 'b')), ((1, 'a'), (1, 'b')), ((1, 'a'), (2, 'a')), ((1, 'a'), (2, 'b')), ((1, 'b'), (2, 'a')), ((1, 'b'), (2, 'b')), ((2, 'a'), (2, 'b'))]
18723
+ [((0, 'a'), (0, 'b')), ((0, 'a'), (1, 'a')), ((0, 'a'), (1, 'b')),
18724
+ ((0, 'b'), (1, 'a')), ((0, 'b'), (1, 'b')), ((1, 'a'), (1, 'b')),
18725
+ ((1, 'a'), (2, 'a')), ((1, 'a'), (2, 'b')), ((1, 'b'), (2, 'a')),
18726
+ ((1, 'b'), (2, 'b')), ((2, 'a'), (2, 'b'))]
18713
18727
sage: T.is_isomorphic(H.lexicographic_product(G))
18714
18728
False
18715
18729
@@ -18719,7 +18733,10 @@ def lexicographic_product(self, other):
18719
18733
sage: J = DiGraph([('a', 'b')])
18720
18734
sage: T = I.lexicographic_product(J)
18721
18735
sage: T.edges(sort=True, labels=None)
18722
- [((0, 'a'), (0, 'b')), ((0, 'a'), (1, 'a')), ((0, 'a'), (1, 'b')), ((0, 'b'), (1, 'a')), ((0, 'b'), (1, 'b')), ((1, 'a'), (1, 'b')), ((1, 'a'), (2, 'a')), ((1, 'a'), (2, 'b')), ((1, 'b'), (2, 'a')), ((1, 'b'), (2, 'b')), ((2, 'a'), (2, 'b'))]
18736
+ [((0, 'a'), (0, 'b')), ((0, 'a'), (1, 'a')), ((0, 'a'), (1, 'b')),
18737
+ ((0, 'b'), (1, 'a')), ((0, 'b'), (1, 'b')), ((1, 'a'), (1, 'b')),
18738
+ ((1, 'a'), (2, 'a')), ((1, 'a'), (2, 'b')), ((1, 'b'), (2, 'a')),
18739
+ ((1, 'b'), (2, 'b')), ((2, 'a'), (2, 'b'))]
18723
18740
sage: T.is_isomorphic(J.lexicographic_product(I))
18724
18741
False
18725
18742
"""
@@ -18861,7 +18878,11 @@ def disjunctive_product(self, other):
18861
18878
sage: H = Graph([('a', 'b')])
18862
18879
sage: T = G.disjunctive_product(H)
18863
18880
sage: T.edges(sort=True, labels=None)
18864
- [((0, 'a'), (0, 'b')), ((0, 'a'), (1, 'a')), ((0, 'a'), (1, 'b')), ((0, 'a'), (2, 'b')), ((0, 'b'), (1, 'a')), ((0, 'b'), (1, 'b')), ((0, 'b'), (2, 'a')), ((1, 'a'), (1, 'b')), ((1, 'a'), (2, 'a')), ((1, 'a'), (2, 'b')), ((1, 'b'), (2, 'a')), ((1, 'b'), (2, 'b')), ((2, 'a'), (2, 'b'))]
18881
+ [((0, 'a'), (0, 'b')), ((0, 'a'), (1, 'a')), ((0, 'a'), (1, 'b')),
18882
+ ((0, 'a'), (2, 'b')), ((0, 'b'), (1, 'a')), ((0, 'b'), (1, 'b')),
18883
+ ((0, 'b'), (2, 'a')), ((1, 'a'), (1, 'b')), ((1, 'a'), (2, 'a')),
18884
+ ((1, 'a'), (2, 'b')), ((1, 'b'), (2, 'a')), ((1, 'b'), (2, 'b')),
18885
+ ((2, 'a'), (2, 'b'))]
18865
18886
sage: T.is_isomorphic(H.disjunctive_product(G))
18866
18887
True
18867
18888
@@ -18871,7 +18892,11 @@ def disjunctive_product(self, other):
18871
18892
sage: J = DiGraph([('a', 'b')])
18872
18893
sage: T = I.disjunctive_product(J)
18873
18894
sage: T.edges(sort=True, labels=None)
18874
- [((0, 'a'), (0, 'b')), ((0, 'a'), (1, 'a')), ((0, 'a'), (1, 'b')), ((0, 'a'), (2, 'b')), ((0, 'b'), (1, 'a')), ((0, 'b'), (1, 'b')), ((1, 'a'), (0, 'b')), ((1, 'a'), (1, 'b')), ((1, 'a'), (2, 'a')), ((1, 'a'), (2, 'b')), ((1, 'b'), (2, 'a')), ((1, 'b'), (2, 'b')), ((2, 'a'), (0, 'b')), ((2, 'a'), (1, 'b')), ((2, 'a'), (2, 'b'))]
18895
+ [((0, 'a'), (0, 'b')), ((0, 'a'), (1, 'a')), ((0, 'a'), (1, 'b')),
18896
+ ((0, 'a'), (2, 'b')), ((0, 'b'), (1, 'a')), ((0, 'b'), (1, 'b')),
18897
+ ((1, 'a'), (0, 'b')), ((1, 'a'), (1, 'b')), ((1, 'a'), (2, 'a')),
18898
+ ((1, 'a'), (2, 'b')), ((1, 'b'), (2, 'a')), ((1, 'b'), (2, 'b')),
18899
+ ((2, 'a'), (0, 'b')), ((2, 'a'), (1, 'b')), ((2, 'a'), (2, 'b'))]
18875
18900
sage: T.is_isomorphic(J.disjunctive_product(I))
18876
18901
True
18877
18902
"""
@@ -19047,8 +19072,7 @@ def is_transitively_reduced(self):
19047
19072
19048
19073
return self.is_forest()
19049
19074
19050
-
19051
- ### Visualization
19075
+ # Visualization
19052
19076
19053
19077
def _color_by_label(self, format='hex', as_function=False, default_color="black"):
19054
19078
"""
@@ -19149,7 +19173,8 @@ def _color_by_label(self, format='hex', as_function=False, default_color="black"
19149
19173
color_of_label = dict(zip(labels, colors))
19150
19174
color_of_label = color_of_label.__getitem__
19151
19175
elif isinstance(format, dict):
19152
- color_of_label = lambda label: format.get(label, default_color)
19176
+ def color_of_label(label):
19177
+ return format.get(label, default_color)
19153
19178
else:
19154
19179
# This assumes that ``format`` is already a function
19155
19180
color_of_label = format
@@ -19218,7 +19243,6 @@ def set_latex_options(self, **kwds):
19218
19243
opts = self.latex_options()
19219
19244
opts.set_options(**kwds)
19220
19245
19221
-
19222
19246
def layout(self, layout=None, pos=None, dim=2, save_pos=False, **options):
19223
19247
"""
19224
19248
Return a layout for the vertices of this graph.
@@ -19341,10 +19365,10 @@ def layout(self, layout=None, pos=None, dim=2, save_pos=False, **options):
19341
19365
if pos is None:
19342
19366
layout = 'default'
19343
19367
19344
- if hasattr(self, "layout_%s"% layout):
19345
- pos = getattr(self, "layout_%s"% layout)(dim=dim, **options)
19368
+ if hasattr(self, "layout_%s" % layout):
19369
+ pos = getattr(self, "layout_%s" % layout)(dim=dim, **options)
19346
19370
elif layout is not None:
19347
- raise ValueError("unknown layout algorithm: %s"% layout)
19371
+ raise ValueError("unknown layout algorithm: %s" % layout)
19348
19372
19349
19373
if len(pos) < self.order():
19350
19374
pos = self.layout_extend_randomly(pos, dim=dim)
@@ -19353,7 +19377,6 @@ def layout(self, layout=None, pos=None, dim=2, save_pos=False, **options):
19353
19377
self.set_pos(pos, dim=dim)
19354
19378
return pos
19355
19379
19356
-
19357
19380
def layout_spring(self, by_component=True, **options):
19358
19381
"""
19359
19382
Return a spring layout for this graph.
@@ -19490,11 +19513,11 @@ def layout_extend_randomly(self, pos, dim=2):
19490
19513
sage: (xmin, ymin) == (0, 0) and (xmax, ymax) == (1, 1)
19491
19514
True
19492
19515
"""
19493
- assert dim == 2 # 3d not yet implemented
19516
+ assert dim == 2 # 3d not yet implemented
19494
19517
from sage.misc.randstate import current_randstate
19495
19518
random = current_randstate().python_random().random
19496
19519
19497
- xmin, xmax,ymin, ymax = self._layout_bounding_box(pos)
19520
+ xmin, xmax, ymin, ymax = self._layout_bounding_box(pos)
19498
19521
19499
19522
dx = xmax - xmin
19500
19523
dy = ymax - ymin
@@ -19505,7 +19528,6 @@ def layout_extend_randomly(self, pos, dim=2):
19505
19528
pos[v] = [xmin + dx * random(), ymin + dy * random()]
19506
19529
return pos
19507
19530
19508
-
19509
19531
def layout_circular(self, dim=2, center=(0, 0), radius=1, shift=0, angle=0, **options):
19510
19532
r"""
19511
19533
Return a circular layout for this graph
@@ -19947,9 +19969,9 @@ def _layout_bounding_box(self, pos):
19947
19969
ys = [pos[v][1] for v in pos]
19948
19970
if not xs:
19949
19971
xmin = -1
19950
- xmax = 1
19972
+ xmax = 1
19951
19973
ymin = -1
19952
- ymax = 1
19974
+ ymax = 1
19953
19975
else:
19954
19976
xmin = min(xs)
19955
19977
xmax = max(xs)
@@ -20047,7 +20069,7 @@ def _circle_embedding(self, vertices, center=(0, 0), radius=1, shift=0, angle=0,
20047
20069
pos = self._pos = {}
20048
20070
20049
20071
from math import sin, cos, pi
20050
- for i,v in enumerate(vertices):
20072
+ for i, v in enumerate(vertices):
20051
20073
i += shift
20052
20074
# We round cos and sin to avoid results like 1.2246467991473532e-16
20053
20075
# when asking for sin(pi)
@@ -20365,7 +20387,11 @@ def plot(self, **options):
20365
20387
20366
20388
::
20367
20389
20368
- sage: D = DiGraph( { 0: [1, 10, 19], 1: [8, 2], 2: [3, 6], 3: [19, 4], 4: [17, 5], 5: [6, 15], 6: [7], 7: [8, 14], 8: [9], 9: [10, 13], 10: [11], 11: [12, 18], 12: [16, 13], 13: [14], 14: [15], 15: [16], 16: [17], 17: [18], 18: [19], 19: []} , sparse=True)
20390
+ sage: D = DiGraph({0: [1, 10, 19], 1: [8, 2], 2: [3, 6], 3: [19, 4],
20391
+ ....: 4: [17, 5], 5: [6, 15], 6: [7], 7: [8, 14],
20392
+ ....: 8: [9], 9: [10, 13], 10: [11], 11: [12, 18],
20393
+ ....: 12: [16, 13], 13: [14], 14: [15], 15: [16],
20394
+ ....: 16: [17], 17: [18], 18: [19]}, sparse=True)
20369
20395
sage: for u,v,l in D.edges(sort=False):
20370
20396
....: D.set_edge_label(u, v, '(' + str(u) + ',' + str(v) + ')')
20371
20397
sage: D.plot(edge_labels=True, layout='circular').show()
@@ -20559,7 +20585,7 @@ def show(self, method="matplotlib", **kwds):
20559
20585
return
20560
20586
from sage.misc.viewer import browser
20561
20587
import os
20562
- os.system('%s %s 2>/dev/null 1>/dev/null &'% (browser(), filename))
20588
+ os.system('%s %s 2>/dev/null 1>/dev/null &' % (browser(), filename))
20563
20589
return
20564
20590
20565
20591
from .graph_plot import graphplot_options
0 commit comments