Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / centrality / tests / test_closeness_centrality.py @ 5cef0f13

History | View | Annotate | Download (6.34 KB)

1
"""
2
Tests for degree centrality.
3
"""
4
from nose.tools import *
5
import networkx as nx
6

    
7

    
8
class TestClosenessCentrality:
9
    def setUp(self):
10
        self.K = nx.krackhardt_kite_graph()
11
        self.P3 = nx.path_graph(3)
12
        self.P4 = nx.path_graph(4)
13
        self.K5 = nx.complete_graph(5)
14

    
15
        self.C4 = nx.cycle_graph(4)
16
        self.T = nx.balanced_tree(r=2, h=2)
17
        self.Gb = nx.Graph()
18
        self.Gb.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3),
19
                                (2, 4), (4, 5), (3, 5)])
20

    
21
        F = nx.florentine_families_graph()
22
        self.F = F
23

    
24
        self.LM = nx.les_miserables_graph()
25

    
26
    def test_wf_improved(self):
27
        G = nx.union(self.P4, nx.path_graph([4, 5, 6]))
28
        c = nx.closeness_centrality(G)
29
        cwf = nx.closeness_centrality(G, wf_improved=False)
30
        res = {0: 0.25, 1: 0.375, 2: 0.375, 3: 0.25,
31
               4: 0.222, 5: 0.333, 6: 0.222}
32
        wf_res = {0: 0.5, 1: 0.75, 2: 0.75, 3: 0.5,
33
                  4: 0.667, 5: 1.0, 6: 0.667}
34
        for n in G:
35
            assert_almost_equal(c[n], res[n], places=3)
36
            assert_almost_equal(cwf[n], wf_res[n], places=3)
37

    
38
    def test_digraph(self):
39
        G = nx.path_graph(3, create_using=nx.DiGraph())
40
        c = nx.closeness_centrality(G)
41
        cr = nx.closeness_centrality(G.reverse())
42
        d = {0: 0.0, 1: 0.500, 2: 0.667}
43
        dr = {0: 0.667, 1: 0.500, 2: 0.0}
44
        for n in sorted(self.P3):
45
            assert_almost_equal(c[n], d[n], places=3)
46
            assert_almost_equal(cr[n], dr[n], places=3)
47

    
48
    def test_k5_closeness(self):
49
        c = nx.closeness_centrality(self.K5)
50
        d = {0: 1.000,
51
             1: 1.000,
52
             2: 1.000,
53
             3: 1.000,
54
             4: 1.000}
55
        for n in sorted(self.K5):
56
            assert_almost_equal(c[n], d[n], places=3)
57

    
58
    def test_p3_closeness(self):
59
        c = nx.closeness_centrality(self.P3)
60
        d = {0: 0.667,
61
             1: 1.000,
62
             2: 0.667}
63
        for n in sorted(self.P3):
64
            assert_almost_equal(c[n], d[n], places=3)
65

    
66
    def test_krackhardt_closeness(self):
67
        c = nx.closeness_centrality(self.K)
68
        d = {0: 0.529,
69
             1: 0.529,
70
             2: 0.500,
71
             3: 0.600,
72
             4: 0.500,
73
             5: 0.643,
74
             6: 0.643,
75
             7: 0.600,
76
             8: 0.429,
77
             9: 0.310}
78
        for n in sorted(self.K):
79
            assert_almost_equal(c[n], d[n], places=3)
80

    
81
    def test_florentine_families_closeness(self):
82
        c = nx.closeness_centrality(self.F)
83
        d = {'Acciaiuoli':    0.368,
84
             'Albizzi':       0.483,
85
             'Barbadori':     0.4375,
86
             'Bischeri':      0.400,
87
             'Castellani':    0.389,
88
             'Ginori':        0.333,
89
             'Guadagni':      0.467,
90
             'Lamberteschi':  0.326,
91
             'Medici':        0.560,
92
             'Pazzi':         0.286,
93
             'Peruzzi':       0.368,
94
             'Ridolfi':       0.500,
95
             'Salviati':      0.389,
96
             'Strozzi':       0.4375,
97
             'Tornabuoni':    0.483}
98
        for n in sorted(self.F):
99
            assert_almost_equal(c[n], d[n], places=3)
100

    
101
    def test_les_miserables_closeness(self):
102
        c = nx.closeness_centrality(self.LM)
103
        d = {'Napoleon': 0.302,
104
             'Myriel': 0.429,
105
             'MlleBaptistine': 0.413,
106
             'MmeMagloire': 0.413,
107
             'CountessDeLo': 0.302,
108
             'Geborand': 0.302,
109
             'Champtercier': 0.302,
110
             'Cravatte': 0.302,
111
             'Count': 0.302,
112
             'OldMan': 0.302,
113
             'Valjean': 0.644,
114
             'Labarre': 0.394,
115
             'Marguerite': 0.413,
116
             'MmeDeR': 0.394,
117
             'Isabeau': 0.394,
118
             'Gervais': 0.394,
119
             'Listolier': 0.341,
120
             'Tholomyes': 0.392,
121
             'Fameuil': 0.341,
122
             'Blacheville': 0.341,
123
             'Favourite': 0.341,
124
             'Dahlia': 0.341,
125
             'Zephine': 0.341,
126
             'Fantine': 0.461,
127
             'MmeThenardier': 0.461,
128
             'Thenardier': 0.517,
129
             'Cosette': 0.478,
130
             'Javert': 0.517,
131
             'Fauchelevent': 0.402,
132
             'Bamatabois': 0.427,
133
             'Perpetue': 0.318,
134
             'Simplice': 0.418,
135
             'Scaufflaire': 0.394,
136
             'Woman1': 0.396,
137
             'Judge': 0.404,
138
             'Champmathieu': 0.404,
139
             'Brevet': 0.404,
140
             'Chenildieu': 0.404,
141
             'Cochepaille': 0.404,
142
             'Pontmercy': 0.373,
143
             'Boulatruelle': 0.342,
144
             'Eponine': 0.396,
145
             'Anzelma': 0.352,
146
             'Woman2': 0.402,
147
             'MotherInnocent': 0.398,
148
             'Gribier': 0.288,
149
             'MmeBurgon': 0.344,
150
             'Jondrette': 0.257,
151
             'Gavroche': 0.514,
152
             'Gillenormand': 0.442,
153
             'Magnon': 0.335,
154
             'MlleGillenormand': 0.442,
155
             'MmePontmercy': 0.315,
156
             'MlleVaubois': 0.308,
157
             'LtGillenormand': 0.365,
158
             'Marius': 0.531,
159
             'BaronessT': 0.352,
160
             'Mabeuf': 0.396,
161
             'Enjolras': 0.481,
162
             'Combeferre': 0.392,
163
             'Prouvaire': 0.357,
164
             'Feuilly': 0.392,
165
             'Courfeyrac': 0.400,
166
             'Bahorel': 0.394,
167
             'Bossuet': 0.475,
168
             'Joly': 0.394,
169
             'Grantaire': 0.358,
170
             'MotherPlutarch': 0.285,
171
             'Gueulemer': 0.463,
172
             'Babet': 0.463,
173
             'Claquesous': 0.452,
174
             'Montparnasse': 0.458,
175
             'Toussaint': 0.402,
176
             'Child1': 0.342,
177
             'Child2': 0.342,
178
             'Brujon': 0.380,
179
             'MmeHucheloup': 0.353}
180
        for n in sorted(self.LM):
181
            assert_almost_equal(c[n], d[n], places=3)
182

    
183
    def test_weighted_closeness(self):
184
        edges = ([('s', 'u', 10), ('s', 'x', 5), ('u', 'v', 1),
185
                  ('u', 'x', 2), ('v', 'y', 1), ('x', 'u', 3),
186
                  ('x', 'v', 5), ('x', 'y', 2), ('y', 's', 7), ('y', 'v', 6)])
187
        XG = nx.Graph()
188
        XG.add_weighted_edges_from(edges)
189
        c = nx.closeness_centrality(XG, distance='weight')
190
        d = {'y': 0.200,
191
             'x': 0.286,
192
             's': 0.138,
193
             'u': 0.235,
194
             'v': 0.200}
195
        for n in sorted(XG):
196
            assert_almost_equal(c[n], d[n], places=3)