Adding weighted edges with multiple attributes

1,097 views
Skip to first unread message

Ula Sagarra

unread,
Apr 4, 2012, 6:12:48 AM4/4/12
to networkx...@googlegroups.com
Hi there!

I have a quick doubt that I am trying to fix in the cleanest possible
way since I am deling with reduced computation power :). Any help would
be very appreciated.

I have a large directed graph of unconnected nodes where I want to add
edges.

My edges are stored in a dictionary whose items are ['node origin',
'node destination']:{'trips': integer, 'v': float, 'theta': float}
(where 'integer' and 'float' refer to numbers).

The thing is I want to add the edges with the 3 attributes (to use them
as a weights whenever needed).

Can someone tell me how would be the proper use of
"G.add_weighted_edges_from" to add the 3 items at a time.

Alternatively, could I just add the edges un-weighted and then add edge
attributes (the 3 at once) using "set_edge_attributes".

Thanks in advance!

Cheers.

--
*************************************
Oleguer Sagarra, "Ula"

"Si a les persones els posaren tants entrebancs com als ordinadors per
considerar-los intel�ligents, el 90% de la humanitat seria considerada
est�pida." I. Asimov

#: Abans d'imprimir aquest correu electr�nic, pensa b� si �s necessari
fer-ho: El medi ambient �s q�esti� de tots. Si l'has d'imprimir, intenta
fer-ho a doble full o re-escalant-lo a dos fulls per p�gina.

*************************************

Aric Hagberg

unread,
Apr 4, 2012, 8:43:06 AM4/4/12
to networkx...@googlegroups.com
On Wed, Apr 4, 2012 at 4:12 AM, Ula Sagarra <uga...@gmail.com> wrote:
> Hi there!
>
> I have a quick doubt that I am trying to fix in the cleanest possible way
> since I am deling with reduced computation power :). Any help would be very
> appreciated.
>
>
>
> I have a large directed graph of unconnected nodes where I want to add
> edges.
>
> My edges are stored in a dictionary whose items are ['node origin', 'node
> destination']:{'trips': integer, 'v': float, 'theta': float}
>  (where 'integer' and 'float' refer to numbers).
>
> The thing is I want to add the edges with the 3 attributes (to use them as a
> weights whenever needed).
>
> Can someone tell me how would be the proper use of
> "G.add_weighted_edges_from" to add the 3 items at a time.

The "one-liner" way is to use add_edges_from(), e.g.

In [1]: import networkx as nx

In [2]: d={('a','b'):{'trips':1,'v':2,'theta':3}}

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

In [4]: G.add_edges_from((k[0],k[1],d) for k,d in d.items())

In [5]: G.edges(data=True)
Out[5]: [('a', 'b', {'theta': 3, 'trips': 1, 'v': 2})]

Aric

Reply all
Reply to author
Forward
0 new messages