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

时间:2015-02-23 20:01:36

标签: python networkx

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

我使用Networkx 1.9.1在Python 3.4.2上。

以下代码是我到目前为止所做的工作,如果程度高于阈值 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']

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

1 个答案:

答案 0 :(得分:2)

这是一种方法(未经测试)。请注意,您的方法(以及下面的代码)删除的边缘取决于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