pub struct ShortestPath<T: Display + Clone> { /* private fields */ }
Expand description
The shortest path from one node to another.
Implementations§
Source§impl<T: Display + Clone> ShortestPath<T>
impl<T: Display + Clone> ShortestPath<T>
Sourcepub fn source(&self) -> Option<&T>
pub fn source(&self) -> Option<&T>
Id of source node where this shortest path starts.
§Example
use simple_graph_algorithms::{Graph, algorithms::dijkstra};
let mut graph = Graph::new();
graph.add_node('a');
graph.add_node('b');
graph.add_edge(4, &'a', &'b');
let spt = dijkstra(&mut graph, &'a')?;
// Calculate shortest path from a to b using dijkstra's algorithm.
// It is ok to use .unwrap() here, because we know that the graph contains node b.
let shortest_path = spt.shortest_path(&'b').unwrap();
assert_eq!(shortest_path.source(), Some(&'a'));
Sourcepub fn target(&self) -> Option<&T>
pub fn target(&self) -> Option<&T>
Id of target node where this shortest path ends.
§Example
use simple_graph_algorithms::{Graph, algorithms::dijkstra};
let mut graph = Graph::new();
graph.add_node('a');
graph.add_node('b');
graph.add_edge(4, &'a', &'b');
let spt = dijkstra(&mut graph, &'a')?;
// Calculate shortest path from a to b using dijkstra's algorithm.
// It is ok to use .unwrap() here, because we know that the graph contains node b.
let shortest_path = spt.shortest_path(&'b').unwrap();
assert_eq!(shortest_path.target(), Some(&'b'));
Sourcepub fn path(&self) -> &Vec<T>
pub fn path(&self) -> &Vec<T>
Id’s of nodes that form the shortest path.
First element is the id of the start node. Last element is the id of the target node.
§Example
use simple_graph_algorithms::{Graph, algorithms::dijkstra};
let mut graph = Graph::new();
graph.add_node('a');
graph.add_node('b');
graph.add_node('c');
graph.add_edge(4, &'a', &'b');
graph.add_edge(2, &'b', &'c');
let spt = dijkstra(&mut graph, &'a')?;
// Calculate shortest path from a to c using dijkstra's algorithm.
// It is ok to use .unwrap() here, because we know that the graph contains node c.
let shortest_path = spt.shortest_path(&'c').unwrap();
assert_eq!(shortest_path.path(), &vec!['a', 'b', 'c']);
Trait Implementations§
Source§impl<T: Clone + Display + Clone> Clone for ShortestPath<T>
impl<T: Clone + Display + Clone> Clone for ShortestPath<T>
Source§fn clone(&self) -> ShortestPath<T>
fn clone(&self) -> ShortestPath<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl<T: Display + Clone + Debug> Display for ShortestPath<T>
impl<T: Display + Clone + Debug> Display for ShortestPath<T>
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Formats the shortest path in a way that can be printed easily.
§Example
use simple_graph_algorithms::{Graph, algorithms::dijkstra};
let mut graph = Graph::new();
graph.add_node('a');
graph.add_node('b');
graph.add_node('c');
graph.add_edge(4, &'a', &'b');
graph.add_edge(2, &'b', &'c');
let spt = dijkstra(&mut graph, &'a')?;
// Calculate shortest path from a to c using dijkstra's algorithm.
// It is ok to use .unwrap() here, because we know that the graph contains node c.
let shortest_path = spt.shortest_path(&'c').unwrap();
assert_eq!(shortest_path.to_string(), "a -> b -> c");
Source§impl<T: Ord + Display + Clone> Ord for ShortestPath<T>
impl<T: Ord + Display + Clone> Ord for ShortestPath<T>
Source§fn cmp(&self, other: &ShortestPath<T>) -> Ordering
fn cmp(&self, other: &ShortestPath<T>) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl<T: PartialOrd + Display + Clone> PartialOrd for ShortestPath<T>
impl<T: PartialOrd + Display + Clone> PartialOrd for ShortestPath<T>
impl<T: Eq + Display + Clone> Eq for ShortestPath<T>
impl<T: Display + Clone> StructuralPartialEq for ShortestPath<T>
Auto Trait Implementations§
impl<T> Freeze for ShortestPath<T>
impl<T> RefUnwindSafe for ShortestPath<T>where
T: RefUnwindSafe,
impl<T> Send for ShortestPath<T>where
T: Send,
impl<T> Sync for ShortestPath<T>where
T: Sync,
impl<T> Unpin for ShortestPath<T>where
T: Unpin,
impl<T> UnwindSafe for ShortestPath<T>where
T: UnwindSafe,
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