Return degree of single node or of nbunch of nodes. If nbunch is omitted or nbunch=None, then return degrees of *all* nodes. The degree of a node is the number of edges attached to that node. Can be called in three ways:  G.degree(n): return the degree of node n  G.degree(nbunch): return a list of values, one for each n in nbunch (nbunch is any iterable container of nodes.)  G.degree(): same as nbunch = all nodes in graph. Always return a list. If with_labels==True, then return a dict that maps each n in nbunch to degree(n). Any nodes in nbunch that are not in the graph are (quietly) ignored. Reimplemented from networkx::base::Graph. Definition at line 569 of file xbase.py. 00569 : """Return degree of single node or of nbunch of nodes. If nbunch is omitted or nbunch=None, then return degrees of *all* nodes. The degree of a node is the number of edges attached to that node. Can be called in three ways:  G.degree(n): return the degree of node n  G.degree(nbunch): return a list of values, one for each n in nbunch (nbunch is any iterable container of nodes.)  G.degree(): same as nbunch = all nodes in graph. Always return a list. If with_labels==True, then return a dict that maps each n in nbunch to degree(n). Any nodes in nbunch that are not in the graph are (quietly) ignored. """ # prepare nbunch if nbunch is None: # include all nodes via iterator bunch=self.nodes_iter() elif nbunch in self: # if nbunch is a single node bunch=[nbunch] else: # if nbunch is a sequence of nodes try: bunch=[n for n in nbunch if n in self] except TypeError: raise NetworkXError, "nbunch is not a node or a sequence of nodes." # nbunch ready d={} if self.multiedges: for n in bunch: deg = sum([len(e) for e in self.adj[n].itervalues()]) if self.adj[n].has_key(n) and self.selfloops: deg+= len(self.adj[n][n]) # double count selfloops d[n]=deg else: for n in bunch: deg=len(self.adj[n]) deg+= self.adj[n].has_key(n) # double count selfloop d[n]=deg if with_labels: return d # return the dict elif nbunch in self: return d.values()[0] # single node, so single value return d.values() # return a list def degree_iter(self,nbunch=None,with_labels=False):
