diff options
| author | Emilien Kofman <emilien.kofman@gmail.com> | 2015-11-04 10:51:13 +0100 |
|---|---|---|
| committer | Emilien Kofman <emilien.kofman@gmail.com> | 2015-11-04 10:51:13 +0100 |
| commit | c8569c4a7274e9171548745c2840477d11c1ae20 (patch) | |
| tree | d4b59dee9b1470df8c04c76de6f267cb9e15a459 | |
| parent | 4ae43620fd5cf4a4ad0dec4a195398228c483a7d (diff) | |
| download | networkx-c8569c4a7274e9171548745c2840477d11c1ae20.tar.gz | |
graph.size update thanks to @jfinkels suggestions. See #1798.
| -rw-r--r-- | networkx/classes/graph.py | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/networkx/classes/graph.py b/networkx/classes/graph.py index fa7c1d2e..e6e0287b 100644 --- a/networkx/classes/graph.py +++ b/networkx/classes/graph.py @@ -13,6 +13,7 @@ For directed graphs see DiGraph and MultiDiGraph. # Pieter Swart <swart@lanl.gov> # All rights reserved. # BSD license. +from __future__ import division from copy import deepcopy import networkx as nx from networkx.exception import NetworkXError @@ -1562,8 +1563,12 @@ class Graph(object): Returns ------- - nedges : int - The number of edges or sum of edge weights in the graph. + size + The integer number of edges in the graph if `weight` + is None. If a weight keyword argument is provided, + and all the weights are integers, the output will + be a float, regardless of whether all the weights + are integers. See Also -------- @@ -1584,7 +1589,12 @@ class Graph(object): >>> G.size(weight='weight') 6.0 """ - return sum(dict(self.degree(weight=weight)).values()) / 2 + s = sum(d for v, d in self.degree(weight=weight)) + # If `weight` is None, the sum of the degrees is guaranteed to be + # even, so we can perform integer division and hence return an + # integer. Otherwise, the sum of the weighted degrees is not + # guaranteed to be an integer, so we perform "real" division. + return s // 2 if weight is None else s / 2 def number_of_edges(self, u=None, v=None): """Return the number of edges between two nodes. |
