Search code examples
python-3.xnetworkx

How to find the similarity between pair of vertices using Katz Index in Python?


I am trying to find the similarity score between pairs of nodes using Katz index. Example if i have edge(V1,V2) what is their katz similarity score? Currently i have tried to use networkx function as below:

import networkx as mx
G=mx.karate_club_graph()
 katz =mx.katz.katz_centrality(G. alpha=0.01, beta=1)

but i am getting centrality for each node. how can i get the same for each pair of nodes? Thank you in advance


Solution

  • This functionality is not available in networkx. An alternative can be to calculate this score by using the formula. For detail please refer this paper: https://appliednetsci.springeropen.com/articles/10.1007/s41109-018-0080-5

    An example code is as follows:

    import networkx as nx
    import numpy as np
    from numpy.linalg import inv
    
    G = nx.karate_club_graph()
    
    #Calculate highest eigenvector
    L = nx.normalized_laplacian_matrix(G)
    e = np.linalg.eigvals(L.A)
    print("Largest eigenvalue:", max(e))
    beta = 1/max(e)
    I = np.identity(len(G.nodes)) #create identity matrix
    
    #Katz score
    inv(I - nx.to_numpy_array(G)*beta) - I