| Crates.io | fa-leiden-cd |
| lib.rs | fa-leiden-cd |
| version | 0.1.0 |
| created_at | 2025-08-27 07:45:26.152711+00 |
| updated_at | 2025-08-27 07:45:26.152711+00 |
| description | A Rust implementation of the Leiden algorithm for community detection in large networks. |
| homepage | https://github.com/fixed-ai/fa-leiden-cd |
| repository | https://github.com/fixed-ai/fa-leiden-cd |
| max_upload_size | |
| id | 1812268 |
| size | 36,527 |
fa-leiden-cd is a Rust implementation of the Leiden algorithm for community detection in large networks.
cargo add fa-leiden-cd
use fa_leiden_cd::{Graph, TrivialModularityOptimizer};
fn test() {
let mut nodes: HashMap<&'static str, usize> = HashMap::new();
let mut g = Graph::new();
let edges: &[(&'static str, &'static str, f32)] = &[
("Fortran", "C", 0.5),
("Fortran", "LISP", 0.3),
("Fortran", "MATLAB", 0.6),
("C", "C++", 0.9),
("C", "Go", 0.6),
("LISP", "ML", 0.5),
("LISP", "OCaml", 0.2),
("LISP", "Haskell", 0.2),
("LISP", "Ruby", 0.5),
("LISP", "Julia", 0.6),
("ML", "OCaml", 0.8),
("ML", "Haskell", 0.5),
("OCaml", "Haskell", 0.3),
("OCaml", "F#", 0.6),
("Haskell", "Julia", 0.2),
("C++", "Python", 0.32),
("C++", "Ruby", 0.2),
("C++", "C#", 0.5),
("Python", "F#", 0.2),
("Python", "Julia", 0.4),
("C#", "F#", 0.3),
];
for (from, to, weight) in edges.iter() {
let from_id = *nodes.entry(from).or_insert_with(|| g.add_node(from));
let to_id = *nodes.entry(to).or_insert_with(|| g.add_node(to));
g.add_edge(from_id, to_id, (), *weight);
}
let mut optimizer = TrivialModularityOptimizer {
parallel_scale: 128,
tol: 1e-11,
};
let hierarchy = g.leiden(Some(100), &mut optimizer);
for (i, node) in hierarchy.node_data_slice().iter().enumerate() {
println!("community {}:", i);
node.collect_nodes(&|i| {
let n = g.node_data_slice()[i];
println!(" {}", n);
});
}
}
// community 0:
// Haskell
// ML
// F#
// OCaml
// community 1:
// Python
// Julia
// community 2:
// Fortran
// MATLAB
// Go
// C
// C#
// C++
// community 3:
// Ruby
// LISP
This project is licensed under the MIT License - see the LICENSE file for details.