MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / compute_modularity

Function compute_modularity

nodedb/src/engine/graph/algo/louvain.rs:188–207  ·  view source on GitHub ↗

Compute modularity Q = (1/2m) * sum_ij [ A_ij - (k_i * k_j) / 2m ] * delta(c_i, c_j).

(
    adj: &[Vec<(usize, f64)>],
    community: &[usize],
    node_degree: &[f64],
    total_weight: f64,
    resolution: f64,
)

Source from the content-addressed store, hash-verified

186
187/// Compute modularity Q = (1/2m) * sum_ij [ A_ij - (k_i * k_j) / 2m ] * delta(c_i, c_j).
188fn compute_modularity(
189 adj: &[Vec<(usize, f64)>],
190 community: &[usize],
191 node_degree: &[f64],
192 total_weight: f64,
193 resolution: f64,
194) -> f64 {
195 let m2 = 2.0 * total_weight;
196 let mut q = 0.0f64;
197
198 for (u, neighbors) in adj.iter().enumerate() {
199 for &(v, w) in neighbors {
200 if community[u] == community[v] {
201 q += w - resolution * node_degree[u] * node_degree[v] / m2;
202 }
203 }
204 }
205
206 q / m2
207}
208
209#[cfg(test)]
210mod tests {

Callers 1

runFunction · 0.85

Calls 1

iterMethod · 0.45

Tested by

no test coverage detected