I am trying to calculate the eigenvector centrality for nodes in a graph
Both packages iGraph and NetworkX have methods to calculate eigenvector centrality, but they are returning different values for the same graph
import igraph as ig
import networkx as nx
import numpy as np
count_crossing = np.loadtxt("some_weight_adjacency_matrix")
#%% iGraph
Gig = ig.Graph.Weighted_Adjacency(count_crossing, mode = 'directed')
Gig_centrality = Gig.evcent(directed = True, weights='weight') #same as G_ig.eigenvector_centrality
#%% NetworkX
Gnx = nx.DiGraph(count_crossing)
Gnx_centrality = nx.eigenvector_centrality(Gnx, max_iter=500, weight = 'weight')
Both methods return a list of eigenvector centralities for each node in the graph, but the values are not equal. The returned values from the iGraph centrality calculation are roughly twice that of the NetworkX centrality calculation, but not exactly. Both functions are running on graphs generated from the same adjacency matrix; any ideas on why they produce such different values would be much appreciated! Thanks
The specific choices igraph makes when calculating eigenvector centrality are documented here:
Typical causes of differences between packages are:
A
. This is mathematically convenient and consistent, e.g. it ensures that \sum_i A_ij
gives twice the degree of vertex j
. However, not all network analysis packages are careful about this choice.The returned values from the iGraph centrality calculation are roughly twice that of the NetworkX centrality calculation
Eigenvector centralities are just the leading eigenvector of the adjacency matrix. Eigenvectors are unique only up to a multiplication by a scalar. Thus the magnitude of eigenvector centrality scores is meaningless in isolation. It only makes sense to compare the scores of two different vertices in the same graph.
but not exactly
If you have concerns about the correctness of the result, you can always verify it by multiplying the eigenvector by the adjacency matrix, and see if you get the same vector (times some scalar) back.
I believe NetworkX uses power iteration to compute eigenvector centrality, and limits the maximum number of steps. It may not have converged to the desired accuracy. igraph uses ARPACK.