in src/dachshund/algorithms/betweenness.rs [17:47]
fn get_node_betweenness_starting_from_sources(
&self,
sources: &[NodeId],
check_is_connected: bool,
nodes_in_connected_component: Option<Vec<NodeId>>,
) -> Result<HashMap<NodeId, f64>, &'static str> {
if self.count_nodes() == 0 {
return Err("Graph is empty");
}
if check_is_connected && !self.get_is_connected().unwrap() {
return Err("Graph should be connected to compute betweenness.");
}
let mut path_counts: HashMap<NodeId, f64> = HashMap::new();
for node_id in self.get_ids_iter() {
path_counts.insert(*node_id, 0.0);
}
for source in sources.iter() {
let (dist, parents) = self.get_shortest_paths(*source, &nodes_in_connected_component);
let shortest_paths = self.enumerate_shortest_paths(&dist, &parents, *source);
for paths in shortest_paths.values() {
let weight: f64 = 0.5 / paths.len() as f64;
for path in paths {
for id in path.iter().skip(1).rev().skip(1) {
*path_counts.get_mut(id).unwrap() += weight;
}
}
}
}
Ok(path_counts)
}