Struct Graph

Source
pub struct Graph { /* private fields */ }
Expand description

Graph data structure

This create implements a Graph data structure

Implementations§

Source§

impl Graph

Source

pub fn new(num_nodes: usize, routes: Vec<(usize, usize)>) -> Self

Create a new graph with the given routes tuple(current, neighbor) current <— neighbor

§Example
use flex_algo::Graph;
 
let graph = Graph::new(6, vec![(1, 0), (2, 1), (2, 5), (0, 3), (4, 3), (3, 5), (4, 5)]);
println!("graph: {:?}", graph);
Source

pub fn is_acyclic_bfs(&self) -> bool

Breadth First Search algorithm to check if it’s an acyclic graph,

§Example
use flex_algo::Graph;
 
let graph = Graph::new(6, vec![(1, 0), (2, 1), (2, 5), (0, 3), (4, 3), (3, 5), (4, 5)]);
println!("graph: {:?}", graph);
assert_eq!(graph.is_acyclic_bfs(), true);
 
Source

pub fn is_acyclic_top_sort(&mut self) -> bool

Topological Sort algorithm to check if it’s an acyclic graph

§Example
use flex_algo::Graph;
 
let mut graph = Graph::new(6, vec![(1, 0), (2, 1), (2, 5), (0, 3), (4, 3), (3, 5), (4, 5)]);
println!("graph: {:?}", graph);
assert_eq!(graph.is_acyclic_top_sort(), true);
 

Return the path by breadth first search algo for the graph

§Example
use flex_algo::Graph;
 
let mut graph = Graph::new(6, vec![(1, 0), (2, 1), (2, 5), (0, 3), (4, 3), (3, 5), (4, 5)]);
let visit = graph.breadth_first_search(5);
assert_eq!(visit, vec![5, 4, 3, 0, 1, 2]);
 

Return the path by depth first search algo for the graph

§Example
use flex_algo::Graph;
 
let graph = Graph::new(8, vec![
    (5, 4), (2, 4), (6, 4), 
    (7, 5), (0, 2), (1, 2), (3, 6)
]);
let visit = graph.depth_first_search(4);
 
assert_eq!(visit, vec![7, 5, 0, 1, 2, 3, 6, 4]);

Trait Implementations§

Source§

impl Debug for Graph

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Graph

§

impl RefUnwindSafe for Graph

§

impl Send for Graph

§

impl Sync for Graph

§

impl Unpin for Graph

§

impl UnwindSafe for Graph

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.