-
-
Notifications
You must be signed in to change notification settings - Fork 648
PNC k shortest simple path (for directed graphs) #40284
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
Conversation
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.
Why is it necessary to define PathCandidate
in a .h file ? can't it be done in file path_enumeration.pxd
?
Because I want to use priority_queue of C++ standard library, the type of PathCandidate must be struct of C++. Thus, I think I need definition of PathCandidate in .h file. However, There may be another way of doing so without .h file. I am looking for it. |
isn't it possible to use a tuple instead of PathCandidate ? or may be |
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.
also, the key to the paper should be ACN2023
(Al Zoobi, Coudert, Nisse)
something goes wrong in the algorithm. Paths are not reported in increasing order of weight sage: g = DiGraph(graphs.Grid2dGraph(2, 6).relabel(inplace=False))
sage: for u, v in g.edge_iterator(labels=False):
....: g.set_edge_label(u, v, 1)
sage: %time X = [w for w, P in pnc_k_shortest_simple_paths(g, 5, 1, by_weight=True, report_weight=True, labels=True, report_edges=True)]
CPU times: user 1.39 ms, sys: 0 ns, total: 1.39 ms
Wall time: 1.4 ms
sage: print(X)
[4.0, 6.0, 6.0, 6.0, 6.0, 6.0, 6.0, 6.0, 6.0, 6.0, 6.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 6.0, 6.0, 6.0, 6.0, 6.0, 6.0, 10.0, 10.0, 10.0, 10.0, 8.0] |
You should rebase the branch on the last beta. If you do another commit, you can add the doi to the paper |
It's not fully fixed sage: from sage.graphs.path_enumeration import yen_k_shortest_simple_paths as yen
sage: from sage.graphs.path_enumeration import feng_k_shortest_simple_paths as feng
sage: from sage.graphs.path_enumeration import pnc_k_shortest_simple_paths as pnc
sage: D = graphs.Grid2dGraph(5, 5).relabel(inplace=False).to_directed()
sage: %time len(list(yen(D, 0, 24)))
CPU times: user 14 s, sys: 408 µs, total: 14 s
Wall time: 14.1 s
8512
sage: %time len(list(feng(D, 0, 24)))
CPU times: user 1.23 s, sys: 988 µs, total: 1.23 s
Wall time: 1.24 s
8512
sage: %time len(list(pnc(D, 0, 24)))
CPU times: user 427 ms, sys: 995 µs, total: 428 ms
Wall time: 428 ms
8530 |
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.
Something goes wrong. The method can yield non simple paths.
sage: from sage.graphs.path_enumeration import pnc_k_shortest_simple_paths as pnc
sage: D = graphs.Grid2dGraph(5, 5).relabel(inplace=False).to_directed()
sage: for P in pnc(D, 0, 24):
....: if len(P) != len(set(P)):
....: print(P)
....: break
....:
[0, 5, 10, 15, 16, 17, 18, 13, 12, 11, 6, 1, 2, 7, 8, 13, 14, 19, 24]
vertex 13 is repeated.
src/sage/graphs/path_enumeration.pyx
Outdated
a path is returned. Otherwise a tuple of path length and path is | ||
returned. | ||
|
||
OUTPUT: iterator |
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.
this line is not useful. It can be removed.
src/sage/graphs/path_enumeration.pyx
Outdated
|
||
ALGORITHM: | ||
|
||
This algorithm is based on the `feng_k_shortest_simple_paths` algorithm |
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.
use the form the ``feng_k_shortest_simple_paths``
. A single `
is for math mode.
src/sage/graphs/path_enumeration.pyx
Outdated
[4.0, 6.0, 6.0, 6.0, 6.0, 6.0, 6.0, 6.0, 6.0, 6.0, 6.0, 8.0, 8.0, | ||
8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 10.0, 10.0, 10.0, 10.0] | ||
|
||
Same tests as yen_k_shortest_simple_paths:: |
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 as well, use ``yen_k_shortest_simple_paths``::
src/sage/graphs/path_enumeration.pyx
Outdated
|
||
Same tests as yen_k_shortest_simple_paths:: | ||
|
||
sage: g = DiGraph([(1, 2, 20), (1, 3, 10), (1, 4, 30), (2, 5, 20), (3, 5, 10), (4, 5, 30)]) |
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.
this first graph is not used.
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.
Let's try to make the doc build. It's currently failing.
src/sage/graphs/path_enumeration.pyx
Outdated
deviations occur. See Postponed Node Classification algorithm in [ACN2023]_ | ||
for the algorithm description. | ||
|
||
EXAMPLES: |
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.
for the EXAMPLES and TESTS blocks, use EXAMPLES::
and TESTS::
, unless it is followed by sub blocks, which is not the case here.
src/sage/graphs/path_enumeration.pyx
Outdated
G.delete_edges(G.incoming_edges(source, labels=False)) | ||
G.delete_edges(G.outgoing_edges(target, labels=False)) | ||
|
||
by_weight, weight_function = self._get_weight_function(by_weight=by_weight, |
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 can use G. _get_weight_function
.
src/sage/graphs/path_enumeration.pyx
Outdated
# target vertex is not reachable from v | ||
return -1 | ||
if v in ancestor_idx_dict: | ||
if ancestor_idx_dict[v] <= t or ancestor_idx_dict[v] == len(path) - 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.
len(path) - 1
could be a parameter of the method. I know it's a minor optimization.
src/sage/graphs/path_enumeration.pyx
Outdated
if is_simple: | ||
# output | ||
if report_edges and labels: | ||
P = [edge_labels[e] for e in zip(path[:-1], path[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.
FYI, you could simply use zip(path, path[1:])
. the zip iterator ends as soon as one of the list is exhausted. No need to change.
Documentation preview for this PR (built with commit 211ae0c; changes) is ready! 🎉 |
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 following improvements can be done. Let me know if you agree.
- vertices in
unnecessary_vertices
could be removed fromG
, and so not considered when callingshortest_path_func
- method
ancestor_idx_func
could be defined outside the while loop. It suffices to add it parameterancestor_idx_dict
- dictionary
ancestor_idx_dict
should be initialized only ifis_simple
isTrue
.
Finally, this new method can be exposed in shortest_simple_paths
and become the default for directed graphs. I'm not sure it's faster for small digraphs than Yen, but for large ones, it's clearly faster.
If you prefer, we can do these changes in a follow up PR.
Yes I agree with you. This implementation can be improved in these ways or more. I will make another follow up PR. |
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-40364: Improve PNC 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". --> Improve the implementation of PNC algorithm. See comments in sagemath#40284. ### 📝 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. - [x] 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#40284 URL: sagemath#40364 Reported by: Yuta Inoue Reviewer(s): David Coudert
sagemathgh-40364: Improve PNC 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". --> Improve the implementation of PNC algorithm. See comments in sagemath#40284. ### 📝 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. - [x] 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#40284 URL: sagemath#40364 Reported by: Yuta Inoue Reviewer(s): David Coudert
sagemathgh-40364: Improve PNC 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". --> Improve the implementation of PNC algorithm. See comments in sagemath#40284. ### 📝 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. - [x] 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#40284 URL: sagemath#40364 Reported by: Yuta Inoue Reviewer(s): David Coudert
sagemathgh-40364: Improve PNC 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". --> Improve the implementation of PNC algorithm. See comments in sagemath#40284. ### 📝 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. - [x] 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#40284 URL: sagemath#40364 Reported by: Yuta Inoue Reviewer(s): David Coudert
sagemathgh-40364: Improve PNC 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". --> Improve the implementation of PNC algorithm. See comments in sagemath#40284. ### 📝 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. - [x] 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#40284 URL: sagemath#40364 Reported by: Yuta Inoue Reviewer(s): David Coudert
sagemathgh-40364: Improve PNC 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". --> Improve the implementation of PNC algorithm. See comments in sagemath#40284. ### 📝 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. - [x] 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#40284 URL: sagemath#40364 Reported by: Yuta Inoue Reviewer(s): David Coudert
Implement PNC k shortest simple path for directed graphs.
I implemented for only directed graphs for simplicity first.
📝 Checklist
⌛ Dependencies
#40248
#40217