pub struct Graph<V = (), E = ()>{
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>
impl<V, E> Graph<V, E>
Sourcepub fn from_nodes(nodes: Vec<V>) -> Self
pub fn from_nodes(nodes: Vec<V>) -> Self
Create a new graph from a given vector of nodes.
pub fn empty() -> Self
Sourcepub fn erase_edge(&mut self, idx: &mut usize)
pub fn erase_edge(&mut self, idx: &mut usize)
Erase the edge with index idx.
Sourcepub fn sort_edges(&mut self, node: usize)
pub fn sort_edges(&mut self, node: usize)
Sort the edges of a node by the node id.
Sourcepub fn sort_edges_by<F>(&mut self, node: usize, compare: F)
pub fn sort_edges_by<F>(&mut self, node: usize, compare: F)
Sort the edges of a node by the given comparator.
Sourcepub fn add_edge(&mut self, from: usize, to: usize, info: E)
pub fn add_edge(&mut self, from: usize, to: usize, info: E)
Add an undirected edge between from and to with information info.
Sourcepub fn get_edges_from(&self, edge: usize) -> impl Iterator<Item = (&usize, &E)>
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.
Sourcepub fn get_edges_enum_from(
&self,
edge: usize,
) -> impl Iterator<Item = (usize, (&usize, &E))>
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.
Sourcepub fn get_edges(&self, node: usize) -> impl Iterator<Item = (&usize, &E)>
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.
Sourcepub fn get_edges_enum(
&self,
node: usize,
) -> impl Iterator<Item = (usize, (&usize, &E))>
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.
Sourcepub fn get_edges_from_once<'a>(
&'a self,
cur: &'a mut usize,
) -> impl Iterator<Item = (&usize, &E)> + 'a
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.
Sourcepub fn get_edges_enum_from_once<'a>(
&'a self,
cur: &'a mut usize,
) -> impl Iterator<Item = (usize, (&usize, &E))> + 'a
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.
Sourcepub fn get_edge(&self, idx: usize) -> (usize, usize, &E)
pub fn get_edge(&self, idx: usize) -> (usize, usize, &E)
Get the edge information of the edge with index idx.
Sourcepub fn get_twin_edge(&self, idx: usize) -> (usize, usize, &E)
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.