Skip to content

Update NC k shortest simple path for Undirected graphs #40547

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 9 commits into
base: develop
Choose a base branch
from

Conversation

kappybar
Copy link
Contributor

@kappybar kappybar commented Aug 6, 2025

Update nc_k_shortest_simple_path algorithm for undirected graphs.

📝 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

#40510

Copy link

github-actions bot commented Aug 6, 2025

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

@@ -1163,9 +1174,11 @@ def nc_k_shortest_simple_paths(self, source, target, weight_function=None,
return

if self.has_loops() or self.allows_multiple_edges():
G = self.to_simple(to_undirected=False, keep_label='min', immutable=False)
G = self.to_simple(to_undirected=self.is_directed(), keep_label='min', immutable=False)
Copy link
Contributor

Choose a reason for hiding this comment

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

Here you force conversion to undirected graph. This seems wrong.

else:
G = self.copy(immutable=False)
if not G.is_directed():
Copy link
Contributor

Choose a reason for hiding this comment

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

we can sometimes save a copy

if self.has_loops() or self.allows_multiple_edges():
    G = self.to_simple(to_undirected=False, keep_label='min', immutable=False)
    if not G.is_directed():
        G = G.to_directed()
elif not self.is_directed():
    # Turn the graph into a mutable directed graph
    G = self.to_directed(data_structure='sparse')
else:
    G = self.copy(immutable=False)

sage: list(nc_k_shortest_simple_paths(g, 5, 1, by_weight=True))
[[5, 3, 1], [5, 2, 1], [5, 4, 1]]
sage: list(nc_k_shortest_simple_paths(g, 5, 1))
[[5, 2, 1], [5, 4, 1], [5, 3, 1]]
Copy link
Contributor

Choose a reason for hiding this comment

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

The order in which the paths are reported may not always be the same. To make the test robust, you could report the length of the paths.

sage: [len(P) for P in nc_k_shortest_simple_paths(g, 5, 1)]
[3, 3, 3]

the alternative is to use a lexicographic sorting of the paths

sage: sorted(nc_k_shortest_simple_paths(g, 5, 1))
[[5, 2, 1], [5, 3, 1], [5, 4, 1]]

Check that "Yen", "Feng" and "PNC" provide same results on random undirected graphs::

sage: from sage.graphs.generators.random import RandomGNP
sage: G = RandomGNP(30, .05)
Copy link
Contributor

Choose a reason for hiding this comment

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

you could simply use G = graphs.RandomGNP(30, .05).

Also, consider increasing the number of edges. With p=0.05 the graph has roughly 20 edges.

@dcoudert dcoudert added c: graph theory gsoc: 2025 Tag for GSoC2025 issues/PRs labels Aug 7, 2025
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 Aug 10, 2025
sagemathgh-40547: Update NC k shortest simple path for Undirected graphs
    
<!-- ^ 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". -->

Update nc_k_shortest_simple_path algorithm for undirected graphs.

### 📝 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: ... -->
sagemath#40510
    
URL: sagemath#40547
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.

2 participants