def weighted_local_path_index(G, ebunch=None):
if ebunch is None:
ebunch = nx.non_edges(G)
def predict(u, v):
#NeighborSet = nx.all_neighbors(G, u)
#len( sorted(nx.common_neighbors(G, u, v) ))
paths = list( nx.all_simple_paths(G, source=u, target=v, cutoff=3))
#print paths
A2_weight = 0.0
A3_weight = 0.0
for path in paths:
if len(path) == 3:
for node in range(0, len(path)-1):
A2_weight = A2_weight + G[path[node]][path[node+1]]['weight']
elif len(path) == 4:
for node in range(0, len(path)-1):
A3_weight = A3_weight + G[path[node]][path[node+1]]['weight']
#value = sum( G[w][u]['weight'] + G[w][v]['weight'] for w in nx.common_neighbors(G, u, v) )
return A2_weight + 0.001 * A3_weight #+value #Coefficient = 0.001
Rank_List = []
for u, v in ebunch:
Rank_List.append((u, v, predict(u, v)))
return Rank_List #((u, v, predict(u, v)) for u, v in ebunch)
#return ((u, v, predict(u, v)) for u, v in ebunch)
评论列表
文章目录