如何将一个图中的边权重添加到 NetworkX 中另一个图中的匹配边?
How to add edge weights from one graph to matching edges in a different graph in NetworkX?
我在 networkx 中有两个不同的图,一个图有全部边。另一个图是总边的子集。我如何从边图中的总集合中获取权重并将它们添加到新图中的匹配边?
#total edge collection
G.edges(data = True)
OutEdgeDataView([(1, 2, {'weight': 10}), (2, 3, {'weight': 0}), (2, 5, {'weight': 0}), (2, 6, {'weight': 0}),
(2, 1, {'weight': 0}), (3, 4, {'weight': 10}), (3, 7, {'weight': 0}), (3, 8, {'weight': 0}), (3, 2, {'weight': 0}), (4, 3, {'weight': 0}), (5, 2, {'weight': 0}), (6, 2, {'weight': 0}),
(7, 3, {'weight': 0}), (8, 3, {'weight': 0})])
T = nx.Graph()
T.add_edges_from([(1, 2), (2, 3), (2, 5), (2, 6), (3, 8), (3, 4), (3, 7)])
T.edges(data = True)
EdgeDataView([(1, 2, {}), (2, 3, {}), (2, 5, {}), (2, 6, {}), (3, 8, {}), (3, 4, {}), (3, 7, {})])
我希望 T EdgeDataView 看起来像
EdgeDataView([(1, 2, {'weight':10}), (2, 3, {'weight': 0}), (2, 5, {'weight': 0}), (2, 6, {'weight': 0}),
(3, 8, {'weight': 0}), (3, 4, {'weight': 10}), (3, 7, {'weight': 0})])
如有任何想法,我们将不胜感激,
您可以尝试 networkx Graph.edge_subgraph
function。
举个例子。
首先创建图表:
G = nx.DiGraph()
G.add_edges_from([(1, 2, {'weight': 10}), (2, 3, {'weight': 0}), (2, 5, {'weight': 0}), (2, 6, {'weight': 0}),
(2, 1, {'weight': 0}), (3, 4, {'weight': 10}), (3, 7, {'weight': 0}), (3, 8, {'weight': 0}), (3, 2, {'weight': 0}), (4, 3, {'weight': 0}), (5, 2, {'weight': 0}), (6, 2, {'weight': 0}),
(7, 3, {'weight': 0}), (8, 3, {'weight': 0})])
接下来,选择您假装添加到新图中的节点:
edge_set = [(1, 2), (2, 3), (2, 5), (2, 6), (3, 8), (3, 4), (3, 7)]
然后,从一个图提取边到另一个图:
Di_T = G.edge_subgraph(edge_set)
请注意,由于 G
是定向的 T
也将是定向的,所以:
T = Di_T.to_undirected() # see NOTE in the end
结果
>>> T.edges(data = True)
EdgeDataView([ (1, 2, {'weight': 10}),
(2, 3, {'weight': 0}),
(2, 5, {'weight': 0}),
(2, 6, {'weight': 0}),
(3, 4, {'weight': 10}),
(3, 7, {'weight': 0}),
(3, 8, {'weight': 0})])
完整代码:
# example graph
G = nx.DiGraph()
G.add_edges_from([(1, 2, {'weight': 10}), (2, 3, {'weight': 0}), (2, 5, {'weight': 0}), (2, 6, {'weight': 0}), (2, 1, {'weight': 0}), (3, 4, {'weight': 10}), (3, 7, {'weight': 0}), (3, 8, {'weight': 0}), (3, 2, {'weight': 0}), (4, 3, {'weight': 0}), (5, 2, {'weight': 0}), (6, 2, {'weight': 0}), (7, 3, {'weight': 0}), (8, 3, {'weight': 0})])
# example edge set
edge_set = [(1, 2), (2, 3), (2, 5), (2, 6), (3, 8), (3, 4), (3, 7)]
# solution
T = G.edge_subgraph(edge_set).to_undirected()
T.edges(data = True)
注意:
通常是 G.edge_subgraph(edge_set)
的副本
(使用 .copy
)
为了获得图表的新副本而不是
原始图表的参考(参见 the notes in the Docs)。
但是,.to_undirected
已经对图表进行了深度复制
所以不需要 .copy
,检查 G.edge_subgraph
和
.to_undirected
更多信息的文档
我在 networkx 中有两个不同的图,一个图有全部边。另一个图是总边的子集。我如何从边图中的总集合中获取权重并将它们添加到新图中的匹配边?
#total edge collection
G.edges(data = True)
OutEdgeDataView([(1, 2, {'weight': 10}), (2, 3, {'weight': 0}), (2, 5, {'weight': 0}), (2, 6, {'weight': 0}),
(2, 1, {'weight': 0}), (3, 4, {'weight': 10}), (3, 7, {'weight': 0}), (3, 8, {'weight': 0}), (3, 2, {'weight': 0}), (4, 3, {'weight': 0}), (5, 2, {'weight': 0}), (6, 2, {'weight': 0}),
(7, 3, {'weight': 0}), (8, 3, {'weight': 0})])
T = nx.Graph()
T.add_edges_from([(1, 2), (2, 3), (2, 5), (2, 6), (3, 8), (3, 4), (3, 7)])
T.edges(data = True)
EdgeDataView([(1, 2, {}), (2, 3, {}), (2, 5, {}), (2, 6, {}), (3, 8, {}), (3, 4, {}), (3, 7, {})])
我希望 T EdgeDataView 看起来像
EdgeDataView([(1, 2, {'weight':10}), (2, 3, {'weight': 0}), (2, 5, {'weight': 0}), (2, 6, {'weight': 0}),
(3, 8, {'weight': 0}), (3, 4, {'weight': 10}), (3, 7, {'weight': 0})])
如有任何想法,我们将不胜感激,
您可以尝试 networkx Graph.edge_subgraph
function。
举个例子。
首先创建图表:
G = nx.DiGraph()
G.add_edges_from([(1, 2, {'weight': 10}), (2, 3, {'weight': 0}), (2, 5, {'weight': 0}), (2, 6, {'weight': 0}),
(2, 1, {'weight': 0}), (3, 4, {'weight': 10}), (3, 7, {'weight': 0}), (3, 8, {'weight': 0}), (3, 2, {'weight': 0}), (4, 3, {'weight': 0}), (5, 2, {'weight': 0}), (6, 2, {'weight': 0}),
(7, 3, {'weight': 0}), (8, 3, {'weight': 0})])
接下来,选择您假装添加到新图中的节点:
edge_set = [(1, 2), (2, 3), (2, 5), (2, 6), (3, 8), (3, 4), (3, 7)]
然后,从一个图提取边到另一个图:
Di_T = G.edge_subgraph(edge_set)
请注意,由于 G
是定向的 T
也将是定向的,所以:
T = Di_T.to_undirected() # see NOTE in the end
结果
>>> T.edges(data = True)
EdgeDataView([ (1, 2, {'weight': 10}),
(2, 3, {'weight': 0}),
(2, 5, {'weight': 0}),
(2, 6, {'weight': 0}),
(3, 4, {'weight': 10}),
(3, 7, {'weight': 0}),
(3, 8, {'weight': 0})])
完整代码:
# example graph
G = nx.DiGraph()
G.add_edges_from([(1, 2, {'weight': 10}), (2, 3, {'weight': 0}), (2, 5, {'weight': 0}), (2, 6, {'weight': 0}), (2, 1, {'weight': 0}), (3, 4, {'weight': 10}), (3, 7, {'weight': 0}), (3, 8, {'weight': 0}), (3, 2, {'weight': 0}), (4, 3, {'weight': 0}), (5, 2, {'weight': 0}), (6, 2, {'weight': 0}), (7, 3, {'weight': 0}), (8, 3, {'weight': 0})])
# example edge set
edge_set = [(1, 2), (2, 3), (2, 5), (2, 6), (3, 8), (3, 4), (3, 7)]
# solution
T = G.edge_subgraph(edge_set).to_undirected()
T.edges(data = True)
注意:
通常是 G.edge_subgraph(edge_set)
的副本
(使用 .copy
)
为了获得图表的新副本而不是
原始图表的参考(参见 the notes in the Docs)。
但是,.to_undirected
已经对图表进行了深度复制
所以不需要 .copy
,检查 G.edge_subgraph
和
.to_undirected
更多信息的文档