sum of edge weights for MultiDiGraphs

735 views
Skip to first unread message

Gary Shea

unread,
Nov 8, 2010, 10:55:51 AM11/8/10
to networkx-discuss
Hello,

I am fairly new to networkx, so this may be very simple. If anyone can
tell me why this example works for Graphs, DiGraphs and MultiGraphs,
but does not work for MultiDiGraphs, I would appreciate it.

>>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_edge('a','b',weight=2)
>>> G.add_edge('b','c',weight=4)
>>> G.size()
2
>>> G.size(weighted=True)
6.0

Many thanks to anyone who enlightens me. Here is what I get when I try
to re-create the example:

>>> G=nx.MultiDiGraph()
>>> G.add_edge('a','b',weight=2)
>>> G.add_edge('b','c',weight=4)
>>> G.size()
2
>>> G.size(weighted=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python31\lib\site-packages\networkx-1.3-py3.1.egg\networkx
\classes\gr
aph.py", line 1614, in size
s=sum(self.degree(weighted=weighted).values())/2
File "C:\Python31\lib\site-packages\networkx-1.3-py3.1.egg\networkx
\classes\gr
aph.py", line 1241, in degree
return dict(self.degree_iter(nbunch,weighted=weighted))
File "C:\Python31\lib\site-packages\networkx-1.3-py3.1.egg\networkx
\classes\mu
ltidigraph.py", line 490, in degree_iter
for (n,succ,pred) in nodes_nbrs:
ValueError: need more than 2 values to unpack
>>>

Aric Hagberg

unread,
Nov 8, 2010, 11:26:44 AM11/8/10
to networkx...@googlegroups.com
On Mon, Nov 8, 2010 at 8:55 AM, Gary Shea <gs...@st-and.ac.uk> wrote:
> I am fairly new to networkx, so this may be very simple. If anyone can
> tell me why this example works for Graphs, DiGraphs and MultiGraphs,
> but does not work for MultiDiGraphs, I would appreciate it.
>
>>>> G=nx.MultiDiGraph()
>>>> G.add_edge('a','b',weight=2)
>>>> G.add_edge('b','c',weight=4)
>>>> G.size()
> 2
>>>> G.size(weighted=True)
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>  File "C:\Python31\lib\site-packages\networkx-1.3-py3.1.egg\networkx

...

> ValueError: need more than 2 values to unpack

It's a bug with a simple fix:
https://networkx.lanl.gov/trac/ticket/460

Thanks for the report!

Aric

Reply all
Reply to author
Forward
0 new messages