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 and E 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>
where Ty: EdgeType, Ix: IndexType,

source

pub fn new() -> Csr<N, E, Ty, Ix>

Create an empty Csr.

source

pub fn with_nodes(n: usize) -> Csr<N, E, Ty, Ix>
where N: Default,

Create a new Csr with n nodes. N must implement Default for the weight of each node.

Example
use petgraph::csr::Csr;
use petgraph::prelude::*;

let graph = Csr::<u8,()>::with_nodes(5);
assert_eq!(graph.node_count(),5);
assert_eq!(graph.edge_count(),0);

assert_eq!(graph[0],0);
assert_eq!(graph[4],0);
source§

impl<N, E, Ix> Csr<N, E, Directed, Ix>
where Ix: IndexType,

source

pub fn from_sorted_edges<Edge>( edges: &[Edge] ) -> Result<Csr<N, E, Directed, Ix>, EdgesNotSorted>
where Edge: Clone + IntoWeightedEdge<E, NodeId = 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),
]);
source§

impl<N, E, Ty, Ix> Csr<N, E, Ty, Ix>
where Ty: EdgeType, Ix: IndexType,

source

pub fn node_count(&self) -> usize

source

pub fn edge_count(&self) -> usize

source

pub fn is_directed(&self) -> bool

source

pub fn clear_edges(&mut self)

Remove all edges

source

pub fn add_node(&mut self, weight: N) -> Ix

Adds a new node with the given weight, returning the corresponding node index.

source

pub fn add_edge(&mut self, a: Ix, b: Ix, weight: E) -> bool
where 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.

source

pub fn contains_edge(&self, a: Ix, b: Ix) -> bool

Computes in O(log |V|) time.

Panics if the node a does not exist.

source

pub fn out_degree(&self, a: Ix) -> usize

Computes in O(1) time.

Panics if the node a does not exist.

source

pub fn neighbors_slice(&self, a: Ix) -> &[Ix]

Computes in O(1) time.

Panics if the node a does not exist.

source

pub fn edges_slice(&self, a: Ix) -> &[E]

Computes in O(1) time.

Panics if the node a does not exist.

source

pub fn edges(&self, a: Ix) -> Edges<'_, E, Ty, Ix>

Return an iterator of all edges of a.

  • Directed: Outgoing edges from a.
  • Undirected: All edges connected to a.

Panics if the node a does not exist.
Iterator element type is EdgeReference<E, Ty, Ix>.

Trait Implementations§

source§

impl<N, E, Ty, Ix> Clone for Csr<N, E, Ty, Ix>
where N: Clone, E: Clone, Ix: Clone,

source§

fn clone(&self) -> Csr<N, E, Ty, Ix>

Returns a copy 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<N, E, Ty, Ix> Data for Csr<N, E, Ty, Ix>
where Ty: EdgeType, Ix: IndexType,

§

type NodeWeight = N

§

type EdgeWeight = E

source§

impl<N, E, Ty, Ix> Debug for Csr<N, E, Ty, Ix>
where N: Debug, E: Debug, Ty: Debug, Ix: Debug,

source§

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

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

impl<N, E, Ty, Ix> Default for Csr<N, E, Ty, Ix>
where Ty: EdgeType, Ix: IndexType,

source§

fn default() -> Csr<N, E, Ty, Ix>

Returns the “default value” for a type. Read more
source§

impl<N, E, Ty, Ix> EdgeCount for Csr<N, E, Ty, Ix>
where Ty: EdgeType, Ix: IndexType,

source§

fn edge_count(&self) -> usize

Return the number of edges in the graph.
source§

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

The associated adjacency matrix type
source§

fn adjacency_matrix(&self) -> FixedBitSet

Create the adjacency matrix
source§

fn is_adjacent(&self, matrix: &FixedBitSet, a: Ix, b: Ix) -> bool

Return true if there is an edge from a to b, false otherwise. Read more
source§

impl<N, E, Ty, Ix> GraphBase for Csr<N, E, Ty, Ix>
where Ty: EdgeType, Ix: IndexType,

§

type NodeId = Ix

node identifier
§

type EdgeId = usize

edge identifier
source§

impl<N, E, Ty, Ix> GraphProp for Csr<N, E, Ty, Ix>
where Ty: EdgeType, Ix: IndexType,

§

type EdgeType = Ty

The kind of edges in the graph.
source§

fn is_directed(&self) -> bool

source§

impl<N, E, Ty, Ix> Index<Ix> for Csr<N, E, Ty, Ix>
where Ty: EdgeType, Ix: IndexType,

§

type Output = N

The returned type after indexing.
source§

fn index(&self, ix: Ix) -> &N

Performs the indexing (container[index]) operation. Read more
source§

impl<N, E, Ty, Ix> IndexMut<Ix> for Csr<N, E, Ty, Ix>
where Ty: EdgeType, Ix: IndexType,

source§

fn index_mut(&mut self, ix: Ix) -> &mut N

Performs the mutable indexing (container[index]) operation. Read more
source§

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>

source§

fn edge_references( self ) -> <&'a Csr<N, E, Ty, Ix> as IntoEdgeReferences>::EdgeReferences

source§

impl<'a, N, E, Ty, Ix> IntoEdges for &'a Csr<N, E, Ty, Ix>
where Ty: EdgeType, Ix: IndexType,

§

type Edges = Edges<'a, E, Ty, Ix>

source§

fn edges( self, a: <&'a Csr<N, E, Ty, Ix> as GraphBase>::NodeId ) -> <&'a Csr<N, E, Ty, Ix> as IntoEdges>::Edges

source§

impl<'a, N, E, Ty, Ix> IntoNeighbors for &'a Csr<N, E, Ty, Ix>
where Ty: EdgeType, Ix: IndexType,

source§

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 from a.
  • Undirected: Opposing endpoints of all edges connected to a.

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>
where Ty: EdgeType, Ix: IndexType,

source§

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>

source§

fn node_references( self ) -> <&'a Csr<N, E, Ty, Ix> as IntoNodeReferences>::NodeReferences

source§

impl<N, E, Ty, Ix> NodeCount for Csr<N, E, Ty, Ix>
where Ty: EdgeType, Ix: IndexType,

source§

impl<N, E, Ty, Ix> NodeIndexable for Csr<N, E, Ty, Ix>
where Ty: EdgeType, Ix: IndexType,

source§

fn node_bound(&self) -> usize

Return an upper bound of the node indices in the graph (suitable for the size of a bitmap).
source§

fn to_index(&self, a: <Csr<N, E, Ty, Ix> as GraphBase>::NodeId) -> usize

Convert a to an integer index.
source§

fn from_index(&self, ix: usize) -> <Csr<N, E, Ty, Ix> as GraphBase>::NodeId

Convert i to a node index. i must be a valid value in the graph.
source§

impl<N, E, Ty, Ix> Visitable for Csr<N, E, Ty, Ix>
where Ty: EdgeType, Ix: IndexType,

§

type Map = FixedBitSet

The associated map type
source§

fn visit_map(&self) -> FixedBitSet

Create a new visitor map
source§

fn reset_map(&self, map: &mut <Csr<N, E, Ty, Ix> as Visitable>::Map)

Reset the visitor map (and resize to new size of graph if needed)
source§

impl<N, E, Ty, Ix> NodeCompactIndexable for Csr<N, E, Ty, Ix>
where Ty: EdgeType, Ix: IndexType,

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>
where E: Send, Ix: Send, N: Send, Ty: Send,

§

impl<N, E, Ty, Ix> Sync for Csr<N, E, Ty, Ix>
where E: Sync, Ix: Sync, N: Sync, Ty: Sync,

§

impl<N, E, Ty, Ix> Unpin for Csr<N, E, Ty, Ix>
where E: Unpin, Ix: Unpin, N: Unpin, Ty: Unpin,

§

impl<N, E, Ty, Ix> UnwindSafe for Csr<N, E, Ty, Ix>
where E: UnwindSafe, Ix: UnwindSafe, N: UnwindSafe, Ty: UnwindSafe,

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, U> AsBindGroupShaderType<U> for T
where U: ShaderType, &'a T: for<'a> Into<U>,

source§

fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U

Return the T ShaderType for self. When used in AsBindGroup derives, it is safe to assume that all images in self exist.
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> Downcast<T> for T

source§

fn downcast(&self) -> &T

source§

impl<T> Downcast for T
where T: Any,

source§

fn into_any(self: Box<T>) -> Box<dyn Any>

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

Convert 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)

Convert &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)

Convert &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
where T: Any + Send + Sync,

source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<S> FromSample<S> for S

source§

fn from_sample_(s: S) -> S

source§

impl<T> FromWorld for T
where T: Default,

source§

fn from_world(_world: &mut World) -> T

Creates Self using data from the given World.
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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,

§

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> ToSample<U> for T
where U: FromSample<T>,

source§

fn to_sample_(self) -> U

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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<T> TypeData for T
where T: 'static + Send + Sync + Clone,

source§

impl<T> Upcast<T> for T

source§

fn upcast(&self) -> Option<&T>

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

source§

impl<T> Settings for T
where T: 'static + Send + Sync,

source§

impl<T> WasmNotSend for T
where T: Send,

source§

impl<T> WasmNotSendSync for T

source§

impl<T> WasmNotSync for T
where T: Sync,