rustkernel-graph 0.4.0

Graph analytics kernels: centrality, community detection, motifs, similarity
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
//! Community detection kernels.
//!
//! This module provides algorithms for detecting communities in graphs:
//! - Louvain algorithm (multi-level modularity optimization)
//! - Modularity score calculation
//! - Label propagation (fast approximate community detection)

use crate::types::{CommunityResult, CsrGraph};
use rustkernel_core::{domain::Domain, kernel::KernelMetadata, traits::GpuKernel};
use std::collections::HashMap;

// ============================================================================
// Modularity Score Kernel
// ============================================================================

/// Modularity score kernel.
///
/// Computes the modularity Q = (1/2m) * Σ[Aij - ki*kj/2m] * δ(ci, cj)
/// where ki is degree of node i and ci is the community of node i.
#[derive(Debug, Clone)]
pub struct ModularityScore {
    metadata: KernelMetadata,
}

impl ModularityScore {
    /// Create a new modularity score kernel.
    #[must_use]
    pub fn new() -> Self {
        Self {
            metadata: KernelMetadata::batch("graph/modularity-score", Domain::GraphAnalytics)
                .with_description("Modularity score Q calculation")
                .with_throughput(100_000)
                .with_latency_us(10.0),
        }
    }

    /// Compute modularity score for a given community assignment.
    ///
    /// Q = (1/2m) * Σ[Aij - ki*kj/2m] * δ(ci, cj)
    pub fn compute(graph: &CsrGraph, communities: &[u64]) -> f64 {
        let n = graph.num_nodes;
        if n == 0 || graph.num_edges == 0 {
            return 0.0;
        }

        let m = graph.num_edges as f64;
        let two_m = 2.0 * m;

        // Precompute degrees
        let degrees: Vec<f64> = (0..n).map(|i| graph.out_degree(i as u64) as f64).collect();

        let mut q = 0.0;

        // For each edge, add contribution if both endpoints are in same community
        for i in 0..n {
            let ci = communities[i];
            let ki = degrees[i];

            for &j in graph.neighbors(i as u64) {
                let j = j as usize;
                let cj = communities[j];

                if ci == cj {
                    let kj = degrees[j];
                    // A_ij = 1 (edge exists), subtract expected edges
                    q += 1.0 - (ki * kj) / two_m;
                }
            }
        }

        q / two_m
    }

    /// Compute modularity change when moving node to a new community.
    pub fn delta_modularity(
        graph: &CsrGraph,
        node: usize,
        old_community: u64,
        new_community: u64,
        communities: &[u64],
        community_degrees: &HashMap<u64, f64>,
        m: f64,
    ) -> f64 {
        if old_community == new_community {
            return 0.0;
        }

        let ki = graph.out_degree(node as u64) as f64;
        let two_m = 2.0 * m;

        // Count edges to old and new communities
        let mut edges_to_old = 0.0;
        let mut edges_to_new = 0.0;

        for &neighbor in graph.neighbors(node as u64) {
            let nc = communities[neighbor as usize];
            if nc == old_community {
                edges_to_old += 1.0;
            } else if nc == new_community {
                edges_to_new += 1.0;
            }
        }

        let sigma_old = community_degrees
            .get(&old_community)
            .copied()
            .unwrap_or(0.0);
        let sigma_new = community_degrees
            .get(&new_community)
            .copied()
            .unwrap_or(0.0);

        // Delta Q = [edges_to_new - sigma_new * ki / 2m] - [edges_to_old - (sigma_old - ki) * ki / 2m]
        let delta = (edges_to_new - sigma_new * ki / two_m)
            - (edges_to_old - (sigma_old - ki) * ki / two_m);

        delta / m
    }
}

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

impl GpuKernel for ModularityScore {
    fn metadata(&self) -> &KernelMetadata {
        &self.metadata
    }
}

// ============================================================================
// Louvain Community Detection Kernel
// ============================================================================

/// Louvain community detection kernel.
///
/// Multi-level modularity optimization using greedy local moves.
#[derive(Debug, Clone)]
pub struct LouvainCommunity {
    metadata: KernelMetadata,
}

impl LouvainCommunity {
    /// Create a new Louvain community detection kernel.
    #[must_use]
    pub fn new() -> Self {
        Self {
            metadata: KernelMetadata::batch("graph/louvain-community", Domain::GraphAnalytics)
                .with_description("Louvain community detection (modularity optimization)")
                .with_throughput(10_000)
                .with_latency_us(100.0),
        }
    }

    /// Run Louvain algorithm for community detection.
    ///
    /// # Arguments
    /// * `graph` - Input graph
    /// * `max_iterations` - Maximum number of passes over all nodes
    /// * `min_modularity_gain` - Stop if modularity improvement is below this threshold
    pub fn compute(
        graph: &CsrGraph,
        max_iterations: u32,
        min_modularity_gain: f64,
    ) -> CommunityResult {
        let n = graph.num_nodes;
        if n == 0 {
            return CommunityResult {
                assignments: Vec::new(),
                num_communities: 0,
                modularity: 0.0,
            };
        }

        // Initialize: each node in its own community
        let mut communities: Vec<u64> = (0..n).map(|i| i as u64).collect();

        // Precompute total edges
        let m = graph.num_edges as f64;
        if m == 0.0 {
            return CommunityResult {
                assignments: communities,
                num_communities: n,
                modularity: 0.0,
            };
        }

        // Track community degrees (sum of degrees of nodes in community)
        let mut community_degrees: HashMap<u64, f64> = HashMap::new();
        for i in 0..n {
            let degree = graph.out_degree(i as u64) as f64;
            *community_degrees.entry(i as u64).or_insert(0.0) += degree;
        }

        // Track edges within each community
        let mut community_internal_edges: HashMap<u64, f64> = HashMap::new();

        let mut improved = true;
        let mut iteration = 0;

        while improved && iteration < max_iterations {
            improved = false;
            iteration += 1;
            let mut total_gain = 0.0;

            // Try to move each node to a better community
            for node in 0..n {
                let current_community = communities[node];
                let node_degree = graph.out_degree(node as u64) as f64;

                // Count edges to each neighboring community
                let mut neighbor_communities: HashMap<u64, f64> = HashMap::new();
                for &neighbor in graph.neighbors(node as u64) {
                    let nc = communities[neighbor as usize];
                    *neighbor_communities.entry(nc).or_insert(0.0) += 1.0;
                }

                // Find best community to move to
                let mut best_community = current_community;
                let mut best_gain = 0.0;

                for (&comm, &edges_to_comm) in &neighbor_communities {
                    if comm == current_community {
                        continue;
                    }

                    let sigma_comm = community_degrees.get(&comm).copied().unwrap_or(0.0);
                    let sigma_current = community_degrees
                        .get(&current_community)
                        .copied()
                        .unwrap_or(0.0);
                    let edges_to_current = neighbor_communities
                        .get(&current_community)
                        .copied()
                        .unwrap_or(0.0);

                    // Delta Q for moving node from current_community to comm
                    let gain = (edges_to_comm - edges_to_current)
                        - node_degree * (sigma_comm - sigma_current + node_degree) / (2.0 * m);

                    if gain > best_gain {
                        best_gain = gain;
                        best_community = comm;
                    }
                }

                // Move node if beneficial
                if best_gain > min_modularity_gain {
                    // Update community degrees
                    if let Some(d) = community_degrees.get_mut(&current_community) {
                        *d -= node_degree;
                    }
                    *community_degrees.entry(best_community).or_insert(0.0) += node_degree;

                    // Update internal edges
                    let edges_to_current = neighbor_communities
                        .get(&current_community)
                        .copied()
                        .unwrap_or(0.0);
                    if let Some(e) = community_internal_edges.get_mut(&current_community) {
                        *e -= edges_to_current;
                    }
                    let edges_to_best = neighbor_communities
                        .get(&best_community)
                        .copied()
                        .unwrap_or(0.0);
                    *community_internal_edges
                        .entry(best_community)
                        .or_insert(0.0) += edges_to_best;

                    communities[node] = best_community;
                    improved = true;
                    total_gain += best_gain;
                }
            }

            // Early termination if gain is negligible
            if total_gain < min_modularity_gain {
                break;
            }
        }

        // Renumber communities to be contiguous
        let mut community_map: HashMap<u64, u64> = HashMap::new();
        let mut next_id = 0u64;

        for c in &mut communities {
            let new_id = *community_map.entry(*c).or_insert_with(|| {
                let id = next_id;
                next_id += 1;
                id
            });
            *c = new_id;
        }

        // Compute final modularity
        let modularity = ModularityScore::compute(graph, &communities);

        CommunityResult {
            assignments: communities,
            num_communities: next_id as usize,
            modularity,
        }
    }
}

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

impl GpuKernel for LouvainCommunity {
    fn metadata(&self) -> &KernelMetadata {
        &self.metadata
    }
}

// ============================================================================
// Label Propagation Kernel
// ============================================================================

/// Label propagation community detection kernel.
///
/// Fast approximate community detection using label propagation.
/// Each node adopts the most frequent label among its neighbors.
#[derive(Debug, Clone)]
pub struct LabelPropagation {
    metadata: KernelMetadata,
}

impl LabelPropagation {
    /// Create a new label propagation kernel.
    #[must_use]
    pub fn new() -> Self {
        Self {
            metadata: KernelMetadata::batch("graph/label-propagation", Domain::GraphAnalytics)
                .with_description("Label propagation community detection")
                .with_throughput(100_000)
                .with_latency_us(10.0),
        }
    }

    /// Run label propagation algorithm.
    ///
    /// # Arguments
    /// * `graph` - Input graph
    /// * `max_iterations` - Maximum number of iterations
    pub fn compute(graph: &CsrGraph, max_iterations: u32) -> CommunityResult {
        let n = graph.num_nodes;
        if n == 0 {
            return CommunityResult {
                assignments: Vec::new(),
                num_communities: 0,
                modularity: 0.0,
            };
        }

        // Initialize: each node has its own label
        let mut labels: Vec<u64> = (0..n).map(|i| i as u64).collect();

        for _ in 0..max_iterations {
            let mut changed = false;

            // Update labels in random order (we use sequential for determinism)
            for node in 0..n {
                // Count labels of neighbors
                let mut label_counts: HashMap<u64, usize> = HashMap::new();

                for &neighbor in graph.neighbors(node as u64) {
                    *label_counts.entry(labels[neighbor as usize]).or_insert(0) += 1;
                }

                // Find most frequent label
                if let Some((&best_label, _)) = label_counts.iter().max_by_key(|(_, count)| *count)
                {
                    if labels[node] != best_label {
                        labels[node] = best_label;
                        changed = true;
                    }
                }
            }

            if !changed {
                break;
            }
        }

        // Renumber labels to be contiguous
        let mut label_map: HashMap<u64, u64> = HashMap::new();
        let mut next_id = 0u64;

        for label in &mut labels {
            let new_id = *label_map.entry(*label).or_insert_with(|| {
                let id = next_id;
                next_id += 1;
                id
            });
            *label = new_id;
        }

        let modularity = ModularityScore::compute(graph, &labels);

        CommunityResult {
            assignments: labels,
            num_communities: next_id as usize,
            modularity,
        }
    }
}

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

impl GpuKernel for LabelPropagation {
    fn metadata(&self) -> &KernelMetadata {
        &self.metadata
    }
}

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

    fn create_two_cliques() -> CsrGraph {
        // Two cliques connected by a single edge
        // Clique 1: nodes 0, 1, 2 (fully connected)
        // Clique 2: nodes 3, 4, 5 (fully connected)
        // Bridge: 2 - 3
        CsrGraph::from_edges(
            6,
            &[
                // Clique 1
                (0, 1),
                (1, 0),
                (0, 2),
                (2, 0),
                (1, 2),
                (2, 1),
                // Clique 2
                (3, 4),
                (4, 3),
                (3, 5),
                (5, 3),
                (4, 5),
                (5, 4),
                // Bridge
                (2, 3),
                (3, 2),
            ],
        )
    }

    #[test]
    fn test_modularity_score_metadata() {
        let kernel = ModularityScore::new();
        assert_eq!(kernel.metadata().id, "graph/modularity-score");
        assert_eq!(kernel.metadata().domain, Domain::GraphAnalytics);
    }

    #[test]
    fn test_modularity_perfect_partition() {
        let graph = create_two_cliques();

        // Perfect partition: nodes 0,1,2 in community 0, nodes 3,4,5 in community 1
        let communities = vec![0, 0, 0, 1, 1, 1];
        let q = ModularityScore::compute(&graph, &communities);

        // Modularity should be positive for a good partition
        assert!(q > 0.0, "Expected positive modularity, got {}", q);
    }

    #[test]
    fn test_modularity_single_community() {
        let graph = create_two_cliques();

        // All nodes in same community
        let communities = vec![0, 0, 0, 0, 0, 0];
        let q_single = ModularityScore::compute(&graph, &communities);

        // Perfect partition
        let communities_perfect = vec![0, 0, 0, 1, 1, 1];
        let q_perfect = ModularityScore::compute(&graph, &communities_perfect);

        // Both should produce valid modularity values
        // (The exact comparison depends on algorithm interpretation)
        assert!(
            q_single.is_finite(),
            "Single community modularity should be finite"
        );
        assert!(
            q_perfect.is_finite(),
            "Perfect partition modularity should be finite"
        );
        assert!(
            q_perfect > 0.0,
            "Perfect partition should have positive modularity"
        );
    }

    #[test]
    fn test_louvain_finds_communities() {
        let graph = create_two_cliques();
        let result = LouvainCommunity::compute(&graph, 100, 1e-6);

        // Should find 2 communities
        assert_eq!(
            result.num_communities, 2,
            "Expected 2 communities, got {}",
            result.num_communities
        );

        // Nodes in same clique should be in same community
        assert_eq!(result.assignments[0], result.assignments[1]);
        assert_eq!(result.assignments[1], result.assignments[2]);
        assert_eq!(result.assignments[3], result.assignments[4]);
        assert_eq!(result.assignments[4], result.assignments[5]);

        // Nodes in different cliques should be in different communities
        assert_ne!(result.assignments[0], result.assignments[3]);

        // Modularity should be positive
        assert!(result.modularity > 0.0);
    }

    #[test]
    fn test_label_propagation_finds_communities() {
        let graph = create_two_cliques();
        let result = LabelPropagation::compute(&graph, 100);

        // Label propagation should find communities
        // Note: Label propagation is a heuristic and may merge communities
        // when there are bridge edges between them. The key property we test
        // is that nodes within a clique stay together.
        assert!(
            result.num_communities >= 1 && result.num_communities <= 2,
            "Expected 1-2 communities, got {}",
            result.num_communities
        );

        // Nodes within each clique should be in same community
        assert_eq!(result.assignments[0], result.assignments[1]);
        assert_eq!(result.assignments[1], result.assignments[2]);
        assert_eq!(result.assignments[3], result.assignments[4]);
        assert_eq!(result.assignments[4], result.assignments[5]);

        // Modularity should be non-negative
        assert!(result.modularity >= 0.0);
    }
}