Skip to content

Fix reporting weight in yen_k_shortest_simple_paths #40248

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

kappybar
Copy link
Contributor

If source equals target, and report_edge=True, report_weight=True, then weight is not reported in yen_k_shortest_simple_paths. This PR fixes this issue.

Fix #40247.

📝 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 and checked the documentation preview.

⌛ Dependencies


sage: g = DiGraph([(1, 2)])
sage: list(yen_k_shortest_simple_paths(g, 1, 1, report_edges=True, report_weight=True))
[(0, [])]
"""
if source not in self:
raise ValueError("vertex '{}' is not in the graph".format(source))
if target not in self:
raise ValueError("vertex '{}' is not in the graph".format(target))

if source == target:
Copy link
Contributor

Choose a reason for hiding this comment

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

A shorter version could be

if source == target:
    P = [] if report_edges else [source]
    yield (0, P) if report_weight else P

@@ -734,19 +734,29 @@ def yen_k_shortest_simple_paths(self, source, target, weight_function=None,
[1, 6, 9, 10, 5],
[1, 6, 9, 3, 4, 5],
[1, 6, 9, 11, 10, 5]]

When s = t and report_edge=True and report_weight=True (issue 40247)::
Copy link
Contributor

Choose a reason for hiding this comment

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

Correct form, in particular to link the issue:

When ``s == t`` and ``report_edge == True`` and ``report_weight == True`` (:issue:`40247`)::

Copy link

github-actions bot commented Jun 12, 2025

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

@dcoudert
Copy link
Contributor

also, make sure the PR is not in conflict with #40217

@dcoudert
Copy link
Contributor

There is something wrong because now this PR and #40217 have exactly the same code.

I think one of the PR (this one) should only modify Yen. Then #40217 should be (re)built on top of this one, and so depend on this one. The alternative is to do everything in the same PR, but with the merge commit you have done, I'm afraid you will have to force push a fresh branch with all modifications.

@kappybar
Copy link
Contributor Author

Its my mistake. I understand the second plan and it may be better. But I cannot understand why I need to force push a fresh branch with all modifications because I don't understand git...... Can I use the current branch in creating new PR?

@dcoudert
Copy link
Contributor

may be @dimpase can give a better advise than me. I'm not a git guru and I don't know what may happen if a branch is based on commits from an abandoned branch/PR.

@dimpase
Copy link
Member

dimpase commented Jun 13, 2025

a PR can of course be based on any branch, but to be merged it needs to be mergeable with the current development branch, or, even better, based on the latter.

The need for force-pushing is not unusual. After a rebase you almost always need to force-push to the remote.

What exactly are you trying to do?

@dimpase
Copy link
Member

dimpase commented Jun 13, 2025

there is absolutely no need for abandoning a PR and starting a new one to solve the same problem.
Just force-push your new shiny solution.

(you might want to keep an old branch in your fork under a different name, this is easy, I can tell you how to do this)

@kappybar
Copy link
Contributor Author

kappybar commented Jun 14, 2025

My solution now is in both report_weight_in_yen and enum_cycle_undirected branches, reset --hard as follows (go back to the last commit for each branch before merge).

sage git:(report_weight_in_yen) git reset --hard f2ef3533ea9
sage git:(enum_cycle_undirected) git reset --hard 9057ffd3933

and only merge report_weight_in_yen into enum_cycle_undirected.

sage git:(enum_cycle_undirected) git merge report_weight_in_yen 

and force push both branches. By doing so,

I think there will be no conflict in these PRs. Is it ok? (in particular, this changes the current commit history)

@kappybar kappybar force-pushed the report_weight_in_yen branch from 4b4a3e3 to f2ef353 Compare June 15, 2025 01:21
@kappybar
Copy link
Contributor Author

I have finished the above changes.
There is no conflict and this branch does not have same code as #40217.

@dcoudert
Copy link
Contributor

FYI, reported lint errors are independent from this PR and fixed in #40254.

Copy link
Contributor

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

vbraun pushed a commit to vbraun/sage that referenced this pull request Jun 17, 2025
sagemathgh-40217: Enum cycle in an undirected graph (and fix bug in yen_k_shortest_simple_path algorithm)
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

Add implementation of enumeration of cycles in an undirected graph.
Specifically, I make ```sage/graphs/cycle_enumeration.py``` and modify

+ ```~sage.graphs.cycle_enumeration._all_simple_cycles_iterator_edge```
+ ```~sage.graphs.cycle_enumeration.all_cycles_iterator```
+ ```~sage.graphs.cycle_enumeration.all_simple_cycles```

In implementing these funcionts, I fix bug in
```~sage.graphs.path_enumeration.yen_k_shortest_simple_paths```.
This bug occurs when there is no path between source and target.

Also, I add
```~sage.graphs.connectivity.biconnected_components_subgraphs```.


### 📝 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.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

sagemath#40248

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#40217
Reported by: Yuta Inoue
Reviewer(s): David Coudert
vbraun pushed a commit to vbraun/sage that referenced this pull request Jun 17, 2025
sagemathgh-40248: Fix reporting weight in yen_k_shortest_simple_paths
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

If source equals target, and report_edge=True, report_weight=True, then
weight is not reported in yen_k_shortest_simple_paths. This PR fixes
this issue.

Fix sagemath#40247.

### 📝 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.
- [ ] 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: ... -->
    
URL: sagemath#40248
Reported by: Yuta Inoue
Reviewer(s): David Coudert
vbraun pushed a commit to vbraun/sage that referenced this pull request Jun 18, 2025
sagemathgh-40217: Enum cycle in an undirected graph (and fix bug in yen_k_shortest_simple_path algorithm)
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

Add implementation of enumeration of cycles in an undirected graph.
Specifically, I make ```sage/graphs/cycle_enumeration.py``` and modify

+ ```~sage.graphs.cycle_enumeration._all_simple_cycles_iterator_edge```
+ ```~sage.graphs.cycle_enumeration.all_cycles_iterator```
+ ```~sage.graphs.cycle_enumeration.all_simple_cycles```

In implementing these funcionts, I fix bug in
```~sage.graphs.path_enumeration.yen_k_shortest_simple_paths```.
This bug occurs when there is no path between source and target.

Also, I add
```~sage.graphs.connectivity.biconnected_components_subgraphs```.


### 📝 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.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

sagemath#40248

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#40217
Reported by: Yuta Inoue
Reviewer(s): David Coudert
vbraun pushed a commit to vbraun/sage that referenced this pull request Jun 18, 2025
sagemathgh-40248: Fix reporting weight in yen_k_shortest_simple_paths
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

If source equals target, and report_edge=True, report_weight=True, then
weight is not reported in yen_k_shortest_simple_paths. This PR fixes
this issue.

Fix sagemath#40247.

### 📝 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.
- [ ] 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: ... -->
    
URL: sagemath#40248
Reported by: Yuta Inoue
Reviewer(s): David Coudert
vbraun pushed a commit to vbraun/sage that referenced this pull request Jun 21, 2025
sagemathgh-40217: Enum cycle in an undirected graph (and fix bug in yen_k_shortest_simple_path algorithm)
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

Add implementation of enumeration of cycles in an undirected graph.
Specifically, I make ```sage/graphs/cycle_enumeration.py``` and modify

+ ```~sage.graphs.cycle_enumeration._all_simple_cycles_iterator_edge```
+ ```~sage.graphs.cycle_enumeration.all_cycles_iterator```
+ ```~sage.graphs.cycle_enumeration.all_simple_cycles```

In implementing these funcionts, I fix bug in
```~sage.graphs.path_enumeration.yen_k_shortest_simple_paths```.
This bug occurs when there is no path between source and target.

Also, I add
```~sage.graphs.connectivity.biconnected_components_subgraphs```.


### 📝 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.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

sagemath#40248

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#40217
Reported by: Yuta Inoue
Reviewer(s): David Coudert
vbraun pushed a commit to vbraun/sage that referenced this pull request Jun 21, 2025
sagemathgh-40248: Fix reporting weight in yen_k_shortest_simple_paths
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

If source equals target, and report_edge=True, report_weight=True, then
weight is not reported in yen_k_shortest_simple_paths. This PR fixes
this issue.

Fix sagemath#40247.

### 📝 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.
- [ ] 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: ... -->
    
URL: sagemath#40248
Reported by: Yuta Inoue
Reviewer(s): David Coudert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No weight information in calling yen_k_shortest_simple_paths(v, v, report_edges=True, report_weight=True)
3 participants