pub fn eigenvector_centrality<G, F, E>(
graph: G,
weight_fn: F,
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 eigenvector centrality of a graph
For details on the eigenvector centrality refer to:
Phillip Bonacich. “Power and Centrality: A Family of Measures.” American Journal of Sociology 92(5):1170–1182, 1986 https://doi.org/10.1086/228631
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
eigenvector_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.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::eigenvector_centrality;
let g = petgraph::graph::UnGraph::<i32, ()>::from_edges(&[
(0, 1), (1, 2)
]);
// Calculate the eigenvector centrality
let output: Result<Option<Vec<f64>>> = eigenvector_centrality(&g, |_| {Ok(1.)}, None, None);