Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a new attribute to an edge in networkx?

What I have: a graph G imported in networkx with nodes and edges loaded by gml file.

Problem: How to add a new attribute to a selected edge E.

What I want to do: I want to add a new attribute 'type' for a particular edge E of my graph. Attention: the attribute 'type' doesn't exist for this edge E.

My code is:

  G.edge[id_source][id_target]['type']= value

But if I print all the edges of G, now I have n+1 edges; all the old edges of G, and a new edge p= (id_source, id_target, {'type'= value}). Furthermore, the old edge E (the one that I want modify) doesn't have the new attribute 'type'.

So my code have added a new edge (that I don't want).

I want to update the old one adding a new attribute that doesn't exist.

like image 749
Stefano C. Avatar asked Nov 01 '14 18:11

Stefano C.


People also ask

What is an edge attribute?

Examples of edge attributes are data associated with edges: most commonly edge weights, or visualization parameters. In recent igraph versions, arbitrary R objects can be assigned as graph, vertex or edge attributes.

How do I add weighted edges to NetworkX?

Add all the edges in ebunch as weighted edges with specified weights. Each edge given in the list or container will be added to the graph. The edges must be given as 3-tuples (u,v,w) where w is a number.

How do I add a node in NetworkX?

Node attributes Note that adding a node to G. nodes does not add it to the graph, use G. add_node() to add new nodes. Similarly for edges.


3 Answers

You may have a networkx MultiGraph instead of a graph and in that case the attribute setting for edges is a little tricker. (You can get a multigraph by loading a graph with more than one edge between nodes). You may be corrupting the data structure by assigning the attribute G.edge[id_source][id_target]['type']= value when you need G.edge[id_source][id_target][key]['type']= value.

Here are examples of how it works differently for Graphs and MultiGraphs.

For the Graph case attributes work like this:

In [1]: import networkx as nx

In [2]: G = nx.Graph()

In [3]: G.add_edge(1,2,color='red')

In [4]: G.edges(data=True)
Out[4]: [(1, 2, {'color': 'red'})]

In [5]: G.add_edge(1,2,color='blue')

In [6]: G.edges(data=True)
Out[6]: [(1, 2, {'color': 'blue'})]

In [7]: G[1][2]
Out[7]: {'color': 'blue'}

In [8]: G[1][2]['color']='green'

In [9]: G.edges(data=True)
Out[9]: [(1, 2, {'color': 'green'})]

With MultiGraphs there is an additional level of keys to keep track of the parallel edges so it works a little differently. If you don't explicitly set a key MultiGraph.add_edge() will add a new edge with an internally chosen key (sequential integers).

In [1]: import networkx as nx

In [2]: G = nx.MultiGraph()

In [3]: G.add_edge(1,2,color='red')

In [4]: G.edges(data=True)
Out[4]: [(1, 2, {'color': 'red'})]

In [5]: G.add_edge(1,2,color='blue')

In [6]: G.edges(data=True)
Out[6]: [(1, 2, {'color': 'red'}), (1, 2, {'color': 'blue'})]

In [7]: G.edges(data=True,keys=True)
Out[7]: [(1, 2, 0, {'color': 'red'}), (1, 2, 1, {'color': 'blue'})]

In [8]: G.add_edge(1,2,key=0,color='blue')

In [9]: G.edges(data=True,keys=True)
Out[9]: [(1, 2, 0, {'color': 'blue'}), (1, 2, 1, {'color': 'blue'})]

In [10]: G[1][2]
Out[10]: {0: {'color': 'blue'}, 1: {'color': 'blue'}}

In [11]: G[1][2][0]['color']='green'

In [12]: G.edges(data=True,keys=True)
Out[12]: [(1, 2, 0, {'color': 'green'}), (1, 2, 1, {'color': 'blue'})]
like image 153
Aric Avatar answered Oct 23 '22 13:10

Aric


I don't quite understand why you want add an attribute to only one edge, instead you can add an attribute to all edges, then you give the the wanted value to your specific edge.

Networkx has a method called set_edge_attributes can add an edge attributes to all edges, for example

    G = nx.path_graph(3)
    bb = nx.edge_betweenness_centrality(G, normalized=False)
    nx.set_edge_attributes(G, 'betweenness', bb)
    G[1][2]['betweenness']

Output: 2.0

like image 3
Xin-Feng Li Avatar answered Oct 23 '22 13:10

Xin-Feng Li


The answer below by Xin-Feng Li works, just note that the arguments for values and name switched between Networkx v1.x (when the answer was originally written) and Networkx v2.x. For v2.x, the code is:

G = nx.path_graph(3)
bb = nx.edge_betweenness_centrality(G, normalized=False)
nx.set_edge_attributes(G, bb, 'betweenness')
G[1][2]['betweenness']
like image 1
scorch421 Avatar answered Oct 23 '22 13:10

scorch421