Skip to content

Commit b016761

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 b016761

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/sage/graphs/generic_graph.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14367,6 +14367,13 @@ 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 set of vertices and edges can be specified using generator expressions (see :issue:`41130`)::
14371+
14372+
sage: g = graphs.CompleteGraph(5)
14373+
sage: h = g.subgraph(vertices=(v for v in range(4)), edges=((0, v) for v in range(5)))
14374+
sage: h.edges(labels=False)
14375+
[(0, 1), (0, 2), (0, 3)]
14376+
1437014377
TESTS:
1437114378

1437214379
The appropriate properties are preserved::
@@ -14519,8 +14526,13 @@ def _subgraph_by_adding(self, vertices=None, edges=None, edge_property=None, imm
1451914526
G.add_vertices(self if vertices is None else vertices)
1452014527

1452114528
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)
14529+
edges_to_keep_labeled = set()
14530+
edges_to_keep_unlabeled = set()
14531+
for e in edges:
14532+
if len(e) == 3:
14533+
edges_to_keep_labeled.add(e)
14534+
elif len(e) == 2:
14535+
edges_to_keep_unlabeled.add(e)
1452414536

1452514537
edges_to_keep = []
1452614538
if self._directed:
@@ -14701,8 +14713,13 @@ def _subgraph_by_deleting(self, vertices=None, edges=None, inplace=False,
1470114713

1470214714
edges_to_delete = []
1470314715
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)
14716+
edges_to_keep_labeled = set()
14717+
edges_to_keep_unlabeled = set()
14718+
for e in edges:
14719+
if len(e) == 3:
14720+
edges_to_keep_labeled.add(e)
14721+
elif len(e) == 2:
14722+
edges_to_keep_unlabeled.add(e)
1470614723
edges_to_delete = []
1470714724
if G._directed:
1470814725
for e in G.edge_iterator():

0 commit comments

Comments
 (0)