-
-
Notifications
You must be signed in to change notification settings - Fork 691
Fix G.subgraph(edges=generator) deleting all edges #41130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
|
Good catch. |
|
Documentation preview for this PR (built with commit 5b6555d; changes) is ready! 🎉 |
c2f230d to
b016761
Compare
|
I promoted the doctest from TESTS to EXAMPLES so that the issue number (PR number to be precise) is part of the rendered documentation. |
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.
b016761 to
5b6555d
Compare
dcoudert
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
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
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
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
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
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
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
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 thesubgraphfunction expects theedgesparameter to be a list that can be iterated over twice but the generator expression((0, v) for v in range(5))becomes empty after looping over it once, which causededges_to_keep_unlabeledto be empty.While the documentation for
subgraph()does not mention thatedgesis 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()andG.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 withvertices = list(vertices). Instead I opted to fix the symptom by only iterating overedgesonce, which I believe is faster and uses less memory.