From 88f51228bd017992e86294e2e64b4b7c9a7cb908 Mon Sep 17 00:00:00 2001 From: Paulo-21 Date: Wed, 24 Apr 2024 15:41:34 +0200 Subject: [PATCH 1/2] Remove the Hashmap of the katz centrality computation to avoid better performance --- rustworkx-core/src/centrality.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/rustworkx-core/src/centrality.rs b/rustworkx-core/src/centrality.rs index 35683bbc8..65ed36a5f 100644 --- a/rustworkx-core/src/centrality.rs +++ b/rustworkx-core/src/centrality.rs @@ -738,23 +738,18 @@ where { let alpha: f64 = alpha.unwrap_or(0.1); - let mut beta: HashMap = beta_map.unwrap_or_default(); - - 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 { + let beta: HashMap = 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() { // 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 } } @@ -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()) From 3087023489817fc07c825b3ab8696d63cbca34a7 Mon Sep 17 00:00:00 2001 From: Paulo-21 Date: Wed, 24 Apr 2024 16:18:54 +0200 Subject: [PATCH 2/2] cargo fmt --- rustworkx-core/src/centrality.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rustworkx-core/src/centrality.rs b/rustworkx-core/src/centrality.rs index 65ed36a5f..09e21affa 100644 --- a/rustworkx-core/src/centrality.rs +++ b/rustworkx-core/src/centrality.rs @@ -740,8 +740,8 @@ where let beta: HashMap = 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()]; - + let mut beta_v = vec![beta_scalar.unwrap_or(1.0); graph.node_bound()]; + if !beta.is_empty() { // Check if beta contains all node indices for node_index in graph.node_identifiers() {