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 nodeicol_indices[row_ptr[i]..row_ptr[i+1]]are the neighbor node IDsvalues[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
impl CsrGraph
Sourcepub fn from_raw(
num_nodes: usize,
row_ptr: Vec<usize>,
col_indices: Vec<usize>,
values: Vec<f64>,
directed: bool,
) -> Result<Self>
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_indicesare< num_nodes col_indices.len() == values.len() == row_ptr[num_nodes]
Sourcepub fn from_edges(
num_nodes: usize,
edges: Vec<(usize, usize, f64)>,
directed: bool,
) -> Result<Self>
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.
Sourcepub fn from_edges_parallel(
num_nodes: usize,
edges: Vec<(usize, usize, f64)>,
directed: bool,
) -> Result<Self>
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.
Sourcepub fn from_unweighted_edges(
num_nodes: usize,
edges: &[(usize, usize)],
directed: bool,
) -> Result<Self>
pub fn from_unweighted_edges( num_nodes: usize, edges: &[(usize, usize)], directed: bool, ) -> Result<Self>
Construct an unweighted CSR graph from an edge list.
Sourcepub fn num_edges(&self) -> usize
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.
Sourcepub fn num_logical_edges(&self) -> usize
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.
Sourcepub fn is_directed(&self) -> bool
pub fn is_directed(&self) -> bool
Whether the graph is directed.
Sourcepub fn degree(&self, node: usize) -> usize
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.
Sourcepub fn neighbors(&self, node: usize) -> NeighborIter<'_> ⓘ
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.
Sourcepub fn has_edge(&self, src: usize, dst: usize) -> bool
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)).
Sourcepub fn edge_weight(&self, src: usize, dst: usize) -> Option<f64>
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.
Sourcepub fn col_indices(&self) -> &[usize]
pub fn col_indices(&self) -> &[usize]
Get the raw column index array (read-only).
Sourcepub fn memory_bytes(&self) -> usize
pub fn memory_bytes(&self) -> usize
Memory usage in bytes (approximate).
Sourcepub fn spmv(&self, x: &[f64]) -> Result<Vec<f64>>
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§impl CsrGraph
impl CsrGraph
Sourcepub fn to_adjacency_list(&self) -> AdjacencyList
pub fn to_adjacency_list(&self) -> AdjacencyList
Convert to an adjacency list representation.
Sourcepub fn from_adjacency_list(adj: &AdjacencyList) -> Result<Self>
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
impl CsrGraph
Sourcepub fn to_graph(&self) -> Graph<usize, f64>
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).
Trait Implementations§
Auto Trait Implementations§
impl Freeze for CsrGraph
impl RefUnwindSafe for CsrGraph
impl Send for CsrGraph
impl Sync for CsrGraph
impl Unpin for CsrGraph
impl UnsafeUnpin for CsrGraph
impl UnwindSafe for CsrGraph
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> 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