1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! 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
}
}