Expand description
Compressed Sparse Row (CSR
) is a sparse adjacency matrix graph.
CSR
is parameterized over:
- Associated data
N
for nodes andE
for edges, called weights. The associated data can be of arbitrary type. - Edge type
Ty
that determines whether the graph edges are directed or undirected. - Index type
Ix
, which determines the maximum size of the graph.
Using O(|E| + |V|) space.
Self loops are allowed, no parallel edges.
Fast iteration of the outgoing edges of a vertex.
Implementations
sourceimpl<N, E, Ty, Ix> Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
impl<N, E, Ty, Ix> Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
sourcepub fn with_nodes(n: usize) -> Selfwhere
N: Default,
pub fn with_nodes(n: usize) -> Selfwhere
N: Default,
sourceimpl<N, E, Ix> Csr<N, E, Directed, Ix>where
Ix: IndexType,
impl<N, E, Ix> Csr<N, E, Directed, Ix>where
Ix: IndexType,
sourcepub fn from_sorted_edges<Edge>(edges: &[Edge]) -> Result<Self, EdgesNotSorted>where
Edge: Clone + IntoWeightedEdge<E, NodeId = NodeIndex<Ix>>,
N: Default,
pub fn from_sorted_edges<Edge>(edges: &[Edge]) -> Result<Self, EdgesNotSorted>where
Edge: Clone + IntoWeightedEdge<E, NodeId = NodeIndex<Ix>>,
N: Default,
Create a new Csr
from a sorted sequence of edges
Edges must be sorted and unique, where the sort order is the default order for the pair (u, v) in Rust (u has priority).
Computes in O(|E| + |V|) time.
Example
use petgraph::csr::Csr;
use petgraph::prelude::*;
let graph = Csr::<(),()>::from_sorted_edges(&[
(0, 1), (0, 2),
(1, 0), (1, 2), (1, 3),
(2, 0),
(3, 1),
]);
sourceimpl<N, E, Ty, Ix> Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
impl<N, E, Ty, Ix> Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
pub fn node_count(&self) -> usize
pub fn edge_count(&self) -> usize
pub fn is_directed(&self) -> bool
sourcepub fn clear_edges(&mut self)
pub fn clear_edges(&mut self)
Remove all edges
sourcepub fn add_node(&mut self, weight: N) -> NodeIndex<Ix>
pub fn add_node(&mut self, weight: N) -> NodeIndex<Ix>
Adds a new node with the given weight, returning the corresponding node index.
sourcepub fn add_edge(&mut self, a: NodeIndex<Ix>, b: NodeIndex<Ix>, weight: E) -> boolwhere
E: Clone,
pub fn add_edge(&mut self, a: NodeIndex<Ix>, b: NodeIndex<Ix>, weight: E) -> boolwhere
E: Clone,
Return true
if the edge was added
If you add all edges in row-major order, the time complexity is O(|V|·|E|) for the whole operation.
Panics if a
or b
are out of bounds.
sourcepub fn contains_edge(&self, a: NodeIndex<Ix>, b: NodeIndex<Ix>) -> bool
pub fn contains_edge(&self, a: NodeIndex<Ix>, b: NodeIndex<Ix>) -> bool
Computes in O(log |V|) time.
Panics if the node a
does not exist.
sourcepub fn out_degree(&self, a: NodeIndex<Ix>) -> usize
pub fn out_degree(&self, a: NodeIndex<Ix>) -> usize
Computes in O(1) time.
Panics if the node a
does not exist.
sourcepub fn neighbors_slice(&self, a: NodeIndex<Ix>) -> &[NodeIndex<Ix>]
pub fn neighbors_slice(&self, a: NodeIndex<Ix>) -> &[NodeIndex<Ix>]
Computes in O(1) time.
Panics if the node a
does not exist.
sourcepub fn edges_slice(&self, a: NodeIndex<Ix>) -> &[E]ⓘNotable traits for &[u8]impl Read for &[u8]impl Write for &mut [u8]
pub fn edges_slice(&self, a: NodeIndex<Ix>) -> &[E]ⓘNotable traits for &[u8]impl Read for &[u8]impl Write for &mut [u8]
Computes in O(1) time.
Panics if the node a
does not exist.
sourcepub fn edges(&self, a: NodeIndex<Ix>) -> Edges<'_, E, Ty, Ix>ⓘNotable traits for Edges<'a, E, Ty, Ix>impl<'a, E, Ty, Ix> Iterator for Edges<'a, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType, type Item = EdgeReference<'a, E, Ty, Ix>;
pub fn edges(&self, a: NodeIndex<Ix>) -> Edges<'_, E, Ty, Ix>ⓘNotable traits for Edges<'a, E, Ty, Ix>impl<'a, E, Ty, Ix> Iterator for Edges<'a, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType, type Item = EdgeReference<'a, E, Ty, Ix>;
Ty: EdgeType,
Ix: IndexType, type Item = EdgeReference<'a, E, Ty, Ix>;
Return an iterator of all edges of a
.
Directed
: Outgoing edges froma
.Undirected
: All edges connected toa
.
Panics if the node a
does not exist.
Iterator element type is EdgeReference<E, Ty, Ix>
.
Trait Implementations
sourceimpl<N, E, Ty, Ix> Data for Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
impl<N, E, Ty, Ix> Data for Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
type NodeWeight = N
type EdgeWeight = E
sourceimpl<N, E, Ty, Ix> EdgeCount for Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
impl<N, E, Ty, Ix> EdgeCount for Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
sourcefn edge_count(&self) -> usize
fn edge_count(&self) -> usize
sourceimpl<'a, N, E, Ty, Ix> GetAdjacencyMatrix for &'a Csr<N, E, Ty, Ix>where
Ix: IndexType,
Ty: EdgeType,
impl<'a, N, E, Ty, Ix> GetAdjacencyMatrix for &'a Csr<N, E, Ty, Ix>where
Ix: IndexType,
Ty: EdgeType,
The adjacency matrix for Csr is a bitmap that’s computed by
.adjacency_matrix()
.
type AdjMatrix = FixedBitSet
type AdjMatrix = FixedBitSet
sourcefn adjacency_matrix(&self) -> FixedBitSet
fn adjacency_matrix(&self) -> FixedBitSet
sourcefn is_adjacent(
&self,
matrix: &FixedBitSet,
a: NodeIndex<Ix>,
b: NodeIndex<Ix>
) -> bool
fn is_adjacent(
&self,
matrix: &FixedBitSet,
a: NodeIndex<Ix>,
b: NodeIndex<Ix>
) -> bool
sourceimpl<N, E, Ty, Ix> GraphProp for Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
impl<N, E, Ty, Ix> GraphProp for Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
type EdgeType = Ty
type EdgeType = Ty
fn is_directed(&self) -> bool
sourceimpl<'a, N, E, Ty, Ix> IntoEdgeReferences for &'a Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
impl<'a, N, E, Ty, Ix> IntoEdgeReferences for &'a Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
type EdgeRef = EdgeReference<'a, E, Ty, Ix>
type EdgeReferences = EdgeReferences<'a, E, Ty, Ix>
fn edge_references(self) -> Self::EdgeReferences
sourceimpl<'a, N, E, Ty, Ix> IntoNeighbors for &'a Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
impl<'a, N, E, Ty, Ix> IntoNeighbors for &'a Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
sourcefn neighbors(self, a: Self::NodeId) -> Self::Neighbors
fn neighbors(self, a: Self::NodeId) -> Self::Neighbors
Return an iterator of all neighbors of a
.
Directed
: Targets of outgoing edges froma
.Undirected
: Opposing endpoints of all edges connected toa
.
Panics if the node a
does not exist.
Iterator element type is NodeIndex<Ix>
.
type Neighbors = Neighbors<'a, Ix>
sourceimpl<'a, N, E, Ty, Ix> IntoNodeIdentifiers for &'a Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
impl<'a, N, E, Ty, Ix> IntoNodeIdentifiers for &'a Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
type NodeIdentifiers = NodeIdentifiers<Ix>
fn node_identifiers(self) -> Self::NodeIdentifiers
sourceimpl<'a, N, E, Ty, Ix> IntoNodeReferences for &'a Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
impl<'a, N, E, Ty, Ix> IntoNodeReferences for &'a Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
type NodeRef = (Ix, &'a N)
type NodeReferences = NodeReferences<'a, N, Ix>
fn node_references(self) -> Self::NodeReferences
sourceimpl<N, E, Ty, Ix> NodeCount for Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
impl<N, E, Ty, Ix> NodeCount for Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
fn node_count(&self) -> usize
sourceimpl<N, E, Ty, Ix> NodeIndexable for Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
impl<N, E, Ty, Ix> NodeIndexable for Csr<N, E, Ty, Ix>where
Ty: EdgeType,
Ix: IndexType,
sourcefn node_bound(&self) -> usize
fn node_bound(&self) -> usize
sourcefn from_index(&self, ix: usize) -> Self::NodeId
fn from_index(&self, ix: usize) -> Self::NodeId
i
to a node index. i
must be a valid value in the graph.