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
1 change: 1 addition & 0 deletions rustworkx/rustworkx.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
IvanIsCoding marked this conversation as resolved.
Show resolved Hide resolved
def insert_node_on_in_edges(self, node: int, ref_node: int, /) -> None: ...
def insert_node_on_in_edges_multiple(self, node: int, ref_nodes: Sequence[int], /) -> None: ...
def insert_node_on_out_edges(self, node: int, ref_node: int, /) -> None: ...
Expand Down
23 changes: 23 additions & 0 deletions src/digraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1842,6 +1842,29 @@ impl PyDiGraph {
}
}

/// Return the list of incoming edge indices to a provided node
///
/// This method will return the incoming edges of the provided
/// ``node``.
///
/// :param int node: The node index to get incoming edges from. If
/// this node index is not present in the graph this method will
/// return an empty list and not error.
///
/// :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 {
IvanIsCoding marked this conversation as resolved.
Show resolved Hide resolved
let node_index = NodeIndex::new(node);
IvanIsCoding marked this conversation as resolved.
Show resolved Hide resolved
EdgeIndices {
edges: self
.graph
.edges_directed(node_index, petgraph::Direction::Incoming)
IvanIsCoding marked this conversation as resolved.
Show resolved Hide resolved
.map(|e| e.id().index())
.collect(),
}
}

/// Return the index map of edges incident to a provided node
///
/// By default this method will only return the outgoing edges of
Expand Down
17 changes: 17 additions & 0 deletions tests/digraph/test_edges.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
IvanIsCoding marked this conversation as resolved.
Show resolved Hide resolved
graph = rustworkx.PyDiGraph()
node_a = graph.add_node(0)
node_b = graph.add_node(1)
node_c = graph.add_node("c")
node_d = graph.add_node("d")
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)
IvanIsCoding marked this conversation as resolved.
Show resolved Hide resolved

def test_in_edge_indices_invalid_node(self):
graph = rustworkx.PyDiGraph()
res = graph.in_edge_indices(0)
self.assertEqual([], res)

def test_incident_edge_index_map(self):
graph = rustworkx.PyDiGraph()
node_a = graph.add_node(0)
Expand Down
Loading