【发布时间】:2015-12-15 14:39:28
【问题描述】:
假设我有两个 networkx 图,G 和 H:
G=nx.Graph()
fromnodes=[0,1,1,1,1,1,2]
tonodes=[1,2,3,4,5,6,7]
for x,y in zip(fromnodes,tonodes):
G.add_edge(x,y)
H=nx.Graph()
fromnodes=range(2,8)
tonodes=range(8,14)
for x,y in zip(fromnodes,tonodes):
H.add_edge(x,y)
加入两个networkx图的最佳方法是什么?
我想保留节点名称(注意公共节点,2 到 7)。当我使用nx.disjoint_union(G,H) 时,并没有发生这种情况:
>>> G.nodes()
[0, 1, 2, 3, 4, 5, 6, 7]
>>> H.nodes()
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
>>> Un= nx.disjoint_union(G,H)
>>> Un.nodes()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
#
H 节点标签已更改(不是我想要的)。我想在具有相同编号的节点处加入图表。
注意。这不是Combine two weighted graphs in NetworkX的重复项@
【问题讨论】:
-
如果两者都存在边,你想做什么?它应该成为双刃剑吗?还是只有一条边?
-
@Joel 嗯我对这两种情况都感兴趣。让我们说单边。
-
您编写的代码可以很好地处理单一边缘情况。你刚刚做的多边案例
U=nx.MultiGraph() -
还有一个简化的例子:
for x,y in zip(fromnodes,tonodes): G.add_edge(x,y)可以写成G.add_edges_from(zip(fromnodes,tonodes))
标签: python graph-theory networkx