Search code examples
networkxeigenvaluenode-centrality

Networkx - Katz centrality in projected graphs


I have the following bipartite network image I'm trying to reproduce:

enter image description here

I tried:

bi_graph = nx.Graph()
bi_graph.add_edges_from([(1,"a"), (2,"a"), (2,"b"), (3,"a"), (3,"b"), (4,"b"), (4,"c"), (5,"b"), (5,"d"), (6,"c"), (6,"d"), (7, "d")])
pos = nx.bipartite_layout(bi_graph, nodes= [1, 2, 3, 4, 5, 6, 7])
nx.draw(bi_graph, pos=pos, with_labels=True, node_color='pink')
plt.title('rede bipartida')
plt.show()

enter image description here


Now I need to make two graph projections:

one for the letters

projection_letters = nx.bipartite.projected_graph(bi_graph, ["a", "b", "c", "d"])

nx.draw(projection_letters, with_labels=True, node_color='gray')
plt.title('projeção (vértices do tipo letras)')
plt.show()

enter image description here

one for the numbers

projection_numbers = nx.bipartite.projected_graph(bi_graph, [1, 2, 3, 4, 5, 6, 7])

nx.draw(projection_numbers, with_labels=True, node_color='pink')
plt.title('projeção (vértices do tipo número)')
plt.show()

enter image description here


Finally I need to calculate 2 things:

-katz centrality for projection_numbers and find lowest and highest node values for alpha=0.15, like so:

print('Katz:')
alpha = 0.15
for node, val in nx.katz_centrality(projection_numbers,alpha=alpha).items():
    print("%s %.2f" % (node, val))

which prints:

1 0.29 (low)
2 0.41
3 0.41
4 0.41
5 0.45 (high)
6 0.35
7 0.29

I though low and high would be nodes 1 and 5, but I'm getting this answer as wrong..


-Calculate the highest alpha value for projection_letters, which I'm told is the inverse of the highest engeinvalue.

How do I calculate this top alpha?


Solution

  • To find the maximum alpha value, you would first need to compute the eigenvalues of the adjacency matrix of your projection_letters graph (see more info here) and then compute the inverse of the max absolute eigenvalue.

    You can extract the set of eigenvalues with nx.adjacency_spectrum (doc).

    See full code below:

    adj_mat_proj_letters_spec=nx.adjacency_spectrum(projection_letters) 
    alpha_max=1/np.amax(np.abs(adj_mat_proj_letters_spec))
    print('Max alpha: ' +str(alpha_max))
    

    And the output returns:

    Max alpha: 0.460811127189111