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) Vec
Example
use flex_algo::Dijkstra;
let times = vec![(1, 2, 9), (1, 4, 2), (2, 5, 1), (4, 2, 4), (4, 5, 6), (3, 2, 3), (5, 3, 7), (3, 1, 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![(1, 2, 9), (1, 4, 2), (2, 5, 1), (4, 2, 4), (4, 5, 6), (3, 2, 3), (5, 3, 7), (3, 1, 5)];
let dijkstra = Dijkstra::new(5, times);
let (max, path) = dijkstra.shortest_path(1).unwrap();
println!("shortest path: {:?}", path);
assert_eq!(max, 14);