Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit dcbbba2

Browse files
committedAug 13, 2022
trac #34358: clean src/sage/graphs/generic_graph.py - part 4
1 parent 12be2d9 commit dcbbba2

File tree

1 file changed

+53
-27
lines changed

1 file changed

+53
-27
lines changed
 

‎src/sage/graphs/generic_graph.py

+53-27
Original file line numberDiff line numberDiff line change
@@ -17605,7 +17605,7 @@ def wiener_index(self, by_weight=False, algorithm=None,
1760517605
G = networkx.Graph(list(self.edges(labels=False, sort=False)))
1760617606
G.add_nodes_from(self)
1760717607
total = sum(sum(networkx.single_source_dijkstra_path_length(G, u).values())
17608-
for u in G)
17608+
for u in G)
1760917609
WI = total if self.is_directed() else (total / 2)
1761017610

1761117611
else:
@@ -17687,14 +17687,14 @@ def average_distance(self, by_weight=False, algorithm=None,
1768717687
"""
1768817688
if self.order() < 2:
1768917689
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)
1769217692
f = 1 if self.is_directed() else 2
1769317693
if WI in ZZ:
1769417694
return QQ((f * WI, self.order() * (self.order() - 1)))
1769517695
return f * WI / (self.order() * (self.order() - 1))
1769617696

17697-
### Searches
17697+
# Searches
1769817698

1769917699
def breadth_first_search(self, start, ignore_direction=False,
1770017700
distance=None, neighbors=None,
@@ -18038,7 +18038,7 @@ def depth_first_search(self, start, ignore_direction=False,
1803818038
if x not in seen:
1803918039
queue.append((w, x, d + 1))
1804018040

18041-
### Constructors
18041+
# Constructors
1804218042

1804318043
def add_clique(self, vertices, loops=False):
1804418044
"""
@@ -18543,7 +18543,9 @@ def cartesian_product(self, other):
1854318543
sage: H = Graph([('a', 'b')])
1854418544
sage: C1 = G.cartesian_product(H)
1854518545
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'))]
1854718549
sage: C2 = H.cartesian_product(G)
1854818550
sage: C1.is_isomorphic(C2)
1854918551
True
@@ -18562,7 +18564,16 @@ def cartesian_product(self, other):
1856218564
sage: B = digraphs.DeBruijn(['a', 'b'], 2)
1856318565
sage: Q = P.cartesian_product(B)
1856418566
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'))]
1856618577
sage: Q.strongly_connected_components_digraph().num_verts()
1856718578
2
1856818579
sage: V = Q.strongly_connected_component_containing_vertex((0, 'aa'))
@@ -18709,7 +18720,10 @@ def lexicographic_product(self, other):
1870918720
sage: H = Graph([('a', 'b')])
1871018721
sage: T = G.lexicographic_product(H)
1871118722
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'))]
1871318727
sage: T.is_isomorphic(H.lexicographic_product(G))
1871418728
False
1871518729

@@ -18719,7 +18733,10 @@ def lexicographic_product(self, other):
1871918733
sage: J = DiGraph([('a', 'b')])
1872018734
sage: T = I.lexicographic_product(J)
1872118735
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'))]
1872318740
sage: T.is_isomorphic(J.lexicographic_product(I))
1872418741
False
1872518742
"""
@@ -18861,7 +18878,11 @@ def disjunctive_product(self, other):
1886118878
sage: H = Graph([('a', 'b')])
1886218879
sage: T = G.disjunctive_product(H)
1886318880
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'))]
1886518886
sage: T.is_isomorphic(H.disjunctive_product(G))
1886618887
True
1886718888

@@ -18871,7 +18892,11 @@ def disjunctive_product(self, other):
1887118892
sage: J = DiGraph([('a', 'b')])
1887218893
sage: T = I.disjunctive_product(J)
1887318894
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'))]
1887518900
sage: T.is_isomorphic(J.disjunctive_product(I))
1887618901
True
1887718902
"""
@@ -19047,8 +19072,7 @@ def is_transitively_reduced(self):
1904719072

1904819073
return self.is_forest()
1904919074

19050-
19051-
### Visualization
19075+
# Visualization
1905219076

1905319077
def _color_by_label(self, format='hex', as_function=False, default_color="black"):
1905419078
"""
@@ -19149,7 +19173,8 @@ def _color_by_label(self, format='hex', as_function=False, default_color="black"
1914919173
color_of_label = dict(zip(labels, colors))
1915019174
color_of_label = color_of_label.__getitem__
1915119175
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)
1915319178
else:
1915419179
# This assumes that ``format`` is already a function
1915519180
color_of_label = format
@@ -19218,7 +19243,6 @@ def set_latex_options(self, **kwds):
1921819243
opts = self.latex_options()
1921919244
opts.set_options(**kwds)
1922019245

19221-
1922219246
def layout(self, layout=None, pos=None, dim=2, save_pos=False, **options):
1922319247
"""
1922419248
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):
1934119365
if pos is None:
1934219366
layout = 'default'
1934319367

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)
1934619370
elif layout is not None:
19347-
raise ValueError("unknown layout algorithm: %s"%layout)
19371+
raise ValueError("unknown layout algorithm: %s" % layout)
1934819372

1934919373
if len(pos) < self.order():
1935019374
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):
1935319377
self.set_pos(pos, dim=dim)
1935419378
return pos
1935519379

19356-
1935719380
def layout_spring(self, by_component=True, **options):
1935819381
"""
1935919382
Return a spring layout for this graph.
@@ -19490,11 +19513,11 @@ def layout_extend_randomly(self, pos, dim=2):
1949019513
sage: (xmin, ymin) == (0, 0) and (xmax, ymax) == (1, 1)
1949119514
True
1949219515
"""
19493-
assert dim == 2 # 3d not yet implemented
19516+
assert dim == 2 # 3d not yet implemented
1949419517
from sage.misc.randstate import current_randstate
1949519518
random = current_randstate().python_random().random
1949619519

19497-
xmin, xmax,ymin, ymax = self._layout_bounding_box(pos)
19520+
xmin, xmax, ymin, ymax = self._layout_bounding_box(pos)
1949819521

1949919522
dx = xmax - xmin
1950019523
dy = ymax - ymin
@@ -19505,7 +19528,6 @@ def layout_extend_randomly(self, pos, dim=2):
1950519528
pos[v] = [xmin + dx * random(), ymin + dy * random()]
1950619529
return pos
1950719530

19508-
1950919531
def layout_circular(self, dim=2, center=(0, 0), radius=1, shift=0, angle=0, **options):
1951019532
r"""
1951119533
Return a circular layout for this graph
@@ -19947,9 +19969,9 @@ def _layout_bounding_box(self, pos):
1994719969
ys = [pos[v][1] for v in pos]
1994819970
if not xs:
1994919971
xmin = -1
19950-
xmax = 1
19972+
xmax = 1
1995119973
ymin = -1
19952-
ymax = 1
19974+
ymax = 1
1995319975
else:
1995419976
xmin = min(xs)
1995519977
xmax = max(xs)
@@ -20047,7 +20069,7 @@ def _circle_embedding(self, vertices, center=(0, 0), radius=1, shift=0, angle=0,
2004720069
pos = self._pos = {}
2004820070

2004920071
from math import sin, cos, pi
20050-
for i,v in enumerate(vertices):
20072+
for i, v in enumerate(vertices):
2005120073
i += shift
2005220074
# We round cos and sin to avoid results like 1.2246467991473532e-16
2005320075
# when asking for sin(pi)
@@ -20365,7 +20387,11 @@ def plot(self, **options):
2036520387

2036620388
::
2036720389

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)
2036920395
sage: for u,v,l in D.edges(sort=False):
2037020396
....: D.set_edge_label(u, v, '(' + str(u) + ',' + str(v) + ')')
2037120397
sage: D.plot(edge_labels=True, layout='circular').show()
@@ -20559,7 +20585,7 @@ def show(self, method="matplotlib", **kwds):
2055920585
return
2056020586
from sage.misc.viewer import browser
2056120587
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))
2056320589
return
2056420590

2056520591
from .graph_plot import graphplot_options

0 commit comments

Comments
 (0)