【问题标题】:How to add edge weights from one graph to matching edges in a different graph in NetworkX?如何将一个图的边权重添加到 NetworkX 中不同图中的匹配边?
【发布时间】:2021-05-22 20:55:34
【问题描述】:

我在 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})])

任何想法都将不胜感激,

【问题讨论】:

    标签: python graph nodes networkx edges


    【解决方案1】:

    你可以试试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 文档了解更多信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 2017-05-29
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多