calculate the adamic–adar index in python

The Adamic-Adar index is a measure of the similarity between two nodes in a network, based on the network's degree distribution. It is commonly used in link prediction tasks. You can calculate the Adamic-Adar index using the adamic_adar_index function in the networkx library.

Here is an example code snippet:

main.py
import networkx as nx

# create a graph
G = nx.Graph()
G.add_edges_from([(1,2),(1,3),(2,3),(3,4),(4,5),(4,6),(5,6)])

# calculate Adamic-Adar index between nodes 1 and 4
aai = nx.adamic_adar_index(G, [(1, 4)])
for u, v, p in aai:
    print(f"Adamic-Adar index({u}, {v}) = {p:.4f}")
282 chars
11 lines

This code will output:

main.py
Adamic-Adar index(1, 4) = 0.9102
33 chars
2 lines

which is the Adamic-Adar index between nodes 1 and 4 in the example graph.

gistlibby LogSnag