Thanks to visit codestin.com
Credit goes to docs.rs

Struct Graph

Source
pub struct Graph<T: Display + Eq + Hash> { /* private fields */ }
Expand description

Graph data structure to organize nodes that are connected to each other with edges.

Implementations§

Source§

impl<T: Display + Clone + Eq + Hash> Graph<T>

Source

pub fn new() -> Self

Creates a new and empty graph.

Source

pub fn add_node(&mut self, id: T) -> bool

Adds a new node with id id to the graph.

The id is the unique identifier of this node, it is used to adddress this node in all other functions.

Return value indicates if the node was added to the graph or if a node with this id already exists.

§Example
use simple_graph_algorithms::Graph;
 
let mut graph = Graph::new();
 
assert_eq!(graph.add_node("a"), true);
assert_eq!(graph.add_node("b"), true);
 
// Returns false because node with id `b` was already added to the graph.
assert_eq!(graph.add_node("b"), false);
Source

pub fn add_nodes(&mut self, ids: Vec<T>) -> i32

Adds new nodes to the graph.

For each entry in the ids vector a new node is added, the entry being the unique identifier of the new node.

Return value indicates how many nodes where not added because a node with the id already exists.

§Example
use simple_graph_algorithms::Graph;
 
let mut graph = Graph::new();
let ids = vec!["a", "b", "c"];
 
assert_eq!(graph.add_nodes(ids.clone()), 0);
 
assert_eq!(graph.contains_node(&"a"), true);
assert_eq!(graph.contains_node(&"b"), true);
assert_eq!(graph.contains_node(&"c"), true);
 
// Add nodes again, returns 3 because all nodes already exist in the graph.
assert_eq!(graph.add_nodes(ids), 3);
Source

pub fn contains_node(&self, id: &T) -> bool

Checks if node with id id is contained inside this graph.

§Example
use simple_graph_algorithms::Graph;
 
let mut graph = Graph::new();
 
graph.add_node("a");
 
assert_eq!(graph.contains_node(&"a"), true);
assert_eq!(graph.contains_node(&"b"), false);
Source

pub fn add_edge(&mut self, weight: i32, source_id: &T, target_id: &T) -> bool

Adds a new edge to the graph that connects two nodes in a single direction from source to target. For that to succeed both nodes are required to be contained within the graph.

The weight defines “the distance” between the nodes with ids source_id and target_id.

Returns true if the edge was added, if false is returned either node is missing in the graph.

Use Graph::try_add_edge(&mut self, i32, &T, &T) instead if you need to know why the edge could not be added.

§Example
use simple_graph_algorithms::Graph;
use simple_graph_algorithms::AddEdgeError;
 
let mut graph = Graph::new();
 
graph.add_node("a");
graph.add_node("b");
 
assert_eq!(graph.add_edge(1, &"a", &"b"), true);
assert_eq!(graph.add_edge(1, &"c", &"d"), false);
Source

pub fn try_add_edge( &mut self, weight: i32, source_id: &T, target_id: &T, ) -> Result<(), AddEdgeError>

Tries to add a new edge to the graph that connects two nodes in a single direction from source to target.

The weight defines “the distance” between the nodes with ids source_id and target_id.

Returns Ok(()) when the edge was added or an AddEdgeError specifing because of which missing node(s) the edge could not be added.

§Example
use simple_graph_algorithms::Graph;
use simple_graph_algorithms::AddEdgeError;
 
let mut graph = Graph::new();
 
graph.add_node("a");
graph.add_node("b");
 
assert_eq!(graph.try_add_edge(1, &"a", &"b"), Ok(()));
 
// Errors because node "c" is missing from the graph
assert_eq!(graph.try_add_edge(1, &"c", &"b"), Err(AddEdgeError::SourceMissing));
 
// Errors because node "d" is missing from the graph
assert_eq!(graph.try_add_edge(1, &"a", &"d"), Err(AddEdgeError::TargetMissing));
 
// Errors because nodes "c" and  "d" are missing from the graph
assert_eq!(graph.try_add_edge(1, &"c", &"d"), Err(AddEdgeError::BothMissing));
Source

pub fn add_double_edge( &mut self, weight: i32, source_id: &T, target: &T, ) -> bool

Adds a new edge to the graph that connects two nodes in a both directions. For that to succeed both nodes are required to be contained within the graph.

The weight defines “the distance” between the nodes with ids source_id and target_id.

Returns true if the edge was added, if false is returned either node is missing in the graph.

Use Graph::try_add_double_edge(&mut self, i32, &T, &T) instead if you need to know why the edge could not be added.

§Example
use simple_graph_algorithms::Graph;
 
let mut graph = Graph::new();
 
graph.add_node("a");
graph.add_node("b");
 
assert_eq!(graph.add_double_edge(1, &"a", &"b"), true);
assert_eq!(graph.add_double_edge(1, &"c", &"d"), false);
Source

pub fn try_add_double_edge( &mut self, weight: i32, source_id: &T, target_id: &T, ) -> Result<(), AddEdgeError>

Tries to add a new edge to the graph that connects two nodes in a single direction from source to target.

The weight defines “the distance” between the nodes with ids source_id and target_id.

Returns Ok(()) when the edge was added or an AddEdgeError specifing because of which missing node(s) the edge could not be added.

§Example
use simple_graph_algorithms::Graph;
use simple_graph_algorithms::AddEdgeError;
 
let mut graph = Graph::new();
 
graph.add_node("a");
graph.add_node("b");
 
assert_eq!(graph.try_add_double_edge(1, &"a", &"b"), Ok(()));
 
// Errors because node "c" is missing from the graph
assert_eq!(graph.try_add_double_edge(1, &"c", &"b"), Err(AddEdgeError::SourceMissing));
 
// Errors because node "d" is missing from the graph
assert_eq!(graph.try_add_double_edge(1, &"a", &"d"), Err(AddEdgeError::TargetMissing));
 
// Errors because nodes "c" and  "d" are missing from the graph
assert_eq!(graph.try_add_double_edge(1, &"c", &"d"), Err(AddEdgeError::BothMissing));
Source

pub fn contains_edge(&self, source_id: &T, target_id: &T) -> bool

Checks if the graph contains an edge from node with id source_id to node with id target_id.

§Example
use simple_graph_algorithms::Graph;
 
let mut graph = Graph::new();
 
graph.add_node("a");
graph.add_node("b");
graph.add_edge(1, &"a", &"b");
 
assert_eq!(graph.contains_edge(&"a", &"b"), true);
assert_eq!(graph.contains_edge(&"c", &"d"), false);
Source

pub fn size(&self) -> usize

Returns the size of this graph, determined by the amount of nodes contained.

§Example
use simple_graph_algorithms::Graph;
 
let mut graph = Graph::new();
 
graph.add_node("a");
graph.add_node("b");
 
assert_eq!(graph.size(), 2);
Source

pub fn clear(&mut self)

Clears the graph, removing all nodes. Keeps the allocated memory for reuse.

§Example
use simple_graph_algorithms::Graph;
 
let mut graph = Graph::new();
 
graph.add_node("a");
graph.add_node("b");
 
assert_eq!(graph.size(), 2);
 
graph.clear();
 
assert_eq!(graph.size(), 0);

Trait Implementations§

Source§

impl<T: Clone + Display + Eq + Hash> Clone for Graph<T>

Source§

fn clone(&self) -> Graph<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Display + Eq + Hash> Debug for Graph<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: Display + Clone + Eq + Hash> Default for Graph<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Display + Eq + Hash> Display for Graph<T>

Source§

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

Formats the graph to show all edges between nodes

Source§

impl<T: Display + Clone + Eq + Hash + From<String>> From<&Vec<Vec<i32>>> for Graph<T>

Source§

fn from(value: &Vec<Vec<i32>>) -> Self

Create a graph from a 2D vector containing i32.

The i32 value is the edge weight of each edge leading into that node.

§Example
use simple_graph_algorithms::Graph;
 
// Prepare example vector
let mut vec: Vec<Vec<i32>> = Vec::new();
let vec_inner_1 = vec![3, 4, 5];
let vec_inner_2 = vec![1, 2, 3];
let vec_inner_3 = vec![1, 8, 2];
vec.push(vec_inner_1);
vec.push(vec_inner_2);
vec.push(vec_inner_3);
 
// Create graph from example vector
let mut graph = Graph::<String>::from(&vec);
 
// Run dijkstra's algorithm
//assert_eq!(8, dijkstra(&mut graph, &String::from("[0|0]"), &String::from("[2|2]")).unwrap_or(-1));
Source§

impl<T: Display + Eq + Hash + Clone> Ord for Graph<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

Compares the size of this graph with the size of another graph.

1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq + Display + Eq + Hash> PartialEq for Graph<T>

Source§

fn eq(&self, other: &Graph<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Display + Eq + Hash + Clone> PartialOrd for Graph<T>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

Compares the size of this graph with the size of another graph.

1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Eq + Display + Eq + Hash> Eq for Graph<T>

Source§

impl<T: Display + Eq + Hash> StructuralPartialEq for Graph<T>

Auto Trait Implementations§

§

impl<T> Freeze for Graph<T>

§

impl<T> !RefUnwindSafe for Graph<T>

§

impl<T> !Send for Graph<T>

§

impl<T> !Sync for Graph<T>

§

impl<T> Unpin for Graph<T>
where T: Unpin,

§

impl<T> !UnwindSafe for Graph<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.