Skip to content

Commit 5b6555d

Browse files
committed
Fix G.subgraph(edges=generator) deleting all edges
The edges generator was looped over twice. If the generator is a generator expression like `((0, v) for v in range(5))` it becomes empty after the first loop, causing `edges_to_keep_unlabeled` to be empty.
1 parent 85c8f1e commit 5b6555d

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/sage/graphs/generic_graph.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14367,6 +14367,14 @@ def subgraph(self, vertices=None, edges=None, inplace=False,
1436714367
sage: g.subgraph(list(range(10))) # uses the 'add' algorithm
1436814368
Subgraph of (Path graph): Graph on 10 vertices
1436914369

14370+
The vertices and edges can be specified using generator expressions
14371+
(see :issue:`41130`)::
14372+
14373+
sage: g = graphs.CompleteGraph(5)
14374+
sage: h = g.subgraph(vertices=(v for v in range(4)), edges=((0, v) for v in range(5)))
14375+
sage: h.edges(labels=False)
14376+
[(0, 1), (0, 2), (0, 3)]
14377+
1437014378
TESTS:
1437114379

1437214380
The appropriate properties are preserved::
@@ -14519,8 +14527,13 @@ def _subgraph_by_adding(self, vertices=None, edges=None, edge_property=None, imm
1451914527
G.add_vertices(self if vertices is None else vertices)
1452014528

1452114529
if edges is not None:
14522-
edges_to_keep_labeled = frozenset(e for e in edges if len(e) == 3)
14523-
edges_to_keep_unlabeled = frozenset(e for e in edges if len(e) == 2)
14530+
edges_to_keep_labeled = set()
14531+
edges_to_keep_unlabeled = set()
14532+
for e in edges:
14533+
if len(e) == 3:
14534+
edges_to_keep_labeled.add(e)
14535+
elif len(e) == 2:
14536+
edges_to_keep_unlabeled.add(e)
1452414537

1452514538
edges_to_keep = []
1452614539
if self._directed:
@@ -14701,8 +14714,13 @@ def _subgraph_by_deleting(self, vertices=None, edges=None, inplace=False,
1470114714

1470214715
edges_to_delete = []
1470314716
if edges is not None:
14704-
edges_to_keep_labeled = frozenset(e for e in edges if len(e) == 3)
14705-
edges_to_keep_unlabeled = frozenset(e for e in edges if len(e) == 2)
14717+
edges_to_keep_labeled = set()
14718+
edges_to_keep_unlabeled = set()
14719+
for e in edges:
14720+
if len(e) == 3:
14721+
edges_to_keep_labeled.add(e)
14722+
elif len(e) == 2:
14723+
edges_to_keep_unlabeled.add(e)
1470614724
edges_to_delete = []
1470714725
if G._directed:
1470814726
for e in G.edge_iterator():

0 commit comments

Comments
 (0)