From 4e869f4c470278ed2e22e5242810e944a120e8b4 Mon Sep 17 00:00:00 2001 From: Edwin Navarro Date: Mon, 31 Jul 2023 06:59:54 -0700 Subject: [PATCH 01/17] Initial tests --- rustworkx-core/src/generators/random_graph.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rustworkx-core/src/generators/random_graph.rs b/rustworkx-core/src/generators/random_graph.rs index 235adbb71..97a835e6a 100644 --- a/rustworkx-core/src/generators/random_graph.rs +++ b/rustworkx-core/src/generators/random_graph.rs @@ -118,6 +118,7 @@ where let num_nodes: isize = num_nodes as isize; let lp: f64 = (1.0 - probability).ln(); + println!("vw1 {:?} {:?}", v, w); let between = Uniform::new(0.0, 1.0); while v < num_nodes { let random: f64 = between.sample(&mut rng); @@ -131,6 +132,7 @@ where w += 1; } } + println!("vw2 {:?} {:?}", v, w); while v < num_nodes && ((directed && num_nodes <= w) || (!directed && v <= w)) { w -= v; v += 1; @@ -139,6 +141,8 @@ where w -= v; v += 1; } + println!("vw3 {:?} {:?}", v, w); + } if v < num_nodes { let v_index = graph.from_index(v as usize); From 9308f1ad3eb175a47b0a091b381c40200d4e587e Mon Sep 17 00:00:00 2001 From: Edwin Navarro Date: Mon, 31 Jul 2023 12:43:46 -0700 Subject: [PATCH 02/17] Convert gnp_random to networkx version --- rustworkx-core/src/generators/random_graph.rs | 55 +++++++++++-------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/rustworkx-core/src/generators/random_graph.rs b/rustworkx-core/src/generators/random_graph.rs index 97a835e6a..81b794062 100644 --- a/rustworkx-core/src/generators/random_graph.rs +++ b/rustworkx-core/src/generators/random_graph.rs @@ -99,6 +99,7 @@ where if !(0.0..=1.0).contains(&probability) { return Err(InvalidInputError {}); } + if probability > 0.0 { if (probability - 1.0).abs() < std::f64::EPSILON { for u in 0..num_nodes { @@ -113,43 +114,51 @@ where } } } else { - let mut v: isize = if directed { 0 } else { 1 }; - let mut w: isize = -1; - let num_nodes: isize = num_nodes as isize; + let num_nodes = num_nodes as isize; let lp: f64 = (1.0 - probability).ln(); - - println!("vw1 {:?} {:?}", v, w); let between = Uniform::new(0.0, 1.0); - while v < num_nodes { - let random: f64 = between.sample(&mut rng); - let lr: f64 = (1.0 - random).ln(); - let ratio: isize = (lr / lp) as isize; - w = w + 1 + ratio; - if directed { - // avoid self loops - if v == w { - w += 1; + if directed { + let mut v: isize = 0; + let mut w: isize = -1; + while v < num_nodes { + let random: f64 = between.sample(&mut rng); + let lr: f64 = (1.0 - random).ln(); + w = w + 1 + ((lr / lp) as isize); + while w >= v && v < num_nodes { + w = w - v; + v = v + 1; } - } - println!("vw2 {:?} {:?}", v, w); - while v < num_nodes && ((directed && num_nodes <= w) || (!directed && v <= w)) { - w -= v; - v += 1; - // avoid self loops - if directed && v == w { + // Skip self-loops + if v == w { w -= v; v += 1; } - println!("vw3 {:?} {:?}", v, w); + if v < num_nodes { + let v_index = graph.from_index(v as usize); + let w_index = graph.from_index(w as usize); + graph.add_edge(w_index, v_index, default_edge_weight()); + } + } + } + // Nodes in graph are from 0,n-1 (start with v as the second node index). + let mut v: isize = 1; + let mut w: isize = -1; + while v < num_nodes { + let random: f64 = between.sample(&mut rng); + let lr: f64 = (1.0 - random).ln(); + w = w + 1 + ((lr / lp) as isize); + while w >= v && v < num_nodes { + w = w - v; + v = v + 1; } if v < num_nodes { let v_index = graph.from_index(v as usize); let w_index = graph.from_index(w as usize); graph.add_edge(v_index, w_index, default_edge_weight()); } - } + } } } Ok(graph) From faf391cb626355737fb0d01f6cd3f6848b0c823c Mon Sep 17 00:00:00 2001 From: Edwin Navarro Date: Mon, 31 Jul 2023 13:46:32 -0700 Subject: [PATCH 03/17] Fix directed gnp random graph --- rustworkx-core/src/generators/random_graph.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/rustworkx-core/src/generators/random_graph.rs b/rustworkx-core/src/generators/random_graph.rs index 81b794062..60de1d7a2 100644 --- a/rustworkx-core/src/generators/random_graph.rs +++ b/rustworkx-core/src/generators/random_graph.rs @@ -118,6 +118,7 @@ where let lp: f64 = (1.0 - probability).ln(); let between = Uniform::new(0.0, 1.0); + // For directed, create inward edges to a v if directed { let mut v: isize = 0; let mut w: isize = -1; @@ -126,8 +127,8 @@ where let lr: f64 = (1.0 - random).ln(); w = w + 1 + ((lr / lp) as isize); while w >= v && v < num_nodes { - w = w - v; - v = v + 1; + w -= v; + v += 1; } // Skip self-loops if v == w { @@ -142,6 +143,7 @@ where } } + // For directed and undirected, create outward edges from a v // Nodes in graph are from 0,n-1 (start with v as the second node index). let mut v: isize = 1; let mut w: isize = -1; @@ -150,8 +152,8 @@ where let lr: f64 = (1.0 - random).ln(); w = w + 1 + ((lr / lp) as isize); while w >= v && v < num_nodes { - w = w - v; - v = v + 1; + w -= v; + v += 1; } if v < num_nodes { let v_index = graph.from_index(v as usize); From 876f76eabfa45ac3c8ab6c3f22776b8504b77a52 Mon Sep 17 00:00:00 2001 From: Edwin Navarro Date: Mon, 31 Jul 2023 13:56:17 -0700 Subject: [PATCH 04/17] Format --- rustworkx-core/src/generators/random_graph.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rustworkx-core/src/generators/random_graph.rs b/rustworkx-core/src/generators/random_graph.rs index 60de1d7a2..0b3fc0887 100644 --- a/rustworkx-core/src/generators/random_graph.rs +++ b/rustworkx-core/src/generators/random_graph.rs @@ -160,7 +160,7 @@ where let w_index = graph.from_index(w as usize); graph.add_edge(v_index, w_index, default_edge_weight()); } - } + } } } Ok(graph) From dfaa1bb437a1122902327214db7b572124487942 Mon Sep 17 00:00:00 2001 From: Edwin Navarro Date: Mon, 31 Jul 2023 14:29:40 -0700 Subject: [PATCH 05/17] Fix test --- rustworkx-core/src/generators/random_graph.rs | 2 +- tests/retworkx_backwards_compat/test_random.py | 2 +- tests/rustworkx_tests/test_random.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rustworkx-core/src/generators/random_graph.rs b/rustworkx-core/src/generators/random_graph.rs index 0b3fc0887..61d847226 100644 --- a/rustworkx-core/src/generators/random_graph.rs +++ b/rustworkx-core/src/generators/random_graph.rs @@ -423,7 +423,7 @@ mod tests { let g: petgraph::graph::DiGraph<(), ()> = gnp_random_graph(20, 0.5, Some(10), || (), || ()).unwrap(); assert_eq!(g.node_count(), 20); - assert_eq!(g.edge_count(), 104); + assert_eq!(g.edge_count(), 189); } #[test] diff --git a/tests/retworkx_backwards_compat/test_random.py b/tests/retworkx_backwards_compat/test_random.py index 67a60760c..173886c5d 100644 --- a/tests/retworkx_backwards_compat/test_random.py +++ b/tests/retworkx_backwards_compat/test_random.py @@ -20,7 +20,7 @@ class TestGNPRandomGraph(unittest.TestCase): def test_random_gnp_directed(self): graph = retworkx.directed_gnp_random_graph(20, 0.5, seed=10) self.assertEqual(len(graph), 20) - self.assertEqual(len(graph.edges()), 104) + self.assertEqual(len(graph.edges()), 189) def test_random_gnp_directed_empty_graph(self): graph = retworkx.directed_gnp_random_graph(20, 0) diff --git a/tests/rustworkx_tests/test_random.py b/tests/rustworkx_tests/test_random.py index 1a96b27ef..6b75c9cd1 100644 --- a/tests/rustworkx_tests/test_random.py +++ b/tests/rustworkx_tests/test_random.py @@ -20,7 +20,7 @@ class TestGNPRandomGraph(unittest.TestCase): def test_random_gnp_directed(self): graph = rustworkx.directed_gnp_random_graph(20, 0.5, seed=10) self.assertEqual(len(graph), 20) - self.assertEqual(len(graph.edges()), 104) + self.assertEqual(len(graph.edges()), 189) def test_random_gnp_directed_empty_graph(self): graph = rustworkx.directed_gnp_random_graph(20, 0) From 50e1e68f2701adf9d672e35b426f3e51c2bf76a3 Mon Sep 17 00:00:00 2001 From: Edwin Navarro Date: Tue, 1 Aug 2023 07:01:05 -0700 Subject: [PATCH 06/17] Fix dot tests --- tests/rustworkx_tests/digraph/test_dot.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/rustworkx_tests/digraph/test_dot.py b/tests/rustworkx_tests/digraph/test_dot.py index 64957ef04..9824e2b6f 100644 --- a/tests/rustworkx_tests/digraph/test_dot.py +++ b/tests/rustworkx_tests/digraph/test_dot.py @@ -57,17 +57,23 @@ def test_digraph_to_dot_to_file(self): def test_digraph_empty_dicts(self): graph = rustworkx.directed_gnp_random_graph(3, 0.9, seed=42) dot_str = graph.to_dot(lambda _: {}, lambda _: {}) - self.assertEqual("digraph {\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n0 -> 2 ;\n}\n", dot_str) + self.assertEqual( + "digraph {\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n0 -> 2 ;\n1 -> 2 ;\n2 -> 0 ;\n2 -> 1 ;\n}\n", + dot_str + ) def test_digraph_graph_attrs(self): graph = rustworkx.directed_gnp_random_graph(3, 0.9, seed=42) dot_str = graph.to_dot(lambda _: {}, lambda _: {}, {"bgcolor": "red"}) self.assertEqual( - "digraph {\nbgcolor=red ;\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n" "0 -> 2 ;\n}\n", + "digraph {\nbgcolor=red ;\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n0 -> 2 ;\n1 -> 2 ;\n2 -> 0 ;\n2 -> 1 ;\n}\n", dot_str, ) def test_digraph_no_args(self): graph = rustworkx.directed_gnp_random_graph(3, 0.95, seed=24) dot_str = graph.to_dot() - self.assertEqual("digraph {\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n0 -> 2 ;\n}\n", dot_str) + self.assertEqual( + "digraph {\n0 ;\n1 ;\n2 ;\n0 -> 2 ;\n1 -> 2 ;\n1 -> 0 ;\n2 -> 0 ;\n2 -> 1 ;\n}\n", + dot_str + ) From 361b12884ac34fb9491cfe4eba370c07ec05bb5e Mon Sep 17 00:00:00 2001 From: Edwin Navarro Date: Tue, 1 Aug 2023 07:24:51 -0700 Subject: [PATCH 07/17] Expand directed gnp test --- tests/rustworkx_tests/test_random.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/rustworkx_tests/test_random.py b/tests/rustworkx_tests/test_random.py index 6b75c9cd1..9498d98ef 100644 --- a/tests/rustworkx_tests/test_random.py +++ b/tests/rustworkx_tests/test_random.py @@ -12,15 +12,17 @@ import unittest import random +from ddt import ddt, data import rustworkx - +@ddt class TestGNPRandomGraph(unittest.TestCase): - def test_random_gnp_directed(self): - graph = rustworkx.directed_gnp_random_graph(20, 0.5, seed=10) - self.assertEqual(len(graph), 20) - self.assertEqual(len(graph.edges()), 189) + @data((15, 20, 0.7, 156), (20, 10, 0.5, 189), (22, 6, 0.2, 91)) + def test_random_gnp_directed(self, args): + graph = rustworkx.directed_gnp_random_graph(args[0], args[2], seed=args[1]) + self.assertEqual(len(graph), args[0]) + self.assertEqual(len(graph.edges()), args[3]) def test_random_gnp_directed_empty_graph(self): graph = rustworkx.directed_gnp_random_graph(20, 0) From 2a2c131c979c98931b67c351448bf838219115b0 Mon Sep 17 00:00:00 2001 From: Edwin Navarro Date: Tue, 1 Aug 2023 08:41:58 -0700 Subject: [PATCH 08/17] Lint --- tests/rustworkx_tests/digraph/test_dot.py | 4 ++-- tests/rustworkx_tests/test_random.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/rustworkx_tests/digraph/test_dot.py b/tests/rustworkx_tests/digraph/test_dot.py index 9824e2b6f..23e6f7373 100644 --- a/tests/rustworkx_tests/digraph/test_dot.py +++ b/tests/rustworkx_tests/digraph/test_dot.py @@ -59,7 +59,7 @@ def test_digraph_empty_dicts(self): dot_str = graph.to_dot(lambda _: {}, lambda _: {}) self.assertEqual( "digraph {\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n0 -> 2 ;\n1 -> 2 ;\n2 -> 0 ;\n2 -> 1 ;\n}\n", - dot_str + dot_str, ) def test_digraph_graph_attrs(self): @@ -75,5 +75,5 @@ def test_digraph_no_args(self): dot_str = graph.to_dot() self.assertEqual( "digraph {\n0 ;\n1 ;\n2 ;\n0 -> 2 ;\n1 -> 2 ;\n1 -> 0 ;\n2 -> 0 ;\n2 -> 1 ;\n}\n", - dot_str + dot_str, ) diff --git a/tests/rustworkx_tests/test_random.py b/tests/rustworkx_tests/test_random.py index 9498d98ef..410a2de76 100644 --- a/tests/rustworkx_tests/test_random.py +++ b/tests/rustworkx_tests/test_random.py @@ -16,6 +16,7 @@ import rustworkx + @ddt class TestGNPRandomGraph(unittest.TestCase): @data((15, 20, 0.7, 156), (20, 10, 0.5, 189), (22, 6, 0.2, 91)) From 2338ec9fa9b27f0948ca072c8fd240275df95655 Mon Sep 17 00:00:00 2001 From: Edwin Navarro Date: Tue, 1 Aug 2023 11:00:25 -0700 Subject: [PATCH 09/17] Remove ddt --- tests/rustworkx_tests/test_random.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/tests/rustworkx_tests/test_random.py b/tests/rustworkx_tests/test_random.py index 410a2de76..0a7896037 100644 --- a/tests/rustworkx_tests/test_random.py +++ b/tests/rustworkx_tests/test_random.py @@ -12,18 +12,25 @@ import unittest import random -from ddt import ddt, data import rustworkx -@ddt class TestGNPRandomGraph(unittest.TestCase): - @data((15, 20, 0.7, 156), (20, 10, 0.5, 189), (22, 6, 0.2, 91)) - def test_random_gnp_directed(self, args): - graph = rustworkx.directed_gnp_random_graph(args[0], args[2], seed=args[1]) - self.assertEqual(len(graph), args[0]) - self.assertEqual(len(graph.edges()), args[3]) + def test_random_gnp_directed_1(self): + graph = rustworkx.directed_gnp_random_graph(15, 0.7, seed=20) + self.assertEqual(len(graph), 15) + self.assertEqual(len(graph.edges()), 156) + + def test_random_gnp_directed_2(self): + graph = rustworkx.directed_gnp_random_graph(20, 0.5, seed=10) + self.assertEqual(len(graph), 20) + self.assertEqual(len(graph.edges()), 189) + + def test_random_gnp_directed_3(self): + graph = rustworkx.directed_gnp_random_graph(22, 0.2, seed=6) + self.assertEqual(len(graph), 22) + self.assertEqual(len(graph.edges()), 91) def test_random_gnp_directed_empty_graph(self): graph = rustworkx.directed_gnp_random_graph(20, 0) From 6a00d6ab95af06ddd6c46c2b5ab480e183e69f3f Mon Sep 17 00:00:00 2001 From: Edwin Navarro Date: Tue, 1 Aug 2023 13:36:29 -0700 Subject: [PATCH 10/17] Fix ret-back-compat --- .../digraph/test_dot.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/tests/retworkx_backwards_compat/digraph/test_dot.py b/tests/retworkx_backwards_compat/digraph/test_dot.py index fb2211c43..2ad3f16af 100644 --- a/tests/retworkx_backwards_compat/digraph/test_dot.py +++ b/tests/retworkx_backwards_compat/digraph/test_dot.py @@ -55,19 +55,25 @@ def test_digraph_to_dot_to_file(self): self.assertEqual(expected, res) def test_digraph_empty_dicts(self): - graph = retworkx.directed_gnp_random_graph(3, 0.9, seed=42) + graph = rustworkx.directed_gnp_random_graph(3, 0.9, seed=42) dot_str = graph.to_dot(lambda _: {}, lambda _: {}) - self.assertEqual("digraph {\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n0 -> 2 ;\n}\n", dot_str) + self.assertEqual( + "digraph {\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n0 -> 2 ;\n1 -> 2 ;\n2 -> 0 ;\n2 -> 1 ;\n}\n", + dot_str, + ) def test_digraph_graph_attrs(self): - graph = retworkx.directed_gnp_random_graph(3, 0.9, seed=42) + graph = rustworkx.directed_gnp_random_graph(3, 0.9, seed=42) dot_str = graph.to_dot(lambda _: {}, lambda _: {}, {"bgcolor": "red"}) self.assertEqual( - "digraph {\nbgcolor=red ;\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n" "0 -> 2 ;\n}\n", + "digraph {\nbgcolor=red ;\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n0 -> 2 ;\n1 -> 2 ;\n2 -> 0 ;\n2 -> 1 ;\n}\n", dot_str, ) def test_digraph_no_args(self): - graph = retworkx.directed_gnp_random_graph(3, 0.95, seed=24) + graph = rustworkx.directed_gnp_random_graph(3, 0.95, seed=24) dot_str = graph.to_dot() - self.assertEqual("digraph {\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n0 -> 2 ;\n}\n", dot_str) + self.assertEqual( + "digraph {\n0 ;\n1 ;\n2 ;\n0 -> 2 ;\n1 -> 2 ;\n1 -> 0 ;\n2 -> 0 ;\n2 -> 1 ;\n}\n", + dot_str, + ) From 01a87d82e7285c0001022b67dbe60b1f236067c5 Mon Sep 17 00:00:00 2001 From: Edwin Navarro Date: Tue, 1 Aug 2023 13:45:48 -0700 Subject: [PATCH 11/17] Release note --- .../notes/fix-gnp-random-directed-41f2a436c49b9064.yaml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 releasenotes/notes/fix-gnp-random-directed-41f2a436c49b9064.yaml diff --git a/releasenotes/notes/fix-gnp-random-directed-41f2a436c49b9064.yaml b/releasenotes/notes/fix-gnp-random-directed-41f2a436c49b9064.yaml new file mode 100644 index 000000000..359692e4b --- /dev/null +++ b/releasenotes/notes/fix-gnp-random-directed-41f2a436c49b9064.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - | + Fixed an issue where the :func:`~rustworkx.generators.directed_gnp_random_graph` + and the :func:`~rustworkx-core.generators.gnp_random_graph` for directed graphs + produced a graph where lower node numbers had only a small number of edges + compared to what was expected and the output was therefore not random. From ac5541a8832c090af8e83bd4207cef745cbca4a1 Mon Sep 17 00:00:00 2001 From: Edwin Navarro Date: Tue, 1 Aug 2023 14:00:21 -0700 Subject: [PATCH 12/17] Ret tests --- .../digraph/test_dot.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/tests/retworkx_backwards_compat/digraph/test_dot.py b/tests/retworkx_backwards_compat/digraph/test_dot.py index 2ad3f16af..b71368d1f 100644 --- a/tests/retworkx_backwards_compat/digraph/test_dot.py +++ b/tests/retworkx_backwards_compat/digraph/test_dot.py @@ -55,25 +55,19 @@ def test_digraph_to_dot_to_file(self): self.assertEqual(expected, res) def test_digraph_empty_dicts(self): - graph = rustworkx.directed_gnp_random_graph(3, 0.9, seed=42) + graph = retworkx.directed_gnp_random_graph(3, 0.9, seed=42) dot_str = graph.to_dot(lambda _: {}, lambda _: {}) - self.assertEqual( - "digraph {\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n0 -> 2 ;\n1 -> 2 ;\n2 -> 0 ;\n2 -> 1 ;\n}\n", - dot_str, - ) + self.assertEqual("digraph {\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n0 -> 2 ;\n}\n", dot_str) def test_digraph_graph_attrs(self): - graph = rustworkx.directed_gnp_random_graph(3, 0.9, seed=42) + graph = retworkx.directed_gnp_random_graph(3, 0.9, seed=42) dot_str = graph.to_dot(lambda _: {}, lambda _: {}, {"bgcolor": "red"}) self.assertEqual( - "digraph {\nbgcolor=red ;\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n0 -> 2 ;\n1 -> 2 ;\n2 -> 0 ;\n2 -> 1 ;\n}\n", + "digraph {\nbgcolor=red ;\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n" "0 -> 2 ;\n}\n", dot_str, ) def test_digraph_no_args(self): - graph = rustworkx.directed_gnp_random_graph(3, 0.95, seed=24) + graph = retworkx.directed_gnp_random_graph(3, 0.95, seed=24) dot_str = graph.to_dot() - self.assertEqual( - "digraph {\n0 ;\n1 ;\n2 ;\n0 -> 2 ;\n1 -> 2 ;\n1 -> 0 ;\n2 -> 0 ;\n2 -> 1 ;\n}\n", - dot_str, - ) + self.assertEqual("digraph {\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n0 -> 2 ;\n}\n", dot_str) \ No newline at end of file From acaa7b737fdccf23c4294fba3a39a6989e611f65 Mon Sep 17 00:00:00 2001 From: Edwin Navarro Date: Tue, 1 Aug 2023 14:04:12 -0700 Subject: [PATCH 13/17] Lint --- tests/retworkx_backwards_compat/digraph/test_dot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/retworkx_backwards_compat/digraph/test_dot.py b/tests/retworkx_backwards_compat/digraph/test_dot.py index b71368d1f..fb2211c43 100644 --- a/tests/retworkx_backwards_compat/digraph/test_dot.py +++ b/tests/retworkx_backwards_compat/digraph/test_dot.py @@ -70,4 +70,4 @@ def test_digraph_graph_attrs(self): def test_digraph_no_args(self): graph = retworkx.directed_gnp_random_graph(3, 0.95, seed=24) dot_str = graph.to_dot() - self.assertEqual("digraph {\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n0 -> 2 ;\n}\n", dot_str) \ No newline at end of file + self.assertEqual("digraph {\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n0 -> 2 ;\n}\n", dot_str) From 12e917fa24ed0c4d6b4b4b295c3c1b79f1757054 Mon Sep 17 00:00:00 2001 From: Edwin Navarro Date: Tue, 1 Aug 2023 14:20:23 -0700 Subject: [PATCH 14/17] Ret again --- tests/retworkx_backwards_compat/digraph/test_dot.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/retworkx_backwards_compat/digraph/test_dot.py b/tests/retworkx_backwards_compat/digraph/test_dot.py index fb2211c43..d33417453 100644 --- a/tests/retworkx_backwards_compat/digraph/test_dot.py +++ b/tests/retworkx_backwards_compat/digraph/test_dot.py @@ -57,17 +57,23 @@ def test_digraph_to_dot_to_file(self): def test_digraph_empty_dicts(self): graph = retworkx.directed_gnp_random_graph(3, 0.9, seed=42) dot_str = graph.to_dot(lambda _: {}, lambda _: {}) - self.assertEqual("digraph {\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n0 -> 2 ;\n}\n", dot_str) + self.assertEqual( + "digraph {\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n0 -> 2 ;\n1 -> 2 ;\n2 -> 0 ;\n2 -> 1 ;\n}\n", + dot_str, + ) def test_digraph_graph_attrs(self): graph = retworkx.directed_gnp_random_graph(3, 0.9, seed=42) dot_str = graph.to_dot(lambda _: {}, lambda _: {}, {"bgcolor": "red"}) self.assertEqual( - "digraph {\nbgcolor=red ;\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n" "0 -> 2 ;\n}\n", + "digraph {\nbgcolor=red ;\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n0 -> 2 ;\n1 -> 2 ;\n2 -> 0 ;\n2 -> 1 ;\n}\n", dot_str, ) def test_digraph_no_args(self): graph = retworkx.directed_gnp_random_graph(3, 0.95, seed=24) dot_str = graph.to_dot() - self.assertEqual("digraph {\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n0 -> 2 ;\n}\n", dot_str) + self.assertEqual( + "digraph {\n0 ;\n1 ;\n2 ;\n0 -> 2 ;\n1 -> 2 ;\n1 -> 0 ;\n2 -> 0 ;\n2 -> 1 ;\n}\n", + dot_str, + ) From 500a1e0605af67ed2dcfda6c378123d20782f1ee Mon Sep 17 00:00:00 2001 From: Edwin Navarro Date: Wed, 18 Oct 2023 15:37:27 -0700 Subject: [PATCH 15/17] Line too long --- tests/rustworkx_tests/digraph/test_dot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/rustworkx_tests/digraph/test_dot.py b/tests/rustworkx_tests/digraph/test_dot.py index 7e5d9a67c..3d893a892 100644 --- a/tests/rustworkx_tests/digraph/test_dot.py +++ b/tests/rustworkx_tests/digraph/test_dot.py @@ -66,7 +66,8 @@ def test_digraph_graph_attrs(self): graph = rustworkx.directed_gnp_random_graph(3, 0.9, seed=42) dot_str = graph.to_dot(lambda _: {}, lambda _: {}, {"bgcolor": "red"}) self.assertEqual( - "digraph {\nbgcolor=red ;\n0 ;\n1 ;\n2 ;\n0 -> 1 ;\n0 -> 2 ;\n1 -> 2 ;\n2 -> 0 ;\n2 -> 1 ;\n}\n", + "digraph {\nbgcolor=red ;\n0 ;\n1 ;\n2 ;\n0 -> 1 \ +;\n0 -> 2 ;\n1 -> 2 ;\n2 -> 0 ;\n2 -> 1 ;\n}\n", dot_str, ) From fd7544a9e10ecded245780b2771910b8c12794da Mon Sep 17 00:00:00 2001 From: Edwin Navarro Date: Fri, 24 Nov 2023 13:53:35 -0800 Subject: [PATCH 16/17] Add try_into for usize to isize --- .../notes/fix-gnp-random-directed-41f2a436c49b9064.yaml | 2 +- rustworkx-core/src/generators/random_graph.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/releasenotes/notes/fix-gnp-random-directed-41f2a436c49b9064.yaml b/releasenotes/notes/fix-gnp-random-directed-41f2a436c49b9064.yaml index 359692e4b..12b03fe1c 100644 --- a/releasenotes/notes/fix-gnp-random-directed-41f2a436c49b9064.yaml +++ b/releasenotes/notes/fix-gnp-random-directed-41f2a436c49b9064.yaml @@ -4,4 +4,4 @@ fixes: Fixed an issue where the :func:`~rustworkx.generators.directed_gnp_random_graph` and the :func:`~rustworkx-core.generators.gnp_random_graph` for directed graphs produced a graph where lower node numbers had only a small number of edges - compared to what was expected and the output was therefore not random. + compared to what was expected. diff --git a/rustworkx-core/src/generators/random_graph.rs b/rustworkx-core/src/generators/random_graph.rs index 61d847226..6b6ab4ed1 100644 --- a/rustworkx-core/src/generators/random_graph.rs +++ b/rustworkx-core/src/generators/random_graph.rs @@ -114,7 +114,7 @@ where } } } else { - let num_nodes = num_nodes as isize; + let num_nodes: isize = num_nodes.try_into().map_err(|_| InvalidInputError).unwrap(); let lp: f64 = (1.0 - probability).ln(); let between = Uniform::new(0.0, 1.0); From b41cf8a127fb71a91137e2dcfafcfeaaafc9e59d Mon Sep 17 00:00:00 2001 From: Edwin Navarro Date: Sat, 25 Nov 2023 07:06:04 -0800 Subject: [PATCH 17/17] Use match for isize Err --- rustworkx-core/src/generators/random_graph.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rustworkx-core/src/generators/random_graph.rs b/rustworkx-core/src/generators/random_graph.rs index 3889ec6a0..b42e10cf7 100644 --- a/rustworkx-core/src/generators/random_graph.rs +++ b/rustworkx-core/src/generators/random_graph.rs @@ -120,7 +120,10 @@ where } } } else { - let num_nodes: isize = num_nodes.try_into().map_err(|_| InvalidInputError).unwrap(); + let num_nodes: isize = match num_nodes.try_into() { + Ok(nodes) => nodes, + Err(_) => return Err(InvalidInputError {}), + }; let lp: f64 = (1.0 - probability).ln(); let between = Uniform::new(0.0, 1.0);