nx.degree(g,i) ... what would this mean?

654 views
Skip to first unread message

rdla...@gmail.com

unread,
Sep 4, 2013, 11:51:13 AM9/4/13
to networkx...@googlegroups.com
I'm going through someone else's code.  It looks like the code is calling 'degree' before there are nodes initialized.  I'm wondering what is the meaning of 'degree' here, a number is returned.

import networkx as nx

. . .

g = nx.Graph()

. . .

def init_nodes():
    print "Length of g.nodes() = %d" % len(g.nodes())
    for i in range(0, len(g.nodes())):
        val = nx.degree(g, i)                                             <--- I'm not sure of what the meaning of this value returned is
                                                                                       This looks like 'degree' is being called on graph g before the graph g is constructed

        print "val=nx.degree(g,i), val=%d, i=%d" % (val,i)
        g.add_node(i,maxgrains=val, numgrains=0, active=False, fired=False)

Here's some of the output:

Length of g.nodes() = 100
val=nx.degree(g,i), val=28, i=0               <-- Values are being returned. Are these random values?
val=nx.degree(g,i), val=23, i=1
val=nx.degree(g,i), val=27, i=2
val=nx.degree(g,i), val=19, i=3
val=nx.degree(g,i), val=22, i=4

Aric Hagberg

unread,
Sep 4, 2013, 11:58:56 AM9/4/13
to networkx...@googlegroups.com
It's hard to tell from your code fragments.  
If you ask for the degree of a node that doesn't exist in the graph you will get an error (not a random value).
>>> import networkx as nx
>>> G = nx.Graph()
>>> nx.degree(G,0)
'int' object is not iterableTraceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/nh/nest/u/aric/.local/lib/python2.7/site-packages/networkx/classes/function.py", line 53, in degree
    return G.degree(nbunch,weight)
  File "/nh/nest/u/aric/.local/lib/python2.7/site-packages/networkx/classes/graph.py", line 1248, in degree
    return dict(self.degree_iter(nbunch,weight))
  File "/nh/nest/u/aric/.local/lib/python2.7/site-packages/networkx/classes/graph.py", line 1291, in degree_iter
    for n,nbrs in nodes_nbrs:
  File "/nh/nest/u/aric/.local/lib/python2.7/site-packages/networkx/classes/graph.py", line 1288, in <genexpr>
    nodes_nbrs=((n,self.adj[n]) for n in self.nbunch_iter(nbunch))
  File "/nh/nest/u/aric/.local/lib/python2.7/site-packages/networkx/classes/graph.py", line 1808, in bunch_iter
    "nbunch is not a node or a sequence of nodes.")
networkx.exception.NetworkXError: nbunch is not a node or a sequence of nodes.

Aric


--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discu...@googlegroups.com.
To post to this group, send email to networkx...@googlegroups.com.
Visit this group at http://groups.google.com/group/networkx-discuss.
For more options, visit https://groups.google.com/groups/opt_out.

rdla...@gmail.com

unread,
Sep 4, 2013, 12:53:20 PM9/4/13
to networkx...@googlegroups.com
Thanks - I think I see a code fragment that helps explain -

                            g = nx.erdos_renyi_graph(n, p)

               This statement above must initialize the graph/network g with n nodes.
and the statement
     val = nx.degree(g,i)  I think then will return the degree of the ith node of the graph g.

Let me know if that doesn't sound correct
Thanks
Reply all
Reply to author
Forward
0 new messages