summaryrefslogtreecommitdiff
path: root/networkx/algorithms/tests/test_core.py
blob: ae546a38c43d16d58389fe0a3a7300eaa11583a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import networkx as nx
from networkx.utils import nodes_equal


class TestCore:
    @classmethod
    def setup_class(cls):
        # G is the example graph in Figure 1 from Batagelj and
        # Zaversnik's paper titled An O(m) Algorithm for Cores
        # Decomposition of Networks, 2003,
        # http://arXiv.org/abs/cs/0310049.  With nodes labeled as
        # shown, the 3-core is given by nodes 1-8, the 2-core by nodes
        # 9-16, the 1-core by nodes 17-20 and node 21 is in the
        # 0-core.
        t1 = nx.convert_node_labels_to_integers(nx.tetrahedral_graph(), 1)
        t2 = nx.convert_node_labels_to_integers(t1, 5)
        G = nx.union(t1, t2)
        G.add_edges_from(
            [
                (3, 7),
                (2, 11),
                (11, 5),
                (11, 12),
                (5, 12),
                (12, 19),
                (12, 18),
                (3, 9),
                (7, 9),
                (7, 10),
                (9, 10),
                (9, 20),
                (17, 13),
                (13, 14),
                (14, 15),
                (15, 16),
                (16, 13),
            ]
        )
        G.add_node(21)
        cls.G = G

        # Create the graph H resulting from the degree sequence
        # [0, 1, 2, 2, 2, 2, 3] when using the Havel-Hakimi algorithm.

        degseq = [0, 1, 2, 2, 2, 2, 3]
        H = nx.havel_hakimi_graph(degseq)
        mapping = {6: 0, 0: 1, 4: 3, 5: 6, 3: 4, 1: 2, 2: 5}
        cls.H = nx.relabel_nodes(H, mapping)

    def test_trivial(self):
        """Empty graph"""
        G = nx.Graph()
        assert nx.find_cores(G) == {}

    def test_find_cores(self):
        core = nx.find_cores(self.G)
        nodes_by_core = [sorted(n for n in core if core[n] == val) for val in range(4)]
        assert nodes_equal(nodes_by_core[0], [21])
        assert nodes_equal(nodes_by_core[1], [17, 18, 19, 20])
        assert nodes_equal(nodes_by_core[2], [9, 10, 11, 12, 13, 14, 15, 16])
        assert nodes_equal(nodes_by_core[3], [1, 2, 3, 4, 5, 6, 7, 8])

    def test_core_number(self):
        # smoke test real name
        cores = nx.core_number(self.G)

    def test_find_cores2(self):
        core = nx.find_cores(self.H)
        nodes_by_core = [sorted(n for n in core if core[n] == val) for val in range(3)]
        assert nodes_equal(nodes_by_core[0], [0])
        assert nodes_equal(nodes_by_core[1], [1, 3])
        assert nodes_equal(nodes_by_core[2], [2, 4, 5, 6])

    def test_directed_find_cores(self):
        """core number had a bug for directed graphs found in issue #1959"""
        # small example where too timid edge removal can make cn[2] = 3
        G = nx.DiGraph()
        edges = [(1, 2), (2, 1), (2, 3), (2, 4), (3, 4), (4, 3)]
        G.add_edges_from(edges)
        assert nx.core_number(G) == {1: 2, 2: 2, 3: 2, 4: 2}
        # small example where too aggressive edge removal can make cn[2] = 2
        more_edges = [(1, 5), (3, 5), (4, 5), (3, 6), (4, 6), (5, 6)]
        G.add_edges_from(more_edges)
        assert nx.core_number(G) == {1: 3, 2: 3, 3: 3, 4: 3, 5: 3, 6: 3}

    def test_main_core(self):
        main_core_subgraph = nx.k_core(self.H)
        assert sorted(main_core_subgraph.nodes()) == [2, 4, 5, 6]

    def test_k_core(self):
        # k=0
        k_core_subgraph = nx.k_core(self.H, k=0)
        assert sorted(k_core_subgraph.nodes()) == sorted(self.H.nodes())
        # k=1
        k_core_subgraph = nx.k_core(self.H, k=1)
        assert sorted(k_core_subgraph.nodes()) == [1, 2, 3, 4, 5, 6]
        # k = 2
        k_core_subgraph = nx.k_core(self.H, k=2)
        assert sorted(k_core_subgraph.nodes()) == [2, 4, 5, 6]

    def test_main_crust(self):
        main_crust_subgraph = nx.k_crust(self.H)
        assert sorted(main_crust_subgraph.nodes()) == [0, 1, 3]

    def test_k_crust(self):
        # k = 0
        k_crust_subgraph = nx.k_crust(self.H, k=2)
        assert sorted(k_crust_subgraph.nodes()) == sorted(self.H.nodes())
        # k=1
        k_crust_subgraph = nx.k_crust(self.H, k=1)
        assert sorted(k_crust_subgraph.nodes()) == [0, 1, 3]
        # k=2
        k_crust_subgraph = nx.k_crust(self.H, k=0)
        assert sorted(k_crust_subgraph.nodes()) == [0]

    def test_main_shell(self):
        main_shell_subgraph = nx.k_shell(self.H)
        assert sorted(main_shell_subgraph.nodes()) == [2, 4, 5, 6]

    def test_k_shell(self):
        # k=0
        k_shell_subgraph = nx.k_shell(self.H, k=2)
        assert sorted(k_shell_subgraph.nodes()) == [2, 4, 5, 6]
        # k=1
        k_shell_subgraph = nx.k_shell(self.H, k=1)
        assert sorted(k_shell_subgraph.nodes()) == [1, 3]
        # k=2
        k_shell_subgraph = nx.k_shell(self.H, k=0)
        assert sorted(k_shell_subgraph.nodes()) == [0]

    def test_k_corona(self):
        # k=0
        k_corona_subgraph = nx.k_corona(self.H, k=2)
        assert sorted(k_corona_subgraph.nodes()) == [2, 4, 5, 6]
        # k=1
        k_corona_subgraph = nx.k_corona(self.H, k=1)
        assert sorted(k_corona_subgraph.nodes()) == [1]
        # k=2
        k_corona_subgraph = nx.k_corona(self.H, k=0)
        assert sorted(k_corona_subgraph.nodes()) == [0]

    def test_k_truss(self):
        # k=-1
        k_truss_subgraph = nx.k_truss(self.G, -1)
        assert sorted(k_truss_subgraph.nodes()) == list(range(1, 21))
        # k=0
        k_truss_subgraph = nx.k_truss(self.G, 0)
        assert sorted(k_truss_subgraph.nodes()) == list(range(1, 21))
        # k=1
        k_truss_subgraph = nx.k_truss(self.G, 1)
        assert sorted(k_truss_subgraph.nodes()) == list(range(1, 21))
        # k=2
        k_truss_subgraph = nx.k_truss(self.G, 2)
        assert sorted(k_truss_subgraph.nodes()) == list(range(1, 21))
        # k=3
        k_truss_subgraph = nx.k_truss(self.G, 3)
        assert sorted(k_truss_subgraph.nodes()) == list(range(1, 13))

        k_truss_subgraph = nx.k_truss(self.G, 4)
        assert sorted(k_truss_subgraph.nodes()) == list(range(1, 9))

        k_truss_subgraph = nx.k_truss(self.G, 5)
        assert sorted(k_truss_subgraph.nodes()) == []

    def test_onion_layers(self):
        layers = nx.onion_layers(self.G)
        nodes_by_layer = [
            sorted(n for n in layers if layers[n] == val) for val in range(1, 7)
        ]
        assert nodes_equal(nodes_by_layer[0], [21])
        assert nodes_equal(nodes_by_layer[1], [17, 18, 19, 20])
        assert nodes_equal(nodes_by_layer[2], [10, 12, 13, 14, 15, 16])
        assert nodes_equal(nodes_by_layer[3], [9, 11])
        assert nodes_equal(nodes_by_layer[4], [1, 2, 4, 5, 6, 8])
        assert nodes_equal(nodes_by_layer[5], [3, 7])