Run Harmonic Centrality on the CSR index. Returns `(node_id, centrality)` sorted by centrality descending.
(csr: &CsrIndex)
| 18 | /// |
| 19 | /// Returns `(node_id, centrality)` sorted by centrality descending. |
| 20 | pub 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. |
| 44 | fn bfs_inverse_distances(csr: &CsrIndex, source: u32, n: usize) -> f64 { |