leiden-rs 0.6.0

High-performance Leiden community detection algorithm for graphs in Rust
Documentation
//! Basic example: detect communities in a small graph using leiden-rs.
//!
//! Run with: cargo run --example basic

use gryf::{core::marker::Undirected, Graph};
use leiden_rs::{Leiden, LeidenConfig};

fn main() {
    // Create an undirected weighted graph
    let mut graph: Graph<i32, f64, Undirected> = Graph::new_undirected();

    // Add vertices (values are arbitrary)
    let v0 = graph.add_vertex(0);
    let v1 = graph.add_vertex(1);
    let v2 = graph.add_vertex(2);
    let v3 = graph.add_vertex(3);
    let v4 = graph.add_vertex(4);
    let v5 = graph.add_vertex(5);

    // Cluster 1: v0, v1, v2 (strongly connected)
    graph.add_edge(&v0, &v1, 1.0);
    graph.add_edge(&v1, &v2, 1.0);
    graph.add_edge(&v0, &v2, 1.0);

    // Cluster 2: v3, v4, v5 (strongly connected)
    graph.add_edge(&v3, &v4, 1.0);
    graph.add_edge(&v4, &v5, 1.0);
    graph.add_edge(&v3, &v5, 1.0);

    // Weak bridge between clusters
    graph.add_edge(&v2, &v3, 0.1);

    // Run Leiden algorithm with default settings
    let leiden = Leiden::new(LeidenConfig::default());
    let result = leiden.run(&graph).expect("leiden failed");

    println!("Graph has {} nodes", graph.vertex_count());
    println!(
        "Detected {} communities (quality: {:.4})",
        result.partition.num_communities(),
        result.quality
    );
    println!();

    for node in 0..graph.vertex_count() {
        println!(
            "  Node {} → Community {}",
            node,
            result.partition.community_of(node)
        );
    }
}