Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove The Hashmap from Shorted Path for Centrality Computation #1307

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
50 changes: 23 additions & 27 deletions rustworkx-core/src/centrality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,11 @@ fn _accumulate_vertices<G>(
let mut delta = vec![0.0; max_index];
for w in &path_calc.verts_sorted_by_distance {
let iw = graph.to_index(*w);
let coeff = (1.0 + delta[iw]) / path_calc.sigma[w];
let p_w = path_calc.predecessors.get(w).unwrap();
for v in p_w {
let iv = graph.to_index(*v);
delta[iv] += path_calc.sigma[v] * coeff;
let coeff = (1.0 + delta[iw]) / path_calc.sigma[iw];
let p_w = path_calc.predecessors.get(iw).unwrap();
for iv in p_w {
//let iv = graph.to_index(*v);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this comment

delta[*iv] += path_calc.sigma[*iv] * coeff;
}
}
let mut betweenness = locked_betweenness.write().unwrap();
Expand Down Expand Up @@ -342,43 +342,39 @@ where
<G as GraphBase>::NodeId: std::cmp::Eq + Hash,
{
verts_sorted_by_distance: Vec<G::NodeId>,
predecessors: HashMap<G::NodeId, Vec<G::NodeId>>,
sigma: HashMap<G::NodeId, f64>,
predecessors: Vec<Vec<usize>>,
sigma: Vec<f64>,
}

fn shortest_path_for_centrality<G>(graph: G, node_s: &G::NodeId) -> ShortestPathData<G>
where
G: NodeIndexable + IntoNodeIdentifiers + IntoNeighborsDirected + NodeCount + GraphBase,
<G as GraphBase>::NodeId: std::cmp::Eq + Hash,
{
let mut verts_sorted_by_distance: Vec<G::NodeId> = Vec::new(); // a stack
let c = graph.node_count();
let mut predecessors = HashMap::<G::NodeId, Vec<G::NodeId>>::with_capacity(c);
let mut sigma = HashMap::<G::NodeId, f64>::with_capacity(c);
let mut distance = HashMap::<G::NodeId, i64>::with_capacity(c);
let max_index = graph.node_bound();
let mut verts_sorted_by_distance: Vec<G::NodeId> = Vec::with_capacity(c); // a stack
let mut predecessors: Vec<Vec<usize>> = vec![Vec::new(); max_index];
let mut sigma: Vec<f64> = vec![0.; max_index];
let mut distance: Vec<i64> = vec![-1; max_index];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more appropriate type here is Option<i64> if you want to represent missing paths. I'd even say Option<usize>

#[allow(non_snake_case)]
let mut Q: VecDeque<G::NodeId> = VecDeque::with_capacity(c);

for node in graph.node_identifiers() {
predecessors.insert(node, Vec::new());
sigma.insert(node, 0.0);
distance.insert(node, -1);
}
Comment on lines -361 to -366
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Se how the hashmap was full filled with value for all node of the graph, so replacing with a vec of size of node bound will no be a problem for cache efficiency, because hashmap was already full when the algorithm started

sigma.insert(*node_s, 1.0);
distance.insert(*node_s, 0);
let node_s_index = graph.to_index(*node_s);
sigma[node_s_index] = 1.0;
distance[node_s_index] = 0;
Q.push_back(*node_s);
while let Some(v) = Q.pop_front() {
verts_sorted_by_distance.push(v);
let distance_v = distance[&v];
let v_idx = graph.to_index(v);
let distance_v = distance[v_idx];
for w in graph.neighbors(v) {
if distance[&w] < 0 {
let w_idx = graph.to_index(w);
if distance[w_idx] < 0 {
Q.push_back(w);
distance.insert(w, distance_v + 1);
distance[w_idx] = distance_v + 1;
}
if distance[&w] == distance_v + 1 {
sigma.insert(w, sigma[&w] + sigma[&v]);
let e_p = predecessors.get_mut(&w).unwrap();
e_p.push(v);
if distance[w_idx] == distance_v + 1 {
sigma[w_idx] += sigma[v_idx];
predecessors[w_idx].push(v_idx);
}
}
}
Expand Down