diff --git a/src/main.rs b/src/main.rs index f5dd09c..9aedb3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -184,8 +184,10 @@ fn main() -> Result<(), Box> { let origin = graph[&origin_id].clone(); let destination = graph[&destination_id].clone(); + // Set starting node distance to 0 origin.deref().borrow_mut().source_distance = NonNan(0.0); + // Populate unvisted set #[allow(clippy::mutable_key_type)] let mut unvisited: BTreeSet = BTreeSet::new(); @@ -193,34 +195,46 @@ fn main() -> Result<(), Box> { unvisited.insert(node.clone()); } + // Begin Dijkstraing while let Some(current_node) = unvisited.pop_first() { + // If at destination, we're done if current_node.borrow().deref() == destination.borrow().deref() { break; } + // Iterate over neighbors of current node for target in ¤t_node.borrow().destinations { + // Calculate source distance let new_distance = current_node.borrow().source_distance.0 + target.distance; + + // If new distance is smaller, update the node if new_distance < target.node.borrow().source_distance.0 { + // Remove from set (necessary to keep the BTreeSet invariant true) let destination_node = unvisited.take(&target.node).unwrap(); + // Update the source distance { let mut node_ref = destination_node.deref().borrow_mut(); node_ref.previous_node = Some(current_node.clone()); node_ref.source_distance = NonNan(new_distance); } + // Reinsert the node to sort it back into the set unvisited.insert(destination_node); } } } + // A little iterator that helps reconstruct the path let follower = PathFollower { next_node: Some(destination.clone()), }; + // Flip the path, as it is stored as previous_node in the node struct let reversed_path: Vec = follower.collect(); let path: Vec = reversed_path.into_iter().rev().collect(); + // If path was found, print output, else state no path found if path.len() > 1 { println!( "Total Distance: {}",