Search code examples
pythongraphnetworkxneighbours

Create graph using edges from csv files and calculate common neighbors between its nodes


I have two files': nodes.csv and edges.csv that contains nodes and edges between them as follow nodes.csv (182 nodes) look like

0
1
2
.
.
.

and edges represented by 2 columns (300 edges) as

1  2
0  2
.
. 

I want to represent nodes and edges that is in these files by graph and calculate the common neighbors between each pair of nodes that is represented by edges

I have trying with code

import networkx as nx
import pandas as pd
G=nx.Graph()

node= pd.read_csv("nodes.csv")
edges=pd.read_csv("edges.csv")
for row in node: 
    G.add_nodes_from(row)
for row in edges:
    if len(row) == 2 : # add an edge only if both values are provided
        G.add_edge(row[0],row[1])

print(G)   # this is give my Graph with 4 nodes and 1 edges 

def predict(u, v):
     cnbors = list(nx.common_neighbors(G, u, v))
     mult_val = G.degree(u) * G.degree(v)
     if mult_val == 0:
         return 0
     else:
         return len(cnbors)/ mult_val

for row in edges:
    predict(row[0], row[1]) # this is give me an error: u is not in the graph

Solution

  • First you can produce your graph object by reading edges.csv file using read_edgelist. There may possibly be some nodes that are not in the edges.csv file, hence you can add them manually later. The graph is ready with the code below:

    import networkx as nx
    import pandas as pd
    
    g = nx.read_edgelist("edges.csv", nodetype=int)
    nodes = pd.read_csv("nodes.csv")
    for node in nodes.values:
        g.add_node(node[0])
    

    Now you can simply get all possible pairs of nodes and their common neighbors like this:

    common_neighbors = [(n1, n2, list(nx.common_neighbors(g, n1, n2))) for n1 in g.nodes() for n2 in g.nodes() if n2 > n1]
    

    The result is a list of tuples each of which with three items, containing the first node, the second node, and the list of common neighbors.

    [(0, 1, [2, 4]),
     (0, 2, [1, 3]),
     (0, 3, [2, 4]),
     (0, 4, [1, 3]),
    ...