Skip to content

Commit

Permalink
Remove the Hashmap from the katz centrality computation (#1174)
Browse files Browse the repository at this point in the history
* Remove the Hashmap of the katz centrality computation to avoid better performance

* cargo fmt
  • Loading branch information
Paulo-21 authored Apr 25, 2024
1 parent 7302c8b commit cc01ee8
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions rustworkx-core/src/centrality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,23 +738,18 @@ where
{
let alpha: f64 = alpha.unwrap_or(0.1);

let mut beta: HashMap<usize, f64> = beta_map.unwrap_or_default();
let beta: HashMap<usize, f64> = beta_map.unwrap_or_default();
//Initialize the beta vector in case a beta map was not provided
let mut beta_v = vec![beta_scalar.unwrap_or(1.0); graph.node_bound()];

if beta.is_empty() {
// beta_map was none
// populate hashmap with default value
let beta_scalar = beta_scalar.unwrap_or(1.0);
for node_index in graph.node_identifiers() {
let node = graph.to_index(node_index);
beta.insert(node, beta_scalar);
}
} else {
if !beta.is_empty() {
// Check if beta contains all node indices
for node_index in graph.node_identifiers() {
let node = graph.to_index(node_index);
if !beta.contains_key(&node) {
return Ok(None); // beta_map was provided but did not include all nodes
}
beta_v[node] = *beta.get(&node).unwrap(); //Initialize the beta vector with the provided values
}
}

Expand All @@ -776,7 +771,7 @@ where
}
for node_index in graph.node_identifiers() {
let node = graph.to_index(node_index);
x[node] = alpha * x[node] + beta.get(&node).unwrap_or(&0.0);
x[node] = alpha * x[node] + beta_v[node];
}
if (0..x.len())
.map(|node| (x[node] - x_last[node]).abs())
Expand Down

0 comments on commit cc01ee8

Please sign in to comment.