Repository navigation
graphs: use by_weight in edge_cut, multiway_cut and max_cut - #42839
Merged
Merged
Conversation
Replace the use_edge_labels parameter of these three methods by the by_weight / weight_function / check_weight triple used elsewhere in the graph module, deprecating the old name via rename_keyword. In edge_cut, the saturation test compared the weight of an edge of the graph with the weight of a label of the flow graph. The latter is a flow value, not an edge label, so a user-supplied weight_function would have been applied to it. It now compares the capacity of the arc with the flow it carries.
|
Documentation preview for this PR (built with commit 5f695f7; changes) is ready! 🎉 |
Collaborator
|
When a PR is positively reviewed, you should not rebase on last beta, unless the release manager identifies a merge conflict when trying to merge with next beta and so that you must change something. |
dcoudert
approved these changes
Sep 26, 2026
4 of 5 tasks
vbraun
pushed a commit
to vbraun/sage
that referenced
this pull request
Oct 6, 2026
sagemathgh-42905: graphs: use by_weight in longest_path and longest_cycle Part of sagemath#13112. Replaces the `use_edge_labels` parameter of `longest_path` and `longest_cycle` by the `by_weight` / `weight_function` / `check_weight` triple used elsewhere in the graph module, deprecating the old name via `rename_keyword`. Follows sagemath#42740, sagemath#42750, sagemath#42760, sagemath#42772 and sagemath#42839. ### Recursive calls Both methods recurse on connected components or blocks, and both return a `(weight, subgraph)` pair or a bare subgraph depending on `by_weight`. Since `_get_weight_function` always returns a function, and a non-`None` `weight_function` turns `by_weight` on, the recursive calls pass the resolved function only when it is used: ```python weight_function=weight_function if by_weight else None, check_weight=False, ``` Otherwise a call with `by_weight=False` would get pairs back from its recursive calls. ### Behaviour change `longest_path` used to treat a `{}` label as a weight of `1` (`x if (x is not None and x != {}) else 1`). The documentation of `longest_cycle` promised the same, but its code only replaced `None`. Both now go through `_get_weight_function`, which raises `ValueError` naming the offending edge, as in the methods converted in the earlier PRs. With `algorithm='heuristic'`, `longest_path` now also rejects a `weight_function`, as it already rejected `use_edge_labels`. ### 📝 Checklist - [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. - [ ] I have updated the documentation accordingly. URL: sagemath#42905 Reported by: Muhammet Doğukan Bingöl Reviewer(s): David Coudert
vbraun
pushed a commit
to vbraun/sage
that referenced
this pull request
Oct 8, 2026
sagemathgh-42905: graphs: use by_weight in longest_path and longest_cycle Part of sagemath#13112. Replaces the `use_edge_labels` parameter of `longest_path` and `longest_cycle` by the `by_weight` / `weight_function` / `check_weight` triple used elsewhere in the graph module, deprecating the old name via `rename_keyword`. Follows sagemath#42740, sagemath#42750, sagemath#42760, sagemath#42772 and sagemath#42839. ### Recursive calls Both methods recurse on connected components or blocks, and both return a `(weight, subgraph)` pair or a bare subgraph depending on `by_weight`. Since `_get_weight_function` always returns a function, and a non-`None` `weight_function` turns `by_weight` on, the recursive calls pass the resolved function only when it is used: ```python weight_function=weight_function if by_weight else None, check_weight=False, ``` Otherwise a call with `by_weight=False` would get pairs back from its recursive calls. ### Behaviour change `longest_path` used to treat a `{}` label as a weight of `1` (`x if (x is not None and x != {}) else 1`). The documentation of `longest_cycle` promised the same, but its code only replaced `None`. Both now go through `_get_weight_function`, which raises `ValueError` naming the offending edge, as in the methods converted in the earlier PRs. With `algorithm='heuristic'`, `longest_path` now also rejects a `weight_function`, as it already rejected `use_edge_labels`. ### 📝 Checklist - [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. - [ ] I have updated the documentation accordingly. URL: sagemath#42905 Reported by: Muhammet Doğukan Bingöl Reviewer(s): David Coudert
vbraun
pushed a commit
to vbraun/sage
that referenced
this pull request
Oct 11, 2026
sagemathgh-42905: graphs: use by_weight in longest_path and longest_cycle Part of sagemath#13112. Replaces the `use_edge_labels` parameter of `longest_path` and `longest_cycle` by the `by_weight` / `weight_function` / `check_weight` triple used elsewhere in the graph module, deprecating the old name via `rename_keyword`. Follows sagemath#42740, sagemath#42750, sagemath#42760, sagemath#42772 and sagemath#42839. ### Recursive calls Both methods recurse on connected components or blocks, and both return a `(weight, subgraph)` pair or a bare subgraph depending on `by_weight`. Since `_get_weight_function` always returns a function, and a non-`None` `weight_function` turns `by_weight` on, the recursive calls pass the resolved function only when it is used: ```python weight_function=weight_function if by_weight else None, check_weight=False, ``` Otherwise a call with `by_weight=False` would get pairs back from its recursive calls. ### Behaviour change `longest_path` used to treat a `{}` label as a weight of `1` (`x if (x is not None and x != {}) else 1`). The documentation of `longest_cycle` promised the same, but its code only replaced `None`. Both now go through `_get_weight_function`, which raises `ValueError` naming the offending edge, as in the methods converted in the earlier PRs. With `algorithm='heuristic'`, `longest_path` now also rejects a `weight_function`, as it already rejected `use_edge_labels`. ### 📝 Checklist - [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. - [ ] I have updated the documentation accordingly. URL: sagemath#42905 Reported by: Muhammet Doğukan Bingöl Reviewer(s): David Coudert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #13112. Replaces the
use_edge_labelsparameter ofedge_cut,multiway_cutandmax_cutby theby_weight/weight_function/check_weighttriple used elsewhere in the graph module, deprecating the oldname via
rename_keyword.Follows #42740, #42750, #42760 and #42772.
A fix in
edge_cutThe saturation test compared the weight of an edge of the graph with the weight
of a label of the flow graph:
lthere is a flow value, not an edge label, so a user-suppliedweight_functionwould have been applied to it. It now compares the capacity ofthe arc with the flow it carries:
This was invisible before because
weightwas the identity on numbers.Behaviour change
max_cutused to treat a non-real label as a weight of1(
x if x in RR else 1), andedge_cutdid the same for{}. Both now gothrough
_get_weight_function, which raisesValueErrornaming the offendingedge. This is the same semantics as the methods converted in the earlier PRs,
and it turns a silent wrong answer into a diagnostic.
📝 Checklist