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

Function run

nodedb/src/engine/graph/algo/harmonic.rs:20–41  ·  view source on GitHub ↗

Run Harmonic Centrality on the CSR index. Returns `(node_id, centrality)` sorted by centrality descending.

(csr: &CsrIndex)

Source from the content-addressed store, hash-verified

18///
19/// Returns `(node_id, centrality)` sorted by centrality descending.
20pub fn run(csr: &CsrIndex) -> AlgoResultBatch {
21 let n = csr.node_count();
22 if n == 0 {
23 return AlgoResultBatch::new(GraphAlgorithm::Harmonic);
24 }
25
26 let normalizer = if n > 1 { (n - 1) as f64 } else { 1.0 };
27 let mut scored: Vec<(usize, f64)> = Vec::with_capacity(n);
28
29 for v in 0..n {
30 let inv_sum = bfs_inverse_distances(csr, v as u32, n);
31 scored.push((v, inv_sum / normalizer));
32 }
33
34 scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
35
36 let mut batch = AlgoResultBatch::new(GraphAlgorithm::Harmonic);
37 for (node, centrality) in scored {
38 batch.push_node_f64(csr.node_name_raw(node as u32).to_string(), centrality);
39 }
40 batch
41}
42
43/// BFS from source, return sum of 1/d(source, u) for all reachable u.
44fn bfs_inverse_distances(csr: &CsrIndex, source: u32, n: usize) -> f64 {

Callers 3

harmonic_pathFunction · 0.70
harmonic_disconnectedFunction · 0.70
harmonic_completeFunction · 0.70

Calls 7

bfs_inverse_distancesFunction · 0.85
push_node_f64Method · 0.80
to_stringMethod · 0.80
node_countMethod · 0.45
pushMethod · 0.45
partial_cmpMethod · 0.45
node_name_rawMethod · 0.45

Tested by 3

harmonic_pathFunction · 0.56
harmonic_disconnectedFunction · 0.56
harmonic_completeFunction · 0.56