pub struct Graph { /* private fields */ }Expand description
An undirected graph using adjacency list representation.
Implementations§
Source§impl Graph
impl Graph
pub fn new() -> Self
Sourcepub fn with_n_nodes(n: usize) -> Self
pub fn with_n_nodes(n: usize) -> Self
Create a graph with n nodes (0..n) and no edges.
Sourcepub fn remove_edge(&mut self, u: usize, v: usize)
pub fn remove_edge(&mut self, u: usize, v: usize)
Remove an undirected edge.
Sourcepub fn remove_node(&mut self, node: usize)
pub fn remove_node(&mut self, node: usize)
Remove a node and all its incident edges.
pub fn node_count(&self) -> usize
pub fn edge_count(&self) -> usize
pub fn nodes(&self) -> Vec<usize>
pub fn has_node(&self, node: usize) -> bool
pub fn has_edge(&self, u: usize, v: usize) -> bool
pub fn neighbors(&self, node: usize) -> Vec<usize>
pub fn degree(&self, node: usize) -> usize
pub fn degrees(&self) -> Vec<(usize, usize)>
Sourcepub fn bfs_distances(&self, source: usize) -> HashMap<usize, usize>
pub fn bfs_distances(&self, source: usize) -> HashMap<usize, usize>
BFS shortest path lengths from source to all reachable nodes.
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Check if the graph is connected.
Sourcepub fn connected_components(&self) -> Vec<Vec<usize>>
pub fn connected_components(&self) -> Vec<Vec<usize>>
Connected components.
Sourcepub fn adjacency(&self) -> &HashMap<usize, HashSet<usize>>
pub fn adjacency(&self) -> &HashMap<usize, HashSet<usize>>
Get the adjacency as a HashMap (for external use).
Sourcepub fn largest_component(&self) -> Graph
pub fn largest_component(&self) -> Graph
Get the largest connected component as a new Graph.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Graph
impl<'de> Deserialize<'de> for Graph
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. 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 UnsafeUnpin for Graph
impl UnwindSafe for Graph
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