Skip to content

Implement Community Detection (Louvain/Leiden) for Graphs #22

@noahgift

Description

@noahgift

Problem Statement

Community detection algorithms identify densely connected groups (communities) in networks. Currently missing from aprender's graph module.

Use Cases:

  • Social network analysis (find friend groups)
  • Protein interaction networks (functional modules)
  • Citation networks (research topics)
  • Web page clustering
  • Recommendation systems

Algorithms:

  • Louvain: Fast, greedy modularity optimization
  • Leiden: Improved version (fixes disconnected communities)

Proposed Solution

Implement Louvain and Leiden algorithms in the graph module with EXTREME TDD.

Algorithm (Louvain)

Modularity Optimization:

  • Measures density of edges within communities vs between
  • Q = (1/2m) Σ[A_ij - k_i*k_j/2m] δ(c_i, c_j)

Steps:

  1. Phase 1: Each node in own community
    • For each node: try moving to neighbor communities
    • Accept move if modularity increases
    • Repeat until no improvement
  2. Phase 2: Build new graph where nodes = communities
    • Repeat Phase 1 on new graph
  3. Continue until modularity converges

Implementation

API Design:

// In src/graph/mod.rs

pub struct Community {
    pub nodes: Vec<NodeId>,
    pub modularity: f64,
}

impl Graph {
    pub fn louvain(&self) -> Vec<Community>;
    pub fn leiden(&self, resolution: f64) -> Vec<Community>;
    pub fn modularity(&self, communities: &[Vec<NodeId>]) -> f64;
}

Success Criteria

  • ✅ Louvain algorithm implementation
  • ✅ Leiden algorithm implementation (optional, better quality)
  • ✅ Modularity computation
  • ✅ 15+ tests (including known community structures)
  • ✅ Zero clippy warnings
  • ✅ Example: examples/community_detection.rs

Estimated Effort

Timeline: 4-5 days
Complexity: Medium-High (modularity optimization, hierarchical structure)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions