Returns the number of edges or total of all edge weights. Parameters ---------- weight : string or None, optional (default=None) The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. Returns
(self, weight=None)
| 1916 | return nx.edge_subgraph(self, edges) |
| 1917 | |
| 1918 | def size(self, weight=None): |
| 1919 | """Returns the number of edges or total of all edge weights. |
| 1920 | |
| 1921 | Parameters |
| 1922 | ---------- |
| 1923 | weight : string or None, optional (default=None) |
| 1924 | The edge attribute that holds the numerical value used |
| 1925 | as a weight. If None, then each edge has weight 1. |
| 1926 | |
| 1927 | Returns |
| 1928 | ------- |
| 1929 | size : numeric |
| 1930 | The number of edges or |
| 1931 | (if weight keyword is provided) the total weight sum. |
| 1932 | |
| 1933 | If weight is None, returns an int. Otherwise a float |
| 1934 | (or more general numeric if the weights are more general). |
| 1935 | |
| 1936 | See Also |
| 1937 | -------- |
| 1938 | number_of_edges |
| 1939 | |
| 1940 | Examples |
| 1941 | -------- |
| 1942 | >>> G = nx.path_graph(4) # or DiGraph, MultiGraph, MultiDiGraph, etc |
| 1943 | >>> G.size() |
| 1944 | 3 |
| 1945 | |
| 1946 | >>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc |
| 1947 | >>> G.add_edge("a", "b", weight=2) |
| 1948 | >>> G.add_edge("b", "c", weight=4) |
| 1949 | >>> G.size() |
| 1950 | 2 |
| 1951 | >>> G.size(weight="weight") |
| 1952 | 6.0 |
| 1953 | """ |
| 1954 | s = sum(d for v, d in self.degree(weight=weight)) |
| 1955 | # If `weight` is None, the sum of the degrees is guaranteed to be |
| 1956 | # even, so we can perform integer division and hence return an |
| 1957 | # integer. Otherwise, the sum of the weighted degrees is not |
| 1958 | # guaranteed to be an integer, so we perform "real" division. |
| 1959 | return s // 2 if weight is None else s / 2 |
| 1960 | |
| 1961 | def number_of_edges(self, u=None, v=None): |
| 1962 | """Returns the number of edges between two nodes. |