Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / community / community_utils.py @ 5cef0f13

History | View | Annotate | Download (1.08 KB)

1
# -*- coding: utf-8 -*-
2
#
3
# utils.py - helper functions for community-finding algorithms
4
#
5
# Copyright 2011 Ben Edwards <bedwards@cs.unm.edu>.
6
# Copyright 2011 Aric Hagberg <hagberg@lanl.gov>.
7
# Copyright 2015 NetworkX developers.
8
#
9
# This file is part of NetworkX.
10
#
11
# NetworkX is distributed under a BSD license; see LICENSE.txt for more
12
# information.
13
"""Helper functions for community-finding algorithms."""
14

    
15
__all__ = ['is_partition']
16

    
17

    
18
def is_partition(G, communities):
19
    """Returns True if and only if `communities` is a partition of
20
    the nodes of `G`.
21

22
    A partition of a universe set is a family of pairwise disjoint sets
23
    whose union is the entire universe set.
24

25
    `G` is a NetworkX graph.
26

27
    `communities` is an iterable of sets of nodes of `G`. This
28
    iterable will be consumed multiple times during the execution of
29
    this function.
30

31
    """
32
    # Alternate implementation:
33
    #
34
    #     return (len(G) == sum(len(c) for c in community) and
35
    #             set(G) == set.union(*community))
36
    #
37
    return all(sum(1 if v in c else 0 for c in communities) == 1 for v in G)