Python 合并(加入)networkx 图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32652149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Combine (join) networkx Graphs
提问by atomh33ls
Say I have two networkx graphs, G
and H
:
假设我有两个 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)
What is the best way to join the two networkx graphs?
加入两个networkx图的最佳方法是什么?
I'd like to preserve the node names (note the common nodes, 2 to 7). When I used nx.disjoint_union(G,H)
, this did not happen:
我想保留节点名称(注意常见节点,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]
#
The H
node labels were changed (not what I want). I want to join the graphs at the nodes with the same number.
该H
节点标签被改变(不是我想要的)。我想在具有相同编号的节点处加入图形。
Note. This is not a duplicate of Combine two weighted graphs in NetworkX.
笔记。这不是在 NetworkX中结合两个加权图的副本。
采纳答案by Joel
The function you're looking for is compose, which produces a graph with all the edges and all the nodes that are in both graphs. If both graphs have a node with the same name, then a single copy ends up in the new graph. Similarly if the same edge exists in both. Here's an example, including edge/node attributes:
您正在寻找的函数是compose,它生成一个图形,其中包含两个图形中的所有边和所有节点。如果两个图都有一个同名的节点,那么新图中会出现一个副本。同样,如果两者都存在相同的边。这是一个示例,包括边/节点属性:
import networkx as nx
G=nx.Graph()
G.add_node(1, weight = 2)
G.add_node(2, weight = 3)
G.add_edge(1,2, flux = 5)
G.add_edge(2,4)
H=nx.Graph()
H.add_node(1, weight = 4)
H.add_edge(1,2, flux = 10)
H.add_edge(1,3)
F = nx.compose(G,H)
#F has all nodes & edges of both graphs, including attributes
#Where the attributes conflict, it uses the attributes of H.
G.nodes(data=True)
> NodeDataView({1: {'weight': 2}, 2: {'weight': 3}, 4: {}})
H.nodes(data=True)
> NodeDataView({1: {'weight': 4}, 2: {}, 3: {}})
F.nodes(data=True)
> NodeDataView({1: {'weight': 4}, 2: {'weight': 3}, 4: {}, 3: {}})
G.edges(data=True)
> EdgeDataView([(1, 2, {'flux': 5}), (2, 4, {})])
H.edges(data=True)
> EdgeDataView([(1, 2, {'flux': 10}), (1, 3, {})])
F.edges(data=True)
EdgeDataView([(1, 2, {'flux': 10}), (1, 3, {}), (2, 4, {})])
These preserve attributes, but obviously if there is a conflict this is not possible. The attributes of H
take precedence.
这些保留属性,但显然如果存在冲突,这是不可能的。H
优先的属性。
There are also other options to do the symmetric difference, intersection, ...
还有其他选项可以进行对称差异,交集,...
If you have multiple graphs to join together, you can use compose_all
, which just wraps a for loop around compose
.
如果您有多个图要连接在一起,则可以使用compose_all
,它只是围绕 包装了一个 for 循环compose
。
回答by atomh33ls
This did it.
这做到了。
U=nx.Graph()
U.add_edges_from(G.edges()+H.edges())
U.add_nodes_from(G.nodes()+H.nodes()) #deals with isolated nodes
or, preserving the edge attributes:
或者,保留边缘属性:
U.add_edges_from(G.edges(data=True)+H.edges(data=True))
and, to also preserve the node attributes:
并且,还要保留节点属性:
U.add_nodes_from(G.nodes(data=True)+H.nodes(data=True))