Skip to content

graphs: use by_weight in edge_cut, multiway_cut and max_cut - #42839

Merged
vbraun merged 3 commits into
sagemath:developfrom
ProfTR55:13112-cut-by-weight
Sep 29, 2026
Merged

vbraun merged 3 commits into
sagemath:developfrom
ProfTR55:13112-cut-by-weight

Conversation

@ProfTR55

Copy link
Copy Markdown
Contributor

Part of #13112. Replaces the use_edge_labels parameter of edge_cut,
multiway_cut and max_cut by the by_weight / weight_function /
check_weight triple used elsewhere in the graph module, deprecating the old
name via rename_keyword.

Follows #42740, #42750, #42760 and #42772.

A fix 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:

if (not use_edge_labels or
        weight(g.edge_label(u, v)) == weight(l)):

l there 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:

if (not by_weight or
        weight_function((u, v, g.edge_label(u, v))) == l):

This was invisible before because weight was the identity on numbers.

Behaviour change

max_cut used to treat a non-real label as a weight of 1
(x if x in RR else 1), and edge_cut did the same for {}. Both now go
through _get_weight_function, which raises ValueError naming the offending
edge. This is the same semantics as the methods converted in the earlier PRs,
and it turns a silent wrong answer into a diagnostic.

📝 Checklist

  • The title is concise and informative.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation accordingly.

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.
@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

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

@dcoudert dcoudert left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. Thanks.

@dcoudert

Copy link
Copy Markdown
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.
No I must put back to positive review the 3 PRs...

@vbraun
vbraun merged commit 3589b49 into sagemath:develop Sep 29, 2026
26 checks passed
vbraun pushed a commit to vbraun/sage that referenced this pull request Oct 6, 2026
Release Manager
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
Release Manager
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
Release Manager
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
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