Graph

Struct Graph 

Source
pub struct Graph<T> { /* private fields */ }
Expand description

A simple undirected weighted graph stored as a sparse adjacency matrix.

The input is interpreted as undirected: whenever you add an edge (u, v) with u != v, we store it in both adjacency entries (u, v) and (v, u). Self-loops (u, u) are stored once as (u, u).

The Graph exposes two common “edge size” metrics that differ in meaning:

  • edge_entry_count: counts adjacency entries (non-zero matrix entries in the upper-triangular part of the matrix, i.e. we count each undirected edge once).
  • total_edge_weight: sums the weight values associated with those entries.

This distinction is important when edges can carry weights: many algorithms (and our quality metrics) need the total weight, not just the number of adjacency entries.

In particular, when we build an aggregated graph during Leiden (WeightedPartitionedGraph::aggregate), each “node” represents a set of original nodes (a community at the current level) and each adjacency weight represents the sum of weights of all original edges between the two sets. Therefore, edge_entry_count tells you only how many distinct neighbor-community pairs exist, while total_edge_weight preserves how strongly communities are connected.

Implementations§

Source§

impl<T: EdgeWeight> Graph<T>

Source

pub fn from_edges<I>(edges: I, num_nodes: NodeIdx) -> Self
where I: Iterator<Item = Edge<T>>,

Source

pub fn from_adjacency_matrix(adjacency_matrix: CsMat<T>) -> Self

Source

pub fn get_adjacency_matrix(&self) -> &CsMat<T>

Source

pub fn get_num_nodes(&self) -> usize

Source

pub fn get_edge_entry_count(&self) -> usize

Returns the number of undirected adjacency entries in the graph.

Each undirected edge contributes one entry (src <= dest), and self loops also contribute one entry.

Source

pub fn get_total_edge_weight(&self) -> usize

Returns the sum of all undirected edge weights in the graph.

The sum is taken over the same set of undirected adjacency entries as Self::get_edge_entry_count (upper triangle src <= dest, including diagonal/self-loops).

For unweighted graphs where every edge has weight 1, this equals Self::get_edge_entry_count.

Source

pub fn get_edge_weight(&self, src: NodeIdx, dst: NodeIdx) -> Option<T>

Source

pub fn get_norm(&self) -> f64

Source

pub fn get_degrees(&self) -> &Vec<usize>

Source

pub fn neighbors(&self, node: NodeIdx) -> Vec<NodeIdx>

Returns a list of nodes connected to node. This does not include node even when it is connected to itself.

Source

pub fn neighbors_iter( &self, node: NodeIdx, ) -> impl Iterator<Item = NodeIdx> + '_

Returns an iterator over node indices connected to node (excluding node itself).

This is the allocation-free counterpart of Self::neighbors, intended for tight loops.

Source

pub fn get_edges_from(&self, node: NodeIdx) -> Vec<Edge<T>>

Returns a list of edge connected to node, including self edges looping back

Source

pub fn edges_from_iter( &self, node: NodeIdx, ) -> impl Iterator<Item = Edge<T>> + '_

Returns an iterator over edges incident to node (including self-loops).

This is the allocation-free counterpart of Self::get_edges_from, intended for tight loops.

Source

pub fn get_edges_iter(&self) -> impl Iterator<Item = Edge<T>>

Iterate over all edges of the graph

NB: this will iterate over edges in both directions

Source

pub fn get_edges_in_selection( &self, selection: &HashSet<NodeIdx>, ) -> impl Iterator<Item = Edge<T>>

Get all edges where at least one of the nodes is in the selection Note that this will include edges that go outside the selection, and edges that loop back to the same node. Every egde will only be included once, i.e. (0,1) and (1,0) will not both be included as (0,1). The edges will be returned in arbitrary order.

Source

pub fn connected_components(&self) -> impl Iterator<Item = HashSet<NodeIdx>>

Returns connected components of the graph

Source

pub fn connected_components_by<F>( &self, filter: F, ) -> impl Iterator<Item = HashSet<NodeIdx>>
where F: Fn(NodeIdx, NodeIdx) -> bool,

Returns connected components where nodes in each components fulfill the filter criteria.

This criteria must be transitive, ie. filter(a, b) ∧ filter(b, c) ⇒ filter(a, c)

Trait Implementations§

Source§

impl<T: Clone> Clone for Graph<T>

Source§

fn clone(&self) -> Graph<T>

Returns a duplicate 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 From<Graph<u8>> for Graph<usize>

Source§

fn from(graph: Graph<u8>) -> Graph<usize>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> Freeze for Graph<T>

§

impl<T> RefUnwindSafe for Graph<T>
where T: RefUnwindSafe,

§

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

§

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

§

impl<T> Unpin for Graph<T>
where T: Unpin,

§

impl<T> UnwindSafe for Graph<T>
where T: 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, dest: *mut u8)

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

unsafe fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,