pub struct Dijkstra { /* private fields */ }
Expand description
Dijkstra algorithm
This crate implements a Dijkstra algorithm to compute the shortest path by given graph.
Implementations§
Source§impl Dijkstra
impl Dijkstra
Sourcepub fn new(num_nodes: usize, edges: Vec<(usize, usize, usize)>) -> Self
pub fn new(num_nodes: usize, edges: Vec<(usize, usize, usize)>) -> Self
Create a new Dijkstra graph with edges tuple(current, neighbor, weight) Vec
§Example
use flex_algo::Dijkstra;
let times = vec![(0, 1, 9), (0, 3, 2), (1, 4, 1), (3, 1, 4), (3, 4, 6), (2, 1, 3), (4, 2, 7), (2, 0, 5)];
let dijkstra = Dijkstra::new(5, times);
Sourcepub fn shortest_path(&self, node: usize) -> Option<(usize, Vec<usize>)>
pub fn shortest_path(&self, node: usize) -> Option<(usize, Vec<usize>)>
Return the shortest path
§Example
use flex_algo::Dijkstra;
let times = vec![(0, 1, 9), (0, 3, 2), (1, 4, 1), (3, 1, 4), (3, 4, 6), (2, 1, 3), (4, 2, 7), (2, 0, 5)];
let dijkstra = Dijkstra::new(5, times);
let (max, path) = dijkstra.shortest_path(0).unwrap();
println!("shortest path: {:?}", path);
assert_eq!(max, 14);
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Dijkstra
impl RefUnwindSafe for Dijkstra
impl Send for Dijkstra
impl Sync for Dijkstra
impl Unpin for Dijkstra
impl UnwindSafe for Dijkstra
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more