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

Add is_semi_connected() to rustworkx #1215

Merged
merged 28 commits into from
Jun 19, 2024

Conversation

Procatv
Copy link
Contributor

@Procatv Procatv commented Jun 10, 2024

Implemented the is_semi_connected function, I don't believe it's fully ready to go so I would appreciate feedback regarding my changes, and any more tests I can add, and etc. Followed implementation from definition: https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.components.is_semiconnected.html#networkx.algorithms.components.is_semiconnected

Fixes #446

@Procatv Procatv changed the title Implemented :func ~rustworkx.is_semi_connected() Add ~rustworkx.is_semi_connected() Jun 10, 2024
@Procatv Procatv changed the title Add ~rustworkx.is_semi_connected() Add is_semi_connected to rustworkx Jun 10, 2024
@Procatv Procatv changed the title Add is_semi_connected to rustworkx Add is_semi_connected() to rustworkx Jun 10, 2024
@coveralls
Copy link

coveralls commented Jun 10, 2024

Pull Request Test Coverage Report for Build 9452263211

Details

  • 28 of 29 (96.55%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.001%) to 95.871%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/connectivity/mod.rs 27 28 96.43%
Totals Coverage Status
Change from base Build 9450395122: 0.001%
Covered Lines: 17414
Relevant Lines: 18164

💛 - Coveralls

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.

Rust code is looking good BUT I think we can still take advantage of multi-threading to boost our speedup compared to sequential libraries. Let me know if you have more questions.

The tests are good but I think we should add some larger inputs. Our generators can help with that

tests/digraph/test_is_semi_connected.py Show resolved Hide resolved
src/connectivity/mod.rs Outdated Show resolved Hide resolved
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, just leaving one final comment about removing has_path_connecting for something simpler

src/connectivity/mod.rs Outdated Show resolved Hide resolved
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.

I left a comment on a test case that is wrong, the condition is simpler, we check only one direction not both

src/connectivity/mod.rs Outdated Show resolved Hide resolved
tests/digraph/test_is_semi_connected.py Outdated Show resolved Hide resolved
@IvanIsCoding IvanIsCoding marked this pull request as ready for review June 15, 2024 01:41
src/connectivity/mod.rs Outdated Show resolved Hide resolved
src/connectivity/mod.rs Outdated Show resolved Hide resolved
Procatv and others added 9 commits June 17, 2024 09:17
Co-authored-by: Ivan Carvalho <8753214+IvanIsCoding@users.noreply.github.com>
Co-authored-by: Ivan Carvalho <8753214+IvanIsCoding@users.noreply.github.com>
Co-authored-by: Ivan Carvalho <8753214+IvanIsCoding@users.noreply.github.com>
@IvanIsCoding
Copy link
Collaborator

I fixed clippy due to the Rust update to 1.79, sorry for the inconvenience. Let's hope CI passes now.

@coveralls
Copy link

coveralls commented Jun 18, 2024

Pull Request Test Coverage Report for Build 9558747991

Details

  • 53 of 57 (92.98%) changed or added relevant lines in 12 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.05%) to 95.879%

Changes Missing Coverage Covered Lines Changed/Added Lines %
rustworkx-core/src/planar/lr_planar.rs 0 1 0.0%
src/connectivity/mod.rs 35 36 97.22%
rustworkx-core/src/generators/random_graph.rs 1 3 33.33%
Totals Coverage Status
Change from base Build 9473947759: 0.05%
Covered Lines: 17426
Relevant Lines: 18175

💛 - Coveralls

@IvanIsCoding
Copy link
Collaborator

So some test cases were not correct, I pushed the test cases I believe are correct (you can double check with NetworkX, every path graph should be semi connected). So let's focus on finding the bug and making the test cases pass.

@Procatv
Copy link
Contributor Author

Procatv commented Jun 18, 2024

So some test cases were not correct, I pushed the test cases I believe are correct (you can double check with NetworkX, every path graph should be semi connected). So let's focus on finding the bug and making the test cases pass.

  • I believe test_is_semi_connected_disconnected_graph should stay false given that there are no edges in the graph.
  • I made a mistake with test_is_semi_connected_false I was copying the test provided in the is_semiconnected() page and realized that in networkx adding nodes using path_graph starts from 0 to n-1, while add_nodes_from in rustworkx is from 1 to n, so the implementation was working correctly, but the test itself was wrong if I wanted to assert false (3 should be 4)
  • Also I believe that we should still check edges in both directions, one direction checking only works for all other graphs except any directed_path_graph which makes sense given that we only check one direction with has_edge -> when I implement the above (bidirectional edge check and test_is_semi_connected_disconnected_graph to false) changes all tests pass

@IvanIsCoding
Copy link
Collaborator

So some test cases were not correct, I pushed the test cases I believe are correct (you can double check with NetworkX, every path graph should be semi connected). So let's focus on finding the bug and making the test cases pass.

  • I believe test_is_semi_connected_disconnected_graph should stay false given that there are no edges in the graph.
  • I made a mistake with test_is_semi_connected_false I was copying the test provided in the is_semiconnected() page and realized that in networkx adding nodes using path_graph starts from 0 to n-1, while add_nodes_from in rustworkx is from 1 to n, so the implementation was working correctly, but the test itself was wrong if I wanted to assert false (3 should be 4)
  • Also I believe that we should still check edges in both directions, one direction checking only works for all other graphs except any directed_path_graph which makes sense given that we only check one direction with has_edge -> when I implement the above (bidirectional edge check and test_is_semi_connected_disconnected_graph to false) changes all tests pass

The disconnected graph was my mistake, but graphs like A -> B <- C are not semi-connected but with the wrong check the code mistakenly reported them as so.

We can use the floyd_warshall functions with the definition in the tests to estabilish a "ground truth" and work from there. For A -> B <- C there is no path from A to C so the algorithm should report it as not connected.

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.

After thinking about the problem more thoroughly, I am jumping in to implement is_semi_connected in a way that reuses almost 100% of our existing code.

I apologize for not thinking about it earlier, but the condition of u->v for each vertex of the NetworkX implementation is simply checking that there is a path covering all vertices in the DAG. It turns out that longest_path that we recently moved to be pure Rust code in #1192 does exactly that! So the NetworkX check is equivalent to seeing if there is a longest path with N-1 nodes in a DAG of N nodes.

Thanks a lot of for writing the original code! I know it ended up not being the original idea but this process led us to implement the least amount of code possible, which will be simpler to maintain in the future.

Comment on lines +303 to +312
match longest_path(&condensed, weight_fn) {
Ok(path_len) => {
if let Some(path_len) = path_len {
Ok(path_len.1 == n - 1)
} else {
Ok(false)
}
}
Err(_) => Err(PyValueError::new_err("Graph could not be condensed")),
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is equivalent to checking the chain of u->v from NetworkX

@IvanIsCoding IvanIsCoding added the automerge Queue a approved PR for merging label Jun 19, 2024
@coveralls
Copy link

coveralls commented Jun 19, 2024

Pull Request Test Coverage Report for Build 9575905154

Details

  • 25 of 27 (92.59%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.005%) to 95.86%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/connectivity/mod.rs 24 26 92.31%
Totals Coverage Status
Change from base Build 9575355125: -0.005%
Covered Lines: 17413
Relevant Lines: 18165

💛 - Coveralls

@IvanIsCoding IvanIsCoding merged commit bdec3ff into Qiskit:main Jun 19, 2024
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Queue a approved PR for merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add is_semiconnected function.
3 participants