parti 0.1.0

Clustering and hierarchical structure primitives
Documentation
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
//! Leiden algorithm for community detection.
//!
//! An improvement over Louvain that guarantees well-connected communities.
//!
//! ## The Leiden Algorithm (Traag et al. 2019)
//!
//! Leiden fixes Louvain's fundamental flaw: Louvain can create disconnected
//! communities because it never re-examines decisions within a community.
//!
//! ### Three Phases
//!
//! 1. **Local Moving**: Like Louvain, greedily move nodes to best community.
//!
//! 2. **Refinement**: The key innovation. Within each community from phase 1:
//!    - Reset all nodes to singletons
//!    - Merge only within the community's boundary
//!    - Check that each merge maintains connectivity
//!
//! 3. **Aggregation**: Build meta-graph and recurse.
//!
//! ### Why Refinement Matters
//!
//! ```text
//! Louvain can produce:        Leiden guarantees:
//!     A---B                       A---B
//!         |                           |
//!     C   D                       C   D
//!                                 (C in separate community)
//! [A,B,C,D] all in one         [A,B,D] connected, [C] alone
//! community despite C          
//! being disconnected!          
//! ```
//!
//! ## Complexity
//!
//! - Time: O(m) per iteration (m = edges), typically O(m log n) total
//! - Space: O(n + m)
//!
//! ## References
//!
//! Traag, Waltman, van Eck (2019). "From Louvain to Leiden: guaranteeing
//! well-connected communities." Scientific Reports 9, 5233.

use super::traits::CommunityDetection;
use crate::error::{Error, Result};
use petgraph::graph::UnGraph;
use petgraph::visit::EdgeRef;
use std::collections::{HashMap, HashSet, VecDeque};

/// Leiden community detection algorithm.
///
/// Guarantees well-connected communities through a refinement phase
/// that Louvain lacks.
#[derive(Debug, Clone)]
pub struct Leiden {
    /// Resolution parameter (gamma). Higher = smaller communities.
    resolution: f64,
    /// Maximum iterations per phase.
    max_iter: usize,
    /// Minimum modularity gain to continue.
    min_gain: f64,
    /// Random seed for tie-breaking.
    seed: u64,
}

impl Leiden {
    /// Create a new Leiden detector.
    pub fn new() -> Self {
        Self {
            resolution: 1.0,
            max_iter: 100,
            min_gain: 1e-7,
            seed: 42,
        }
    }

    /// Set resolution parameter.
    ///
    /// Higher values produce smaller communities.
    pub fn with_resolution(mut self, resolution: f64) -> Self {
        self.resolution = resolution;
        self
    }

    /// Set maximum iterations.
    pub fn with_max_iter(mut self, max_iter: usize) -> Self {
        self.max_iter = max_iter;
        self
    }

    /// Set minimum modularity gain threshold.
    pub fn with_min_gain(mut self, min_gain: f64) -> Self {
        self.min_gain = min_gain;
        self
    }

    /// Set random seed.
    pub fn with_seed(mut self, seed: u64) -> Self {
        self.seed = seed;
        self
    }

    /// Deprecated: use `with_max_iter` instead.
    #[deprecated(since = "0.2.0", note = "Use with_max_iter instead")]
    pub fn with_refinement(self, n: usize) -> Self {
        self.with_max_iter(n)
    }
}

impl Default for Leiden {
    fn default() -> Self {
        Self::new()
    }
}

/// Internal graph representation for weighted operations.
struct WeightedGraph {
    n: usize,
    /// Adjacency: node -> [(neighbor, weight)]
    adj: Vec<Vec<(usize, f64)>>,
    /// Weighted degree of each node
    degrees: Vec<f64>,
    /// Total edge weight (2m in modularity formula)
    total_weight: f64,
}

impl WeightedGraph {
    fn from_edges(n: usize, edges: &[(usize, usize, f64)]) -> Self {
        let mut adj = vec![Vec::new(); n];
        let mut degrees = vec![0.0; n];
        let mut total_weight = 0.0;

        for &(i, j, w) in edges {
            adj[i].push((j, w));
            adj[j].push((i, w));
            degrees[i] += w;
            degrees[j] += w;
            total_weight += 2.0 * w; // Each edge counted twice
        }

        Self {
            n,
            adj,
            degrees,
            total_weight,
        }
    }

    /// Compute modularity gain from moving node to target community.
    ///
    /// delta_Q = k_i,in / m - gamma * sigma_tot * k_i / (2m^2)
    fn modularity_gain(
        &self,
        node: usize,
        target_comm: usize,
        communities: &[usize],
        comm_total_weight: &[f64],
        resolution: f64,
    ) -> f64 {
        if self.total_weight == 0.0 {
            return 0.0;
        }

        let m = self.total_weight / 2.0;
        let ki = self.degrees[node];

        // Sum of edge weights from node to target community
        let ki_in: f64 = self.adj[node]
            .iter()
            .filter(|(neighbor, _)| communities[*neighbor] == target_comm)
            .map(|(_, w)| w)
            .sum();

        let sigma_tot = comm_total_weight[target_comm];

        ki_in / m - resolution * sigma_tot * ki / (2.0 * m * m)
    }
}

/// Community assignment with cached statistics.
struct CommunityState {
    /// Community assignment for each node.
    assignment: Vec<usize>,
    /// Total weighted degree in each community.
    comm_total_weight: Vec<f64>,
    /// Number of communities (some may be empty).
    n_communities: usize,
}

impl CommunityState {
    fn new_singletons(n: usize, degrees: &[f64]) -> Self {
        Self {
            assignment: (0..n).collect(),
            comm_total_weight: degrees.to_vec(),
            n_communities: n,
        }
    }

    fn move_node(&mut self, node: usize, from: usize, to: usize, degree: f64) {
        self.assignment[node] = to;
        self.comm_total_weight[from] -= degree;
        self.comm_total_weight[to] += degree;
    }
}

impl CommunityDetection for Leiden {
    fn detect<N, E>(&self, graph: &UnGraph<N, E>) -> Result<Vec<usize>> {
        let n = graph.node_count();
        if n == 0 {
            return Err(Error::EmptyInput);
        }

        if graph.edge_count() == 0 {
            return Ok((0..n).collect());
        }

        // Convert to weighted edge list
        let edges: Vec<(usize, usize, f64)> = graph
            .edge_references()
            .filter_map(|e| {
                let i = e.source().index();
                let j = e.target().index();
                if i < j {
                    Some((i, j, 1.0))
                } else {
                    None
                }
            })
            .collect();

        let wg = WeightedGraph::from_edges(n, &edges);
        let mut state = CommunityState::new_singletons(n, &wg.degrees);

        // Main Leiden loop
        for _level in 0..self.max_iter {
            // Phase 1: Local moving
            let improved = self.local_moving_phase(&wg, &mut state);
            if !improved {
                break;
            }

            // Phase 2: Refinement (the key Leiden innovation)
            self.refinement_phase(&wg, &mut state);
        }

        // Renumber communities consecutively
        Ok(renumber_communities(&state.assignment))
    }

    fn resolution(&self) -> f64 {
        self.resolution
    }
}

impl Leiden {
    /// Phase 1: Local moving (similar to Louvain).
    ///
    /// Greedily move nodes to neighboring communities with best modularity gain.
    fn local_moving_phase(&self, wg: &WeightedGraph, state: &mut CommunityState) -> bool {
        let mut improved = false;
        let mut queue: VecDeque<usize> = (0..wg.n).collect();
        let mut in_queue = vec![true; wg.n];

        while let Some(node) = queue.pop_front() {
            in_queue[node] = false;
            let current_comm = state.assignment[node];

            // Find neighboring communities
            let mut neighbor_comms: HashSet<usize> = HashSet::new();
            for &(neighbor, _) in &wg.adj[node] {
                let _ = neighbor_comms.insert(state.assignment[neighbor]);
            }
            let _ = neighbor_comms.insert(current_comm);

            // Find best community
            let mut best_comm = current_comm;
            let mut best_gain = 0.0;

            // Temporarily remove node from current community
            state.comm_total_weight[current_comm] -= wg.degrees[node];

            for &target_comm in &neighbor_comms {
                let gain = wg.modularity_gain(
                    node,
                    target_comm,
                    &state.assignment,
                    &state.comm_total_weight,
                    self.resolution,
                );
                if gain > best_gain + 1e-10 {
                    best_gain = gain;
                    best_comm = target_comm;
                }
            }

            // Restore node to community (will be moved below if needed)
            state.comm_total_weight[current_comm] += wg.degrees[node];

            if best_comm != current_comm {
                state.move_node(node, current_comm, best_comm, wg.degrees[node]);
                improved = true;

                // Add neighbors back to queue
                for &(neighbor, _) in &wg.adj[node] {
                    if !in_queue[neighbor] {
                        queue.push_back(neighbor);
                        in_queue[neighbor] = true;
                    }
                }
            }
        }

        improved
    }

    /// Phase 2: Refinement (Leiden's key innovation).
    ///
    /// Within each community from phase 1:
    /// 1. Reset nodes to singletons
    /// 2. Merge only within the community boundary
    /// 3. Ensure each sub-community is connected
    fn refinement_phase(&self, wg: &WeightedGraph, state: &mut CommunityState) {
        // Get current partition (from phase 1)
        let phase1_communities = state.assignment.clone();

        // Find unique communities
        let mut unique_comms: Vec<usize> = phase1_communities.to_vec();
        unique_comms.sort_unstable();
        unique_comms.dedup();

        // Process each community independently
        for &comm in &unique_comms {
            // Get nodes in this community
            let nodes_in_comm: Vec<usize> = (0..wg.n)
                .filter(|&i| phase1_communities[i] == comm)
                .collect();

            if nodes_in_comm.len() <= 1 {
                continue;
            }

            // Refine within this community
            self.refine_community(wg, state, &nodes_in_comm);
        }
    }

    /// Refine a single community.
    ///
    /// Ensures the community is well-connected by:
    /// 1. Finding connected components within the community
    /// 2. Splitting disconnected components into separate communities
    fn refine_community(&self, wg: &WeightedGraph, state: &mut CommunityState, nodes: &[usize]) {
        let node_set: HashSet<usize> = nodes.iter().copied().collect();

        // Find connected components within this subset
        let components = find_components_in_subset(wg, &node_set);

        if components.len() <= 1 {
            // Already connected, nothing to do
            return;
        }

        // Split disconnected components into separate communities
        let base_comm = state.n_communities;
        state.n_communities += components.len() - 1;

        // Extend comm_total_weight if needed
        while state.comm_total_weight.len() < state.n_communities {
            state.comm_total_weight.push(0.0);
        }

        // Keep first component in original community, assign others to new communities
        for (idx, component) in components.iter().enumerate().skip(1) {
            let new_comm = base_comm + idx - 1;
            for &node in component {
                let old_comm = state.assignment[node];
                state.move_node(node, old_comm, new_comm, wg.degrees[node]);
            }
        }
    }
}

/// Find connected components within a subset of nodes.
fn find_components_in_subset(wg: &WeightedGraph, node_set: &HashSet<usize>) -> Vec<Vec<usize>> {
    let mut visited = HashSet::new();
    let mut components = Vec::new();

    for &start in node_set {
        if visited.contains(&start) {
            continue;
        }

        let mut component = Vec::new();
        let mut queue = VecDeque::new();
        queue.push_back(start);

        while let Some(node) = queue.pop_front() {
            if !visited.insert(node) {
                continue;
            }
            component.push(node);

            for &(neighbor, _) in &wg.adj[node] {
                if node_set.contains(&neighbor) && !visited.contains(&neighbor) {
                    queue.push_back(neighbor);
                }
            }
        }

        if !component.is_empty() {
            components.push(component);
        }
    }

    components
}

/// Renumber communities to consecutive integers starting at 0.
fn renumber_communities(assignment: &[usize]) -> Vec<usize> {
    let mut unique: Vec<usize> = assignment.to_vec();
    unique.sort_unstable();
    unique.dedup();

    let mapping: HashMap<usize, usize> = unique
        .into_iter()
        .enumerate()
        .map(|(new, old)| (old, new))
        .collect();

    assignment
        .iter()
        .map(|&c| mapping.get(&c).copied().unwrap_or(0))
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_leiden_basic() {
        let mut graph = UnGraph::<(), ()>::new_undirected();
        let n0 = graph.add_node(());
        let n1 = graph.add_node(());
        let n2 = graph.add_node(());

        let _ = graph.add_edge(n0, n1, ());
        let _ = graph.add_edge(n1, n2, ());
        let _ = graph.add_edge(n0, n2, ());

        let leiden = Leiden::new();
        let communities = leiden.detect(&graph).unwrap();

        // All in one community (triangle)
        assert_eq!(communities[0], communities[1]);
        assert_eq!(communities[1], communities[2]);
    }

    #[test]
    fn test_leiden_two_cliques() {
        // Two triangles connected by a single edge
        let mut graph = UnGraph::<(), ()>::new_undirected();

        // First clique
        let a0 = graph.add_node(());
        let a1 = graph.add_node(());
        let a2 = graph.add_node(());
        let _ = graph.add_edge(a0, a1, ());
        let _ = graph.add_edge(a1, a2, ());
        let _ = graph.add_edge(a0, a2, ());

        // Second clique
        let b0 = graph.add_node(());
        let b1 = graph.add_node(());
        let b2 = graph.add_node(());
        let _ = graph.add_edge(b0, b1, ());
        let _ = graph.add_edge(b1, b2, ());
        let _ = graph.add_edge(b0, b2, ());

        // Bridge
        let _ = graph.add_edge(a2, b0, ());

        let leiden = Leiden::new();
        let communities = leiden.detect(&graph).unwrap();

        assert_eq!(communities.len(), 6);

        // First clique should be in same community
        assert_eq!(communities[0], communities[1]);
        assert_eq!(communities[1], communities[2]);

        // Second clique should be in same community
        assert_eq!(communities[3], communities[4]);
        assert_eq!(communities[4], communities[5]);

        // Two cliques should be in different communities
        assert_ne!(communities[0], communities[3]);
    }

    #[test]
    fn test_leiden_disconnected_within_community() {
        // This is the key test that Louvain fails but Leiden should pass.
        // Create a graph where naive merging could create disconnected communities.
        //
        // Structure: A--B--C  D--E (D,E disconnected from A,B,C)
        // If Louvain merges all into one community, it's wrong.
        // Leiden should detect and split.

        let mut graph = UnGraph::<(), ()>::new_undirected();
        let a = graph.add_node(());
        let b = graph.add_node(());
        let c = graph.add_node(());
        let d = graph.add_node(());
        let e = graph.add_node(());

        let _ = graph.add_edge(a, b, ());
        let _ = graph.add_edge(b, c, ());
        let _ = graph.add_edge(d, e, ());

        let leiden = Leiden::new();
        let communities = leiden.detect(&graph).unwrap();

        // A, B, C should be in one community (connected)
        assert_eq!(communities[0], communities[1]);
        assert_eq!(communities[1], communities[2]);

        // D, E should be in another community (connected)
        assert_eq!(communities[3], communities[4]);

        // The two groups should be in different communities
        assert_ne!(communities[0], communities[3]);
    }

    #[test]
    fn test_leiden_empty_graph() {
        let graph = UnGraph::<(), ()>::new_undirected();
        let leiden = Leiden::new();
        let result = leiden.detect(&graph);
        assert!(result.is_err());
    }

    #[test]
    fn test_leiden_single_node() {
        let mut graph = UnGraph::<(), ()>::new_undirected();
        let _ = graph.add_node(());

        let leiden = Leiden::new();
        let communities = leiden.detect(&graph).unwrap();

        assert_eq!(communities.len(), 1);
        assert_eq!(communities[0], 0);
    }

    #[test]
    fn test_leiden_resolution_parameter() {
        // Resolution parameter affects community detection
        // Note: Due to greedy local optima, higher resolution doesn't *guarantee*
        // more communities, but both should produce valid partitions.
        let mut graph = UnGraph::<(), ()>::new_undirected();

        // Create a larger structure
        for _ in 0..10 {
            let _ = graph.add_node(());
        }
        // Connect as a chain
        for i in 0..9 {
            let n1 = petgraph::graph::NodeIndex::new(i);
            let n2 = petgraph::graph::NodeIndex::new(i + 1);
            let _ = graph.add_edge(n1, n2, ());
        }

        let low_res = Leiden::new().with_resolution(0.5);
        let high_res = Leiden::new().with_resolution(2.0);

        let comms_low = low_res.detect(&graph).unwrap();
        let comms_high = high_res.detect(&graph).unwrap();

        // Both should assign all nodes
        assert_eq!(comms_low.len(), 10);
        assert_eq!(comms_high.len(), 10);

        let unique_low: HashSet<_> = comms_low.iter().collect();
        let unique_high: HashSet<_> = comms_high.iter().collect();

        // Both should produce valid partitions (at least 1 community)
        assert!(!unique_low.is_empty());
        assert!(!unique_high.is_empty());
    }

    #[test]
    fn test_leiden_connectivity_guarantee() {
        // Verify that every community is internally connected
        let mut graph = UnGraph::<(), ()>::new_undirected();

        // Create a moderately complex graph
        for _ in 0..20 {
            let _ = graph.add_node(());
        }
        // Add some edges
        for i in 0..15 {
            let n1 = petgraph::graph::NodeIndex::new(i);
            let n2 = petgraph::graph::NodeIndex::new(i + 1);
            let _ = graph.add_edge(n1, n2, ());
        }
        // Add some cross-links
        let _ = graph.add_edge(
            petgraph::graph::NodeIndex::new(0),
            petgraph::graph::NodeIndex::new(5),
            (),
        );
        let _ = graph.add_edge(
            petgraph::graph::NodeIndex::new(10),
            petgraph::graph::NodeIndex::new(15),
            (),
        );

        let leiden = Leiden::new();
        let communities = leiden.detect(&graph).unwrap();

        // Group nodes by community
        let mut by_community: HashMap<usize, Vec<usize>> = HashMap::new();
        for (node, &comm) in communities.iter().enumerate() {
            by_community.entry(comm).or_default().push(node);
        }

        // Verify each community is connected
        for (_comm, nodes) in by_community {
            if nodes.len() <= 1 {
                continue;
            }

            let node_set: HashSet<usize> = nodes.iter().copied().collect();

            // Build subgraph adjacency
            let mut adj: HashMap<usize, Vec<usize>> = HashMap::new();
            for edge in graph.edge_references() {
                let i = edge.source().index();
                let j = edge.target().index();
                if node_set.contains(&i) && node_set.contains(&j) {
                    adj.entry(i).or_default().push(j);
                    adj.entry(j).or_default().push(i);
                }
            }

            // BFS from first node should reach all nodes
            let start = nodes[0];
            let mut visited = HashSet::new();
            let mut queue = VecDeque::new();
            queue.push_back(start);

            while let Some(node) = queue.pop_front() {
                if !visited.insert(node) {
                    continue;
                }
                if let Some(neighbors) = adj.get(&node) {
                    for &n in neighbors {
                        if !visited.contains(&n) {
                            queue.push_back(n);
                        }
                    }
                }
            }

            assert_eq!(
                visited.len(),
                nodes.len(),
                "Community is not fully connected!"
            );
        }
    }
}