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 in and out edge indices functions #1369

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

ThisuraGallage
Copy link

@ThisuraGallage ThisuraGallage commented Jan 21, 2025

This PR introduces a new method, in_edge_indices, to the rustworkx.PyDiGraph class. The method returns the indices of incoming edges for a specified node.
#1360

  • I ran rustfmt locally
  • I have added the tests to cover my changes.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.

I'll add the documentation once the function signature is finalized.

@ThisuraGallage ThisuraGallage changed the title Add in edge indices function [WIP]Add in edge indices function Jan 21, 2025
@coveralls
Copy link

Pull Request Test Coverage Report for Build 12892224234

Details

  • 10 of 10 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.002%) to 95.813%

Totals Coverage Status
Change from base Build 12817417491: 0.002%
Covered Lines: 18353
Relevant Lines: 19155

💛 - 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.

The current implementation looks good to me. I left some minor nitpick comments but they are not blocking.

What is going to be more work is adding even more methods...

  • PyGraph should have the same methods even if the implementation is just calling incident_edges for consistency
  • PyDiGraph and PyGraph could use a out_edge_indices

So that is 3 more methods which were in the original request but are useful to maintain some coherence accross the API

@@ -1423,6 +1423,7 @@ class PyDiGraph(Generic[_S, _T]):
def in_edges(self, node: int, /) -> WeightedEdgeList[_T]: ...
def incident_edge_index_map(self, node: int, /, all_edges: bool = ...) -> EdgeIndexMap: ...
def incident_edges(self, node: int, /, all_edges: bool = ...) -> EdgeIndices: ...
def in_edge_indices(self, node: int, /) -> EdgeIndices: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add this method to PyGraph as well. It is dumb BUT it doesn't hurt to keep the API consistent.

It will just return all edges for the PyGraph case and you should note that in the docstring

src/digraph.rs Outdated
EdgeIndices {
edges: self
.graph
.edges_directed(node_index, petgraph::Direction::Incoming)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
.edges_directed(node_index, petgraph::Direction::Incoming)
.edges_directed(node_index, dir)

/// :rtype: EdgeIndices
#[pyo3(text_signature = "(self, node, /)")]
pub fn in_edge_indices(&self, node: usize) -> EdgeIndices {
let node_index = NodeIndex::new(node);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
let node_index = NodeIndex::new(node);
let node_index = NodeIndex::new(node);
let dir = petgraph::Direction::Incoming;

This is a nit the other functions just use this style. I didn't write them but I am ok sticking with it

/// :returns: A list of the incoming edge indices to a node in the graph
/// :rtype: EdgeIndices
#[pyo3(text_signature = "(self, node, /)")]
pub fn in_edge_indices(&self, node: usize) -> EdgeIndices {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Another suggestion is adding out_edge_indices which is the same function but with dir = petgraph::Direction::Outgoing

@@ -813,6 +813,23 @@ def test_incident_edges_all_edges(self):
res = graph.incident_edges(node_d, all_edges=True)
self.assertEqual([2, 1], res)

def test_in_edge_indices(self):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I know you copied this test case from above but in hindsight we can improve it

Comment on lines 822 to 826
graph.add_edge(node_a, node_c, "edge a")
graph.add_edge(node_b, node_d, "edge b")
graph.add_edge(node_d, node_c, "edge c")
res = graph.in_edge_indices(node_c)
self.assertEqual([2, 0], res)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
graph.add_edge(node_a, node_c, "edge a")
graph.add_edge(node_b, node_d, "edge b")
graph.add_edge(node_d, node_c, "edge c")
res = graph.in_edge_indices(node_c)
self.assertEqual([2, 0], res)
edge_ac = graph.add_edge(node_a, node_c, "edge a")
graph.add_edge(node_b, node_d, "edge b")
edge_dc = graph.add_edge(node_d, node_c, "edge c")
res = graph.in_edge_indices(node_c)
self.assertEqual([edge_dc, edge_ac], res)

@IvanIsCoding IvanIsCoding added this to the 0.17.0 milestone Jan 23, 2025
@ThisuraGallage ThisuraGallage marked this pull request as ready for review January 23, 2025 11:02
@ThisuraGallage ThisuraGallage changed the title [WIP]Add in edge indices function Add in and out edge indices functions Jan 23, 2025
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.

3 participants