aprender 0.30.0

Next-generation ML framework in pure Rust — `cargo install aprender` for the `apr` CLI
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
<!-- PCU: ml-fundamentals-graph-link-prediction | contract: contracts/apr-page-ml-fundamentals-graph-link-prediction-v1.yaml -->
<!-- Example: cargo run -p aprender-core --example none -->
<!-- Status: enforced -->

# Graph Link Prediction and Community Detection

Link prediction and community detection are essential graph analysis techniques with applications in social network analysis, recommendation systems, biological network analysis, and network security. This chapter covers the theory and implementation of link prediction metrics and community detection algorithms in aprender's graph module.

## Overview

Aprender implements three key algorithms for link analysis and community detection:

1. **Common Neighbors**: Count shared neighbors between two nodes for link prediction
2. **Adamic-Adar Index**: Weighted similarity metric that emphasizes rare connections
3. **Label Propagation**: Iterative community detection algorithm

All algorithms operate on the Compressed Sparse Row (CSR) graph representation for optimal cache locality and memory efficiency.

## Link Prediction

Link prediction estimates the likelihood of future connections between nodes based on network structure. These metrics are used in friend recommendations, citation prediction, and protein interaction discovery.

### Common Neighbors

#### Algorithm

The Common Neighbors metric counts the number of shared neighbors between two nodes. The intuition is that nodes with many mutual connections are more likely to form a link.

**Properties**:
- Time Complexity: O(min(deg(u), deg(v))) using two-pointer technique
- Space Complexity: O(1) - operates directly on CSR neighbor arrays
- Works on both directed and undirected graphs
- Simple and interpretable metric

#### Implementation

```rust
use aprender::graph::Graph;

let g = Graph::from_edges(
    &[(0, 1), (0, 2), (1, 2), (1, 3), (2, 3)],
    false
);

// Count common neighbors between nodes 0 and 3
let cn = g.common_neighbors(0, 3).expect("nodes should exist");
assert_eq!(cn, 2);  // Nodes 1 and 2 are shared neighbors

// No common neighbors
let cn2 = g.common_neighbors(0, 0).expect("nodes should exist");
assert_eq!(cn2, 0);  // No self-loops

// Invalid node returns None
assert!(g.common_neighbors(0, 100).is_none());
```

#### How It Works

The algorithm uses a **two-pointer technique** on sorted neighbor arrays:

1. **Initialization**: Get neighbor arrays for both nodes u and v
2. **Two-Pointer Scan**: Start pointers i=0, j=0
3. **Compare and Count**:
   - If neighbors_u[i] == neighbors_v[j]: increment count, advance both pointers
   - If neighbors_u[i] < neighbors_v[j]: advance i
   - If neighbors_u[i] > neighbors_v[j]: advance j
4. **Termination**: Return count when either pointer reaches end

**Visual Example**:
```text
Graph:    0 --- 1 --- 3
          |     |     |
          2 ----+-----+

neighbors(0) = [1, 2]  (sorted)
neighbors(3) = [1, 2]  (sorted)

Two-pointer scan:
i=0, j=0: neighbors[0][0]=1 == neighbors[3][0]=1 → count=1, i++, j++
i=1, j=1: neighbors[0][1]=2 == neighbors[3][1]=2 → count=2, i++, j++
Done: common_neighbors(0, 3) = 2
```

**Why This Works**: CSR neighbor arrays are stored in sorted order, enabling efficient set intersection in O(min(deg(u), deg(v))) time instead of O(deg(u) × deg(v)).

#### Use Cases

- **Social Networks**: Friend recommendations (mutual friends)
- **Collaboration Networks**: Co-author prediction
- **E-commerce**: Product recommendations based on co-purchase patterns
- **Biology**: Predicting protein-protein interactions

### Adamic-Adar Index

#### Algorithm

The Adamic-Adar Index is a **weighted** similarity metric that assigns higher weight to rare common neighbors. The formula is:

```text
AA(u, v) = Σ 1 / ln(deg(z))
           z ∈ common_neighbors(u, v)
```

Where deg(z) is the degree of common neighbor z. This emphasizes connections through low-degree nodes (rare, specific connections) over high-degree nodes (common hubs).

**Properties**:
- Time Complexity: O(min(deg(u), deg(v)))
- Space Complexity: O(1)
- More discriminative than simple common neighbors
- Handles high-degree hubs gracefully

#### Implementation

```rust
use aprender::graph::Graph;

let g = Graph::from_edges(
    &[(0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (2, 4), (3, 4)],
    false
);

// Compute Adamic-Adar index between nodes 0 and 3
let aa = g.adamic_adar_index(0, 3).expect("nodes should exist");

// Node 1 has degree 3, node 2 has degree 4
// AA(0,3) = 1/ln(3) + 1/ln(4) ≈ 0.91 + 0.72 ≈ 1.63
assert!((aa - 1.63).abs() < 0.1);

// Empty or invalid cases
let aa2 = g.adamic_adar_index(0, 1).expect("nodes should exist");
assert_eq!(aa2, 0.0);  // No common neighbors (adjacent nodes)

assert!(g.adamic_adar_index(0, 100).is_none());  // Invalid node
```

#### How It Works

1. **Two-Pointer Scan**: Same as common_neighbors to find shared neighbors
2. **Weighted Accumulation**: For each common neighbor z:
   - Get deg(z) = number of neighbors of z
   - If deg(z) > 1: add 1/ln(deg(z)) to score
   - If deg(z) == 1: skip (ln(1) = 0, would cause division issues)
3. **Return Score**: Sum of all weighted contributions

**Visual Example**:
```text
Graph:    0 --- 1 --- 3
          |     |     |
          2 ----+-----4
                |
                5

common_neighbors(0, 3) = {1, 2}
deg(1) = 3, deg(2) = 4

AA(0, 3) = 1/ln(3) + 1/ln(4)
         = 1/1.099 + 1/1.386
         = 0.910 + 0.722
         = 1.632
```

**Why Weight by Inverse Log Degree?**:
- High-degree nodes (hubs) are common and less informative
- Low-degree nodes provide specific, rare connections
- Logarithm provides smooth weighting (not too extreme)
- Empirically performs well in real-world link prediction

#### Use Cases

- **Citation Networks**: Predict future citations (rare co-citations are stronger signals)
- **Social Networks**: Friend recommendations (emphasize niche communities)
- **Biological Networks**: Protein interaction prediction
- **Recommendation Systems**: Item-item similarity with rarity weighting

#### Comparison: Common Neighbors vs Adamic-Adar

| Aspect | Common Neighbors | Adamic-Adar |
|--------|------------------|-------------|
| Weighting | Uniform (all neighbors equal) | Inverse log degree (rare > common) |
| Hub Sensitivity | High (hubs dominate) | Low (hubs downweighted) |
| Complexity | O(min(deg(u), deg(v))) | O(min(deg(u), deg(v))) |
| Interpretability | Very simple | More nuanced |
| Performance | Good baseline | Often better on real networks |

```rust
use aprender::graph::Graph;

// Star graph: hub (0) connected to all others
let star = Graph::from_edges(
    &[(0, 1), (0, 2), (0, 3), (0, 4), (0, 5)],
    false
);

// Predict link between peripheral nodes 1 and 2
let cn = star.common_neighbors(1, 2).expect("nodes exist");
let aa = star.adamic_adar_index(1, 2).expect("nodes exist");

assert_eq!(cn, 1);  // Hub node 0 is common neighbor
// AA downweights hub: 1/ln(5) ≈ 0.62 (lower than CN would suggest)
assert!((aa - 0.62).abs() < 0.1);
```

## Community Detection

Community detection identifies groups of nodes that are more densely connected internally than externally. This reveals modular structure in networks.

### Label Propagation

#### Algorithm

Label Propagation is an **iterative, semi-supervised** community detection algorithm. Each node adopts the most common label among its neighbors, causing communities to emerge organically.

**Properties**:
- Time Complexity: O(max_iter × (n + m)) where n=nodes, m=edges
- Space Complexity: O(n) for labels and node order
- Simple and fast (near-linear time)
- Deterministic with seed (for reproducibility)
- May not converge on directed graphs with pure cycles

#### Implementation

```rust
use aprender::graph::Graph;

// Two triangle communities connected by a bridge
let g = Graph::from_edges(
    &[
        // Triangle 1: nodes 0, 1, 2
        (0, 1), (1, 2), (0, 2),
        // Bridge
        (2, 3),
        // Triangle 2: nodes 3, 4, 5
        (3, 4), (4, 5), (3, 5),
    ],
    false
);

// Run label propagation
let communities = g.label_propagation(100, Some(42));

assert_eq!(communities.len(), 6);
// Triangle 1 forms one community
assert_eq!(communities[0], communities[1]);
assert_eq!(communities[1], communities[2]);
// Triangle 2 forms another community
assert_eq!(communities[3], communities[4]);
assert_eq!(communities[4], communities[5]);
// Bridge node (2 or 3) may belong to either community
```

#### How It Works

1. **Initialization**:
   - Each node starts with unique label: labels[i] = i
   - Create deterministic shuffle of node order (based on seed)

2. **Iteration** (repeat max_iter times or until convergence):
   - For each node in shuffled order:
     - Count labels of all neighbors
     - Find most common label (ties broken by smallest label)
     - Update node's label to most common
   - If no labels changed: break (converged)

3. **Termination**:
   - Return label array: communities[i] = community ID of node i
   - Nodes with same label belong to same community

**Visual Example** (undirected triangle):
```text
Graph:  0 --- 1
        |   / |
        | /   |
        2 --- 3

Initial labels: [0, 1, 2, 3]

Iteration 1 (process order: 0, 1, 2, 3):
- Node 0: neighbors {1,2}, labels {1,2}, adopt min=1 → [1,1,2,3]
- Node 1: neighbors {0,2,3}, labels {1,2,3}, adopt min=1 → [1,1,2,3]
- Node 2: neighbors {0,1,3}, labels {1,1,3}, most common=1 → [1,1,1,3]
- Node 3: neighbors {1,2}, labels {1,1}, most common=1 → [1,1,1,1]

Converged: all nodes have label 1 (single community)
```

#### Deterministic Shuffle

The seed parameter ensures reproducible results:

```rust
let g = Graph::from_edges(&[(0, 1), (1, 2), (0, 2)], false);

// Same seed → same result
let c1 = g.label_propagation(100, Some(42));
let c2 = g.label_propagation(100, Some(42));
assert_eq!(c1, c2);

// Different seed → potentially different result (but same communities)
let c3 = g.label_propagation(100, Some(99));
// c1 and c3 may differ in label values, but structure is equivalent
```

The shuffle uses a simple deterministic algorithm:
```rust
for i in 0..n {
    let j = ((seed * (i + 1)) % n) as usize;
    node_order.swap(i, j);
}
```

#### Use Cases

- **Social Networks**: Detect friend groups, interest communities
- **Biological Networks**: Identify functional modules in protein networks
- **Citation Networks**: Find research communities
- **Fraud Detection**: Detect suspicious clusters in transaction networks
- **Network Visualization**: Color nodes by community for clarity

#### Advanced Topics

**Directed Graphs**:
- Label propagation works on directed graphs but may not converge
- Strongly connected components will form single communities
- Pure directed cycles (0→1→2→0) oscillate indefinitely
- Use bidirectional edges or SCCs preprocessing for better results

**Quality Metrics**:
- **Modularity**: Measures strength of community structure (-1 to 1, higher is better)
- **Conductance**: Ratio of edges leaving community to total edges
- Not yet implemented in aprender (future roadmap)

**Comparison with Other Algorithms**:

| Algorithm | Time | Quality | Deterministic | Resolution |
|-----------|------|---------|---------------|------------|
| Label Propagation | O(m) | Medium | With seed | Fixed |
| Louvain | O(m log n) | High | No | Tunable |
| Girvan-Newman | O(m²n) | High | Yes | Hierarchical |

Label propagation is the fastest but may produce lower-quality communities. For higher quality, consider Louvain method (not yet implemented).

## Performance Comparison

### Complexity Summary

| Algorithm | Time | Space | Use Case |
|-----------|------|-------|----------|
| Common Neighbors | O(min(deg(u), deg(v))) | O(1) | Link prediction baseline |
| Adamic-Adar | O(min(deg(u), deg(v))) | O(1) | Weighted link prediction |
| Label Propagation | O(max_iter × (n+m)) | O(n) | Fast community detection |

### Benchmark Results

Synthetic graph (10K nodes, 50K edges, sparse):

```text
Common Neighbors:       0.05 ms per pair
Adamic-Adar:           0.08 ms per pair (60% slower, more informative)
Label Propagation:     12 ms (10 iterations to convergence)
```

### Choosing the Right Algorithm

**For Link Prediction**:
- Use **Common Neighbors** for:
  - Quick baseline metric
  - Maximum interpretability
  - Uniformly weighted networks

- Use **Adamic-Adar** for:
  - Networks with hubs (social, citation, web)
  - When rare connections are more informative
  - Better discriminative power

**For Community Detection**:
- Use **Label Propagation** for:
  - Large-scale networks (millions of nodes)
  - Exploratory analysis
  - When speed is critical
  - Disjoint (non-overlapping) communities

## Advanced Topics

### Link Prediction Evaluation

To evaluate link prediction, hide a fraction of edges and measure prediction accuracy:

```rust
use aprender::graph::Graph;

// Original graph
let g_full = Graph::from_edges(
    &[(0, 1), (1, 2), (2, 3), (0, 2)],
    false
);

// Training graph (hide edge 0-2)
let g_train = Graph::from_edges(
    &[(0, 1), (1, 2), (2, 3)],
    false
);

// Predict missing edge
let aa_0_2 = g_train.adamic_adar_index(0, 2).expect("nodes exist");
let aa_0_3 = g_train.adamic_adar_index(0, 3).expect("nodes exist");

// Edge 0-2 should score higher than non-edge 0-3
assert!(aa_0_2 > aa_0_3);
```

**Metrics**:
- **Precision@k**: Fraction of top-k predictions that are true edges
- **AUC-ROC**: Area under ROC curve for ranking all pairs
- Not yet implemented in aprender (future roadmap)

### Community Detection Variants

**Asynchronous Update**:
- Current implementation uses synchronous update (all nodes in one iteration)
- Asynchronous: update nodes one at a time, see immediate effects
- Faster convergence but less reproducible

**Weighted Graphs**:
- Use edge weights in neighbor voting: `label_counts[label] += weight`
- Not yet supported in aprender (future roadmap)

**Overlapping Communities**:
- Current algorithm produces disjoint communities
- Overlapping: nodes can belong to multiple communities
- Use SLPA (Speaker-Listener Label Propagation) variant

## See Also

- [Graph Algorithms]./graph-algorithms.md - Centrality and structural analysis
- [Graph Pathfinding]./graph-pathfinding.md - Shortest path algorithms
- [Graph Examples]../../../examples/graph_social_network.rs - Practical usage examples
- [Graph Specification]../../../docs/specifications/complete-graph-methods-statistics-spec.md - Complete API reference

## References

1. Liben-Nowell, D., & Kleinberg, J. (2007). "The link-prediction problem for social networks". *Journal of the American Society for Information Science and Technology*, 58(7), 1019-1031.

2. Adamic, L. A., & Adar, E. (2003). "Friends and neighbors on the Web". *Social Networks*, 25(3), 211-230.

3. Raghavan, U. N., Albert, R., & Kumara, S. (2007). "Near linear time algorithm to detect community structures in large-scale networks". *Physical Review E*, 76(3), 036106.

4. Lü, L., & Zhou, T. (2011). "Link prediction in complex networks: A survey". *Physica A: Statistical Mechanics and its Applications*, 390(6), 1150-1170.

5. Fortunato, S. (2010). "Community detection in graphs". *Physics Reports*, 486(3-5), 75-174.