Skip to main content

CsrMatrix

Struct CsrMatrix 

Source
pub struct CsrMatrix {
    pub num_rows: usize,
    pub num_cols: usize,
    pub row_ptr: Vec<u64>,
    pub col_idx: Vec<u32>,
    pub values: Option<Vec<f64>>,
}
Expand description

Compressed Sparse Row matrix for graph adjacency.

For a graph with N nodes and M edges:

  • row_ptr: N+1 elements, where row_ptr[i] is the start of row i’s edges
  • col_idx: M elements, the column indices (neighbor node IDs)
  • values: Optional M elements for weighted graphs

Fields§

§num_rows: usize

Number of rows (nodes).

§num_cols: usize

Number of columns (typically equals num_rows for square adjacency).

§row_ptr: Vec<u64>

Row pointers (length = num_rows + 1).

§col_idx: Vec<u32>

Column indices (length = nnz).

§values: Option<Vec<f64>>

Optional edge values/weights.

Implementations§

Source§

impl CsrMatrix

Source

pub fn empty(num_nodes: usize) -> Self

Create an empty CSR matrix.

Source

pub fn from_edges(num_nodes: usize, edges: &[(u32, u32)]) -> Self

Create CSR from edge list.

§Arguments
  • num_nodes - Number of nodes in the graph
  • edges - List of (source, destination) pairs
§Example
use ringkernel_graph::CsrMatrix;

// Graph: 0 -> 1 -> 2
let csr = CsrMatrix::from_edges(3, &[(0, 1), (1, 2)]);
assert_eq!(csr.num_nonzeros(), 2);
Source

pub fn from_weighted_edges(num_nodes: usize, edges: &[(u32, u32, f64)]) -> Self

Create CSR from edge list with weights.

Source

pub fn num_nonzeros(&self) -> usize

Number of non-zero entries (edges).

Source

pub fn is_empty(&self) -> bool

Check if matrix is empty.

Source

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

Get the degree (number of outgoing edges) of a node.

Source

pub fn neighbors(&self, node: NodeId) -> &[u32]

Get neighbors of a node.

Source

pub fn weighted_neighbors(&self, node: NodeId) -> Vec<(NodeId, f64)>

Get neighbors with weights (returns empty if unweighted).

Source

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

Check if edge exists from src to dst.

Source

pub fn validate(&self) -> Result<()>

Validate CSR structure.

Source

pub fn transpose(&self) -> Self

Create transpose (reverse graph).

Trait Implementations§

Source§

impl Clone for CsrMatrix

Source§

fn clone(&self) -> CsrMatrix

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 CsrMatrix

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