Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (2.1 KB)

1
from itertools import combinations
2

    
3
from nose.tools import assert_equal
4
from nose.tools import raises
5

    
6
import networkx as nx
7
from networkx.algorithms.community import k_clique_communities
8

    
9

    
10
def test_overlapping_K5():
11
    G = nx.Graph()
12
    G.add_edges_from(combinations(range(5), 2))  # Add a five clique
13
    G.add_edges_from(combinations(range(2, 7), 2))  # Add another five clique
14
    c = list(k_clique_communities(G, 4))
15
    assert_equal(c, [frozenset(range(7))])
16
    c = set(k_clique_communities(G, 5))
17
    assert_equal(c, {frozenset(range(5)), frozenset(range(2, 7))})
18

    
19

    
20
def test_isolated_K5():
21
    G = nx.Graph()
22
    G.add_edges_from(combinations(range(0, 5), 2))  # Add a five clique
23
    G.add_edges_from(combinations(range(5, 10), 2))  # Add another five clique
24
    c = set(k_clique_communities(G, 5))
25
    assert_equal(c, {frozenset(range(5)), frozenset(range(5, 10))})
26

    
27

    
28
class TestZacharyKarateClub(object):
29

    
30
    def setup(self):
31
        self.G = nx.karate_club_graph()
32

    
33
    def _check_communities(self, k, expected):
34
        communities = set(k_clique_communities(self.G, k))
35
        assert_equal(communities, expected)
36

    
37
    def test_k2(self):
38
        # clique percolation with k=2 is just connected components
39
        expected = {frozenset(self.G)}
40
        self._check_communities(2, expected)
41

    
42
    def test_k3(self):
43
        comm1 = [0, 1, 2, 3, 7, 8, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23,
44
                 26, 27, 28, 29, 30, 31, 32, 33]
45
        comm2 = [0, 4, 5, 6, 10, 16]
46
        comm3 = [24, 25, 31]
47
        expected = {frozenset(comm1), frozenset(comm2), frozenset(comm3)}
48
        self._check_communities(3, expected)
49

    
50
    def test_k4(self):
51
        expected = {frozenset([0, 1, 2, 3, 7, 13]), frozenset([8, 32, 30, 33]),
52
                    frozenset([32, 33, 29, 23])}
53
        self._check_communities(4, expected)
54

    
55
    def test_k5(self):
56
        expected = {frozenset([0, 1, 2, 3, 7, 13])}
57
        self._check_communities(5, expected)
58

    
59
    def test_k6(self):
60
        expected = set()
61
        self._check_communities(6, expected)
62

    
63

    
64
@raises(nx.NetworkXError)
65
def test_bad_k():
66
    list(k_clique_communities(nx.Graph(), 1))