Skip to content

Commit 49727f8

Browse files
authored
Ensure uniqueness of neighbor indices returned by neighbors() (#250)
* Ensure uniqueness of neighbor indices returned by `neightbors()` petgraph's underlaying `neighbors()` method returns duplicated nodes in multigraphs (on per-edge basis). The desired behavior for the public API is not to include these repetitions though. This commit collects the returned indices in a HashSet to remove duplications, then it exhausts the set with `drain()` and collect it into a list. This commit also adds new tests to check there is no duplication in the list of neighbors indices and adapt the existing tests to ignore the order. * Add a release note summarizing the change * Fix tests
1 parent 8c7a743 commit 49727f8

File tree

5 files changed

+38
-3
lines changed

5 files changed

+38
-3
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
upgrade:
3+
- |
4+
If you are using the method :meth:`~retworkx.PyDiGraph.neighbors`, notice
5+
the returned iterator now removes repeated node indices. See
6+
`#250 <https://github.com/Qiskit/retworkx/issues/250>`__ for more details.
7+
fixes:
8+
- |
9+
The iterator returned by the method :meth:`~retworkx.PyDiGraph.neighbors`
10+
now goes throw all the node indices without repetition. See
11+
`#250 <https://github.com/Qiskit/retworkx/issues/250>`__ for more details.

src/digraph.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ impl PyDiGraph {
13911391
///
13921392
/// :param int node: The index of the node to get the neighbors of
13931393
///
1394-
/// :returns: A list of the neighbor node indicies
1394+
/// :returns: A list of the neighbor node indices
13951395
/// :rtype: NodeIndices
13961396
#[text_signature = "(self, node, /)"]
13971397
pub fn neighbors(&self, node: usize) -> NodeIndices {
@@ -1400,6 +1400,8 @@ impl PyDiGraph {
14001400
.graph
14011401
.neighbors(NodeIndex::new(node))
14021402
.map(|node| node.index())
1403+
.collect::<HashSet<usize>>()
1404+
.drain()
14031405
.collect(),
14041406
}
14051407
}

src/graph.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,8 @@ impl PyGraph {
857857
.graph
858858
.neighbors(NodeIndex::new(node))
859859
.map(|node| node.index())
860+
.collect::<HashSet<usize>>()
861+
.drain()
860862
.collect(),
861863
}
862864
}

tests/graph/test_neighbors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_single_neighbor(self):
2424
node_c = graph.add_node('c')
2525
graph.add_edge(node_a, node_c, {'a': 2})
2626
res = graph.neighbors(node_a)
27-
self.assertEqual([node_c, node_b], res)
27+
self.assertCountEqual([node_c, node_b], res)
2828

2929
def test_no_neighbor(self):
3030
graph = retworkx.PyGraph()

tests/test_neighbors.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,27 @@ def test_single_neighbor(self):
2222
node_b = dag.add_child(node_a, 'b', {'a': 1})
2323
node_c = dag.add_child(node_a, 'c', {'a': 2})
2424
res = dag.neighbors(node_a)
25-
self.assertEqual([node_c, node_b], res)
25+
self.assertCountEqual([node_c, node_b], res)
26+
27+
def test_unique_neighbors_on_dags(self):
28+
dag = retworkx.PyDAG()
29+
node_a = dag.add_node('a')
30+
node_b = dag.add_child(node_a, 'b', ['edge a->b'])
31+
node_c = dag.add_child(node_a, 'c', ['edge a->c'])
32+
dag.add_edge(node_a, node_b, ['edge a->b bis'])
33+
res = dag.neighbors(node_a)
34+
self.assertCountEqual([node_c, node_b], res)
35+
36+
def test_unique_neighbors_on_graphs(self):
37+
dag = retworkx.PyGraph()
38+
node_a = dag.add_node('a')
39+
node_b = dag.add_node('b')
40+
node_c = dag.add_node('c')
41+
dag.add_edge(node_a, node_b, ['edge a->b'])
42+
dag.add_edge(node_a, node_b, ['edge a->b bis'])
43+
dag.add_edge(node_a, node_c, ['edge a->c'])
44+
res = dag.neighbors(node_a)
45+
self.assertCountEqual([node_c, node_b], res)
2646

2747
def test_single_neighbor_dir(self):
2848
dag = retworkx.PyDAG()

0 commit comments

Comments
 (0)