Skip to main content

Graph

Struct Graph 

Source
pub struct Graph<V = (), E = ()>
where V: Default + Clone, E: Default + Clone,
{ pub nodes: Vec<V>, pub head: Vec<usize>, pub edges: Vec<(usize, usize, E)>, /* private fields */ }
Expand description

Graph representation using adjacency list.

V is the information stored in each node, and E is the information stored in each edge.

Fields§

§nodes: Vec<V>

The information stored in each node.

§head: Vec<usize>

The head node in adjacency list for each node in graph.

§edges: Vec<(usize, usize, E)>

The information stored in each edge.

Implementations§

Source§

impl<V, E> Graph<V, E>
where V: Default + Clone, E: Default + Clone,

Source

pub fn new(n: usize) -> Self

Create a new graph with n nodes.

Source

pub fn from_nodes(nodes: Vec<V>) -> Self

Create a new graph from a given vector of nodes.

Source

pub fn empty() -> Self

Source

pub fn len_nodes(&self) -> usize

Get the number of nodes in the graph.

Source

pub fn len_edges(&self) -> usize

Get the number of edges in the graph.

Source

pub fn erase_edge(&mut self, idx: &mut usize)

Erase the edge with index idx.

Source

pub fn sort_edges(&mut self, node: usize)

Sort the edges of a node by the node id.

Source

pub fn sort_edges_by<F>(&mut self, node: usize, compare: F)
where F: FnMut(&(usize, &V), &(usize, &V)) -> Ordering,

Sort the edges of a node by the given comparator.

Source

pub fn add_edge(&mut self, from: usize, to: usize, info: E)

Add an undirected edge between from and to with information info.

Source

pub fn get_edges_from(&self, edge: usize) -> impl Iterator<Item = (&usize, &E)>

Returns an iterator over the edges from the edge with index edge. The iterator returns the destination node, and the information stored in the edge.

Source

pub fn get_edges_enum_from( &self, edge: usize, ) -> impl Iterator<Item = (usize, (&usize, &E))>

Returns an iterator over the edges from the edge with index edge. The iterator returns the index of the edge, the destination node, and the information stored in the edge.

Source

pub fn get_edges(&self, node: usize) -> impl Iterator<Item = (&usize, &E)>

Returns an iterator over the edges of a node. The iterator returns the destination node, and the information stored in the edge.

Source

pub fn get_edges_enum( &self, node: usize, ) -> impl Iterator<Item = (usize, (&usize, &E))>

Returns an iterator over the edges of a node. The iterator returns the index of the edge, the destination node, and the information stored in the edge.

Source

pub fn get_edges_from_once<'a>( &'a self, cur: &'a mut usize, ) -> impl Iterator<Item = (&usize, &E)> + 'a

Returns an iterator over the edges of a node. The iterator returns the destination node, and the information stored in the edge. This is used if you want to iterate edges only once.

Source

pub fn get_edges_enum_from_once<'a>( &'a self, cur: &'a mut usize, ) -> impl Iterator<Item = (usize, (&usize, &E))> + 'a

Returns an iterator over the edges of a node. The iterator returns the index of the edge, the destination node, and the information stored in the edge. This is used if you want to iterate edges only once.

Source

pub fn get_edge(&self, idx: usize) -> (usize, usize, &E)

Get the edge information of the edge with index idx.

Source

pub fn get_twin_edge(&self, idx: usize) -> (usize, usize, &E)

Get the twin edge information of the edge with index idx. Often used in undirected graphs as the reverse edge.

Source

pub fn get_edge_mut(&mut self, idx: usize) -> (usize, usize, &mut E)

Get the edge information of the edge with index idx.

Source

pub fn get_twin_edge_mut(&mut self, idx: usize) -> (usize, usize, &mut E)

Get the twin edge information of the edge with index idx. Often used in undirected graphs as the reverse edge.

Auto Trait Implementations§

§

impl<V, E> Freeze for Graph<V, E>

§

impl<V, E> RefUnwindSafe for Graph<V, E>

§

impl<V, E> Send for Graph<V, E>
where V: Send, E: Send,

§

impl<V, E> Sync for Graph<V, E>
where V: Sync, E: Sync,

§

impl<V, E> Unpin for Graph<V, E>
where V: Unpin, E: Unpin,

§

impl<V, E> UnsafeUnpin for Graph<V, E>

§

impl<V, E> UnwindSafe for Graph<V, E>
where V: UnwindSafe, E: UnwindSafe,

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.