简体   繁体   中英

How to properly access edge properties in Networkx

I have a graph G that I first construct all edges on by a set of rules, and then later I want to remove some of them randomly. (Each node has a property "label" and all edges have the weight property assigned when created.)

I'm on Python 3.4.2 using Networkx 1.9.1.

The following code is what I've been trying to do so far, where I intend to remove an edge from a node if its degree is above the threshold 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])

First of all, this does not work, and secondly my guts tells me this is messy and cumbersome. The tutorial suggest that the following code is how to access edge properties. The documentation points to the tutorial, but I come short of this:

    #  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']

Please, I'd welcome suggested solutions to my problem, or a better approach.

Here is one way to do it (untested). Note that the edges removed by your method (and the code below) depends on the order of the nodes produced by for node in G which is not random.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM