Skip to content

Fix all_cycles_iterator for immutable graphs#41488

Merged
vbraun merged 1 commit intosagemath:developfrom
Ordoviz:cycles_iterator_immutable
Jan 25, 2026
Merged

Fix all_cycles_iterator for immutable graphs#41488
vbraun merged 1 commit intosagemath:developfrom
Ordoviz:cycles_iterator_immutable

Conversation

@Ordoviz
Copy link
Copy Markdown
Contributor

@Ordoviz Ordoviz commented Jan 21, 2026

This small PR fixes TypeError: this graph is immutable and so cannot be changed when calling G.all_simple_cycles(), where G is a non-empty immutable graph.

See also #41487

A mutable copy is required for `hh.delete_edge(e)`.
@github-actions
Copy link
Copy Markdown

Documentation preview for this PR (built with commit 799842a; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

@dcoudert
Copy link
Copy Markdown
Collaborator

dcoudert commented Jan 22, 2026

A better solution is to add parameter immutable to methods strongly_connected_components_subgraphs and biconnected_components_subgraphs. This way we avoid extra copies of the graphs.

For undirected graph, we can do it in #40965 if needed.

@Ordoviz
Copy link
Copy Markdown
Contributor Author

Ordoviz commented Jan 22, 2026

Do you mean adding parameter immutable to biconnected_components_subgraphs and changing its implementation to [G.subgraph(c, immutable=immutable) for c in G.strongly_connected_components()] to avoid having to inline this function call here?

I'm not following how the extra parameter would help to avoid copies. The cycle finding algorithm needs each component hh to be a mutable copy. Since we also delete edges from the components, it would not be sufficient to keep track of forbidden vertices.

@dcoudert
Copy link
Copy Markdown
Collaborator

no, I mean to use in all_cycles_iterator a call to G.strongly_connected_components_subgraphs(immutable=False) and to modify method G.strongly_connected_components_subgraphs to return mutable/imutable subgraphs.

Your solution is fine. My proposal is more general and can be useful for other methods.

@dcoudert
Copy link
Copy Markdown
Collaborator

See #41493.

Copy link
Copy Markdown
Collaborator

@dcoudert dcoudert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

This solution can be modified later, when #41493 will be included.

@vbraun vbraun merged commit 4ea9bb1 into sagemath:develop Jan 25, 2026
23 of 25 checks passed
vbraun pushed a commit to vbraun/sage that referenced this pull request Jan 30, 2026
sagemathgh-41493: add parameter immutable to `(bi_|strongly_)connected_components_subgraphs`
    
This PR adds parameter immutable to methods
`connected_components_subgraphs`, `biconnected_components_subgraphs` and
`strongly_connected_components_subgraphs`. This way, we can choose
whether the returned subgraphs should behave as the input graph (`None`)
or be mutable (`False`) or immutable (`True`).

This was discussed in sagemath#41488.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->

sagemath#40965: add parameter `forbidden_vertices` to methods related to
biconnected components
    
URL: sagemath#41493
Reported by: David Coudert
Reviewer(s): David Coudert, Lennard Hofmann, Martin Rubey
vbraun pushed a commit to vbraun/sage that referenced this pull request Jan 31, 2026
sagemathgh-41493: add parameter immutable to `(bi_|strongly_)connected_components_subgraphs`
    
This PR adds parameter immutable to methods
`connected_components_subgraphs`, `biconnected_components_subgraphs` and
`strongly_connected_components_subgraphs`. This way, we can choose
whether the returned subgraphs should behave as the input graph (`None`)
or be mutable (`False`) or immutable (`True`).

This was discussed in sagemath#41488.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->

sagemath#40965: add parameter `forbidden_vertices` to methods related to
biconnected components
    
URL: sagemath#41493
Reported by: David Coudert
Reviewer(s): David Coudert, Lennard Hofmann, Martin Rubey
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants