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 the katz centrality computation #1174

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading