pub struct Csr<N = (), E = (), Ty = Directed, Ix = u32> { /* private fields */ }
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§
source§impl<N, E, Ty, Ix> Csr<N, E, Ty, Ix>
impl<N, E, Ty, Ix> Csr<N, E, Ty, Ix>
sourcepub fn with_nodes(n: usize) -> Csr<N, E, Ty, Ix>where
N: Default,
pub fn with_nodes(n: usize) -> Csr<N, E, Ty, Ix>where
N: Default,
source§impl<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<Csr<N, E, Directed, Ix>, EdgesNotSorted>
pub fn from_sorted_edges<Edge>( edges: &[Edge] ) -> Result<Csr<N, E, Directed, Ix>, EdgesNotSorted>
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),
]);
source§impl<N, E, Ty, Ix> Csr<N, E, Ty, Ix>
impl<N, E, Ty, Ix> Csr<N, E, Ty, Ix>
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) -> Ix
pub fn add_node(&mut self, weight: N) -> Ix
Adds a new node with the given weight, returning the corresponding node index.
sourcepub fn add_edge(&mut self, a: Ix, b: Ix, weight: E) -> boolwhere
E: Clone,
pub fn add_edge(&mut self, a: Ix, b: 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: Ix, b: Ix) -> bool
pub fn contains_edge(&self, a: Ix, b: Ix) -> bool
Computes in O(log |V|) time.
Panics if the node a
does not exist.
sourcepub fn out_degree(&self, a: Ix) -> usize
pub fn out_degree(&self, a: Ix) -> usize
Computes in O(1) time.
Panics if the node a
does not exist.
sourcepub fn neighbors_slice(&self, a: Ix) -> &[Ix]
pub fn neighbors_slice(&self, a: Ix) -> &[Ix]
Computes in O(1) time.
Panics if the node a
does not exist.
sourcepub fn edges_slice(&self, a: Ix) -> &[E]
pub fn edges_slice(&self, a: Ix) -> &[E]
Computes in O(1) time.
Panics if the node a
does not exist.
Trait Implementations§
source§impl<N, E, Ty, Ix> Data for Csr<N, E, Ty, Ix>
impl<N, E, Ty, Ix> Data for Csr<N, E, Ty, Ix>
type NodeWeight = N
type EdgeWeight = E
source§impl<N, E, Ty, Ix> EdgeCount for Csr<N, E, Ty, Ix>
impl<N, E, Ty, Ix> EdgeCount for Csr<N, E, Ty, Ix>
source§fn edge_count(&self) -> usize
fn edge_count(&self) -> usize
source§impl<'a, N, E, Ty, Ix> GetAdjacencyMatrix for &'a Csr<N, E, Ty, Ix>
impl<'a, N, E, Ty, Ix> GetAdjacencyMatrix for &'a Csr<N, E, Ty, Ix>
The adjacency matrix for Csr is a bitmap that’s computed by
.adjacency_matrix()
.
§type AdjMatrix = FixedBitSet
type AdjMatrix = FixedBitSet
source§fn adjacency_matrix(&self) -> FixedBitSet
fn adjacency_matrix(&self) -> FixedBitSet
source§fn is_adjacent(&self, matrix: &FixedBitSet, a: Ix, b: Ix) -> bool
fn is_adjacent(&self, matrix: &FixedBitSet, a: Ix, b: Ix) -> bool
source§impl<'a, N, E, Ty, Ix> IntoEdgeReferences for &'a Csr<N, E, Ty, Ix>
impl<'a, N, E, Ty, Ix> IntoEdgeReferences for &'a Csr<N, E, Ty, Ix>
type EdgeRef = EdgeReference<'a, E, Ty, Ix>
type EdgeReferences = EdgeReferences<'a, E, Ty, Ix>
fn edge_references( self ) -> <&'a Csr<N, E, Ty, Ix> as IntoEdgeReferences>::EdgeReferences
source§impl<'a, N, E, Ty, Ix> IntoNeighbors for &'a Csr<N, E, Ty, Ix>
impl<'a, N, E, Ty, Ix> IntoNeighbors for &'a Csr<N, E, Ty, Ix>
source§fn neighbors(
self,
a: <&'a Csr<N, E, Ty, Ix> as GraphBase>::NodeId
) -> <&'a Csr<N, E, Ty, Ix> as IntoNeighbors>::Neighbors
fn neighbors( self, a: <&'a Csr<N, E, Ty, Ix> as GraphBase>::NodeId ) -> <&'a Csr<N, E, Ty, Ix> as IntoNeighbors>::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>
source§impl<'a, N, E, Ty, Ix> IntoNodeIdentifiers for &'a Csr<N, E, Ty, Ix>
impl<'a, N, E, Ty, Ix> IntoNodeIdentifiers for &'a Csr<N, E, Ty, Ix>
type NodeIdentifiers = NodeIdentifiers<Ix>
fn node_identifiers( self ) -> <&'a Csr<N, E, Ty, Ix> as IntoNodeIdentifiers>::NodeIdentifiers
source§impl<'a, N, E, Ty, Ix> IntoNodeReferences for &'a Csr<N, E, Ty, Ix>
impl<'a, N, E, Ty, Ix> IntoNodeReferences for &'a Csr<N, E, Ty, Ix>
type NodeRef = (Ix, &'a N)
type NodeReferences = NodeReferences<'a, N, Ix>
fn node_references( self ) -> <&'a Csr<N, E, Ty, Ix> as IntoNodeReferences>::NodeReferences
source§impl<N, E, Ty, Ix> NodeIndexable for Csr<N, E, Ty, Ix>
impl<N, E, Ty, Ix> NodeIndexable for Csr<N, E, Ty, Ix>
impl<N, E, Ty, Ix> NodeCompactIndexable for Csr<N, E, Ty, Ix>
Auto Trait Implementations§
impl<N, E, Ty, Ix> RefUnwindSafe for Csr<N, E, Ty, Ix>
impl<N, E, Ty, Ix> Send for Csr<N, E, Ty, Ix>
impl<N, E, Ty, Ix> Sync for Csr<N, E, Ty, Ix>
impl<N, E, Ty, Ix> Unpin for Csr<N, E, Ty, Ix>
impl<N, E, Ty, Ix> UnwindSafe for Csr<N, E, Ty, Ix>
Blanket Implementations§
source§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
source§fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
T
ShaderType
for self
. When used in AsBindGroup
derives, it is safe to assume that all images in self
exist.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> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Self
using data from the given World
.