Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/networkx/algorithms/community/community_utils.py: 50%

8 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-20 07:00 +0000

1"""Helper functions for community-finding algorithms.""" 

2import networkx as nx 

3 

4__all__ = ["is_partition"] 

5 

6 

7@nx._dispatch 

8def is_partition(G, communities): 

9 """Returns *True* if `communities` is a partition of the nodes of `G`. 

10 

11 A partition of a universe set is a family of pairwise disjoint sets 

12 whose union is the entire universe set. 

13 

14 Parameters 

15 ---------- 

16 G : NetworkX graph. 

17 

18 communities : list or iterable of sets of nodes 

19 If not a list, the iterable is converted internally to a list. 

20 If it is an iterator it is exhausted. 

21 

22 """ 

23 # Alternate implementation: 

24 # return all(sum(1 if v in c else 0 for c in communities) == 1 for v in G) 

25 if not isinstance(communities, list): 

26 communities = list(communities) 

27 nodes = {n for c in communities for n in c if n in G} 

28 

29 return len(G) == len(nodes) == sum(len(c) for c in communities)