-
-
Notifications
You must be signed in to change notification settings - Fork 646
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
base: develop
Are you sure you want to change the base?
Conversation
Documentation preview for this PR (built with commit 7fb197d; changes) is ready! 🎉 |
src/sage/graphs/path_enumeration.pyx
Outdated
@@ -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) |
There was a problem hiding this comment.
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.
src/sage/graphs/path_enumeration.pyx
Outdated
else: | ||
G = self.copy(immutable=False) | ||
if not G.is_directed(): |
There was a problem hiding this comment.
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)
src/sage/graphs/path_enumeration.pyx
Outdated
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]] |
There was a problem hiding this comment.
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]]
src/sage/graphs/path_enumeration.pyx
Outdated
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
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
Update nc_k_shortest_simple_path algorithm for undirected graphs.
📝 Checklist
⌛ Dependencies
#40510