Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit be39f48

Browse files
committed
Added reminder to update bellman-ford to return Err() when the graph contains negative circles
1 parent 08f15ac commit be39f48

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/algorithms.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,20 @@ pub fn bellman_ford<T: Display + Eq + Clone + Hash>(graph: &mut Graph<T>, source
176176
}
177177

178178
for edge in &node_ref.edges {
179-
let target_node = edge.target.clone();
179+
let target_node = Rc::clone(&edge.target);
180+
let mut target_node_ref = target_node.borrow_mut();
180181
let new_distance = node_ref.distance + edge.weight;
181182

182-
if new_distance < target_node.borrow().distance {
183-
target_node.borrow_mut().distance = new_distance;
183+
if new_distance < target_node_ref.distance {
184+
target_node_ref.distance = new_distance;
184185
let mut shortest_path = node_ref.shortest_path.clone();
185-
shortest_path.push(node.clone());
186-
target_node.borrow_mut().shortest_path = shortest_path;
186+
shortest_path.push(Rc::clone(&node));
187+
target_node_ref.shortest_path = shortest_path;
187188
}
188189
}
189190
}
190191
}
192+
//TODO Add in check that detects negative circles and return Err() when found, update docs accordingly and add in a test that checks that
191193

192194
Ok(ShortestPathTree::from_graph(&graph, &source_node_id))
193195
}

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
//! ```
4444
4545
use std::{fmt::{Display, Debug}, rc::Rc, cell::RefCell, collections::HashMap, hash::Hash};
46-
46+
// TODO update graph print to properly print negative edge weights
47+
// TODO add available from_instruction feature to main doc page and explain briefly what it does
4748
/// Contains implementations for the graph to work.
4849
mod graph;
4950
/// Contains all algorithms that are implemented in this crate.

0 commit comments

Comments
 (0)