From c9a9bdf8517f976c87d4f12977e931cf6b6e8303 Mon Sep 17 00:00:00 2001 From: Dom Miketa Date: Fri, 12 Sep 2025 21:55:57 +0100 Subject: [PATCH 1/7] set node_removed if nodes contracted --- src/digraph.rs | 1 + src/graph.rs | 1 + tests/digraph/test_pickle.py | 23 +++++++++++++++++++ tests/graph/test_pickle.py | 43 ++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+) diff --git a/src/digraph.rs b/src/digraph.rs index fa44c70e2b..8142699b5d 100644 --- a/src/digraph.rs +++ b/src/digraph.rs @@ -3007,6 +3007,7 @@ impl PyDiGraph { } (None, true) => self.graph.contract_nodes(nodes, obj, check_cycle)?, }; + self.node_removed = true; Ok(res.index()) } diff --git a/src/graph.rs b/src/graph.rs index 86dba28faa..8e238ab1d3 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -1899,6 +1899,7 @@ impl PyGraph { } (None, true) => self.graph.contract_nodes(nodes, obj), }; + self.node_removed = true; Ok(res.index()) } diff --git a/tests/digraph/test_pickle.py b/tests/digraph/test_pickle.py index dce9c73cbc..af1beb6dcc 100644 --- a/tests/digraph/test_pickle.py +++ b/tests/digraph/test_pickle.py @@ -39,3 +39,26 @@ def test_weight_graph(self): self.assertEqual([1, 2, 3], gprime.node_indices()) self.assertEqual(["B", "C", "D"], gprime.nodes()) self.assertEqual({1: (1, 2, "B -> C"), 3: (3, 1, "D -> B")}, dict(gprime.edge_index_map())) + + def test_contracted_nodes_pickle(self): + """Test pickle/unpickle of directed graphs with contracted nodes (issue #1503)""" + g = rx.PyDiGraph() + g.add_node("A") # Node 0 + g.add_node("B") # Node 1 + g.add_node("C") # Node 2 + + # Contract nodes 0 and 1 into a new node + contracted_idx = g.contract_nodes([0, 1], "AB") + g.add_edge(2, contracted_idx, "C -> AB") + + # Verify initial state + self.assertEqual([2, contracted_idx], g.node_indices()) + self.assertEqual([(2, contracted_idx)], g.edge_list()) + + # Test pickle/unpickle + gprime = pickle.loads(pickle.dumps(g)) + + # Verify the unpickled graph matches + self.assertEqual(g.node_indices(), gprime.node_indices()) + self.assertEqual(g.edge_list(), gprime.edge_list()) + self.assertEqual(g.nodes(), gprime.nodes()) diff --git a/tests/graph/test_pickle.py b/tests/graph/test_pickle.py index d0135fdda6..e4357ae366 100644 --- a/tests/graph/test_pickle.py +++ b/tests/graph/test_pickle.py @@ -39,3 +39,46 @@ def test_weight_graph(self): self.assertEqual([1, 2, 3], gprime.node_indices()) self.assertEqual(["B", "C", "D"], gprime.nodes()) self.assertEqual({1: (1, 2, "B -> C"), 3: (3, 1, "D -> B")}, dict(gprime.edge_index_map())) + + def test_contracted_nodes_pickle(self): + """Test pickle/unpickle of graphs with contracted nodes (issue #1503)""" + g = rx.PyGraph() + g.add_node("A") # Node 0 + g.add_node("B") # Node 1 + g.add_node("C") # Node 2 + + # Contract nodes 0 and 1 into a new node + contracted_idx = g.contract_nodes([0, 1], "AB") + g.add_edge(2, contracted_idx, "C -> AB") + + # Verify initial state + self.assertEqual([2, contracted_idx], g.node_indices()) + self.assertEqual([(2, contracted_idx)], g.edge_list()) + + # Test pickle/unpickle + gprime = pickle.loads(pickle.dumps(g)) + + # Verify the unpickled graph matches + self.assertEqual(g.node_indices(), gprime.node_indices()) + self.assertEqual(g.edge_list(), gprime.edge_list()) + self.assertEqual(g.nodes(), gprime.nodes()) + + def test_contracted_nodes_with_weights_pickle(self): + """Test pickle/unpickle of graphs with contracted nodes and edge weights""" + g = rx.PyGraph() + g.add_nodes_from(["Node0", "Node1", "Node2", "Node3"]) + g.add_edges_from([(0, 2, "edge_0_2"), (1, 3, "edge_1_3")]) + + # Contract multiple nodes + contracted_idx = g.contract_nodes([0, 1], "Contracted_0_1") + g.add_edge(contracted_idx, 2, "contracted_to_2") + g.add_edge(3, contracted_idx, "3_to_contracted") + + # Test pickle/unpickle + gprime = pickle.loads(pickle.dumps(g)) + + # Verify complete graph state is preserved + self.assertEqual(g.node_indices(), gprime.node_indices()) + self.assertEqual(g.edge_list(), gprime.edge_list()) + self.assertEqual(g.nodes(), gprime.nodes()) + self.assertEqual(dict(g.edge_index_map()), dict(gprime.edge_index_map())) From 14ec240bc57d5434a8fd7fc8822008cc24ddb40a Mon Sep 17 00:00:00 2001 From: Dom Miketa Date: Fri, 12 Sep 2025 22:03:08 +0100 Subject: [PATCH 2/7] format --- tests/digraph/test_pickle.py | 8 ++++---- tests/graph/test_pickle.py | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/digraph/test_pickle.py b/tests/digraph/test_pickle.py index af1beb6dcc..b3de7760ce 100644 --- a/tests/digraph/test_pickle.py +++ b/tests/digraph/test_pickle.py @@ -46,18 +46,18 @@ def test_contracted_nodes_pickle(self): g.add_node("A") # Node 0 g.add_node("B") # Node 1 g.add_node("C") # Node 2 - + # Contract nodes 0 and 1 into a new node contracted_idx = g.contract_nodes([0, 1], "AB") g.add_edge(2, contracted_idx, "C -> AB") - + # Verify initial state self.assertEqual([2, contracted_idx], g.node_indices()) self.assertEqual([(2, contracted_idx)], g.edge_list()) - + # Test pickle/unpickle gprime = pickle.loads(pickle.dumps(g)) - + # Verify the unpickled graph matches self.assertEqual(g.node_indices(), gprime.node_indices()) self.assertEqual(g.edge_list(), gprime.edge_list()) diff --git a/tests/graph/test_pickle.py b/tests/graph/test_pickle.py index e4357ae366..c1755830d3 100644 --- a/tests/graph/test_pickle.py +++ b/tests/graph/test_pickle.py @@ -46,18 +46,18 @@ def test_contracted_nodes_pickle(self): g.add_node("A") # Node 0 g.add_node("B") # Node 1 g.add_node("C") # Node 2 - + # Contract nodes 0 and 1 into a new node contracted_idx = g.contract_nodes([0, 1], "AB") g.add_edge(2, contracted_idx, "C -> AB") - + # Verify initial state self.assertEqual([2, contracted_idx], g.node_indices()) self.assertEqual([(2, contracted_idx)], g.edge_list()) - + # Test pickle/unpickle gprime = pickle.loads(pickle.dumps(g)) - + # Verify the unpickled graph matches self.assertEqual(g.node_indices(), gprime.node_indices()) self.assertEqual(g.edge_list(), gprime.edge_list()) @@ -68,15 +68,15 @@ def test_contracted_nodes_with_weights_pickle(self): g = rx.PyGraph() g.add_nodes_from(["Node0", "Node1", "Node2", "Node3"]) g.add_edges_from([(0, 2, "edge_0_2"), (1, 3, "edge_1_3")]) - + # Contract multiple nodes - contracted_idx = g.contract_nodes([0, 1], "Contracted_0_1") + contracted_idx = g.contract_nodes([0, 1], "Contracted_0_1") g.add_edge(contracted_idx, 2, "contracted_to_2") g.add_edge(3, contracted_idx, "3_to_contracted") - + # Test pickle/unpickle gprime = pickle.loads(pickle.dumps(g)) - + # Verify complete graph state is preserved self.assertEqual(g.node_indices(), gprime.node_indices()) self.assertEqual(g.edge_list(), gprime.edge_list()) From f870d2626fce70cc05b6e91401b77846dc8578e3 Mon Sep 17 00:00:00 2001 From: Dom Miketa Date: Thu, 16 Oct 2025 11:49:52 +0100 Subject: [PATCH 3/7] json preserves node gaps --- src/json/mod.rs | 16 ++++---- src/json/node_link_data.rs | 83 ++++++++++++++++++++++++++++++-------- 2 files changed, 75 insertions(+), 24 deletions(-) diff --git a/src/json/mod.rs b/src/json/mod.rs index e72fa79995..1456e3d5c9 100644 --- a/src/json/mod.rs +++ b/src/json/mod.rs @@ -74,12 +74,12 @@ pub fn from_node_link_json_file<'py>( Ok(if graph.directed { let mut inner_graph: StablePyGraph = StablePyGraph::with_capacity(graph.nodes.len(), graph.links.len()); - node_link_data::parse_node_link_data(&py, graph, &mut inner_graph, node_attrs, edge_attrs)?; + let node_removed = node_link_data::parse_node_link_data(&py, graph, &mut inner_graph, node_attrs, edge_attrs)?; digraph::PyDiGraph { graph: inner_graph, cycle_state: algo::DfsSpace::default(), check_cycle: false, - node_removed: false, + node_removed, multigraph, attrs, } @@ -88,11 +88,11 @@ pub fn from_node_link_json_file<'py>( } else { let mut inner_graph: StablePyGraph = StablePyGraph::with_capacity(graph.nodes.len(), graph.links.len()); - node_link_data::parse_node_link_data(&py, graph, &mut inner_graph, node_attrs, edge_attrs)?; + let node_removed = node_link_data::parse_node_link_data(&py, graph, &mut inner_graph, node_attrs, edge_attrs)?; graph::PyGraph { graph: inner_graph, - node_removed: false, + node_removed, multigraph, attrs, } @@ -150,12 +150,12 @@ pub fn parse_node_link_json<'py>( Ok(if graph.directed { let mut inner_graph: StablePyGraph = StablePyGraph::with_capacity(graph.nodes.len(), graph.links.len()); - node_link_data::parse_node_link_data(&py, graph, &mut inner_graph, node_attrs, edge_attrs)?; + let node_removed = node_link_data::parse_node_link_data(&py, graph, &mut inner_graph, node_attrs, edge_attrs)?; digraph::PyDiGraph { graph: inner_graph, cycle_state: algo::DfsSpace::default(), check_cycle: false, - node_removed: false, + node_removed, multigraph, attrs, } @@ -164,10 +164,10 @@ pub fn parse_node_link_json<'py>( } else { let mut inner_graph: StablePyGraph = StablePyGraph::with_capacity(graph.nodes.len(), graph.links.len()); - node_link_data::parse_node_link_data(&py, graph, &mut inner_graph, node_attrs, edge_attrs)?; + let node_removed = node_link_data::parse_node_link_data(&py, graph, &mut inner_graph, node_attrs, edge_attrs)?; graph::PyGraph { graph: inner_graph, - node_removed: false, + node_removed, multigraph, attrs, } diff --git a/src/json/node_link_data.rs b/src/json/node_link_data.rs index 8ceb6b449b..802b09cb92 100644 --- a/src/json/node_link_data.rs +++ b/src/json/node_link_data.rs @@ -83,22 +83,73 @@ pub fn parse_node_link_data( out_graph: &mut StablePyGraph, node_attrs: Option, edge_attrs: Option, -) -> PyResult<()> { +) -> PyResult { let mut id_mapping: HashMap = HashMap::with_capacity(graph.nodes.len()); - for node in graph.nodes { - let payload = match node.data { - Some(data) => match node_attrs { - Some(ref callback) => callback.call1(*py, (data,))?, - None => data.into_py_any(*py)?, - }, - None => py.None(), - }; - let id = out_graph.add_node(payload); - match node.id { - Some(input_id) => id_mapping.insert(input_id, id), - None => id_mapping.insert(id.index(), id), - }; - } + + // Check if nodes have explicit IDs that need preservation + let preserve_ids = graph.nodes.iter().any(|n| n.id.is_some()); + + let node_removed = if preserve_ids { + // Find the maximum node ID to determine how many placeholder nodes we need + let max_id = graph.nodes.iter() + .filter_map(|n| n.id) + .max() + .unwrap_or(0); + + // Create placeholder nodes up to max_id + let mut tmp_nodes: Vec = Vec::new(); + for i in 0..=max_id { + let idx = out_graph.add_node(py.None()); + tmp_nodes.push(idx); + } + + // Replace placeholder nodes with actual data and track which to keep + for node in graph.nodes { + let payload = match node.data { + Some(data) => match node_attrs { + Some(ref callback) => callback.call1(*py, (data,))?, + None => data.into_py_any(*py)?, + }, + None => py.None(), + }; + let node_id = node.id.unwrap_or(0); + let idx = NodeIndex::new(node_id); + + // Replace the placeholder with actual data + if let Some(weight) = out_graph.node_weight_mut(idx) { + *weight = payload; + } + + id_mapping.insert(node_id, idx); + // Mark this index as used (remove from tmp_nodes) + tmp_nodes.retain(|&n| n != idx); + } + + // Track if we're removing any nodes (indicates gaps in indices) + let has_gaps = !tmp_nodes.is_empty(); + + // Remove remaining placeholder nodes + for tmp_node in tmp_nodes { + out_graph.remove_node(tmp_node); + } + + has_gaps + } else { + // No explicit IDs, just add nodes sequentially (legacy behavior) + for node in graph.nodes { + let payload = match node.data { + Some(data) => match node_attrs { + Some(ref callback) => callback.call1(*py, (data,))?, + None => data.into_py_any(*py)?, + }, + None => py.None(), + }; + let id = out_graph.add_node(payload); + id_mapping.insert(id.index(), id); + } + false + }; + for edge in graph.links { let data = match edge.data { Some(data) => match edge_attrs { @@ -109,7 +160,7 @@ pub fn parse_node_link_data( }; out_graph.add_edge(id_mapping[&edge.source], id_mapping[&edge.target], data); } - Ok(()) + Ok(node_removed) } #[allow(clippy::too_many_arguments)] From 31db01e9fccb27654bf47a498dba8bc525e2d93c Mon Sep 17 00:00:00 2001 From: Dom Miketa Date: Thu, 16 Oct 2025 11:56:08 +0100 Subject: [PATCH 4/7] add json tests --- tests/digraph/test_node_link_json.py | 42 +++++++++++++++++ tests/graph/test_node_link_json.py | 67 ++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) diff --git a/tests/digraph/test_node_link_json.py b/tests/digraph/test_node_link_json.py index 3ea26a4fe7..86675a9e06 100644 --- a/tests/digraph/test_node_link_json.py +++ b/tests/digraph/test_node_link_json.py @@ -223,3 +223,45 @@ def test_not_JSON(self): """ with self.assertRaises(rustworkx.JSONDeserializationError): rustworkx.parse_node_link_json(invalid_input) + + def test_node_indices_preserved_with_deletion(self): + """Test that node indices are preserved after deletion (related to issue #1503)""" + graph = rustworkx.PyDiGraph() + graph.add_node("A") # 0 + graph.add_node("B") # 1 + graph.add_node("C") # 2 + graph.add_edge(0, 2, "A->C") + graph.remove_node(1) # Remove middle node + + # Verify original has gaps in indices + self.assertEqual([0, 2], graph.node_indices()) + + # Round-trip through JSON + json_str = rustworkx.node_link_json(graph) + restored = rustworkx.parse_node_link_json(json_str) + + # Verify indices are preserved + self.assertEqual(graph.node_indices(), restored.node_indices()) + self.assertEqual(graph.edge_list(), restored.edge_list()) + + def test_node_indices_preserved_with_contraction(self): + """Test that node indices are preserved after contraction (issue #1503)""" + graph = rustworkx.PyDiGraph() + graph.add_node("A") # 0 + graph.add_node("B") # 1 + graph.add_node("C") # 2 + + # Contract nodes 0 and 1 + contracted_idx = graph.contract_nodes([0, 1], "AB") + graph.add_edge(2, contracted_idx, "C->AB") + + # Verify original has non-consecutive indices + self.assertEqual([2, contracted_idx], graph.node_indices()) + + # Round-trip through JSON + json_str = rustworkx.node_link_json(graph) + restored = rustworkx.parse_node_link_json(json_str) + + # Verify indices are preserved + self.assertEqual(graph.node_indices(), restored.node_indices()) + self.assertEqual(graph.edge_list(), restored.edge_list()) diff --git a/tests/graph/test_node_link_json.py b/tests/graph/test_node_link_json.py index 1408f9c8c5..bf8f0536f2 100644 --- a/tests/graph/test_node_link_json.py +++ b/tests/graph/test_node_link_json.py @@ -204,3 +204,70 @@ def test_round_trip_with_file_no_graph_attr(self): self.assertEqual(new.nodes(), graph.nodes()) self.assertEqual(new.weighted_edge_list(), graph.weighted_edge_list()) self.assertEqual(new.attrs, {"label": graph.attrs}) + + def test_node_indices_preserved_with_deletion(self): + """Test that node indices are preserved after deletion (related to issue #1503)""" + graph = rustworkx.PyGraph() + graph.add_node("A") # 0 + graph.add_node("B") # 1 + graph.add_node("C") # 2 + graph.remove_node(1) # Remove middle node + + # Verify original has gaps in indices + self.assertEqual([0, 2], graph.node_indices()) + + # Round-trip through JSON + json_str = rustworkx.node_link_json(graph) + restored = rustworkx.parse_node_link_json(json_str) + + # Verify indices are preserved + self.assertEqual(graph.node_indices(), restored.node_indices()) + self.assertEqual(graph.nodes(), restored.nodes()) + + def test_node_indices_preserved_with_contraction(self): + """Test that node indices are preserved after contraction (issue #1503)""" + graph = rustworkx.PyGraph() + graph.add_node("A") # 0 + graph.add_node("B") # 1 + graph.add_node("C") # 2 + + # Contract nodes 0 and 1 + contracted_idx = graph.contract_nodes([0, 1], "AB") + graph.add_edge(2, contracted_idx, "C->AB") + + # Verify original has non-consecutive indices + self.assertEqual([2, contracted_idx], graph.node_indices()) + + # Round-trip through JSON + json_str = rustworkx.node_link_json(graph) + restored = rustworkx.parse_node_link_json(json_str) + + # Verify indices are preserved + self.assertEqual(graph.node_indices(), restored.node_indices()) + self.assertEqual(graph.edge_list(), restored.edge_list()) + + def test_node_indices_preserved_complex(self): + """Test index preservation with multiple deletions and edges""" + graph = rustworkx.PyGraph() + for i in range(6): + graph.add_node(f"Node{i}") + + graph.add_edge(0, 1, "0-1") + graph.add_edge(2, 3, "2-3") + graph.add_edge(4, 5, "4-5") + + # Remove nodes 1 and 4 + graph.remove_node(1) + graph.remove_node(4) + + # Verify gaps exist + self.assertEqual([0, 2, 3, 5], graph.node_indices()) + + # Round-trip through JSON + json_str = rustworkx.node_link_json(graph) + restored = rustworkx.parse_node_link_json(json_str) + + # Verify complete state is preserved + self.assertEqual(graph.node_indices(), restored.node_indices()) + self.assertEqual(graph.edge_list(), restored.edge_list()) + self.assertEqual(graph.nodes(), restored.nodes()) From 922e1a5f102a4c4097424906e308e585f432817e Mon Sep 17 00:00:00 2001 From: Dom Miketa Date: Thu, 16 Oct 2025 12:11:08 +0100 Subject: [PATCH 5/7] run rustfmt --- src/json/mod.rs | 32 ++++++++++++++++++++++++++++---- src/json/node_link_data.rs | 5 +---- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/json/mod.rs b/src/json/mod.rs index 1456e3d5c9..f35388f197 100644 --- a/src/json/mod.rs +++ b/src/json/mod.rs @@ -74,7 +74,13 @@ pub fn from_node_link_json_file<'py>( Ok(if graph.directed { let mut inner_graph: StablePyGraph = StablePyGraph::with_capacity(graph.nodes.len(), graph.links.len()); - let node_removed = node_link_data::parse_node_link_data(&py, graph, &mut inner_graph, node_attrs, edge_attrs)?; + let node_removed = node_link_data::parse_node_link_data( + &py, + graph, + &mut inner_graph, + node_attrs, + edge_attrs, + )?; digraph::PyDiGraph { graph: inner_graph, cycle_state: algo::DfsSpace::default(), @@ -88,7 +94,13 @@ pub fn from_node_link_json_file<'py>( } else { let mut inner_graph: StablePyGraph = StablePyGraph::with_capacity(graph.nodes.len(), graph.links.len()); - let node_removed = node_link_data::parse_node_link_data(&py, graph, &mut inner_graph, node_attrs, edge_attrs)?; + let node_removed = node_link_data::parse_node_link_data( + &py, + graph, + &mut inner_graph, + node_attrs, + edge_attrs, + )?; graph::PyGraph { graph: inner_graph, @@ -150,7 +162,13 @@ pub fn parse_node_link_json<'py>( Ok(if graph.directed { let mut inner_graph: StablePyGraph = StablePyGraph::with_capacity(graph.nodes.len(), graph.links.len()); - let node_removed = node_link_data::parse_node_link_data(&py, graph, &mut inner_graph, node_attrs, edge_attrs)?; + let node_removed = node_link_data::parse_node_link_data( + &py, + graph, + &mut inner_graph, + node_attrs, + edge_attrs, + )?; digraph::PyDiGraph { graph: inner_graph, cycle_state: algo::DfsSpace::default(), @@ -164,7 +182,13 @@ pub fn parse_node_link_json<'py>( } else { let mut inner_graph: StablePyGraph = StablePyGraph::with_capacity(graph.nodes.len(), graph.links.len()); - let node_removed = node_link_data::parse_node_link_data(&py, graph, &mut inner_graph, node_attrs, edge_attrs)?; + let node_removed = node_link_data::parse_node_link_data( + &py, + graph, + &mut inner_graph, + node_attrs, + edge_attrs, + )?; graph::PyGraph { graph: inner_graph, node_removed, diff --git a/src/json/node_link_data.rs b/src/json/node_link_data.rs index 802b09cb92..4061bc28e3 100644 --- a/src/json/node_link_data.rs +++ b/src/json/node_link_data.rs @@ -91,10 +91,7 @@ pub fn parse_node_link_data( let node_removed = if preserve_ids { // Find the maximum node ID to determine how many placeholder nodes we need - let max_id = graph.nodes.iter() - .filter_map(|n| n.id) - .max() - .unwrap_or(0); + let max_id = graph.nodes.iter().filter_map(|n| n.id).max().unwrap_or(0); // Create placeholder nodes up to max_id let mut tmp_nodes: Vec = Vec::new(); From 388ad0094eff6b7417937899b680ba2c58d52e9f Mon Sep 17 00:00:00 2001 From: Dom Miketa Date: Thu, 16 Oct 2025 12:41:00 +0100 Subject: [PATCH 6/7] rename unused variable --- src/json/node_link_data.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/json/node_link_data.rs b/src/json/node_link_data.rs index 4061bc28e3..eb852c1c2f 100644 --- a/src/json/node_link_data.rs +++ b/src/json/node_link_data.rs @@ -95,7 +95,7 @@ pub fn parse_node_link_data( // Create placeholder nodes up to max_id let mut tmp_nodes: Vec = Vec::new(); - for i in 0..=max_id { + for _ in 0..=max_id { let idx = out_graph.add_node(py.None()); tmp_nodes.push(idx); } From 7d1e5168efe877dac197f50c41a0fa121da0c245 Mon Sep 17 00:00:00 2001 From: Dom Miketa Date: Thu, 16 Oct 2025 15:04:25 +0100 Subject: [PATCH 7/7] fix test --- tests/graph/test_node_link_json.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/graph/test_node_link_json.py b/tests/graph/test_node_link_json.py index bf8f0536f2..d801ecbb91 100644 --- a/tests/graph/test_node_link_json.py +++ b/tests/graph/test_node_link_json.py @@ -208,9 +208,10 @@ def test_round_trip_with_file_no_graph_attr(self): def test_node_indices_preserved_with_deletion(self): """Test that node indices are preserved after deletion (related to issue #1503)""" graph = rustworkx.PyGraph() - graph.add_node("A") # 0 - graph.add_node("B") # 1 - graph.add_node("C") # 2 + graph.add_node(None) # 0 + graph.add_node(None) # 1 + graph.add_node(None) # 2 + graph.add_edge(0, 2, None) graph.remove_node(1) # Remove middle node # Verify original has gaps in indices @@ -222,18 +223,18 @@ def test_node_indices_preserved_with_deletion(self): # Verify indices are preserved self.assertEqual(graph.node_indices(), restored.node_indices()) - self.assertEqual(graph.nodes(), restored.nodes()) + self.assertEqual(graph.edge_list(), restored.edge_list()) def test_node_indices_preserved_with_contraction(self): """Test that node indices are preserved after contraction (issue #1503)""" graph = rustworkx.PyGraph() - graph.add_node("A") # 0 - graph.add_node("B") # 1 - graph.add_node("C") # 2 + graph.add_node(None) # 0 + graph.add_node(None) # 1 + graph.add_node(None) # 2 # Contract nodes 0 and 1 - contracted_idx = graph.contract_nodes([0, 1], "AB") - graph.add_edge(2, contracted_idx, "C->AB") + contracted_idx = graph.contract_nodes([0, 1], None) + graph.add_edge(2, contracted_idx, None) # Verify original has non-consecutive indices self.assertEqual([2, contracted_idx], graph.node_indices()) @@ -250,11 +251,11 @@ def test_node_indices_preserved_complex(self): """Test index preservation with multiple deletions and edges""" graph = rustworkx.PyGraph() for i in range(6): - graph.add_node(f"Node{i}") + graph.add_node(None) - graph.add_edge(0, 1, "0-1") - graph.add_edge(2, 3, "2-3") - graph.add_edge(4, 5, "4-5") + graph.add_edge(0, 1, None) + graph.add_edge(2, 3, None) + graph.add_edge(4, 5, None) # Remove nodes 1 and 4 graph.remove_node(1) @@ -270,4 +271,3 @@ def test_node_indices_preserved_complex(self): # Verify complete state is preserved self.assertEqual(graph.node_indices(), restored.node_indices()) self.assertEqual(graph.edge_list(), restored.edge_list()) - self.assertEqual(graph.nodes(), restored.nodes())