保留原始权重的加权双峰二部图投影 [英] Weighted Bimodal Bipartite Graph Projection conserving original weights
问题描述
我有一个大的(36k顶点,50k边)加权双峰二部图,我想生成一个投影,不仅像默认的加权实现那样对邻居进行计数,而且还要对边上的权重求和.您可以将其视为包含黑色顶点和蓝色顶点的二部图,在只有蓝色顶点的情况下,我想保留原始图的权重.
I have a large ( 36k vertices, 50k edges ) weighted bimodal bipartite graph and I would like to generate a projection that not only count the neighbors like the default weighted implementation but also sum the weights on the edges. You can think of it as a bipartite graph containing black vertices and blue vertices, where I want to conserve the original graph weights when there are only blue vertices.
我遇到的实现保持橙色值,我对红色表示感兴趣(或者希望得到一个双向加权的投影).
The implementations I came across keep the orange value, I am interested on the red one (or hopefully get a bi-weighted projection).
到目前为止,我已经在igraph,networkx和python-tool中进行了查看,但到目前为止,我只观察到了计算边缘数量的投影.
I've looked so far in igraph, networkx and python-tool but in so far I only observed the projection counting the amount of edges.
Networkx方法generic_weighted_projected_graph(B,节点,weight_function = None)可能可行,但我看不到(sna对我来说是新手,尽管我是Python的用户).
Networkx method generic_weighted_projected_graph(B, nodes, weight_function=None) may make this viable but I can't see how (sna is new to me, although I am an so so python user).
推荐答案
您在它是这样的:
import networkx as nx
from networkx.algorithms import bipartite
edges = [('A1','B1',3),
('A1','B2',7),
('A2','B1',2),
('A2','B2',4),
]
B = nx.Graph()
B.add_weighted_edges_from(edges)
def my_weight(G, u, v, weight='weight'):
w = 0
for nbr in set(G[u]) & set(G[v]):
w += G.edge[u][nbr].get(weight, 1) + G.edge[v][nbr].get(weight,1)
return w
G = bipartite.generic_weighted_projected_graph(B, ['A1', 'A2'], weight_function=my_weight)
print G.edges(data=True)
输出
[('A1', 'A2', {'weight': 16})]
这篇关于保留原始权重的加权双峰二部图投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!