Skip to main content

SparseGraph

Struct SparseGraph 

Source
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

Source

pub fn new() -> Self

Create an empty graph with no vertices.

Source

pub fn with_capacity(n: usize) -> Self

Create an empty graph pre-allocated for n vertices.

Source

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.

Source

pub fn ensure_capacity(&mut self, n: usize)

Ensure the graph can represent at least n vertices.

Source

pub fn num_vertices(&self) -> usize

Number of vertices (equal to the length of the adjacency array).

Source

pub fn num_edges(&self) -> usize

Number of undirected edges.

Source

pub fn total_weight(&self) -> f64

Sum of all edge weights (each undirected edge counted once).

Source

pub fn degree(&self, u: usize) -> usize

Degree of vertex u (number of neighbours).

Returns 0 if u is out of bounds.

Source

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.

Source

pub fn neighbors(&self, u: usize) -> impl Iterator<Item = (usize, f64)> + '_

Iterator over neighbours of u yielding (v, weight).

§Panics

Panics if u >= num_vertices().

Source

pub fn edge_weight(&self, u: usize, v: usize) -> Option<f64>

Weight of the edge (u, v), or None if it does not exist.

Source

pub fn has_edge(&self, u: usize, v: usize) -> bool

Check whether edge (u, v) exists.

Source

pub fn edges(&self) -> impl Iterator<Item = (usize, usize, f64)> + '_

Iterate over all edges yielding (u, v, weight) with u < v.

Source

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
Source

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.

Source

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.

Source

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.

Source

pub fn clear(&mut self)

Remove all edges and vertices.

Source

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().

Source

pub fn to_csr(&self) -> (Vec<usize>, Vec<usize>, Vec<f64>, usize)

Export to compressed sparse row (CSR) format.

Returns (row_ptr, col_indices, values, n) where n is the number of vertices. The CSR matrix represents the weighted adjacency matrix (symmetric, both directions stored).

Source

pub fn from_csr( row_ptr: &[usize], col_indices: &[usize], values: &[f64], n: usize, ) -> Self

Import from compressed sparse row (CSR) format.

The CSR data is interpreted as a symmetric adjacency matrix. Only entries with col >= row are inserted to avoid double-counting.

Trait Implementations§

Source§

impl Clone for SparseGraph

Source§

fn clone(&self) -> SparseGraph

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SparseGraph

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SparseGraph

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for SparseGraph

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for SparseGraph

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,