Skip to content

Commit

Permalink
fix: shortest path
Browse files Browse the repository at this point in the history
  • Loading branch information
JPena-code committed Mar 8, 2024
1 parent 0025bf0 commit 65749ba
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
14 changes: 14 additions & 0 deletions releasenotes/notes/fix-panic-3962ad36788cab00.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
fixes:
- |
Fixed an issue with the Dijkstra path functions:
* :func:`rustworkx.dijkstra_shortest_paths`
* :func:`rustworkx.dijkstra_shortest_path_lengths`
* :func:`rustworkx.bellman_ford_shortest_path_lengths`
* :func:`rustworkx.bellman_ford_shortest_paths`
* :func:`rustworkx.astar_shortest_path`
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>`__
64 changes: 63 additions & 1 deletion src/shortest_path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ pub fn graph_dijkstra_shortest_paths(
default_weight: f64,
) -> PyResult<PathMapping> {
let start = NodeIndex::new(source);
if !graph.graph.contains_node(start) {
return Err(PyIndexError::new_err(format!(
"Node source index \"{source}\" out of graph bound"
)));
}

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 @@ -217,6 +223,12 @@ pub fn digraph_dijkstra_shortest_paths(
as_undirected: bool,
) -> PyResult<PathMapping> {
let start = NodeIndex::new(source);
if !graph.graph.contains_node(start) {
return Err(PyIndexError::new_err(format!(
"Node source index \"{source}\" out of graph bound"
)));
}

let goal_index: Option<NodeIndex> = target.map(NodeIndex::new);
let mut paths: DictMap<NodeIndex, Vec<NodeIndex>> = DictMap::with_capacity(graph.node_count());
let cost_fn = CostFn::try_from((weight_fn, default_weight))?;
Expand Down Expand Up @@ -371,10 +383,16 @@ pub fn graph_dijkstra_shortest_path_lengths(
edge_cost_fn: PyObject,
goal: Option<usize>,
) -> PyResult<PathLengthMapping> {
let edge_cost_callable = CostFn::from(edge_cost_fn);
let start = NodeIndex::new(node);
let edge_cost_callable = CostFn::from(edge_cost_fn);
let goal_index: Option<NodeIndex> = goal.map(NodeIndex::new);

if !graph.graph.contains_node(start) {
return Err(PyIndexError::new_err(format!(
"Node source index \"{node}\" out of graph bound"
)));
}

let res: Vec<Option<f64>> = dijkstra(
&graph.graph,
start,
Expand Down Expand Up @@ -445,6 +463,12 @@ pub fn digraph_dijkstra_shortest_path_lengths(
let start = NodeIndex::new(node);
let goal_index: Option<NodeIndex> = goal.map(NodeIndex::new);

if !graph.graph.contains_node(start) {
return Err(PyIndexError::new_err(format!(
"Node source index \"{node}\" out of graph bound"
)));
}

let res: Vec<Option<f64>> = dijkstra(
&graph.graph,
start,
Expand Down Expand Up @@ -671,6 +695,12 @@ pub fn digraph_astar_shortest_path(
let estimate_cost_callable = CostFn::from(estimate_cost_fn);
let start = NodeIndex::new(node);

if !graph.graph.contains_node(start) {
return Err(PyIndexError::new_err(format!(
"Node source index \"{node}\" out of graph bound"
)));
}

let astar_res = astar(
&graph.graph,
start,
Expand Down Expand Up @@ -729,6 +759,12 @@ pub fn graph_astar_shortest_path(
let estimate_cost_callable = CostFn::from(estimate_cost_fn);
let start = NodeIndex::new(node);

if !graph.graph.contains_node(start) {
return Err(PyIndexError::new_err(format!(
"Node source index \"{node}\" out of graph bound"
)));
}

let astar_res = astar(
&graph.graph,
start,
Expand Down Expand Up @@ -1561,6 +1597,12 @@ pub fn digraph_bellman_ford_shortest_path_lengths(

let start = NodeIndex::new(node);

if !graph.graph.contains_node(start) {
return Err(PyIndexError::new_err(format!(
"Node source index \"{node}\" out of graph bound"
)));
}

let res: Option<Vec<Option<f64>>> =
bellman_ford(&graph.graph, start, |e| edge_cost(e.id()), None)?;

Expand Down Expand Up @@ -1640,6 +1682,12 @@ pub fn graph_bellman_ford_shortest_path_lengths(

let start = NodeIndex::new(node);

if !graph.graph.contains_node(start) {
return Err(PyIndexError::new_err(format!(
"Node source index \"{node}\" out of graph bound"
)));
}

let res: Option<Vec<Option<f64>>> =
bellman_ford(&graph.graph, start, |e| edge_cost(e.id()), None)?;

Expand Down Expand Up @@ -1715,6 +1763,13 @@ pub fn graph_bellman_ford_shortest_paths(
default_weight: f64,
) -> PyResult<PathMapping> {
let start = NodeIndex::new(source);

if !graph.graph.contains_node(start) {
return Err(PyIndexError::new_err(format!(
"Node source index \"{source}\" out of graph bound"
)));
}

let mut paths: DictMap<NodeIndex, Vec<NodeIndex>> = DictMap::with_capacity(graph.node_count());

let edge_weights: Vec<Option<f64>> =
Expand Down Expand Up @@ -1801,6 +1856,13 @@ pub fn digraph_bellman_ford_shortest_paths(
}

let start = NodeIndex::new(source);

if !graph.graph.contains_node(start) {
return Err(PyIndexError::new_err(format!(
"Node source index \"{source}\" out of graph bound"
)));
}

let mut paths: DictMap<NodeIndex, Vec<NodeIndex>> = DictMap::with_capacity(graph.node_count());

let edge_weights: Vec<Option<f64>> =
Expand Down

0 comments on commit 65749ba

Please sign in to comment.