-
Notifications
You must be signed in to change notification settings - Fork 0
/
CNR.py
44 lines (31 loc) · 966 Bytes
/
CNR.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 18 12:21:14 2019
@author: georgiabaltsou
"""
import networkx as nx
import csv
import os
import time
import shutil
import sys
import copy
from collections import defaultdict
from numpy import array
def cnr(seeds, G, Graph):
for node in seeds:
node_nei = len(list(Graph[node].keys()))
for i in G[node]:
i_nei = len(list(Graph[i].keys()))
if G.has_edge(node, i):
A = 1
else:
A = 0
Ac = len(sorted(nx.common_neighbors(G, node, i)))
c = (2*(A+Ac))/(node_nei+i_nei)
Graph[node][i] = c + 1 #add 1 because c is between 0 and 1
Graph[i][node] = c + 1
for source, target in G.edges():
G[source][target]['weight'] = Graph[source][target]
return G, Graph