pub struct SparseGraph { /* private fields */ }Expand description
A weighted undirected graph with dynamic edge support.
Internally stores adjacency lists keyed by vertex index. Vertices are
implicitly numbered 0..num_vertices. When an edge (u, v) is
inserted with max(u, v) >= num_vertices, the vertex count grows
automatically.
§Thread safety
SparseGraph is not internally synchronised. Wrap it in
parking_lot::RwLock for concurrent access (the sparsifier does this).
Implementations§
Source§impl SparseGraph
impl SparseGraph
Sourcepub fn with_capacity(n: usize) -> Self
pub fn with_capacity(n: usize) -> Self
Create an empty graph pre-allocated for n vertices.
Sourcepub fn from_edges(edges: &[(usize, usize, f64)]) -> Self
pub fn from_edges(edges: &[(usize, usize, f64)]) -> Self
Build a graph from a list of weighted edges (u, v, weight).
Duplicate edges are silently overwritten with the last weight.
Sourcepub fn ensure_capacity(&mut self, n: usize)
pub fn ensure_capacity(&mut self, n: usize)
Ensure the graph can represent at least n vertices.
Sourcepub fn num_vertices(&self) -> usize
pub fn num_vertices(&self) -> usize
Number of vertices (equal to the length of the adjacency array).
Sourcepub fn total_weight(&self) -> f64
pub fn total_weight(&self) -> f64
Sum of all edge weights (each undirected edge counted once).
Sourcepub fn degree(&self, u: usize) -> usize
pub fn degree(&self, u: usize) -> usize
Degree of vertex u (number of neighbours).
Returns 0 if u is out of bounds.
Sourcepub fn weighted_degree(&self, u: usize) -> f64
pub fn weighted_degree(&self, u: usize) -> f64
Weighted degree of vertex u (sum of incident edge weights).
Note: This iterates incident edges each call. For hot-path usage, consider caching the result externally.
Sourcepub fn edge_weight(&self, u: usize, v: usize) -> Option<f64>
pub fn edge_weight(&self, u: usize, v: usize) -> Option<f64>
Weight of the edge (u, v), or None if it does not exist.
Sourcepub fn edges(&self) -> impl Iterator<Item = (usize, usize, f64)> + '_
pub fn edges(&self) -> impl Iterator<Item = (usize, usize, f64)> + '_
Iterate over all edges yielding (u, v, weight) with u < v.
Sourcepub fn insert_edge(&mut self, u: usize, v: usize, weight: f64) -> Result<()>
pub fn insert_edge(&mut self, u: usize, v: usize, weight: f64) -> Result<()>
Insert an undirected edge (u, v) with the given weight.
The vertex set is automatically expanded if necessary.
§Errors
SparsifierError::InvalidWeightifweightis not positive/finite.SparsifierError::EdgeAlreadyExistsif the edge is present.
Sourcepub fn insert_or_update_edge(
&mut self,
u: usize,
v: usize,
weight: f64,
) -> Result<Option<f64>>
pub fn insert_or_update_edge( &mut self, u: usize, v: usize, weight: f64, ) -> Result<Option<f64>>
Insert or overwrite an edge without returning an error on duplicates.
Returns the old weight if the edge already existed.
Sourcepub fn delete_edge(&mut self, u: usize, v: usize) -> Result<f64>
pub fn delete_edge(&mut self, u: usize, v: usize) -> Result<f64>
Delete the undirected edge (u, v).
§Errors
Returns SparsifierError::EdgeNotFound if the edge does not exist.
Sourcepub fn update_weight(
&mut self,
u: usize,
v: usize,
new_weight: f64,
) -> Result<f64>
pub fn update_weight( &mut self, u: usize, v: usize, new_weight: f64, ) -> Result<f64>
Update the weight of edge (u, v).
§Errors
Returns an error if the edge does not exist or the weight is invalid.
Sourcepub fn laplacian_quadratic_form(&self, x: &[f64]) -> f64
pub fn laplacian_quadratic_form(&self, x: &[f64]) -> f64
Compute the Laplacian quadratic form x^T L x.
For each edge (u, v, w): sum += w * (x[u] - x[v])^2
§Panics
Panics if x.len() < num_vertices().
Trait Implementations§
Source§impl Clone for SparseGraph
impl Clone for SparseGraph
Source§fn clone(&self) -> SparseGraph
fn clone(&self) -> SparseGraph
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SparseGraph
impl Debug for SparseGraph
Source§impl Default for SparseGraph
impl Default for SparseGraph
Source§impl<'de> Deserialize<'de> for SparseGraph
impl<'de> Deserialize<'de> for SparseGraph
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>,
Auto Trait Implementations§
impl Freeze for SparseGraph
impl RefUnwindSafe for SparseGraph
impl Send for SparseGraph
impl Sync for SparseGraph
impl Unpin for SparseGraph
impl UnsafeUnpin for SparseGraph
impl UnwindSafe for SparseGraph
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more