pub struct Graph { /* private fields */ }Expand description
A directed or undirected graph using adjacency list representation.
§Storage Layout
- Nodes stored in vec for O(1) access and cache locality
- Edges stored as Vec<Vec
> for adjacency list - Optional weights stored in separate hash maps to minimize memory when unweighted
Implementations§
Source§impl Graph
impl Graph
Sourcepub fn new(num_nodes: u32, is_directed: bool) -> Self
pub fn new(num_nodes: u32, is_directed: bool) -> Self
Creates a new empty graph with capacity for num_nodes nodes.
Sourcepub fn is_directed(&self) -> bool
pub fn is_directed(&self) -> bool
Returns whether the graph is directed.
Sourcepub fn add_edge(&mut self, from: NodeId, to: NodeId, weight: Option<EdgeWeight>)
pub fn add_edge(&mut self, from: NodeId, to: NodeId, weight: Option<EdgeWeight>)
Adds an edge from from to to with optional weight.
For undirected graphs, this automatically adds the reverse edge.
§Panics
Panics if from or to are >= num_nodes.
Sourcepub fn neighbors(&self, node: NodeId) -> &[NodeId]
pub fn neighbors(&self, node: NodeId) -> &[NodeId]
Gets the neighbors (outgoing edges) for a node.
Sourcepub fn edge_weight(&self, from: NodeId, to: NodeId) -> Option<EdgeWeight>
pub fn edge_weight(&self, from: NodeId, to: NodeId) -> Option<EdgeWeight>
Gets the weight of an edge.
Sourcepub fn set_node_weights(&mut self, weights: Vec<NodeWeight>)
pub fn set_node_weights(&mut self, weights: Vec<NodeWeight>)
Sets weights for all nodes.
Sourcepub fn node_weight(&self, node: NodeId) -> Option<NodeWeight>
pub fn node_weight(&self, node: NodeId) -> Option<NodeWeight>
Gets the weight of a node.
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 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