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 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

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:

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:

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

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:

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

AspectCommon NeighborsAdamic-Adar
WeightingUniform (all neighbors equal)Inverse log degree (rare > common)
Hub SensitivityHigh (hubs dominate)Low (hubs downweighted)
ComplexityO(min(deg(u), deg(v)))O(min(deg(u), deg(v)))
InterpretabilityVery simpleMore nuanced
PerformanceGood baselineOften better on real networks
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

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):

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:

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:

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:

AlgorithmTimeQualityDeterministicResolution
Label PropagationO(m)MediumWith seedFixed
LouvainO(m log n)HighNoTunable
Girvan-NewmanO(m²n)HighYesHierarchical

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

Performance Comparison

Complexity Summary

AlgorithmTimeSpaceUse Case
Common NeighborsO(min(deg(u), deg(v)))O(1)Link prediction baseline
Adamic-AdarO(min(deg(u), deg(v)))O(1)Weighted link prediction
Label PropagationO(max_iter × (n+m))O(n)Fast community detection

Benchmark Results

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

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

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

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

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.