Skip to main content

modularity_matrix

Function modularity_matrix 

Source
pub fn modularity_matrix(
    graph: &Graph,
    weights: Option<&[f64]>,
    resolution: f64,
    directed: bool,
) -> IgraphResult<Vec<Vec<f64>>>
Expand description

Compute the modularity matrix B of a graph.

Returns a dense n×n matrix (row-major Vec<Vec<f64>>).

  • weights: optional edge weights (length must equal ecount()).
  • resolution: the γ parameter (≥ 0; default 1.0).
  • directed: for directed graphs, whether to use directed formula. Ignored for undirected graphs.

Returns an empty matrix for graphs with no vertices. Returns an error when the total edge weight is zero (the matrix is undefined in that case, matching upstream’s documentation).

§Examples

use rust_igraph::{Graph, modularity_matrix};

// Triangle (K3): A is all-1 off-diagonal (undirected stores both
// directions). Each vertex has degree 2, m = 3, so
// B_ij = A_ij - 2*2/(2*3) = 1 - 2/3 = 1/3 for i≠j,
// B_ii = 0 - 4/6 = -2/3.
let mut g = Graph::with_vertices(3);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 0).unwrap();
let b = modularity_matrix(&g, None, 1.0, true).unwrap();
assert!((b[0][0] - (-2.0/3.0)).abs() < 1e-10);
assert!((b[0][1] - 1.0/3.0).abs() < 1e-10);