Skip to main content

CsrGraph

Struct CsrGraph 

Source
pub struct CsrGraph { /* private fields */ }
Expand description

A compressed sparse row (CSR) graph representation.

Stores edges in three contiguous arrays:

  • row_ptr[i]..row_ptr[i+1] gives the range of neighbors for node i
  • col_indices[row_ptr[i]..row_ptr[i+1]] are the neighbor node IDs
  • values[row_ptr[i]..row_ptr[i+1]] are the corresponding edge weights

This format is identical to the CSR sparse matrix format from numerical linear algebra, enabling direct use in sparse matrix-vector products (e.g., for PageRank).

Implementations§

Source§

impl CsrGraph

Source

pub fn from_raw( num_nodes: usize, row_ptr: Vec<usize>, col_indices: Vec<usize>, values: Vec<f64>, directed: bool, ) -> Result<Self>

Construct a CSR graph directly from raw arrays.

§Safety contract (logical, not unsafe)

The caller must ensure:

  • row_ptr.len() == num_nodes + 1
  • All values in col_indices are < num_nodes
  • col_indices.len() == values.len() == row_ptr[num_nodes]
Source

pub fn from_edges( num_nodes: usize, edges: Vec<(usize, usize, f64)>, directed: bool, ) -> Result<Self>

Construct from an edge list (convenience wrapper).

For undirected graphs, reverse edges are added automatically.

Source

pub fn from_edges_parallel( num_nodes: usize, edges: Vec<(usize, usize, f64)>, directed: bool, ) -> Result<Self>

Construct from an edge list using parallel construction.

Source

pub fn from_unweighted_edges( num_nodes: usize, edges: &[(usize, usize)], directed: bool, ) -> Result<Self>

Construct an unweighted CSR graph from an edge list.

Source

pub fn num_nodes(&self) -> usize

Number of nodes.

Source

pub fn num_edges(&self) -> usize

Number of stored (directed) edges.

For undirected graphs built from n input edges, this returns 2n because both directions are stored.

Source

pub fn num_logical_edges(&self) -> usize

Number of logical edges.

For undirected graphs, returns num_edges / 2 (the original count). For directed graphs, returns num_edges.

Source

pub fn is_directed(&self) -> bool

Whether the graph is directed.

Source

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

Out-degree of a node (number of outgoing edges).

Returns 0 if the node index is out of range.

Source

pub fn neighbors(&self, node: usize) -> NeighborIter<'_>

Iterator over neighbors of node as (neighbor_id, weight) pairs.

Returns an empty iterator if node is out of range.

Source

pub fn has_edge(&self, src: usize, dst: usize) -> bool

Check if an edge exists from src to dst.

Uses binary search on the sorted neighbor list. O(log(degree)).

Source

pub fn edge_weight(&self, src: usize, dst: usize) -> Option<f64>

Get the weight of an edge from src to dst.

Returns None if the edge does not exist.

Source

pub fn row_ptr(&self) -> &[usize]

Get the raw row pointer array (read-only).

Source

pub fn col_indices(&self) -> &[usize]

Get the raw column index array (read-only).

Source

pub fn values(&self) -> &[f64]

Get the raw values array (read-only).

Source

pub fn memory_bytes(&self) -> usize

Memory usage in bytes (approximate).

Source

pub fn spmv(&self, x: &[f64]) -> Result<Vec<f64>>

Sparse matrix-vector product: y = A * x.

This is the core operation for iterative algorithms like PageRank. x and the returned vector both have length num_nodes.

Source

pub fn spmv_parallel(&self, x: &[f64]) -> Result<Vec<f64>>

Parallel sparse matrix-vector product: y = A * x.

Source

pub fn transpose(&self) -> Result<Self>

Transpose the graph (reverse all edge directions).

For undirected graphs, the transpose is the same graph.

Source§

impl CsrGraph

Source

pub fn to_adjacency_list(&self) -> AdjacencyList

Convert to an adjacency list representation.

Source

pub fn from_adjacency_list(adj: &AdjacencyList) -> Result<Self>

Construct a CSR graph from an adjacency list.

The adjacency list is consumed and edges are extracted directly. For undirected graphs, the adjacency list should already contain both directions (i.e., if (u,v) is present in adj[u], then (v,u) should be in adj[v]).

Source§

impl CsrGraph

Source

pub fn to_graph(&self) -> Graph<usize, f64>

Convert to a Graph<usize, f64> (undirected adjacency-list graph).

For directed CSR graphs, the resulting Graph will be undirected (edges are treated as undirected).

Source

pub fn from_graph(graph: &Graph<usize, f64>) -> Result<Self>

Construct a CSR graph from a Graph<usize, f64>.

Source

pub fn from_digraph(graph: &DiGraph<usize, f64>) -> Result<Self>

Construct a CSR graph from a DiGraph<usize, f64>.

Trait Implementations§

Source§

impl Clone for CsrGraph

Source§

fn clone(&self) -> CsrGraph

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 CsrGraph

Source§

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

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

impl Display for CsrGraph

Source§

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

Formats the value using the given formatter. 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, 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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