Skip to content

Commit 991a463

Browse files
author
Release Manager
committed
gh-36278: `sage.graphs`: some care with return ... else statements in some .pyx files We change this kind of statements in files `src/sage/graphs/*.pyx`: ```py if ....: return ... else: return ... ``` to ```py if ....: return ... return ... ``` ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> <!-- If your change requires a documentation PR, please link it appropriately --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> <!-- Feel free to remove irrelevant items. --> - [x] The title is concise, informative, and self-explanatory. - [x] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation accordingly. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on - #12345: short description why this is a dependency - #34567: ... --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> URL: #36278 Reported by: David Coudert Reviewer(s): github-actions[bot], Kwankyu Lee
2 parents 342e32b + 58af879 commit 991a463

13 files changed

+192
-239
lines changed

src/sage/graphs/bliss.pyx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,7 @@ cdef canonical_form_from_edge_list(int Vnr, list Vout, list Vin, int Lnr=1, list
375375

376376
if certificate:
377377
return new_edges, relabel
378-
else:
379-
return new_edges
378+
return new_edges
380379

381380

382381
cpdef canonical_form(G, partition=None, return_graph=False, use_edge_labels=True, certificate=False):

src/sage/graphs/centrality.pyx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ def centrality_betweenness(G, bint exact=False, bint normalize=True):
119119
"""
120120
if exact:
121121
return centrality_betweenness_C(G, <mpq_t> 0, normalize=normalize)
122-
else:
123-
return centrality_betweenness_C(G, <double>0, normalize=normalize)
122+
return centrality_betweenness_C(G, <double>0, normalize=normalize)
124123

125124

126125
@cython.cdivision(True)

src/sage/graphs/comparability.pyx

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -306,15 +306,13 @@ def greedy_is_comparability(g, no_certificate=False, equivalence_class=False):
306306
# added twice.
307307
return True, sorted(set(edges))
308308
309-
else:
310-
return True
311-
else:
312-
if no_certificate:
313-
certif.append(certif[0])
314-
cycle = [v for v, _ in certif]
315-
return False, cycle
316-
else:
317-
return False
309+
return True
310+
311+
if no_certificate:
312+
certif.append(certif[0])
313+
cycle = [v for v, _ in certif]
314+
return False, cycle
315+
return False
318316
319317
320318
def greedy_is_comparability_with_certificate(g, certificate=False):
@@ -361,8 +359,7 @@ def greedy_is_comparability_with_certificate(g, certificate=False):
361359
if not isit:
362360
if certificate:
363361
return False, certif
364-
else:
365-
return False
362+
return False
366363
367364
elif not certificate:
368365
return True
@@ -552,8 +549,7 @@ def is_comparability(g, algorithm="greedy", certificate=False, check=True,
552549
if certificate:
553550
from sage.graphs.digraph import DiGraph
554551
return True, DiGraph(g)
555-
else:
556-
return True
552+
return True
557553
558554
if algorithm == "greedy":
559555
comparability_test = greedy_is_comparability_with_certificate(g, certificate=certificate)
@@ -677,46 +673,44 @@ def is_permutation(g, algorithm="greedy", certificate=False, check=True,
677673
....: break
678674

679675
"""
680-
if certificate:
681-
682-
# First poset, we stop if it fails
683-
isit, certif = is_comparability(g, algorithm=algorithm, certificate=True,
684-
solver=solver, verbose=verbose)
685-
if not isit:
686-
return False, certif
687-
688-
# Second poset
689-
isit, co_certif = is_comparability(g.complement(), algorithm=algorithm, certificate=True,
690-
solver=solver, verbose=verbose)
691-
if not isit:
692-
return False, co_certif
693-
694-
# Building the two orderings
695-
tmp = list(co_certif.edges(labels=False, sort=False))
696-
for u, v in certif.edge_iterator(labels=False):
697-
co_certif.add_edge(v, u)
698-
certif.add_edges(tmp)
699-
700-
ordering = certif.topological_sort()
701-
co_ordering = co_certif.topological_sort()
702-
703-
# Try to build the Permutation graph from the permutations, just to make
704-
# sure nothing weird happened !
705-
if check:
706-
from sage.graphs.graph_generators import GraphGenerators
707-
pg = GraphGenerators().PermutationGraph(ordering, co_ordering)
708-
if not pg.is_isomorphic(g):
709-
raise ValueError("There is a mistake somewhere ! It looks like "
710-
"the Permutation Graph model computed does "
711-
"not match the input graph !")
712-
713-
return True, (ordering, co_ordering)
714-
715-
# No certificate... A piece of cake
716-
else:
676+
if not certificate:
677+
# No certificate... A piece of cake
717678
return (is_comparability(g, algorithm=algorithm, solver=solver, verbose=verbose) and
718679
is_comparability(g.complement(), algorithm=algorithm, solver=solver, verbose=verbose))
719680
681+
# First poset, we stop if it fails
682+
isit, certif = is_comparability(g, algorithm=algorithm, certificate=True,
683+
solver=solver, verbose=verbose)
684+
if not isit:
685+
return False, certif
686+
687+
# Second poset
688+
isit, co_certif = is_comparability(g.complement(), algorithm=algorithm, certificate=True,
689+
solver=solver, verbose=verbose)
690+
if not isit:
691+
return False, co_certif
692+
693+
# Building the two orderings
694+
tmp = list(co_certif.edges(labels=False, sort=False))
695+
for u, v in certif.edge_iterator(labels=False):
696+
co_certif.add_edge(v, u)
697+
certif.add_edges(tmp)
698+
699+
ordering = certif.topological_sort()
700+
co_ordering = co_certif.topological_sort()
701+
702+
# Try to build the Permutation graph from the permutations, just to make
703+
# sure nothing weird happened !
704+
if check:
705+
from sage.graphs.graph_generators import GraphGenerators
706+
pg = GraphGenerators().PermutationGraph(ordering, co_ordering)
707+
if not pg.is_isomorphic(g):
708+
raise ValueError("There is a mistake somewhere ! It looks like "
709+
"the Permutation Graph model computed does "
710+
"not match the input graph !")
711+
712+
return True, (ordering, co_ordering)
713+
720714
721715
def is_transitive(g, certificate=False):
722716
r"""

src/sage/graphs/connectivity.pyx

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,8 +1177,7 @@ def edge_connectivity(G,
11771177
return 0
11781178
elif vertices:
11791179
return [0, [], [{}, {}]]
1180-
else:
1181-
return [0, []]
1180+
return [0, []]
11821181

11831182
if implementation == "boost":
11841183
from sage.graphs.base.boost_graph import edge_connectivity
@@ -1275,29 +1274,27 @@ def edge_connectivity(G,
12751274
if value_only:
12761275
return obj
12771276

1278-
else:
1279-
val = [obj]
1280-
1281-
in_set = p.get_values(in_set, convert=bool, tolerance=integrality_tolerance)
1277+
val = [obj]
1278+
in_set = p.get_values(in_set, convert=bool, tolerance=integrality_tolerance)
12821279

1283-
if g.is_directed():
1284-
edges = [(u, v, l) for u, v, l in g.edge_iterator() if in_cut[u, v]]
1285-
else:
1286-
edges = [(u, v, l) for u, v, l in g.edge_iterator() if in_cut[frozenset((u, v))]]
1280+
if g.is_directed():
1281+
edges = [(u, v, l) for u, v, l in g.edge_iterator() if in_cut[u, v]]
1282+
else:
1283+
edges = [(u, v, l) for u, v, l in g.edge_iterator() if in_cut[frozenset((u, v))]]
12871284

1288-
val.append(edges)
1285+
val.append(edges)
12891286

1290-
if vertices:
1291-
a = {}
1292-
b = {}
1293-
for v in g:
1294-
if in_set[0, v]:
1295-
a.add(v)
1296-
else:
1297-
b.add(v)
1298-
val.append([a, b])
1287+
if vertices:
1288+
a = {}
1289+
b = {}
1290+
for v in g:
1291+
if in_set[0, v]:
1292+
a.add(v)
1293+
else:
1294+
b.add(v)
1295+
val.append([a, b])
12991296

1300-
return val
1297+
return val
13011298

13021299

13031300
def vertex_connectivity(G, value_only=True, sets=False, k=None, solver=None, verbose=0,
@@ -1503,8 +1500,7 @@ def vertex_connectivity(G, value_only=True, sets=False, k=None, solver=None, ver
15031500
return max(g.order() - 1, 0)
15041501
elif not sets:
15051502
return max(g.order() - 1, 0), []
1506-
else:
1507-
return max(g.order() - 1, 0), [], [[], []]
1503+
return max(g.order() - 1, 0), [], [[], []]
15081504

15091505
if value_only:
15101506
if G.is_directed():
@@ -3298,8 +3294,7 @@ cdef class TriconnectivitySPQR:
32983294
cdef _LinkedListNode * head = _LinkedList_get_head(self.highpt[v])
32993295
if head:
33003296
return head.data
3301-
else:
3302-
return 0
3297+
return 0
33033298

33043299
cdef __del_high(self, int e_index):
33053300
"""

src/sage/graphs/distances_all_pairs.pyx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -629,8 +629,7 @@ def is_distance_regular(G, parameters=False):
629629
bi[diameter] = None
630630
ci[0] = None
631631
return bi, ci
632-
else:
633-
return True
632+
return True
634633

635634

636635
###################################
@@ -1882,8 +1881,7 @@ def diameter(G, algorithm=None, source=None):
18821881
if LB < 0 or LB > n:
18831882
from sage.rings.infinity import Infinity
18841883
return +Infinity
1885-
else:
1886-
return int(LB)
1884+
return int(LB)
18871885

18881886

18891887
###########
@@ -2722,8 +2720,7 @@ def floyd_warshall(gg, paths=True, distances=False):
27222720
if not gverts:
27232721
if distances and paths:
27242722
return {}, {}
2725-
else:
2726-
return {}
2723+
return {}
27272724

27282725
cdef unsigned int n = max(gverts) + 1
27292726

src/sage/graphs/generic_graph_pyx.pyx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,11 +487,10 @@ def small_integer_to_graph6(n):
487487
"""
488488
if n < 63:
489489
return chr(n + 63)
490-
else:
491-
# get 18-bit rep of n
492-
n = int_to_binary_string(n)
493-
n = '0'*(18 - len(n)) + n
494-
return chr(126) + binary_string_to_graph6(n)
490+
# get 18-bit rep of n
491+
n = int_to_binary_string(n)
492+
n = '0'*(18 - len(n)) + n
493+
return chr(126) + binary_string_to_graph6(n)
495494

496495

497496
def length_and_string_from_graph6(s):

src/sage/graphs/genus.pyx

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -597,31 +597,31 @@ def simple_connected_graph_genus(G, set_embedding=False, check=True, minimal=Tru
597597

598598
if minimal and G.is_planar(set_embedding=set_embedding):
599599
return 0
600+
601+
if check:
602+
if not G.is_connected():
603+
raise ValueError("Cannot compute the genus of a disconnected graph")
604+
605+
if G.is_directed() or G.has_multiple_edges() or G.has_loops():
606+
G = G.to_simple()
607+
608+
G, vmap = G.relabel(inplace=False, return_map=True)
609+
backmap = {u: v for v, u in vmap.items()}
610+
G = Graph(G, sparse=False)
611+
GG = simple_connected_genus_backtracker(G._backend.c_graph()[0])
612+
613+
if minimal:
614+
style = 1
615+
cutoff = 1
600616
else:
601-
if check:
602-
if not G.is_connected():
603-
raise ValueError("Cannot compute the genus of a disconnected graph")
604-
605-
if G.is_directed() or G.has_multiple_edges() or G.has_loops():
606-
G = G.to_simple()
607-
608-
G, vmap = G.relabel(inplace=False, return_map=True)
609-
backmap = {u: v for v, u in vmap.items()}
610-
G = Graph(G, sparse=False)
611-
GG = simple_connected_genus_backtracker(G._backend.c_graph()[0])
612-
613-
if minimal:
614-
style = 1
615-
cutoff = 1
616-
else:
617-
style = 2
618-
cutoff = 1 + (G.num_edges() - G.num_verts()) / 2 # rounding here is ok
619-
620-
g = GG.genus(style=style, cutoff=cutoff, record_embedding=set_embedding)
621-
if set_embedding:
622-
oE = {}
623-
E = GG.get_embedding()
624-
for v in E:
625-
oE[backmap[v]] = [backmap[x] for x in E[v]]
626-
oG.set_embedding(oE)
627-
return g
617+
style = 2
618+
cutoff = 1 + (G.num_edges() - G.num_verts()) / 2 # rounding here is ok
619+
620+
g = GG.genus(style=style, cutoff=cutoff, record_embedding=set_embedding)
621+
if set_embedding:
622+
oE = {}
623+
E = GG.get_embedding()
624+
for v in E:
625+
oE[backmap[v]] = [backmap[x] for x in E[v]]
626+
oG.set_embedding(oE)
627+
return g

src/sage/graphs/graph_coloring.pyx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,7 @@ def vertex_coloring(g, k=None, value_only=False, hex_colors=False, solver=None,
555555
return 0
556556
elif hex_colors:
557557
return dict()
558-
else:
559-
return []
558+
return []
560559
# - Independent set
561560
if not g.size():
562561
if value_only:
@@ -1754,11 +1753,10 @@ def round_robin(n):
17541753
g.set_edge_label(n - 1, i, i)
17551754
for j in range(1, (n - 1) // 2 + 1):
17561755
g.set_edge_label(my_mod(i - j, n - 1), my_mod(i + j, n - 1), i)
1757-
return g
17581756
else:
17591757
g = round_robin(n + 1)
17601758
g.delete_vertex(n)
1761-
return g
1759+
return g
17621760

17631761

17641762
def linear_arboricity(g, plus_one=None, hex_colors=False, value_only=False,

src/sage/graphs/hyperbolicity.pyx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,7 @@ cdef tuple hyperbolicity_basic_algorithm(int N,
327327
# Last, we return the computed value and the certificate
328328
if h_LB != -1:
329329
return (h_LB, certificate)
330-
else:
331-
return (-1, [])
330+
return (-1, [])
332331

333332

334333
######################################################################
@@ -825,10 +824,10 @@ cdef tuple hyperbolicity_BCCM(int N,
825824
# Last, we return the computed value and the certificate
826825
if not certificate:
827826
return (-1, [], h_UB)
828-
else:
829-
# When using far-apart pairs, the loops may end before improving the
830-
# upper-bound
831-
return (h, certificate, h_UB)
827+
828+
# When using far-apart pairs, the loops may end before improving the
829+
# upper-bound
830+
return (h, certificate, h_UB)
832831

833832

834833
######################################################################
@@ -1043,10 +1042,10 @@ cdef tuple hyperbolicity_CCL(int N,
10431042
# Last, we return the computed value and the certificate
10441043
if not certificate:
10451044
return (-1, [], h_UB)
1046-
else:
1047-
# When using far-apart pairs, the loops may end before improving the
1048-
# upper-bound
1049-
return (h, certificate, h_UB if GOTO_RETURN else h)
1045+
1046+
# When using far-apart pairs, the loops may end before improving the
1047+
# upper-bound
1048+
return (h, certificate, h_UB if GOTO_RETURN else h)
10501049

10511050

10521051
def hyperbolicity(G,

0 commit comments

Comments
 (0)