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

Commit 959c85d

Browse files
committed
Compare labels edge by edge without relying on total order.
1 parent e96b20c commit 959c85d

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

src/sage/graphs/generic_graph.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -474,28 +474,33 @@ def __eq__(self, other):
474474
return False
475475
# Finally, we are prepared to check edges:
476476
if not self.allows_multiple_edges():
477-
return all(other.has_edge(*edge) for edge in self.edge_iterator(
478-
labels=self._weighted))
479-
else:
480-
ii = jj = None
481-
for i,j in self.edge_iterator(labels = False):
482-
if i == ii and j == jj:
483-
continue
484-
ii = i
485-
jj = j
486-
487-
# The labels of edge ij in each graph
488-
labels1 = self.edge_label(i, j)
489-
try:
490-
labels2 = other.edge_label(i, j)
491-
except LookupError:
492-
return False
493-
494-
if len(labels1) != len(labels2):
495-
return False
496-
if self._weighted and sorted(labels1) != sorted(labels2):
497-
return False
498-
return True
477+
return all(other.has_edge(*edge)
478+
for edge in self.edge_iterator(labels=self._weighted))
479+
# The problem with multiple edges is that labels may not have total
480+
# ordering, which makes it difficult to compare lists of labels.
481+
last_i = last_j = None
482+
for i, j in self.edge_iterator(labels=False):
483+
if i == last_i and j == last_j:
484+
continue
485+
last_i, last_j = i, j
486+
# All labels between i and j
487+
labels1 = self.edge_label(i, j)
488+
try:
489+
labels2 = other.edge_label(i, j)
490+
except LookupError:
491+
return False
492+
if len(labels1) != len(labels2):
493+
return False
494+
if self._weighted:
495+
# If there is total ordering, sorting will speed up things
496+
labels1.sort()
497+
labels2.sort()
498+
for l in labels1:
499+
try:
500+
labels2.remove(l)
501+
except ValueError:
502+
return False
503+
return True
499504

500505
@cached_method
501506
def __hash__(self):

0 commit comments

Comments
 (0)