Skip to content

Commit

Permalink
fix all_simple_paths (#1015)
Browse files Browse the repository at this point in the history
* fix `min_depth=0` in `all_simple_paths`
Fixes #955

* added reno
  • Loading branch information
prakharb10 authored Oct 30, 2023
1 parent c83f1bd commit c53f37a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
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

0 comments on commit c53f37a

Please sign in to comment.