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

fix all_simple_paths #1015

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions releasenotes/notes/fix-all-simple-paths-03f4534438d1068d.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
Fixed the behavior of :func:`~rustworkx.graph_all_simple_paths` and
:func:`~rustworkx.digraph_all_simple_paths` when ``min_depth`` is set to
``0``. Refer to `#955 <https://github.com/Qiskit/rustworkx/issues/955>`__ for
more information.
6 changes: 3 additions & 3 deletions src/connectivity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,8 @@ pub fn graph_all_simple_paths(
));
}
let min_intermediate_nodes: usize = match min_depth {
Some(0) | None => 0,
Some(depth) => depth - 2,
None => 0,
};
let cutoff_petgraph: Option<usize> = cutoff.map(|depth| depth - 2);
let result: Vec<Vec<usize>> = algo::all_simple_paths(
Expand All @@ -573,7 +573,7 @@ pub fn graph_all_simple_paths(
/// :param int to: The node index to find the paths to
/// :param int min_depth: The minimum depth of the path to include in the output
/// list of paths. By default all paths are included regardless of depth,
/// sett to 0 will behave like the default.
/// setting to 0 will behave like the default.
/// :param int cutoff: The maximum depth of path to include in the output list
/// of paths. By default includes all paths regardless of depth, setting to
/// 0 will behave like default.
Expand Down Expand Up @@ -602,8 +602,8 @@ pub fn digraph_all_simple_paths(
));
}
let min_intermediate_nodes: usize = match min_depth {
Some(0) | None => 0,
Some(depth) => depth - 2,
None => 0,
};
let cutoff_petgraph: Option<usize> = cutoff.map(|depth| depth - 2);
let result: Vec<Vec<usize>> = algo::all_simple_paths(
Expand Down
19 changes: 19 additions & 0 deletions tests/rustworkx_tests/digraph/test_all_simple_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ def test_all_simple_paths(self):
for i in expected:
self.assertIn(i, paths)

def test_all_simple_paths_default_min_depth(self):
dag = rustworkx.PyDAG()
for i in range(6):
dag.add_node(i)
dag.add_edges_from_no_data(self.edges)
paths = rustworkx.digraph_all_simple_paths(dag, 0, 5, min_depth=0)
expected = [
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 4, 5],
[0, 1, 3, 2, 4, 5],
[0, 1, 3, 4, 5],
[0, 2, 3, 4, 5],
[0, 2, 4, 5],
[0, 3, 2, 4, 5],
[0, 3, 4, 5],
]
for i in expected:
self.assertIn(i, paths)

def test_all_simple_paths_min_depth(self):
dag = rustworkx.PyDAG()
for i in range(6):
Expand Down
55 changes: 55 additions & 0 deletions tests/rustworkx_tests/graph/test_all_simple_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,61 @@ def test_all_simple_paths(self):
for i in expected:
self.assertIn(i, paths)

def test_all_simple_paths_default_min_depth(self):
graph = rustworkx.PyGraph()
for i in range(6):
graph.add_node(i)
graph.add_edges_from_no_data(self.edges)
paths = rustworkx.graph_all_simple_paths(graph, 0, 5, min_depth=0)
expected = [
[0, 3, 4, 5],
[0, 3, 4, 2, 5],
[0, 3, 4, 2, 5],
[0, 3, 2, 4, 5],
[0, 3, 2, 5],
[0, 3, 2, 4, 5],
[0, 3, 5],
[0, 3, 2, 4, 5],
[0, 3, 2, 5],
[0, 3, 2, 4, 5],
[0, 3, 1, 2, 4, 5],
[0, 3, 1, 2, 5],
[0, 3, 1, 2, 4, 5],
[0, 2, 4, 5],
[0, 2, 4, 3, 5],
[0, 2, 3, 4, 5],
[0, 2, 3, 5],
[0, 2, 5],
[0, 2, 4, 5],
[0, 2, 4, 3, 5],
[0, 2, 3, 4, 5],
[0, 2, 3, 5],
[0, 2, 1, 3, 4, 5],
[0, 2, 1, 3, 5],
[0, 1, 3, 4, 5],
[0, 1, 3, 4, 2, 5],
[0, 1, 3, 4, 2, 5],
[0, 1, 3, 2, 4, 5],
[0, 1, 3, 2, 5],
[0, 1, 3, 2, 4, 5],
[0, 1, 3, 5],
[0, 1, 3, 2, 4, 5],
[0, 1, 3, 2, 5],
[0, 1, 3, 2, 4, 5],
[0, 1, 2, 4, 5],
[0, 1, 2, 4, 3, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 5],
[0, 1, 2, 5],
[0, 1, 2, 4, 5],
[0, 1, 2, 4, 3, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 5],
]
self.assertEqual(len(expected), len(paths))
for i in expected:
self.assertIn(i, paths)

def test_all_simple_paths_with_min_depth(self):
graph = rustworkx.PyGraph()
for i in range(6):
Expand Down