Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NetworkX: how to add weights to an existing G.edges()?

Given any graph G created in NetworkX, I want to be able to assign some weights to G.edges() after the graph is created. The graphs involved are grids, erdos-reyni, barabasi-albert, and so forth.

Given my G.edges():

[(0, 1), (0, 10), (1, 11), (1, 2), (2, 3), (2, 12), ...]

And my weights:

{(0,1):1.0, (0,10):1.0, (1,2):1.0, (1,11):1.0, (2,3):1.0, (2,12):1.0, ...}

How can I assign each edge the relevant weight? In this trivial case all weights are 1.

I've tried to add the weights to G.edges() directly like this

for i, edge in enumerate(G.edges()):
    G.edges[i]['weight']=weights[edge]

But I get this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-48-6119dc6b7af0> in <module>()
     10 
     11 for i, edge in enumerate(G.edges()):
---> 12     G.edges[i]['weight']=weights[edge]

TypeError: 'instancemethod' object has no attribute '__getitem__'

What's wrong? Since G.edges() is a list, why can't I access its elements as with any other list?

like image 871
FaCoffee Avatar asked Oct 19 '16 10:10

FaCoffee


2 Answers

It fails because edges is a method.

The documentation says to do this like:

G[source][target]['weight'] = weight

For example, the following works for me:

import networkx as nx

G = nx.Graph()

G.add_path([0, 1, 2, 3])

G[0][1]['weight'] = 3

>>> G.get_edge_data(0, 1)
{'weight': 3}

However, your type of code indeed fails:

G.edges[0][1]['weight'] = 3
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-97b10ad2279a> in <module>()
----> 1 G.edges[0][1]['weight'] = 3

TypeError: 'instancemethod' object has no attribute '__getitem__'

In your case, I'd suggest

for e in G.edges():
    G[e[0]][e[1]] = weights[e]
like image 73
Ami Tavory Avatar answered Oct 10 '22 07:10

Ami Tavory


From the docs:

  • You can set all the edge weights at once to the same value with
nx.set_edge_attributes(G, values = 1, name = 'weight')
  • Given a dictionary with keys corresponding to edge tuples (your weights), you can assign edge weights to values from that dictionary with
nx.set_edge_attributes(G, values = weights, name = 'weight')
  • To view and verify that these edge attributes have been set
G.edges(data = True)
like image 37
rlchqrd Avatar answered Oct 10 '22 05:10

rlchqrd