Skip to content

Commit 745e87b

Browse files
jberg5IvanIsCoding
andauthored
Fix is_semi_connected (#1561)
Co-authored-by: Ivan Carvalho <8753214+IvanIsCoding@users.noreply.github.com>
1 parent 27bdc76 commit 745e87b

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

src/connectivity/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -618,10 +618,10 @@ pub fn is_semi_connected(graph: &digraph::PyDiGraph) -> PyResult<bool> {
618618
}
619619

620620
let mut temp_graph = DiGraph::new();
621-
let mut node_map = Vec::new();
621+
let mut node_map = vec![NodeIndex::end(); graph.graph.node_bound()];
622622

623-
for _node in graph.graph.node_indices() {
624-
node_map.push(temp_graph.add_node(()));
623+
for node in graph.graph.node_indices() {
624+
node_map[node.index()] = temp_graph.add_node(());
625625
}
626626

627627
for edge in graph.graph.edge_indices() {

tests/digraph/test_is_semi_connected.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,14 @@ def test_is_semi_connected_directed_star_graph(self):
7474
def test_is_semi_connected_directed_grid_graph(self):
7575
graph = rustworkx.generators.directed_grid_graph(10, 10)
7676
self.assertEqual(rustworkx.is_semi_connected(graph), naive_semi_connected(graph))
77+
78+
def test_is_semi_connected_with_node_gaps(self):
79+
graph = rustworkx.PyDiGraph()
80+
graph.add_nodes_from(list(range(5)))
81+
graph.remove_node(1)
82+
# Remaining nodes 0, 2, 3, 4 form a path
83+
graph.add_edge(0, 2, None)
84+
graph.add_edge(2, 3, None)
85+
graph.add_edge(3, 4, None)
86+
87+
self.assertEqual(rustworkx.is_semi_connected(graph), naive_semi_connected(graph))

0 commit comments

Comments
 (0)