Skip to content

Commit ce82f81

Browse files
author
Release Manager
committed
Trac #34358: pycodestyle cleanup in src/sage/graphs/generic_graph.py (part 4)
URL: https://trac.sagemath.org/34358 Reported by: dcoudert Ticket author(s): David Coudert Reviewer(s): Kwankyu Lee
2 parents c8c541f + b1db4f1 commit ce82f81

File tree

1 file changed

+81
-38
lines changed

1 file changed

+81
-38
lines changed

src/sage/graphs/generic_graph.py

+81-38
Original file line numberDiff line numberDiff line change
@@ -17116,17 +17116,33 @@ def shortest_path_all_pairs(self, by_weight=False, algorithm=None,
1711617116
sage: G.plot(edge_labels=True).show() # long time
1711717117
sage: dist, pred = G.shortest_path_all_pairs(by_weight = True)
1711817118
sage: dist
17119-
{0: {0: 0, 1: 1, 2: 2, 3: 3, 4: 2}, 1: {0: 1, 1: 0, 2: 1, 3: 2, 4: 3}, 2: {0: 2, 1: 1, 2: 0, 3: 1, 4: 3}, 3: {0: 3, 1: 2, 2: 1, 3: 0, 4: 2}, 4: {0: 2, 1: 3, 2: 3, 3: 2, 4: 0}}
17119+
{0: {0: 0, 1: 1, 2: 2, 3: 3, 4: 2},
17120+
1: {0: 1, 1: 0, 2: 1, 3: 2, 4: 3},
17121+
2: {0: 2, 1: 1, 2: 0, 3: 1, 4: 3},
17122+
3: {0: 3, 1: 2, 2: 1, 3: 0, 4: 2},
17123+
4: {0: 2, 1: 3, 2: 3, 3: 2, 4: 0}}
1712017124
sage: pred
17121-
{0: {0: None, 1: 0, 2: 1, 3: 2, 4: 0}, 1: {0: 1, 1: None, 2: 1, 3: 2, 4: 0}, 2: {0: 1, 1: 2, 2: None, 3: 2, 4: 3}, 3: {0: 1, 1: 2, 2: 3, 3: None, 4: 3}, 4: {0: 4, 1: 0, 2: 3, 3: 4, 4: None}}
17125+
{0: {0: None, 1: 0, 2: 1, 3: 2, 4: 0},
17126+
1: {0: 1, 1: None, 2: 1, 3: 2, 4: 0},
17127+
2: {0: 1, 1: 2, 2: None, 3: 2, 4: 3},
17128+
3: {0: 1, 1: 2, 2: 3, 3: None, 4: 3},
17129+
4: {0: 4, 1: 0, 2: 3, 3: 4, 4: None}}
1712217130
sage: pred[0]
1712317131
{0: None, 1: 0, 2: 1, 3: 2, 4: 0}
1712417132
sage: G = Graph( { 0: {1: {'weight':1}}, 1: {2: {'weight':1}}, 2: {3: {'weight':1}}, 3: {4: {'weight':2}}, 4: {0: {'weight':2}} }, sparse=True)
1712517133
sage: dist, pred = G.shortest_path_all_pairs(weight_function = lambda e:e[2]['weight'])
1712617134
sage: dist
17127-
{0: {0: 0, 1: 1, 2: 2, 3: 3, 4: 2}, 1: {0: 1, 1: 0, 2: 1, 3: 2, 4: 3}, 2: {0: 2, 1: 1, 2: 0, 3: 1, 4: 3}, 3: {0: 3, 1: 2, 2: 1, 3: 0, 4: 2}, 4: {0: 2, 1: 3, 2: 3, 3: 2, 4: 0}}
17135+
{0: {0: 0, 1: 1, 2: 2, 3: 3, 4: 2},
17136+
1: {0: 1, 1: 0, 2: 1, 3: 2, 4: 3},
17137+
2: {0: 2, 1: 1, 2: 0, 3: 1, 4: 3},
17138+
3: {0: 3, 1: 2, 2: 1, 3: 0, 4: 2},
17139+
4: {0: 2, 1: 3, 2: 3, 3: 2, 4: 0}}
1712817140
sage: pred
17129-
{0: {0: None, 1: 0, 2: 1, 3: 2, 4: 0}, 1: {0: 1, 1: None, 2: 1, 3: 2, 4: 0}, 2: {0: 1, 1: 2, 2: None, 3: 2, 4: 3}, 3: {0: 1, 1: 2, 2: 3, 3: None, 4: 3}, 4: {0: 4, 1: 0, 2: 3, 3: 4, 4: None}}
17141+
{0: {0: None, 1: 0, 2: 1, 3: 2, 4: 0},
17142+
1: {0: 1, 1: None, 2: 1, 3: 2, 4: 0},
17143+
2: {0: 1, 1: 2, 2: None, 3: 2, 4: 3},
17144+
3: {0: 1, 1: 2, 2: 3, 3: None, 4: 3},
17145+
4: {0: 4, 1: 0, 2: 3, 3: 4, 4: None}}
1713017146

1713117147
So for example the shortest weighted path from `0` to `3` is obtained as
1713217148
follows. The predecessor of `3` is ``pred[0][3] == 2``, the predecessor
@@ -17344,11 +17360,12 @@ def shortest_path_all_pairs(self, by_weight=False, algorithm=None,
1734417360
return_predecessors=True, unweighted=not by_weight)
1734517361

1734617362
# and format the result
17347-
dist = {int_to_vertex[i]: {int_to_vertex[j]: dd[i, j] for j in range(n) if dd[i, j] != +Infinity}
17348-
for i in range(n)}
17363+
dist = {int_to_vertex[i]: {int_to_vertex[j]: dd[i, j]
17364+
for j in range(n) if dd[i, j] != +Infinity}
17365+
for i in range(n)}
1734917366
pred = {int_to_vertex[i]: {int_to_vertex[j]: (int_to_vertex[pp[i, j]] if i != j else None)
17350-
for j in range(n) if (i == j or pp[i, j] != -9999)}
17351-
for i in range(n)}
17367+
for j in range(n) if (i == j or pp[i, j] != -9999)}
17368+
for i in range(n)}
1735217369
return dist, pred
1735317370

1735417371
elif algorithm == "Johnson_Boost":
@@ -17367,9 +17384,9 @@ def shortest_path_all_pairs(self, by_weight=False, algorithm=None,
1736717384
dist = dict()
1736817385
pred = dict()
1736917386
for u in self:
17370-
paths=self.shortest_paths(u, by_weight=by_weight,
17371-
algorithm=algorithm,
17372-
weight_function=weight_function)
17387+
paths = self.shortest_paths(u, by_weight=by_weight,
17388+
algorithm=algorithm,
17389+
weight_function=weight_function)
1737317390
dist[u] = {v: self._path_length(p, by_weight=by_weight,
1737417391
weight_function=weight_function)
1737517392
for v, p in paths.items()}
@@ -17605,7 +17622,7 @@ def wiener_index(self, by_weight=False, algorithm=None,
1760517622
G = networkx.Graph(list(self.edges(labels=False, sort=False)))
1760617623
G.add_nodes_from(self)
1760717624
total = sum(sum(networkx.single_source_dijkstra_path_length(G, u).values())
17608-
for u in G)
17625+
for u in G)
1760917626
WI = total if self.is_directed() else (total / 2)
1761017627

1761117628
else:
@@ -17687,14 +17704,14 @@ def average_distance(self, by_weight=False, algorithm=None,
1768717704
"""
1768817705
if self.order() < 2:
1768917706
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)
17707+
WI = self.wiener_index(by_weight=by_weight, algorithm=algorithm,
17708+
weight_function=weight_function, check_weight=check_weight)
1769217709
f = 1 if self.is_directed() else 2
1769317710
if WI in ZZ:
1769417711
return QQ((f * WI, self.order() * (self.order() - 1)))
1769517712
return f * WI / (self.order() * (self.order() - 1))
1769617713

17697-
### Searches
17714+
# Searches
1769817715

1769917716
def breadth_first_search(self, start, ignore_direction=False,
1770017717
distance=None, neighbors=None,
@@ -18038,7 +18055,7 @@ def depth_first_search(self, start, ignore_direction=False,
1803818055
if x not in seen:
1803918056
queue.append((w, x, d + 1))
1804018057

18041-
### Constructors
18058+
# Constructors
1804218059

1804318060
def add_clique(self, vertices, loops=False):
1804418061
"""
@@ -18543,7 +18560,9 @@ def cartesian_product(self, other):
1854318560
sage: H = Graph([('a', 'b')])
1854418561
sage: C1 = G.cartesian_product(H)
1854518562
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'))]
18563+
[((0, 'a'), (0, 'b')), ((0, 'a'), (1, 'a')), ((0, 'b'), (1, 'b')),
18564+
((1, 'a'), (1, 'b')), ((1, 'a'), (2, 'a')), ((1, 'b'), (2, 'b')),
18565+
((2, 'a'), (2, 'b'))]
1854718566
sage: C2 = H.cartesian_product(G)
1854818567
sage: C1.is_isomorphic(C2)
1854918568
True
@@ -18562,7 +18581,16 @@ def cartesian_product(self, other):
1856218581
sage: B = digraphs.DeBruijn(['a', 'b'], 2)
1856318582
sage: Q = P.cartesian_product(B)
1856418583
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'))]
18584+
[((0, 'aa'), (0, 'aa')), ((0, 'aa'), (0, 'ab')),
18585+
((0, 'aa'), (1, 'aa')), ((0, 'ab'), (0, 'ba')),
18586+
((0, 'ab'), (0, 'bb')), ((0, 'ab'), (1, 'ab')),
18587+
((0, 'ba'), (0, 'aa')), ((0, 'ba'), (0, 'ab')),
18588+
((0, 'ba'), (1, 'ba')), ((0, 'bb'), (0, 'ba')),
18589+
((0, 'bb'), (0, 'bb')), ((0, 'bb'), (1, 'bb')),
18590+
((1, 'aa'), (1, 'aa')), ((1, 'aa'), (1, 'ab')),
18591+
((1, 'ab'), (1, 'ba')), ((1, 'ab'), (1, 'bb')),
18592+
((1, 'ba'), (1, 'aa')), ((1, 'ba'), (1, 'ab')),
18593+
((1, 'bb'), (1, 'ba')), ((1, 'bb'), (1, 'bb'))]
1856618594
sage: Q.strongly_connected_components_digraph().num_verts()
1856718595
2
1856818596
sage: V = Q.strongly_connected_component_containing_vertex((0, 'aa'))
@@ -18709,7 +18737,10 @@ def lexicographic_product(self, other):
1870918737
sage: H = Graph([('a', 'b')])
1871018738
sage: T = G.lexicographic_product(H)
1871118739
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'))]
18740+
[((0, 'a'), (0, 'b')), ((0, 'a'), (1, 'a')), ((0, 'a'), (1, 'b')),
18741+
((0, 'b'), (1, 'a')), ((0, 'b'), (1, 'b')), ((1, 'a'), (1, 'b')),
18742+
((1, 'a'), (2, 'a')), ((1, 'a'), (2, 'b')), ((1, 'b'), (2, 'a')),
18743+
((1, 'b'), (2, 'b')), ((2, 'a'), (2, 'b'))]
1871318744
sage: T.is_isomorphic(H.lexicographic_product(G))
1871418745
False
1871518746

@@ -18719,7 +18750,10 @@ def lexicographic_product(self, other):
1871918750
sage: J = DiGraph([('a', 'b')])
1872018751
sage: T = I.lexicographic_product(J)
1872118752
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'))]
18753+
[((0, 'a'), (0, 'b')), ((0, 'a'), (1, 'a')), ((0, 'a'), (1, 'b')),
18754+
((0, 'b'), (1, 'a')), ((0, 'b'), (1, 'b')), ((1, 'a'), (1, 'b')),
18755+
((1, 'a'), (2, 'a')), ((1, 'a'), (2, 'b')), ((1, 'b'), (2, 'a')),
18756+
((1, 'b'), (2, 'b')), ((2, 'a'), (2, 'b'))]
1872318757
sage: T.is_isomorphic(J.lexicographic_product(I))
1872418758
False
1872518759
"""
@@ -18861,7 +18895,11 @@ def disjunctive_product(self, other):
1886118895
sage: H = Graph([('a', 'b')])
1886218896
sage: T = G.disjunctive_product(H)
1886318897
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'))]
18898+
[((0, 'a'), (0, 'b')), ((0, 'a'), (1, 'a')), ((0, 'a'), (1, 'b')),
18899+
((0, 'a'), (2, 'b')), ((0, 'b'), (1, 'a')), ((0, 'b'), (1, 'b')),
18900+
((0, 'b'), (2, 'a')), ((1, 'a'), (1, 'b')), ((1, 'a'), (2, 'a')),
18901+
((1, 'a'), (2, 'b')), ((1, 'b'), (2, 'a')), ((1, 'b'), (2, 'b')),
18902+
((2, 'a'), (2, 'b'))]
1886518903
sage: T.is_isomorphic(H.disjunctive_product(G))
1886618904
True
1886718905

@@ -18871,7 +18909,11 @@ def disjunctive_product(self, other):
1887118909
sage: J = DiGraph([('a', 'b')])
1887218910
sage: T = I.disjunctive_product(J)
1887318911
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'))]
18912+
[((0, 'a'), (0, 'b')), ((0, 'a'), (1, 'a')), ((0, 'a'), (1, 'b')),
18913+
((0, 'a'), (2, 'b')), ((0, 'b'), (1, 'a')), ((0, 'b'), (1, 'b')),
18914+
((1, 'a'), (0, 'b')), ((1, 'a'), (1, 'b')), ((1, 'a'), (2, 'a')),
18915+
((1, 'a'), (2, 'b')), ((1, 'b'), (2, 'a')), ((1, 'b'), (2, 'b')),
18916+
((2, 'a'), (0, 'b')), ((2, 'a'), (1, 'b')), ((2, 'a'), (2, 'b'))]
1887518917
sage: T.is_isomorphic(J.disjunctive_product(I))
1887618918
True
1887718919
"""
@@ -19047,8 +19089,7 @@ def is_transitively_reduced(self):
1904719089

1904819090
return self.is_forest()
1904919091

19050-
19051-
### Visualization
19092+
# Visualization
1905219093

1905319094
def _color_by_label(self, format='hex', as_function=False, default_color="black"):
1905419095
"""
@@ -19149,7 +19190,8 @@ def _color_by_label(self, format='hex', as_function=False, default_color="black"
1914919190
color_of_label = dict(zip(labels, colors))
1915019191
color_of_label = color_of_label.__getitem__
1915119192
elif isinstance(format, dict):
19152-
color_of_label = lambda label: format.get(label, default_color)
19193+
def color_of_label(label):
19194+
return format.get(label, default_color)
1915319195
else:
1915419196
# This assumes that ``format`` is already a function
1915519197
color_of_label = format
@@ -19218,7 +19260,6 @@ def set_latex_options(self, **kwds):
1921819260
opts = self.latex_options()
1921919261
opts.set_options(**kwds)
1922019262

19221-
1922219263
def layout(self, layout=None, pos=None, dim=2, save_pos=False, **options):
1922319264
"""
1922419265
Return a layout for the vertices of this graph.
@@ -19341,10 +19382,10 @@ def layout(self, layout=None, pos=None, dim=2, save_pos=False, **options):
1934119382
if pos is None:
1934219383
layout = 'default'
1934319384

19344-
if hasattr(self, "layout_%s"%layout):
19345-
pos = getattr(self, "layout_%s"%layout)(dim=dim, **options)
19385+
if hasattr(self, "layout_%s" % layout):
19386+
pos = getattr(self, "layout_%s" % layout)(dim=dim, **options)
1934619387
elif layout is not None:
19347-
raise ValueError("unknown layout algorithm: %s"%layout)
19388+
raise ValueError("unknown layout algorithm: %s" % layout)
1934819389

1934919390
if len(pos) < self.order():
1935019391
pos = self.layout_extend_randomly(pos, dim=dim)
@@ -19353,7 +19394,6 @@ def layout(self, layout=None, pos=None, dim=2, save_pos=False, **options):
1935319394
self.set_pos(pos, dim=dim)
1935419395
return pos
1935519396

19356-
1935719397
def layout_spring(self, by_component=True, **options):
1935819398
"""
1935919399
Return a spring layout for this graph.
@@ -19490,11 +19530,11 @@ def layout_extend_randomly(self, pos, dim=2):
1949019530
sage: (xmin, ymin) == (0, 0) and (xmax, ymax) == (1, 1)
1949119531
True
1949219532
"""
19493-
assert dim == 2 # 3d not yet implemented
19533+
assert dim == 2 # 3d not yet implemented
1949419534
from sage.misc.randstate import current_randstate
1949519535
random = current_randstate().python_random().random
1949619536

19497-
xmin, xmax,ymin, ymax = self._layout_bounding_box(pos)
19537+
xmin, xmax, ymin, ymax = self._layout_bounding_box(pos)
1949819538

1949919539
dx = xmax - xmin
1950019540
dy = ymax - ymin
@@ -19505,7 +19545,6 @@ def layout_extend_randomly(self, pos, dim=2):
1950519545
pos[v] = [xmin + dx * random(), ymin + dy * random()]
1950619546
return pos
1950719547

19508-
1950919548
def layout_circular(self, dim=2, center=(0, 0), radius=1, shift=0, angle=0, **options):
1951019549
r"""
1951119550
Return a circular layout for this graph
@@ -19947,9 +19986,9 @@ def _layout_bounding_box(self, pos):
1994719986
ys = [pos[v][1] for v in pos]
1994819987
if not xs:
1994919988
xmin = -1
19950-
xmax = 1
19989+
xmax = 1
1995119990
ymin = -1
19952-
ymax = 1
19991+
ymax = 1
1995319992
else:
1995419993
xmin = min(xs)
1995519994
xmax = max(xs)
@@ -20047,7 +20086,7 @@ def _circle_embedding(self, vertices, center=(0, 0), radius=1, shift=0, angle=0,
2004720086
pos = self._pos = {}
2004820087

2004920088
from math import sin, cos, pi
20050-
for i,v in enumerate(vertices):
20089+
for i, v in enumerate(vertices):
2005120090
i += shift
2005220091
# We round cos and sin to avoid results like 1.2246467991473532e-16
2005320092
# when asking for sin(pi)
@@ -20365,7 +20404,11 @@ def plot(self, **options):
2036520404

2036620405
::
2036720406

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)
20407+
sage: D = DiGraph({0: [1, 10, 19], 1: [8, 2], 2: [3, 6], 3: [19, 4],
20408+
....: 4: [17, 5], 5: [6, 15], 6: [7], 7: [8, 14],
20409+
....: 8: [9], 9: [10, 13], 10: [11], 11: [12, 18],
20410+
....: 12: [16, 13], 13: [14], 14: [15], 15: [16],
20411+
....: 16: [17], 17: [18], 18: [19]}, sparse=True)
2036920412
sage: for u,v,l in D.edges(sort=False):
2037020413
....: D.set_edge_label(u, v, '(' + str(u) + ',' + str(v) + ')')
2037120414
sage: D.plot(edge_labels=True, layout='circular').show()
@@ -20559,7 +20602,7 @@ def show(self, method="matplotlib", **kwds):
2055920602
return
2056020603
from sage.misc.viewer import browser
2056120604
import os
20562-
os.system('%s %s 2>/dev/null 1>/dev/null &'% (browser(), filename))
20605+
os.system('%s %s 2>/dev/null 1>/dev/null &' % (browser(), filename))
2056320606
return
2056420607

2056520608
from .graph_plot import graphplot_options

0 commit comments

Comments
 (0)