Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (3.48 KB)

1
"""
2
    Unit tests for degree centrality.
3
"""
4

    
5
from nose.tools import *
6

    
7
import networkx as nx
8

    
9

    
10
class TestDegreeCentrality:
11
    def __init__(self):
12

    
13
        self.K = nx.krackhardt_kite_graph()
14
        self.P3 = nx.path_graph(3)
15
        self.K5 = nx.complete_graph(5)
16

    
17
        F = nx.Graph()  # Florentine families
18
        F.add_edge('Acciaiuoli', 'Medici')
19
        F.add_edge('Castellani', 'Peruzzi')
20
        F.add_edge('Castellani', 'Strozzi')
21
        F.add_edge('Castellani', 'Barbadori')
22
        F.add_edge('Medici', 'Barbadori')
23
        F.add_edge('Medici', 'Ridolfi')
24
        F.add_edge('Medici', 'Tornabuoni')
25
        F.add_edge('Medici', 'Albizzi')
26
        F.add_edge('Medici', 'Salviati')
27
        F.add_edge('Salviati', 'Pazzi')
28
        F.add_edge('Peruzzi', 'Strozzi')
29
        F.add_edge('Peruzzi', 'Bischeri')
30
        F.add_edge('Strozzi', 'Ridolfi')
31
        F.add_edge('Strozzi', 'Bischeri')
32
        F.add_edge('Ridolfi', 'Tornabuoni')
33
        F.add_edge('Tornabuoni', 'Guadagni')
34
        F.add_edge('Albizzi', 'Ginori')
35
        F.add_edge('Albizzi', 'Guadagni')
36
        F.add_edge('Bischeri', 'Guadagni')
37
        F.add_edge('Guadagni', 'Lamberteschi')
38
        self.F = F
39

    
40
        G = nx.DiGraph()
41
        G.add_edge(0, 5)
42
        G.add_edge(1, 5)
43
        G.add_edge(2, 5)
44
        G.add_edge(3, 5)
45
        G.add_edge(4, 5)
46
        G.add_edge(5, 6)
47
        G.add_edge(5, 7)
48
        G.add_edge(5, 8)
49
        self.G = G
50

    
51
    def test_degree_centrality_1(self):
52
        d = nx.degree_centrality(self.K5)
53
        exact = dict(zip(range(5), [1] * 5))
54
        for n, dc in d.items():
55
            assert_almost_equal(exact[n], dc)
56

    
57
    def test_degree_centrality_2(self):
58
        d = nx.degree_centrality(self.P3)
59
        exact = {0: 0.5, 1: 1, 2: 0.5}
60
        for n, dc in d.items():
61
            assert_almost_equal(exact[n], dc)
62

    
63
    def test_degree_centrality_3(self):
64
        d = nx.degree_centrality(self.K)
65
        exact = {0: .444, 1: .444, 2: .333, 3: .667, 4: .333,
66
                 5: .556, 6: .556, 7: .333, 8: .222, 9: .111}
67
        for n, dc in d.items():
68
            assert_almost_equal(exact[n], float("%5.3f" % dc))
69

    
70
    def test_degree_centrality_4(self):
71
        d = nx.degree_centrality(self.F)
72
        names = sorted(self.F.nodes())
73
        dcs = [0.071, 0.214, 0.143, 0.214, 0.214, 0.071, 0.286,
74
               0.071, 0.429, 0.071, 0.214, 0.214, 0.143, 0.286, 0.214]
75
        exact = dict(zip(names, dcs))
76
        for n, dc in d.items():
77
            assert_almost_equal(exact[n], float("%5.3f" % dc))
78

    
79
    def test_indegree_centrality(self):
80
        d = nx.in_degree_centrality(self.G)
81
        exact = {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0,
82
                 5: 0.625, 6: 0.125, 7: 0.125, 8: 0.125}
83
        for n, dc in d.items():
84
            assert_almost_equal(exact[n], dc)
85

    
86
    def test_outdegree_centrality(self):
87
        d = nx.out_degree_centrality(self.G)
88
        exact = {0: 0.125, 1: 0.125, 2: 0.125, 3: 0.125,
89
                 4: 0.125, 5: 0.375, 6: 0.0, 7: 0.0, 8: 0.0}
90
        for n, dc in d.items():
91
            assert_almost_equal(exact[n], dc)
92

    
93
    def test_small_graph_centrality(self):
94
        G = nx.empty_graph(create_using=nx.DiGraph)
95
        assert_equal({}, nx.degree_centrality(G))
96
        assert_equal({}, nx.out_degree_centrality(G))
97
        assert_equal({}, nx.in_degree_centrality(G))
98

    
99
        G = nx.empty_graph(1, create_using=nx.DiGraph)
100
        assert_equal({0: 1}, nx.degree_centrality(G))
101
        assert_equal({0: 1}, nx.out_degree_centrality(G))
102
        assert_equal({0: 1}, nx.in_degree_centrality(G))