petgraph::csr

Struct Csr

Source
pub struct Csr<N = (), E = (), Ty = Directed, Ix = DefaultIx> { /* 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() -> Self

Create an empty Csr.

Source

pub fn with_nodes(n: usize) -> Self
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<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),
]);
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) -> NodeIndex<Ix>

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

Source

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

Computes in O(log |V|) time.

Panics if the node a does not exist.

Source

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

Computes in O(1) time.

Panics if the node a does not exist.

Source

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

Computes in O(1) time.

Panics if the node a does not exist.

Source

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

Computes in O(1) time.

Panics if the node a does not exist.

Source

pub fn edges(&self, a: NodeIndex<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: Clone, E: Clone, Ty, Ix: Clone> Clone for Csr<N, E, Ty, Ix>

Source§

fn clone(&self) -> Self

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,

Source§

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

Source§

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

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() -> Self

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

Source§

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: NodeIndex<Ix>, b: NodeIndex<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,

Source§

type NodeId = Ix

node identifier
Source§

type EdgeId = usize

edge identifier
Source§

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

Source§

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,

Source§

type Output = N

The returned type after indexing.
Source§

fn index(&self, ix: NodeIndex<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: NodeIndex<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,

Source§

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

Source§

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

Source§

fn edges(self, a: Self::NodeId) -> Self::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: Self::NodeId) -> Self::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>.

Source§

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,

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: Self::NodeId) -> usize

Convert a to an integer index.
Source§

fn from_index(&self, ix: usize) -> Self::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,

Source§

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 Self::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> Freeze for Csr<N, E, Ty, Ix>

§

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

§

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

§

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

§

impl<N, E, Ty, Ix> UnwindSafe for Csr<N, E, Ty, Ix>
where Ty: UnwindSafe, Ix: UnwindSafe, E: UnwindSafe, N: 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> 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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.