diff options
| author | Dan Schult <dschult@colgate.edu> | 2015-12-29 07:40:32 -0500 |
|---|---|---|
| committer | Dan Schult <dschult@colgate.edu> | 2015-12-29 07:40:32 -0500 |
| commit | 04718ecb1ad3c5bf919c80d86f1a9a52d08aecc5 (patch) | |
| tree | 3c826ab88eff5c5c2469c691156a1c793d531ad4 | |
| parent | d73dae5e50e3c17a3cabd512eede36e3658840d3 (diff) | |
| parent | c8569c4a7274e9171548745c2840477d11c1ae20 (diff) | |
| download | networkx-04718ecb1ad3c5bf919c80d86f1a9a52d08aecc5.tar.gz | |
Merge pull request #1798 from emilienkofman/master
Make Graph class size() return type consistent
| -rw-r--r-- | networkx/classes/graph.py | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/networkx/classes/graph.py b/networkx/classes/graph.py index 431fd836..635efff7 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 @@ -1570,8 +1571,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 -------- @@ -1592,11 +1597,12 @@ class Graph(object): >>> G.size(weight='weight') 6.0 """ - s = sum(dict(self.degree(weight=weight)).values()) / 2 - if weight is None: - return int(s) - else: - return float(s) + 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. |
