Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (3.21 KB)

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

    
8

    
9
class TestClosenessCentrality:
10
    def setUp(self):
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.C5 = nx.cycle_graph(5)
17

    
18
        self.T = nx.balanced_tree(r=2, h=2)
19

    
20
        self.Gb = nx.DiGraph()
21
        self.Gb.add_edges_from([(0, 1), (0, 2), (0, 4), (2, 1),
22
                                (2, 3), (4, 3)])
23

    
24
    def test_p3_harmonic(self):
25
        c = harmonic_centrality(self.P3)
26
        d = {0: 1.5,
27
             1: 2,
28
             2: 1.5}
29
        for n in sorted(self.P3):
30
            assert_almost_equal(c[n], d[n], places=3)
31

    
32
    def test_p4_harmonic(self):
33
        c = harmonic_centrality(self.P4)
34
        d = {0: 1.8333333,
35
             1: 2.5,
36
             2: 2.5,
37
             3: 1.8333333}
38
        for n in sorted(self.P4):
39
            assert_almost_equal(c[n], d[n], places=3)
40

    
41
    def test_clique_complete(self):
42
        c = harmonic_centrality(self.K5)
43
        d = {0: 4,
44
             1: 4,
45
             2: 4,
46
             3: 4,
47
             4: 4}
48
        for n in sorted(self.P3):
49
            assert_almost_equal(c[n], d[n], places=3)
50

    
51
    def test_cycle_C4(self):
52
        c = harmonic_centrality(self.C4)
53
        d = {0: 2.5,
54
             1: 2.5,
55
             2: 2.5,
56
             3: 2.5, }
57
        for n in sorted(self.C4):
58
            assert_almost_equal(c[n], d[n], places=3)
59

    
60
    def test_cycle_C5(self):
61
        c = harmonic_centrality(self.C5)
62
        d = {0: 3,
63
             1: 3,
64
             2: 3,
65
             3: 3,
66
             4: 3,
67
             5: 4}
68
        for n in sorted(self.C5):
69
            assert_almost_equal(c[n], d[n], places=3)
70

    
71
    def test_bal_tree(self):
72
        c = harmonic_centrality(self.T)
73
        d = {0: 4.0,
74
             1: 4.1666,
75
             2: 4.1666,
76
             3: 2.8333,
77
             4: 2.8333,
78
             5: 2.8333,
79
             6: 2.8333}
80
        for n in sorted(self.T):
81
            assert_almost_equal(c[n], d[n], places=3)
82

    
83
    def test_exampleGraph(self):
84
        c = harmonic_centrality(self.Gb)
85
        d = {0: 0,
86
             1: 2,
87
             2: 1,
88
             3: 2.5,
89
             4: 1}
90
        for n in sorted(self.Gb):
91
            assert_almost_equal(c[n], d[n], places=3)
92

    
93
    def test_weighted_harmonic(self):
94
        XG = nx.DiGraph()
95
        XG.add_weighted_edges_from([('a', 'b', 10), ('d', 'c', 5), ('a', 'c', 1),
96
                                    ('e', 'f', 2), ('f', 'c', 1), ('a', 'f', 3),
97
                                    ])
98
        c = harmonic_centrality(XG, distance='weight')
99
        d = {'a': 0,
100
             'b': 0.1,
101
             'c': 2.533,
102
             'd': 0,
103
             'e': 0,
104
             'f': 0.83333}
105
        for n in sorted(XG):
106
            assert_almost_equal(c[n], d[n], places=3)
107

    
108
    def test_empty(self):
109
        G = nx.DiGraph()
110
        c = harmonic_centrality(G, distance='weight')
111
        d = {}
112
        assert_equal(c, d)
113

    
114
    def test_singleton(self):
115
        G = nx.DiGraph()
116
        G.add_node(0)
117
        c = harmonic_centrality(G, distance='weight')
118
        d = {0: 0}
119
        assert_equal(c, d)