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
//! Short cycle detection kernels.
//!
//! This module provides cycle participation analysis:
//! - 2-cycle (reciprocal edge) detection
//! - 3-cycle (triangle) detection - KEY AML INDICATOR
//! - 4-cycle (square) detection - CRITICAL AML INDICATOR

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

// ============================================================================
// Cycle Participation Result
// ============================================================================

/// Cycle participation result for a node.
#[derive(Debug, Clone)]
pub struct CycleParticipationResult {
    /// Node index.
    pub node_index: usize,
    /// Number of 2-cycles (reciprocal edges) this node participates in.
    pub cycle_count_2hop: u32,
    /// Number of 3-cycles (triangles) this node participates in - KEY AML INDICATOR.
    pub cycle_count_3hop: u32,
    /// Number of 4-cycles (squares) this node participates in - CRITICAL AML INDICATOR.
    pub cycle_count_4hop: u32,
    /// Total weight (sum of edge weights) in all cycles.
    pub total_cycle_weight: f64,
    /// Cycle ratio: fraction of edges that participate in cycles `[0,1]`.
    pub cycle_ratio: f64,
    /// Risk level based on cycle participation.
    pub risk_level: CycleRiskLevel,
}

/// Risk level based on cycle participation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CycleRiskLevel {
    /// Baseline - zero or minimal 2-cycles.
    Baseline,
    /// Low risk - 1-2 triangles (could be legitimate).
    Low,
    /// Medium risk - 3+ triangles (investigate for layering).
    Medium,
    /// High risk - ANY 4-cycles (structured laundering pattern).
    High,
    /// Critical risk - Multiple 4-cycles (professional laundering ring).
    Critical,
}

// ============================================================================
// Short Cycle Participation Kernel
// ============================================================================

/// Short cycle participation kernel.
///
/// Detects participation in 2-4 hop cycles. Critical for AML:
/// - Triangles (3-cycles) indicate layering patterns
/// - Squares (4-cycles) indicate organized money laundering
#[derive(Debug, Clone)]
pub struct ShortCycleParticipation {
    metadata: KernelMetadata,
}

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

impl ShortCycleParticipation {
    /// Create a new short cycle participation kernel.
    #[must_use]
    pub fn new() -> Self {
        Self {
            metadata: KernelMetadata::batch("graph/cycle-participation", Domain::GraphAnalytics)
                .with_description("Short cycle (2-4 hop) participation detection")
                .with_throughput(25_000)
                .with_latency_us(200.0),
        }
    }

    /// Detect 2-cycles (reciprocal edges) for all nodes.
    #[allow(clippy::needless_range_loop)]
    pub fn detect_2_cycles(graph: &CsrGraph) -> Vec<u32> {
        let n = graph.num_nodes;
        let mut cycle_counts = vec![0u32; n];

        // For each edge, check if reciprocal exists
        for u in 0..n {
            for &v in graph.neighbors(u as u64) {
                // Check if edge v -> u exists (reciprocal)
                if graph.neighbors(v).contains(&(u as u64)) {
                    cycle_counts[u] += 1;
                }
            }
        }

        // Each reciprocal edge is counted once per endpoint
        // (already normalized since we check u->v and v->u separately)

        cycle_counts
    }

    /// Detect 3-cycles (triangles) for all nodes.
    ///
    /// This is the KEY AML INDICATOR for layering detection.
    pub fn detect_3_cycles(graph: &CsrGraph) -> Vec<u32> {
        let n = graph.num_nodes;
        let mut cycle_counts = vec![0u32; n];

        for u in 0..n {
            let neighbors_u: HashSet<u64> = graph.neighbors(u as u64).iter().copied().collect();

            for &v in graph.neighbors(u as u64) {
                if v as usize <= u {
                    continue; // Avoid counting twice
                }

                // Find common neighbors (triangles)
                for &w in graph.neighbors(v) {
                    if w as usize <= v as usize {
                        continue;
                    }

                    if neighbors_u.contains(&w) {
                        // Found triangle u-v-w
                        cycle_counts[u] += 1;
                        cycle_counts[v as usize] += 1;
                        cycle_counts[w as usize] += 1;
                    }
                }
            }
        }

        cycle_counts
    }

    /// Detect 4-cycles (squares) for all nodes.
    ///
    /// This is the CRITICAL AML INDICATOR for organized laundering.
    #[allow(clippy::needless_range_loop)]
    pub fn detect_4_cycles(graph: &CsrGraph) -> Vec<u32> {
        let n = graph.num_nodes;
        let mut cycle_counts = vec![0u32; n];

        // For efficiency, limit to smaller graphs or use sampling
        if n > 1000 {
            // Use sampling for large graphs
            return Self::detect_4_cycles_sampled(graph, 0.1);
        }

        for u in 0..n {
            let neighbors_u: HashSet<u64> = graph.neighbors(u as u64).iter().copied().collect();

            // Enumerate 2-paths starting from u: u -> v -> w
            for &v in graph.neighbors(u as u64) {
                for &w in graph.neighbors(v) {
                    if w as usize == u {
                        continue; // Skip immediate back-edge
                    }

                    // Look for x such that w -> x -> u forms a 4-cycle
                    for &x in graph.neighbors(w) {
                        if x as usize != u && x != v && neighbors_u.contains(&x) {
                            // Found 4-cycle: u -> v -> w -> x -> u
                            cycle_counts[u] += 1;
                        }
                    }
                }
            }
        }

        // Each 4-cycle is counted 4 times (once per node), normalize
        for count in &mut cycle_counts {
            *count /= 4;
        }

        cycle_counts
    }

    /// Detect 4-cycles using sampling for large graphs.
    fn detect_4_cycles_sampled(graph: &CsrGraph, sample_rate: f64) -> Vec<u32> {
        let n = graph.num_nodes;
        let mut cycle_counts = vec![0u32; n];

        let sample_count = (n as f64 * sample_rate).max(1.0) as usize;
        let step = (n / sample_count).max(1);

        for u in (0..n).step_by(step) {
            let neighbors_u: HashSet<u64> = graph.neighbors(u as u64).iter().copied().collect();

            for &v in graph.neighbors(u as u64) {
                for &w in graph.neighbors(v) {
                    if w as usize == u {
                        continue;
                    }

                    for &x in graph.neighbors(w) {
                        if x as usize != u && x != v && neighbors_u.contains(&x) {
                            cycle_counts[u] += 1;
                        }
                    }
                }
            }
        }

        // Scale up for sampling
        let scale = 1.0 / sample_rate;
        for count in &mut cycle_counts {
            *count = (*count as f64 * scale) as u32 / 4;
        }

        cycle_counts
    }

    /// Compute full cycle participation for all nodes.
    pub fn compute_all(graph: &CsrGraph) -> Vec<CycleParticipationResult> {
        let cycles_2 = Self::detect_2_cycles(graph);
        let cycles_3 = Self::detect_3_cycles(graph);
        let cycles_4 = Self::detect_4_cycles(graph);

        let n = graph.num_nodes;

        (0..n)
            .map(|i| {
                let c2 = cycles_2[i];
                let c3 = cycles_3[i];
                let c4 = cycles_4[i];

                let degree = graph.out_degree(i as u64);
                let cycle_ratio = if degree > 0 {
                    (c2 + c3 + c4) as f64 / degree as f64
                } else {
                    0.0
                };

                let risk_level = Self::calculate_risk_level(c2, c3, c4);

                CycleParticipationResult {
                    node_index: i,
                    cycle_count_2hop: c2,
                    cycle_count_3hop: c3,
                    cycle_count_4hop: c4,
                    total_cycle_weight: 0.0, // Would need weighted graph
                    cycle_ratio,
                    risk_level,
                }
            })
            .collect()
    }

    /// Calculate risk level based on cycle participation.
    fn calculate_risk_level(cycles_2: u32, cycles_3: u32, cycles_4: u32) -> CycleRiskLevel {
        if cycles_4 > 3 {
            CycleRiskLevel::Critical
        } else if cycles_4 > 0 {
            CycleRiskLevel::High
        } else if cycles_3 >= 3 {
            CycleRiskLevel::Medium
        } else if cycles_3 >= 1 || cycles_2 >= 3 {
            CycleRiskLevel::Low
        } else {
            CycleRiskLevel::Baseline
        }
    }

    /// Find high-risk nodes based on cycle participation.
    pub fn find_high_risk_nodes(graph: &CsrGraph) -> Vec<CycleParticipationResult> {
        Self::compute_all(graph)
            .into_iter()
            .filter(|r| {
                matches!(
                    r.risk_level,
                    CycleRiskLevel::High | CycleRiskLevel::Critical
                )
            })
            .collect()
    }

    /// Find nodes with any 4-cycle participation.
    pub fn find_4_cycle_nodes(graph: &CsrGraph) -> Vec<CycleParticipationResult> {
        Self::compute_all(graph)
            .into_iter()
            .filter(|r| r.cycle_count_4hop > 0)
            .collect()
    }

    /// Count total triangles in the graph.
    pub fn count_triangles(graph: &CsrGraph) -> u64 {
        let cycles_3 = Self::detect_3_cycles(graph);
        // Each triangle is counted 3 times (once per vertex)
        cycles_3.iter().map(|&c| c as u64).sum::<u64>() / 3
    }
}

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

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

    fn create_triangle_graph() -> CsrGraph {
        // Undirected triangle: 0 - 1 - 2 - 0 (bidirectional edges)
        CsrGraph::from_edges(3, &[(0, 1), (1, 0), (0, 2), (2, 0), (1, 2), (2, 1)])
    }

    #[allow(dead_code)]
    fn create_square_graph() -> CsrGraph {
        // Square: 0 -> 1 -> 2 -> 3 -> 0
        CsrGraph::from_edges(4, &[(0, 1), (1, 2), (2, 3), (3, 0)])
    }

    fn create_reciprocal_graph() -> CsrGraph {
        // Bidirectional edges: 0 <-> 1, 1 <-> 2
        CsrGraph::from_edges(3, &[(0, 1), (1, 0), (1, 2), (2, 1)])
    }

    fn create_complex_graph() -> CsrGraph {
        // Graph with two triangles (undirected)
        CsrGraph::from_edges(
            5,
            &[
                // Triangle 0-1-2
                (0, 1),
                (1, 0),
                (1, 2),
                (2, 1),
                (0, 2),
                (2, 0),
                // Triangle 2-3-4
                (2, 3),
                (3, 2),
                (3, 4),
                (4, 3),
                (2, 4),
                (4, 2),
            ],
        )
    }

    #[test]
    fn test_cycle_participation_metadata() {
        let kernel = ShortCycleParticipation::new();
        assert_eq!(kernel.metadata().id, "graph/cycle-participation");
        assert_eq!(kernel.metadata().domain, Domain::GraphAnalytics);
    }

    #[test]
    fn test_detect_2_cycles() {
        let graph = create_reciprocal_graph();
        let counts = ShortCycleParticipation::detect_2_cycles(&graph);

        // Node 1 has reciprocal edges with both 0 and 2
        assert!(counts[1] >= 2);
    }

    #[test]
    fn test_detect_3_cycles() {
        let graph = create_triangle_graph();
        let counts = ShortCycleParticipation::detect_3_cycles(&graph);

        // All three nodes participate in one triangle
        assert!(
            counts[0] >= 1,
            "Node 0 should participate in triangle: got {}",
            counts[0]
        );
        assert!(
            counts[1] >= 1,
            "Node 1 should participate in triangle: got {}",
            counts[1]
        );
        assert!(
            counts[2] >= 1,
            "Node 2 should participate in triangle: got {}",
            counts[2]
        );
    }

    #[test]
    fn test_count_triangles() {
        let graph = create_triangle_graph();
        let count = ShortCycleParticipation::count_triangles(&graph);

        assert_eq!(
            count, 1,
            "Should find 1 triangle in undirected triangle graph"
        );
    }

    #[test]
    fn test_complex_graph_triangles() {
        let graph = create_complex_graph();
        let count = ShortCycleParticipation::count_triangles(&graph);

        assert_eq!(count, 2, "Should find 2 triangles"); // Two triangles
    }

    #[test]
    fn test_risk_level_baseline() {
        let level = ShortCycleParticipation::calculate_risk_level(0, 0, 0);
        assert_eq!(level, CycleRiskLevel::Baseline);
    }

    #[test]
    fn test_risk_level_low() {
        let level = ShortCycleParticipation::calculate_risk_level(0, 1, 0);
        assert_eq!(level, CycleRiskLevel::Low);
    }

    #[test]
    fn test_risk_level_medium() {
        let level = ShortCycleParticipation::calculate_risk_level(0, 3, 0);
        assert_eq!(level, CycleRiskLevel::Medium);
    }

    #[test]
    fn test_risk_level_high() {
        let level = ShortCycleParticipation::calculate_risk_level(0, 0, 1);
        assert_eq!(level, CycleRiskLevel::High);
    }

    #[test]
    fn test_risk_level_critical() {
        let level = ShortCycleParticipation::calculate_risk_level(0, 0, 5);
        assert_eq!(level, CycleRiskLevel::Critical);
    }

    #[test]
    fn test_find_high_risk_nodes() {
        let graph = create_triangle_graph();
        let high_risk = ShortCycleParticipation::find_high_risk_nodes(&graph);

        // No 4-cycles, so no high-risk nodes
        assert!(high_risk.is_empty());
    }

    #[test]
    fn test_compute_all() {
        let graph = create_triangle_graph();
        let results = ShortCycleParticipation::compute_all(&graph);

        assert_eq!(results.len(), 3);
        for result in &results {
            assert!(
                result.cycle_count_3hop >= 1,
                "Each node should have at least 1 triangle participation"
            );
            assert_eq!(result.cycle_count_4hop, 0);
        }
    }
}