leiden-rs 0.6.0

High-performance Leiden community detection algorithm for graphs in Rust
Documentation
//! Hierarchical community detection output.

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::partition::Partition;

/// A single level in the hierarchical community detection output.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct HierarchyLevel {
    /// Number of nodes at this aggregation level.
    pub node_count: usize,
    /// Number of communities at this level.
    pub num_communities: usize,
    /// Quality score at this level.
    pub quality: f64,
    /// Community membership of each original node at this level.
    pub membership: Vec<usize>,
}

/// Hierarchical output from the Leiden algorithm, capturing intermediate
/// community structure at each aggregation level.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct HierarchicalOutput {
    /// Community structure at each aggregation level.
    pub levels: Vec<HierarchyLevel>,
    /// The final community partition (same as `run()` would produce).
    pub partition: Partition,
    /// Quality score of the final partition.
    pub quality: f64,
}

impl HierarchicalOutput {
    /// Number of hierarchy levels recorded.
    pub fn num_levels(&self) -> usize {
        self.levels.len()
    }

    /// Get community of original node at a specific level.
    ///
    /// Level 0 = finest granularity, last level = coarsest.
    pub fn community_of_at_level(&self, node: usize, level: usize) -> usize {
        self.levels[level].membership[node]
    }

    /// Get the membership vector at a specific level.
    pub fn membership_at_level(&self, level: usize) -> &[usize] {
        &self.levels[level].membership
    }
}