def collaboration_weighted_projected_graph(B, nodes):
if B.is_multigraph():
raise nx.NetworkXError("not defined for multigraphs")
if B.is_directed():
pred=B.pred
G=nx.DiGraph()
else:
pred=B.adj
G=nx.Graph()
G.graph.update(B.graph)
G.add_nodes_from((n,B.node[n]) for n in nodes)
i = 0
nodes = set(nodes)
tenpercent = len(nodes) / 10
for u in nodes:
if i % tenpercent == 0:
logging.info(str(10 * i / tenpercent) + "%")
i += 1
unbrs = set(B[u])
nbrs2 = set((n for nbr in unbrs for n in B[nbr])) & nodes - set([u])
for v in nbrs2:
vnbrs = set(pred[v])
common = unbrs & vnbrs
weight = sum([1.0/(len(B[n]) - 1) for n in common if len(B[n])>1])
G.add_edge(u,v,w=weight)
return G
评论列表
文章目录