Skip to content
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

Save next nodes in Floyd-Warshall numpy variants for path reconstruction #1040

Conversation

kazuki0824
Copy link
Contributor

@kazuki0824 kazuki0824 commented Dec 17, 2023

This PR adds "next node" matrix to the Floyd-Warshall implementations' return value.
This helps re-construct the shortest path from the result.

digraph_floyd_warshall_numpy and graph_floyd_warshall_numpy return two variable by applying this patch. In recent version, they returned only a length table.

The next node table is inherently dense, even though the graph is sparse. To not break the advantage of the implementation optimized for the sparse graph, I modified only numpy variants.

@CLAassistant
Copy link

CLAassistant commented Dec 17, 2023

CLA assistant check
All committers have signed the CLA.

Copy link
Collaborator

@IvanIsCoding IvanIsCoding left a comment

Choose a reason for hiding this comment

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

Thanks for making your first contribution! I agree that we can add predecessors to floydwarshall_numpy.

However, changing the return type is a breaking change. It will also fail all our tests, which should give you a hint that it is not exactly the change you're looking to make.

Can you create a new function rustworkx.floyd_warshall_predecessor_and_distance to incorporate the new code? It would be akin to https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.dense.floyd_warshall_predecessor_and_distance.html

src/shortest_path/floyd_warshall.rs Outdated Show resolved Hide resolved
@kazuki0824
Copy link
Contributor Author

kazuki0824 commented Dec 20, 2023

@IvanIsCoding now that the new implementations are separated to not break the existing APIs

@coveralls
Copy link

coveralls commented Dec 21, 2023

Pull Request Test Coverage Report for Build 7436857178

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.004%) to 95.872%

Totals Coverage Status
Change from base Build 7423918679: 0.004%
Covered Lines: 15816
Relevant Lines: 16497

💛 - Coveralls

@kazuki0824
Copy link
Contributor Author

kazuki0824 commented Dec 22, 2023

If we choose predecessors to represent shortest paths, it may cause much more change of the parallelism implementation than the case we choose successors. That's why I don't use predecessors.

@IvanIsCoding
Copy link
Collaborator

If we choose predecessors to represent shortest paths, it may cause much more change of the parallelism implementation than the case we choose successors. That's why I don't use predecessors.

I won’t have time to review this week but I hope to get a review by January 2. In the meantime, I encourage you to write unit tests. You can simply copy the existing Floyd Warshall test cases and reuse them for the new methods

Copy link
Collaborator

@IvanIsCoding IvanIsCoding left a comment

Choose a reason for hiding this comment

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

Overall the code looks correct, thanks for implementing it!

There are two items we need to address however:

src/shortest_path/floyd_warshall.rs Outdated Show resolved Hide resolved
@kazuki0824
Copy link
Contributor Author

kazuki0824 commented Jan 5, 2024

It would be best to split the code path for the predecessors from the code path for Floyd Warshall Numpy. The additional overhead of the extra memory doubles the memory consumption in a function that already uses O(n^2) memory. So I'd rather keep the constant down

@IvanIsCoding
I got it and resolved by an extra argument generate_successors. The new floyd_warshall_numpy returns a pair, whose latter part is Option<Array2>, and it has content only if generate_successors is true.

We need to add tests for correctness.

It's done.

@kazuki0824 kazuki0824 force-pushed the feat/floyd-warshall-path-reconstruction-table branch from 6a032b8 to 37f3973 Compare January 5, 2024 16:12
Copy link
Collaborator

@IvanIsCoding IvanIsCoding left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for making your first contribution!

I am going to merge this soon, but as a last step can you add it to the docs? These are the files you need to change:

docs/source/api/algorithm_functions/shortest_paths.rst
docs/source/api/pygraph_api_functions.rst
docs/source/api/pydigraph_api_functions.rst

You will also need to write a short docstring adding a description of the function you implemented. I can do it for you if you want

@kazuki0824
Copy link
Contributor Author

kazuki0824 commented Jan 6, 2024

@IvanIsCoding Can you please tell me the file location where to add a docstring?

@IvanIsCoding
Copy link
Collaborator

@IvanIsCoding Can you please tell me the file location where to add a docstring?

You add a comment in the Rust code. See the Floyd Warshall one for example:

/// Find all-pairs shortest path lengths using Floyd's algorithm

Copy link
Collaborator

@IvanIsCoding IvanIsCoding left a comment

Choose a reason for hiding this comment

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

LGTM, I will add the release notes and merge it today. Thanks for your contribution!

@IvanIsCoding
Copy link
Collaborator

I don't have permission to edit your branch so I will merge this and add the release notes in a new PR

@IvanIsCoding IvanIsCoding merged commit e4ff44e into Qiskit:main Jan 7, 2024
25 checks passed
mtreinish pushed a commit that referenced this pull request Jan 8, 2024
…or_and_distance` (#1049)

Follow up of #1040

This adds the release notes, universal function, and shortens the name of the new floyd_warshall_successor_and_distance

* Save next nodes in Floyd-Warshall numpy variants

* Create `graph_floyd_warshall_successor_and_distance_numpy`

* Reformat

* Split a function by by `generate_successors`

* Add testcases

* Add the functions to docs

* Add docstring to Rust code

* Change name and add universal function

* Add release notes

---------

Co-authored-by: maleicacid <maleicacid824+dev@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants