Function rustworkx_core::centrality::katz_centrality
source · pub fn katz_centrality<G, F, E>(
graph: G,
weight_fn: F,
alpha: Option<f64>,
beta_map: Option<HashMap<usize, f64>>,
beta_scalar: Option<f64>,
max_iter: Option<usize>,
tol: Option<f64>
) -> Result<Option<Vec<f64>>, E>where
G: NodeIndexable + IntoNodeIdentifiers + IntoNeighbors + IntoEdges + NodeCount,
G::NodeId: Eq + Hash,
F: FnMut(G::EdgeRef) -> Result<f64, E>,
Expand description
Compute the Katz centrality of a graph
For details on the Katz centrality refer to:
Leo Katz. “A New Status Index Derived from Sociometric Index.” Psychometrika 18(1):39–43, 1953 https://link.springer.com/content/pdf/10.1007/BF02289026.pdf
This function uses a power iteration method to compute the eigenvector
and convergence is not guaranteed. The function will stop when max_iter
iterations is reached or when the computed vector between two iterations
is smaller than the error tolerance multiplied by the number of nodes.
The implementation of this algorithm is based on the NetworkX
katz_centrality()
function.
In the case of multigraphs the weights of any parallel edges will be summed when computing the eigenvector centrality.
Arguments:
graph
- The graph object to run the algorithm onweight_fn
- An input callable that will be passed theEdgeRef
for an edge in the graph and is expected to return aResult<f64>
of the weight of that edge.alpha
- Attenuation factor. If set toNone
, a default value of 0.1 is used.beta_map
- Immediate neighbourhood weights. Must contain all node indices or beNone
.beta_scalar
- Immediate neighbourhood scalar that replacesbeta_map
in casebeta_map
is None. Defaults to 1.0 in caseNone
is provided.max_iter
- The maximum number of iterations in the power method. If set toNone
a default value of 100 is used.tol
- The error tolerance used when checking for convergence in the power method. If set toNone
a default value of 1e-6 is used.
Example
use rustworkx_core::Result;
use rustworkx_core::petgraph;
use rustworkx_core::petgraph::visit::{IntoEdges, IntoNodeIdentifiers};
use rustworkx_core::centrality::katz_centrality;
let g = petgraph::graph::UnGraph::<i32, ()>::from_edges(&[
(0, 1), (1, 2)
]);
// Calculate the eigenvector centrality
let output: Result<Option<Vec<f64>>> = katz_centrality(&g, |_| {Ok(1.)}, None, None, None, None, None);
let centralities = output.unwrap().unwrap();
assert!(centralities[1] > centralities[0], "Node 1 is more central than node 0");
assert!(centralities[1] > centralities[2], "Node 1 is more central than node 2");