Skip to main content

clustering_coefficients

Function clustering_coefficients 

Source
pub fn clustering_coefficients<G: Graph>(graph: &G) -> Vec<f64>
Expand description

Local clustering coefficient for each node.

C(v) = 2 * triangles(v) / (deg(v) * (deg(v) - 1)), or 0.0 if deg < 2.

use graphops::graph::Graph;
use graphops::triangle::clustering_coefficients;

struct G(Vec<Vec<usize>>);
impl Graph for G {
    fn node_count(&self) -> usize { self.0.len() }
    fn neighbors(&self, n: usize) -> Vec<usize> { self.0[n].clone() }
}

// K4: every node has clustering coefficient 1.0
let g = G(vec![
    vec![1, 2, 3], vec![0, 2, 3], vec![0, 1, 3], vec![0, 1, 2],
]);
let cc = clustering_coefficients(&g);
for &c in &cc {
    assert!((c - 1.0).abs() < 1e-12);
}