Commit 7f07937
Release Manager
sagemathgh-41130: Fix G.subgraph(edges=generator) deleting all edges
This is a simple fix for a nasty issue:
I would expect that the Sage expression
`graphs.CompleteGraph(5).subgraph(edges=((0, v) for v in range(5)))`
returns a star graph on 4 edges. But it actually returned an edgeless
graph! This is because the `subgraph` function expects the `edges`
parameter to be a list that can be iterated over twice but the
[generator expression](https://peps.python.org/pep-0289/) `((0, v) for v
in range(5))` becomes empty after looping over it once, which caused
`edges_to_keep_unlabeled` to be empty.
While the [documentation for `subgraph()`](https://doc-develop--sagemath
.netlify.app/html/en/reference/graphs/sage/graphs/generic_graph.html#sag
e.graphs.generic_graph.GenericGraph.subgraph) does not mention that
`edges` is allowed to be a generator expression, many other functions
that expect an "iterable container" as parameter work just fine if a
generator expression is passed instead. For example,
`subdivide_edges()`, `delete_edges()` and `G.subgraph(v for v in
range(4))` work as expected.
There are multiple ways to fix this issue. The simplest and most
reliable would be to write `edges = list(edges)`, which would go well
with [`vertices = list(vertices)`](https://github.com/sagemath/sage/blob
/aa27703384a568d85f1751197efe06ff135be352/src/sage/graphs/generic_graph.
py#L14400). Instead I opted to fix the symptom by only iterating over
`edges` once, which I believe is faster and uses less memory.
URL: sagemath#41130
Reported by: Lennard Hofmann
Reviewer(s): David Coudert
1 file changed
+22
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14378 | 14378 | | |
14379 | 14379 | | |
14380 | 14380 | | |
| 14381 | + | |
| 14382 | + | |
| 14383 | + | |
| 14384 | + | |
| 14385 | + | |
| 14386 | + | |
| 14387 | + | |
| 14388 | + | |
14381 | 14389 | | |
14382 | 14390 | | |
14383 | 14391 | | |
| |||
14530 | 14538 | | |
14531 | 14539 | | |
14532 | 14540 | | |
14533 | | - | |
14534 | | - | |
| 14541 | + | |
| 14542 | + | |
| 14543 | + | |
| 14544 | + | |
| 14545 | + | |
| 14546 | + | |
| 14547 | + | |
14535 | 14548 | | |
14536 | 14549 | | |
14537 | 14550 | | |
| |||
14712 | 14725 | | |
14713 | 14726 | | |
14714 | 14727 | | |
14715 | | - | |
14716 | | - | |
| 14728 | + | |
| 14729 | + | |
| 14730 | + | |
| 14731 | + | |
| 14732 | + | |
| 14733 | + | |
| 14734 | + | |
14717 | 14735 | | |
14718 | 14736 | | |
14719 | 14737 | | |
| |||
0 commit comments