Skip to content

Commit 7ca8f0e

Browse files
committed
some care in line_graph.pyx
1 parent a07eb9e commit 7ca8f0e

File tree

1 file changed

+43
-50
lines changed

1 file changed

+43
-50
lines changed

src/sage/graphs/line_graph.pyx

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,13 @@ def is_line_graph(g, certificate=False):
232232
R, isom = root_graph(g)
233233
if certificate:
234234
return True, R, isom
235-
else:
236-
return True
235+
return True
237236
except ValueError as VE:
238237
if str(VE) == "this graph is not a line graph !":
239238
# g is not a line graph
240239
if certificate:
241240
return False, get_certificate(g)
242-
else:
243-
return False
241+
return False
244242
raise VE
245243
246244
# g is not connected, so we apply the above procedure to each connected
@@ -256,15 +254,13 @@ def is_line_graph(g, certificate=False):
256254
# gg is not a line graph
257255
if certificate:
258256
return False, get_certificate(gg)
259-
else:
260-
return False
257+
return False
261258
raise VE
262259
263260
if certificate:
264261
_, isom = g.is_isomorphic(R.line_graph(labels=False), certificate=True)
265262
return True, R, isom
266-
else:
267-
return True
263+
return True
268264
269265
270266
def line_graph(g, labels=True):
@@ -366,54 +362,52 @@ def line_graph(g, labels=True):
366362
G.add_edges((e, f) for e in g.incoming_edge_iterator(v, labels=labels)
367363
for f in g.outgoing_edge_iterator(v, labels=labels))
368364
return G
369-
else:
370-
from sage.graphs.graph import Graph
371-
G = Graph()
372-
373-
# We must sort the edges' endpoints so that (1,2,None) is seen as the
374-
# same edge as (2,1,None).
375-
#
376-
# We do so by comparing hashes, just in case all the natural order (<)
377-
# on vertices would not be a total order (for instance when vertices are
378-
# sets). If two adjacent vertices have the same hash, then we store the
379-
# pair in the dictionary of conflicts
380365
381-
# 1) List of vertices in the line graph
382-
383-
for e in g.edge_iterator(labels=labels):
366+
from sage.graphs.graph import Graph
367+
G = Graph()
368+
369+
# We must sort the edges' endpoints so that (1,2,None) is seen as the
370+
# same edge as (2,1,None).
371+
#
372+
# We do so by comparing hashes, just in case all the natural order (<)
373+
# on vertices would not be a total order (for instance when vertices are
374+
# sets). If two adjacent vertices have the same hash, then we store the
375+
# pair in the dictionary of conflicts
376+
377+
# 1) List of vertices in the line graph
378+
for e in g.edge_iterator(labels=labels):
379+
if hash(e[0]) < hash(e[1]):
380+
elist.append(e)
381+
elif hash(e[0]) > hash(e[1]):
382+
elist.append((e[1], e[0]) + e[2:])
383+
else:
384+
# Settle the conflict arbitrarily
385+
conflicts[e] = e
386+
conflicts[(e[1], e[0]) + e[2:]] = e
387+
elist.append(e)
388+
389+
G.add_vertices(elist)
390+
391+
# 2) adjacencies in the line graph
392+
for v in g:
393+
elist = []
394+
395+
# Add the edge to the list, according to hashes, as previously
396+
for e in g.edge_iterator(v, labels=labels):
384397
if hash(e[0]) < hash(e[1]):
385398
elist.append(e)
386399
elif hash(e[0]) > hash(e[1]):
387400
elist.append((e[1], e[0]) + e[2:])
388401
else:
389-
# Settle the conflict arbitrarily
390-
conflicts[e] = e
391-
conflicts[(e[1], e[0]) + e[2:]] = e
392-
elist.append(e)
393-
394-
G.add_vertices(elist)
402+
elist.append(conflicts[e])
395403
396-
# 2) adjacencies in the line graph
397-
for v in g:
398-
elist = []
399-
400-
# Add the edge to the list, according to hashes, as previously
401-
for e in g.edge_iterator(v, labels=labels):
402-
if hash(e[0]) < hash(e[1]):
403-
elist.append(e)
404-
elif hash(e[0]) > hash(e[1]):
405-
elist.append((e[1], e[0]) + e[2:])
406-
else:
407-
elist.append(conflicts[e])
408-
409-
# All pairs of elements in elist are edges of the
410-
# line graph
411-
while elist:
412-
x = elist.pop()
413-
for y in elist:
414-
G.add_edge(x, y)
404+
# All pairs of elements in elist are edges of the line graph
405+
while elist:
406+
x = elist.pop()
407+
for y in elist:
408+
G.add_edge(x, y)
415409
416-
return G
410+
return G
417411
418412
419413
def root_graph(g, verbose=False):
@@ -629,5 +623,4 @@ def root_graph(g, verbose=False):
629623
is_isom, isom = g.is_isomorphic(R.line_graph(labels=False), certificate=True)
630624
if is_isom:
631625
return R, isom
632-
else:
633-
raise ValueError(not_line_graph)
626+
raise ValueError(not_line_graph)

0 commit comments

Comments
 (0)