如何正确访问 Networkx 中的边缘属性

How to properly access edge properties in Networkx

我有一个图 G,我首先通过一组规则构建其上的所有边,然后我想随机删除其中的一些边。 (每个节点都有一个 属性 "label" 并且所有边在创建时都分配了权重 属性。)

我正在 Python 3.4.2 使用 Networkx 1.9.1。

下面的代码是我到目前为止一直在尝试做的,如果节点的度数高于阈值,我打算从节点中删除一条边 max_degree.

    import networkx as nx
    import random

    G = nx.Graph()
    #  ...rest of code, adding nodes and edges...

    # Remove undesired edges
    max_degree = 4
    for node in G.nodes():
        if nx.degree(G, node) >= max_degree:
            node_edges = G.edges([node])
            edge_to_remove = random.choice(node_edges)
            #  Each edge is a tuple (u,v), 
            #  where u and v are nodes in G.
            edge_u = G.node[edge_to_remove[0]]
            edge_v = G.node[edge_to_remove[1]]
            weight_loss = edge_to_remove['weight']
            print("Removing edge (weight={2}) from {0} to {1}"
                    .format(edge_u['label'], edge_v['label'], weight_loss))
            G.remove_edges_from([edge_to_remove])

首先,这行不通,其次,我的直觉告诉我,这很麻烦。 tutorial提示下面的代码是如何访问边缘属性的。文档指向教程,但我缺少这个:

    #  From tutorial, accessing edge properties:
    G.add_edge(1, 2, weight=4.7)
    G[1][2]['weight'] = 4.7
    G.edge[1][2]['weight'] = 4s

    #  I'd expect adaption with my code  should be
    #  the following, but alas..
    weight_of_edge = G.edge[node_u][node_v]['weight']

拜托,欢迎为我的问题提供建议的解决方案或更好的方法。

这是一种方法(未经测试)。请注意,您的方法(以及下面的代码)删除的边取决于 for node in G 生成的节点顺序,这不是随机的。

import networkx as nx                                                                            
import random                                                                                    

G = nx.Graph()                                                                                   
G.add_edge(1,2,weight=7)                                                                         
G.add_edge(1,3,weight=2)                                                                         
G.add_edge(1,4,weight=2)                                                                         
G.add_edge(1,5,weight=6)                                                                         
G.add_edge(2,3,weight=3)                                                                         
G.add_edge(4,5,weight=3)                                                                         

max_degree = 2                                                                                   
for node in G:                                                                                   
    number_to_remove = G.degree(node) - max_degree                                               
    if number_to_remove > 0:                                                                     
        remove = random.sample(G.edges(node), number_to_remove)                                  
        weight_loss = sum(G[u][v]['weight'] for (u,v) in remove)                                 
        G.remove_edges_from(remove)                                                              
        print remove, weight_loss