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

Commit 444a55a

Browse files
committed
trac #34318: clean src/sage/graphs/generic_graph.py - part 1
1 parent 12be2d9 commit 444a55a

File tree

1 file changed

+78
-57
lines changed

1 file changed

+78
-57
lines changed

src/sage/graphs/generic_graph.py

+78-57
Original file line numberDiff line numberDiff line change
@@ -22965,17 +22965,18 @@ def is_hamiltonian(self, solver=None, constraint_generation=None,
2296522965
except EmptySetError:
2296622966
return False
2296722967

22968+
2296822969
def is_isomorphic(self, other, certificate=False, verbosity=0, edge_labels=False):
2296922970
r"""
2297022971
Tests for isomorphism between self and other.
2297122972

2297222973
INPUT:
2297322974

22974-
- ``certificate`` -- if True, then output is `(a, b)`, where `a`
22975-
is a boolean and `b` is either a map or ``None``.
22975+
- ``certificate`` -- if True, then output is `(a, b)`, where `a`
22976+
is a boolean and `b` is either a map or ``None``
2297622977

22977-
- ``edge_labels`` -- boolean (default: ``False``); if ``True`` allows
22978-
only permutations respecting edge labels.
22978+
- ``edge_labels`` -- boolean (default: ``False``); if ``True`` allows
22979+
only permutations respecting edge labels
2297922980

2298022981
OUTPUT:
2298122982

@@ -23200,10 +23201,12 @@ def is_isomorphic(self, other, certificate=False, verbosity=0, edge_labels=False
2320023201
if not self.order() and not other.order():
2320123202
return (True, None) if certificate else True
2320223203

23203-
if (self.is_directed() != other.is_directed() or self.order() != other.order() or
23204-
self.size() != other.size() or self.degree_sequence() != other.degree_sequence()):
23204+
if (self.is_directed() != other.is_directed() or
23205+
self.order() != other.order() or
23206+
self.size() != other.size() or
23207+
self.degree_sequence() != other.degree_sequence()):
2320523208
if certificate:
23206-
return False,None
23209+
return False, None
2320723210
else:
2320823211
return False
2320923212

@@ -23215,36 +23218,53 @@ def is_isomorphic(self, other, certificate=False, verbosity=0, edge_labels=False
2321523218
if edge_labels and sorted(self.edge_labels(), key=str) != sorted(other.edge_labels(), key=str):
2321623219
return (False, None) if certificate else False
2321723220
else:
23218-
G, partition, relabeling, G_edge_labels = graph_isom_equivalent_non_edge_labeled_graph(self, return_relabeling=True, ignore_edge_labels=(not edge_labels), return_edge_labels=True)
23219-
self_vertices = sum(partition,[])
23220-
G2, partition2, relabeling2, G2_edge_labels = graph_isom_equivalent_non_edge_labeled_graph(other, return_relabeling=True, ignore_edge_labels=(not edge_labels), return_edge_labels=True)
23221+
ret = graph_isom_equivalent_non_edge_labeled_graph(self, return_relabeling=True,
23222+
ignore_edge_labels=(not edge_labels),
23223+
return_edge_labels=True)
23224+
G, partition, relabeling, G_edge_labels = ret
23225+
self_vertices = sum(partition, [])
23226+
ret = graph_isom_equivalent_non_edge_labeled_graph(other, return_relabeling=True,
23227+
ignore_edge_labels=(not edge_labels),
23228+
return_edge_labels=True)
23229+
G2, partition2, relabeling2, G2_edge_labels = ret
23230+
2322123231
if [len(_) for _ in partition] != [len(_) for _ in partition2]:
2322223232
return (False, None) if certificate else False
23223-
multilabel = (lambda e:e) if edge_labels else (lambda e:[[None, el[1]] for el in e])
23233+
23234+
if edge_labels:
23235+
def multilabel(e):
23236+
return e
23237+
else:
23238+
def multilabel(e):
23239+
return [[None, el[1]] for el in e]
23240+
2322423241
if [multilabel(_) for _ in G_edge_labels] != [multilabel(_) for _ in G2_edge_labels]:
2322523242
return (False, None) if certificate else False
23226-
partition2 = sum(partition2,[])
23243+
partition2 = sum(partition2, [])
2322723244
other_vertices = partition2
2322823245
else:
2322923246
G = self
2323023247
partition = [self_vertices]
2323123248
G2 = other
2323223249
partition2 = other_vertices
23233-
G_to = {u: i for i,u in enumerate(self_vertices)}
23234-
from sage.graphs.graph import Graph
23235-
from sage.graphs.digraph import DiGraph
23236-
DoDG = DiGraph if self._directed else Graph
23250+
G_to = {u: i for i, u in enumerate(self_vertices)}
23251+
if self._directed:
23252+
from sage.graphs.digraph import DiGraph
23253+
DoDG = DiGraph
23254+
else:
23255+
from sage.graphs.graph import Graph
23256+
DoDG = Graph
2323723257
H = DoDG(len(self_vertices), loops=G.allows_loops())
2323823258
HB = H._backend
23239-
for u,v in G.edge_iterator(labels=False):
23259+
for u, v in G.edge_iterator(labels=False):
2324023260
HB.add_edge(G_to[u], G_to[v], None, G._directed)
2324123261
G = HB.c_graph()[0]
2324223262
partition = [[G_to[vv] for vv in cell] for cell in partition]
2324323263
GC = G
23244-
G2_to = {u: i for i,u in enumerate(other_vertices)}
23264+
G2_to = {u: i for i, u in enumerate(other_vertices)}
2324523265
H2 = DoDG(len(other_vertices), loops=G2.allows_loops())
2324623266
H2B = H2._backend
23247-
for u,v in G2.edge_iterator(labels=False):
23267+
for u, v in G2.edge_iterator(labels=False):
2324823268
H2B.add_edge(G2_to[u], G2_to[v], None, G2._directed)
2324923269
G2 = H2B.c_graph()[0]
2325023270
partition2 = [G2_to[vv] for vv in partition2]
@@ -23448,7 +23468,6 @@ class by some canonization function `c`. If `G` and `H` are graphs,
2344823468
....: assert gcan0 == gcan1, (edges, labels, part, pp)
2344923469
....: assert gcan0 == gcan2, (edges, labels, part, pp)
2345023470
"""
23451-
2345223471
# Check parameter combinations
2345323472
if algorithm not in [None, 'sage', 'bliss']:
2345423473
raise ValueError("'algorithm' must be equal to 'bliss', 'sage', or None")
@@ -23492,29 +23511,29 @@ class by some canonization function `c`. If `G` and `H` are graphs,
2349223511
if edge_labels or self.has_multiple_edges():
2349323512
G, partition, relabeling = graph_isom_equivalent_non_edge_labeled_graph(self, partition, return_relabeling=True)
2349423513
G_vertices = list(chain(*partition))
23495-
G_to = {u: i for i,u in enumerate(G_vertices)}
23514+
G_to = {u: i for i, u in enumerate(G_vertices)}
2349623515
DoDG = DiGraph if self._directed else Graph
2349723516
H = DoDG(len(G_vertices), loops=G.allows_loops())
2349823517
HB = H._backend
23499-
for u,v in G.edge_iterator(labels=False):
23518+
for u, v in G.edge_iterator(labels=False):
2350023519
HB.add_edge(G_to[u], G_to[v], None, G._directed)
2350123520
GC = HB.c_graph()[0]
2350223521
partition = [[G_to[vv] for vv in cell] for cell in partition]
23503-
a,b,c = search_tree(GC, partition, certificate=True, dig=dig)
23522+
a, b, c = search_tree(GC, partition, certificate=True, dig=dig)
2350423523
# c is a permutation to the canonical label of G, which depends only on isomorphism class of self.
2350523524
H = copy(self)
2350623525
c_new = {v: c[G_to[relabeling[v]]] for v in self}
2350723526
else:
2350823527
G_vertices = list(chain(*partition))
23509-
G_to = {u: i for i,u in enumerate(G_vertices)}
23528+
G_to = {u: i for i, u in enumerate(G_vertices)}
2351023529
DoDG = DiGraph if self._directed else Graph
2351123530
H = DoDG(len(G_vertices), loops=self.allows_loops())
2351223531
HB = H._backend
2351323532
for u, v in self.edge_iterator(labels=False):
2351423533
HB.add_edge(G_to[u], G_to[v], None, self._directed)
2351523534
GC = HB.c_graph()[0]
2351623535
partition = [[G_to[vv] for vv in cell] for cell in partition]
23517-
a,b,c = search_tree(GC, partition, certificate=True, dig=dig)
23536+
a, b, c = search_tree(GC, partition, certificate=True, dig=dig)
2351823537
H = copy(self)
2351923538
c_new = {v: c[G_to[v]] for v in G_to}
2352023539
H.relabel(c_new)
@@ -23523,8 +23542,8 @@ class by some canonization function `c`. If `G` and `H` are graphs,
2352323542
else:
2352423543
return H
2352523544

23526-
def is_cayley(self, return_group = False, mapping = False,
23527-
generators = False, allow_disconnected = False):
23545+
def is_cayley(self, return_group=False, mapping=False,
23546+
generators=False, allow_disconnected=False):
2352823547
r"""
2352923548
Check whether the graph is a Cayley graph.
2353023549

@@ -23629,11 +23648,11 @@ def is_cayley(self, return_group = False, mapping = False,
2362923648
....: generators = True)
2363023649
(False, False, False)
2363123650
"""
23632-
if self.order() == 0:
23651+
if not self:
2363323652
n = return_group + mapping + generators
23634-
if n == 0:
23653+
if not n:
2363523654
return False
23636-
return tuple([False] * (n+1))
23655+
return tuple([False] * (n + 1))
2363723656

2363823657
compute_map = mapping or generators
2363923658
certificate = return_group or compute_map
@@ -23642,7 +23661,7 @@ def is_cayley(self, return_group = False, mapping = False,
2364223661
if allow_disconnected and self.is_vertex_transitive():
2364323662
C = self.connected_components_subgraphs()
2364423663
if certificate:
23645-
c, CG = C[0].is_cayley(return_group = True)
23664+
c, CG = C[0].is_cayley(return_group=True)
2364623665
if c:
2364723666
from sage.groups.perm_gps.permgroup import PermutationGroup
2364823667
I = [C[0].is_isomorphic(g, certificate=True)[1] for g in C]
@@ -23652,24 +23671,24 @@ def is_cayley(self, return_group = False, mapping = False,
2365223671
for h in CG.gens()] + \
2365323672
[[tuple([M[v] for M in I])
2365423673
for v in C[0].vertices(sort=False)]]
23655-
G = PermutationGroup(gens, domain = self.vertices(sort=False))
23674+
G = PermutationGroup(gens, domain=self.vertices(sort=False))
2365623675
else:
23657-
c = C[0].is_cayley(return_group = False)
23658-
elif not self.allows_loops() and not self.allows_multiple_edges() and \
23659-
self.density() > Rational(1)/Rational(2):
23676+
c = C[0].is_cayley(return_group=False)
23677+
elif (not self.allows_loops() and not self.allows_multiple_edges() and
23678+
self.density() > Rational(1)/Rational(2)):
2366023679
if certificate:
23661-
c, G = self.complement().is_cayley(return_group = True,
23662-
allow_disconnected = True)
23680+
c, G = self.complement().is_cayley(return_group=True,
23681+
allow_disconnected=True)
2366323682
else:
23664-
c = self.complement().is_cayley(return_group = False,
23665-
allow_disconnected = True)
23683+
c = self.complement().is_cayley(return_group=False,
23684+
allow_disconnected=True)
2366623685
else:
2366723686
A = self.automorphism_group()
2366823687
if certificate:
23669-
G = A.has_regular_subgroup(return_group = True)
23688+
G = A.has_regular_subgroup(return_group=True)
2367023689
c = G is not None
2367123690
else:
23672-
c = A.has_regular_subgroup(return_group = False)
23691+
c = A.has_regular_subgroup(return_group=False)
2367323692
if c and compute_map:
2367423693
v = next(self.vertex_iterator())
2367523694
map = {(f**-1)(v): f for f in G}
@@ -23912,7 +23931,7 @@ def katz_matrix(self, alpha, nonedgesonly=False, vertices=None):
2391223931
raise ValueError('the parameter alpha must be strictly positive')
2391323932

2391423933
n = self.order()
23915-
if n == 0 :
23934+
if not n:
2391623935
raise ValueError('graph is empty')
2391723936
if vertices is None:
2391823937
vertices = self.vertices(sort=True)
@@ -23930,15 +23949,15 @@ def katz_matrix(self, alpha, nonedgesonly=False, vertices=None):
2393023949
raise ValueError('the parameter alpha must be less than the reciprocal of the spectral radius of the graph')
2393123950

2393223951
In = matrix.identity(n)
23933-
K = (In - alpha * A.transpose()).inverse() - In
23952+
K = (In - alpha * A.transpose()).inverse() - In
2393423953
if nonedgesonly:
2393523954
onesmat = matrix(QQ, n, n, lambda i, j: 1)
2393623955
Missing = onesmat - A - In
2393723956
return K.elementwise_product(Missing)
2393823957
else:
2393923958
return K
2394023959

23941-
def katz_centrality(self, alpha , u=None):
23960+
def katz_centrality(self, alpha, u=None):
2394223961
r"""
2394323962
Return the Katz centrality of vertex `u`.
2394423963

@@ -24118,7 +24137,7 @@ def edge_polytope(self, backend=None):
2411824137
dim = self.num_verts()
2411924138
e = identity_matrix(dim).rows()
2412024139
dic = {v: e[i] for i, v in enumerate(self)}
24121-
vertices = ((dic[i] + dic[j]) for i,j in self.edge_iterator(sort_vertices=False, labels=False))
24140+
vertices = ((dic[i] + dic[j]) for i, j in self.edge_iterator(sort_vertices=False, labels=False))
2412224141
parent = Polyhedra(ZZ, dim, backend=backend)
2412324142
return parent([vertices, [], []], None)
2412424143

@@ -24249,13 +24268,13 @@ def symmetric_edge_polytope(self, backend=None):
2424924268
dim = self.num_verts()
2425024269
e = identity_matrix(dim).rows()
2425124270
dic = {v: e[i] for i, v in enumerate(self)}
24252-
vertices = chain(((dic[i] - dic[j]) for i,j in self.edge_iterator(sort_vertices=False, labels=False)),
24253-
((dic[j] - dic[i]) for i,j in self.edge_iterator(sort_vertices=False, labels=False)))
24271+
vertices = chain(((dic[i] - dic[j]) for i, j in self.edge_iterator(sort_vertices=False, labels=False)),
24272+
((dic[j] - dic[i]) for i, j in self.edge_iterator(sort_vertices=False, labels=False)))
2425424273
parent = Polyhedra(ZZ, dim, backend=backend)
2425524274
return parent([vertices, [], []], None)
2425624275

2425724276

24258-
def tachyon_vertex_plot(g, bgcolor=(1,1,1),
24277+
def tachyon_vertex_plot(g, bgcolor=(1, 1, 1),
2425924278
vertex_colors=None,
2426024279
vertex_size=0.06,
2426124280
pos3d=None,
@@ -24286,12 +24305,12 @@ def tachyon_vertex_plot(g, bgcolor=(1,1,1),
2428624305
from math import sqrt
2428724306
from sage.plot.plot3d.tachyon import Tachyon
2428824307

24289-
c = [0,0,0]
24308+
c = [0, 0, 0]
2429024309
r = []
2429124310
verts = list(g)
2429224311

2429324312
if vertex_colors is None:
24294-
vertex_colors = {(1,0,0): verts}
24313+
vertex_colors = {(1, 0, 0): verts}
2429524314
try:
2429624315
for v in verts:
2429724316
c[0] += pos3d[v][0]
@@ -24324,14 +24343,17 @@ def tachyon_vertex_plot(g, bgcolor=(1,1,1),
2432424343
i = 0
2432524344
for color in vertex_colors:
2432624345
i += 1
24327-
TT.texture('node_color_%d'%i, ambient=0.1, diffuse=0.9,
24346+
TT.texture('node_color_%d' % i, ambient=0.1, diffuse=0.9,
2432824347
specular=0.03, opacity=1.0, color=color)
2432924348
for v in vertex_colors[color]:
24330-
TT.sphere((pos3d[v][0], pos3d[v][1], pos3d[v][2]), vertex_size, 'node_color_%d'%i)
24349+
TT.sphere((pos3d[v][0], pos3d[v][1], pos3d[v][2]), vertex_size, 'node_color_%d' % i)
2433124350

2433224351
return TT, pos3d
2433324352

24334-
def graph_isom_equivalent_non_edge_labeled_graph(g, partition=None, standard_label=None, return_relabeling=False, return_edge_labels=False, inplace=False, ignore_edge_labels=False):
24353+
24354+
def graph_isom_equivalent_non_edge_labeled_graph(g, partition=None, standard_label=None,
24355+
return_relabeling=False, return_edge_labels=False,
24356+
inplace=False, ignore_edge_labels=False):
2433524357
r"""
2433624358
Helper function for canonical labeling of edge labeled (di)graphs.
2433724359

@@ -24549,13 +24571,12 @@ def graph_isom_equivalent_non_edge_labeled_graph(g, partition=None, standard_lab
2454924571

2455024572
# We build the list of distinct edge labels
2455124573
edge_labels = []
24552-
for _,_,label in G.edge_iterator():
24574+
for _, _, label in G.edge_iterator():
2455324575
if label != standard_label and label not in edge_labels:
2455424576
edge_labels.append(label)
2455524577

2455624578
edge_labels = sorted(edge_labels, key=str)
2455724579

24558-
2455924580
# We now add to G, for each edge (u, v, l), a new vertex i in [n..n + m] and
2456024581
# arcs (u, i, None) and (i, v, None). We record for each distinct label l
2456124582
# the list of added vertices.
@@ -24571,7 +24592,7 @@ def graph_isom_equivalent_non_edge_labeled_graph(g, partition=None, standard_lab
2457124592
edges = list(G._backend.iterator_edges(G, True))
2457224593

2457324594
i = G_order
24574-
for u,v,l in edges:
24595+
for u, v, l in edges:
2457524596
if l != standard_label:
2457624597
for el, part in edge_partition:
2457724598
if el == l:
@@ -24621,7 +24642,7 @@ def graph_isom_equivalent_non_edge_labeled_graph(g, partition=None, standard_lab
2462124642

2462224643
else:
2462324644
# Flatten edge_partition to [list of edges, list of edges, ...]
24624-
edge_partition = [part for _,part in edge_partition]
24645+
edge_partition = [part for _, part in edge_partition]
2462524646

2462624647
new_partition = [part for part in chain(partition, edge_partition) if part]
2462724648

0 commit comments

Comments
 (0)