Skip to content

Commit

Permalink
fix: dijkstra index out of boud
Browse files Browse the repository at this point in the history
  • Loading branch information
JPena-code committed Mar 7, 2024
1 parent 1c56c0e commit 9737c87
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
11 changes: 11 additions & 0 deletions releasenotes/notes/fix-dijkstra-panic-3962ad36788cab00.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
fixes:
- |
Fixed an issue with the Dijkstra path functions:
* :func:`rustworkx.dijkstra_shortest_paths`
* :func:`rustworkx.dijkstra_shortest_path_lengths`
where a `Pyo3.PanicException`were raise with no much detail at the moment
of pass in the `source` argument the index of an out of bound node.
Fixed `#1117 <https://github.com/Qiskit/rustworkx/issues/1117>`__
43 changes: 43 additions & 0 deletions src/shortest_path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ pub fn graph_dijkstra_shortest_paths(
weight_fn: Option<PyObject>,
default_weight: f64,
) -> PyResult<PathMapping> {
if !graph
.node_indices()
.nodes
.iter()
.any(|&node| node == source)
{
return Err(PyIndexError::new_err(format!(
"Node source index \"{source}\" out of graph bound"
)));
}

let start = NodeIndex::new(source);
let goal_index: Option<NodeIndex> = target.map(NodeIndex::new);
let mut paths: DictMap<NodeIndex, Vec<NodeIndex>> = DictMap::with_capacity(graph.node_count());
Expand Down Expand Up @@ -216,6 +227,16 @@ pub fn digraph_dijkstra_shortest_paths(
default_weight: f64,
as_undirected: bool,
) -> PyResult<PathMapping> {
if !graph
.node_indices()
.nodes
.iter()
.any(|&node| node == source)
{
return Err(PyIndexError::new_err(format!(
"Node source index \"{source}\" out of graph bound"
)));
}
let start = NodeIndex::new(source);
let goal_index: Option<NodeIndex> = target.map(NodeIndex::new);
let mut paths: DictMap<NodeIndex, Vec<NodeIndex>> = DictMap::with_capacity(graph.node_count());
Expand Down Expand Up @@ -371,6 +392,17 @@ pub fn graph_dijkstra_shortest_path_lengths(
edge_cost_fn: PyObject,
goal: Option<usize>,
) -> PyResult<PathLengthMapping> {
if !graph
.node_indices()
.nodes
.iter()
.any(|&index| index == node)
{
return Err(PyIndexError::new_err(format!(
"Node source index \"{node}\" out of graph bound"
)));
}

let edge_cost_callable = CostFn::from(edge_cost_fn);
let start = NodeIndex::new(node);
let goal_index: Option<NodeIndex> = goal.map(NodeIndex::new);
Expand Down Expand Up @@ -440,6 +472,17 @@ pub fn digraph_dijkstra_shortest_path_lengths(
edge_cost_fn: PyObject,
goal: Option<usize>,
) -> PyResult<PathLengthMapping> {
if !graph
.node_indices()
.nodes
.iter()
.any(|&index| index == node)
{
return Err(PyIndexError::new_err(format!(
"Node source index \"{node}\" out of graph bound"
)));
}

let edge_cost_callable = CostFn::from(edge_cost_fn);

let start = NodeIndex::new(node);
Expand Down

0 comments on commit 9737c87

Please sign in to comment.