Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (8.31 KB)

1
#!/usr/bin/env python
2
from nose.tools import *
3
import networkx as nx
4

    
5

    
6
class TestSubsetBetweennessCentrality:
7

    
8
    def test_K5(self):
9
        """Betweenness Centrality Subset: K5"""
10
        G = nx.complete_graph(5)
11
        b = nx.betweenness_centrality_subset(G, sources=[0], targets=[1, 3],
12
                                             weight=None)
13
        b_answer = {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0}
14
        for n in sorted(G):
15
            assert_almost_equal(b[n], b_answer[n])
16

    
17
    def test_P5_directed(self):
18
        """Betweenness Centrality Subset: P5 directed"""
19
        G = nx.DiGraph()
20
        nx.add_path(G, range(5))
21
        b_answer = {0: 0, 1: 1, 2: 1, 3: 0, 4: 0, 5: 0}
22
        b = nx.betweenness_centrality_subset(G, sources=[0], targets=[3],
23
                                             weight=None)
24
        for n in sorted(G):
25
            assert_almost_equal(b[n], b_answer[n])
26

    
27
    def test_P5(self):
28
        """Betweenness Centrality Subset: P5"""
29
        G = nx.Graph()
30
        nx.add_path(G, range(5))
31
        b_answer = {0: 0, 1: 0.5, 2: 0.5, 3: 0, 4: 0, 5: 0}
32
        b = nx.betweenness_centrality_subset(G, sources=[0], targets=[3],
33
                                             weight=None)
34
        for n in sorted(G):
35
            assert_almost_equal(b[n], b_answer[n])
36

    
37
    def test_P5_multiple_target(self):
38
        """Betweenness Centrality Subset: P5 multiple target"""
39
        G = nx.Graph()
40
        nx.add_path(G, range(5))
41
        b_answer = {0: 0, 1: 1, 2: 1, 3: 0.5, 4: 0, 5: 0}
42
        b = nx.betweenness_centrality_subset(G, sources=[0], targets=[3, 4],
43
                                             weight=None)
44
        for n in sorted(G):
45
            assert_almost_equal(b[n], b_answer[n])
46

    
47
    def test_box(self):
48
        """Betweenness Centrality Subset: box"""
49
        G = nx.Graph()
50
        G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3)])
51
        b_answer = {0: 0, 1: 0.25, 2: 0.25, 3: 0}
52
        b = nx.betweenness_centrality_subset(G, sources=[0], targets=[3],
53
                                             weight=None)
54
        for n in sorted(G):
55
            assert_almost_equal(b[n], b_answer[n])
56

    
57
    def test_box_and_path(self):
58
        """Betweenness Centrality Subset: box and path"""
59
        G = nx.Graph()
60
        G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3), (3, 4), (4, 5)])
61
        b_answer = {0: 0, 1: 0.5, 2: 0.5, 3: 0.5, 4: 0, 5: 0}
62
        b = nx.betweenness_centrality_subset(G, sources=[0], targets=[3, 4],
63
                                             weight=None)
64
        for n in sorted(G):
65
            assert_almost_equal(b[n], b_answer[n])
66

    
67
    def test_box_and_path2(self):
68
        """Betweenness Centrality Subset: box and path multiple target"""
69
        G = nx.Graph()
70
        G.add_edges_from([(0, 1), (1, 2), (2, 3), (1, 20), (20, 3), (3, 4)])
71
        b_answer = {0: 0, 1: 1.0, 2: 0.5, 20: 0.5, 3: 0.5, 4: 0}
72
        b = nx.betweenness_centrality_subset(G, sources=[0], targets=[3, 4],
73
                                             weight=None)
74
        for n in sorted(G):
75
            assert_almost_equal(b[n], b_answer[n])
76

    
77
    def test_diamond_multi_path(self):
78
        """Betweenness Centrality Subset: Diamond Multi Path"""
79
        G = nx.Graph()
80
        G.add_edges_from([
81
            (1,2), 
82
            (1,3), 
83
            (1,4), 
84
            (1,5), 
85
            (1,10), 
86
            (10,11), 
87
            (11,12), 
88
            (12,9), 
89
            (2,6), 
90
            (3,6), 
91
            (4,6), 
92
            (5,7), 
93
            (7,8), 
94
            (6,8), 
95
            (8,9)
96
        ])
97
        b = nx.betweenness_centrality_subset(
98
            G,
99
            sources=[1],
100
            targets=[9],
101
            weight=None
102
        )
103

    
104
        expected_b = {
105
        1: 0,
106
        2: 1./10,
107
        3: 1./10,
108
        4: 1./10,
109
        5: 1./10,
110
        6: 3./10,
111
        7:1./10,
112
        8:4./10,
113
        9:0,
114
        10:1./10,
115
        11:1./10,
116
        12:1./10,
117
        }
118

    
119
        for n in sorted(G):
120
            assert_almost_equal(b[n], expected_b[n])
121

    
122

    
123
class TestBetweennessCentralitySources:
124

    
125
    def test_K5(self):
126
        """Betweenness Centrality Sources: K5"""
127
        G = nx.complete_graph(5)
128
        b = nx.betweenness_centrality_source(G, weight=None, normalized=False)
129
        b_answer = {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0}
130
        for n in sorted(G):
131
            assert_almost_equal(b[n], b_answer[n])
132

    
133
    def test_P3(self):
134
        """Betweenness Centrality Sources: P3"""
135
        G = nx.path_graph(3)
136
        b_answer = {0: 0.0, 1: 1.0, 2: 0.0}
137
        b = nx.betweenness_centrality_source(G, weight=None, normalized=True)
138
        for n in sorted(G):
139
            assert_almost_equal(b[n], b_answer[n])
140

    
141

    
142
class TestEdgeSubsetBetweennessCentrality:
143

    
144
    def test_K5(self):
145
        """Edge betweenness subset centrality: K5"""
146
        G = nx.complete_graph(5)
147
        b = nx.edge_betweenness_centrality_subset(G, sources=[0],
148
                                                  targets=[1, 3], weight=None)
149
        b_answer = dict.fromkeys(G.edges(), 0)
150
        b_answer[(0, 3)] = b_answer[(0, 1)] = 0.5
151
        for n in sorted(G.edges()):
152
            assert_almost_equal(b[n], b_answer[n])
153

    
154
    def test_P5_directed(self):
155
        """Edge betweenness subset centrality: P5 directed"""
156
        G = nx.DiGraph()
157
        nx.add_path(G, range(5))
158
        b_answer = dict.fromkeys(G.edges(), 0)
159
        b_answer[(0, 1)] = b_answer[(1, 2)] = b_answer[(2, 3)] = 1
160
        b = nx.edge_betweenness_centrality_subset(G, sources=[0], targets=[3],
161
                                                  weight=None)
162
        for n in sorted(G.edges()):
163
            assert_almost_equal(b[n], b_answer[n])
164

    
165
    def test_P5(self):
166
        """Edge betweenness subset centrality: P5"""
167
        G = nx.Graph()
168
        nx.add_path(G, range(5))
169
        b_answer = dict.fromkeys(G.edges(), 0)
170
        b_answer[(0, 1)] = b_answer[(1, 2)] = b_answer[(2, 3)] = 0.5
171
        b = nx.edge_betweenness_centrality_subset(G, sources=[0], targets=[3],
172
                                                  weight=None)
173
        for n in sorted(G.edges()):
174
            assert_almost_equal(b[n], b_answer[n])
175

    
176
    def test_P5_multiple_target(self):
177
        """Edge betweenness subset centrality: P5 multiple target"""
178
        G = nx.Graph()
179
        nx.add_path(G, range(5))
180
        b_answer = dict.fromkeys(G.edges(), 0)
181
        b_answer[(0, 1)] = b_answer[(1, 2)] = b_answer[(2, 3)] = 1
182
        b_answer[(3, 4)] = 0.5
183
        b = nx.edge_betweenness_centrality_subset(G, sources=[0],
184
                                                  targets=[3, 4], weight=None)
185
        for n in sorted(G.edges()):
186
            assert_almost_equal(b[n], b_answer[n])
187

    
188
    def test_box(self):
189
        """Edge betweenness subset centrality: box"""
190
        G = nx.Graph()
191
        G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3)])
192
        b_answer = dict.fromkeys(G.edges(), 0)
193
        b_answer[(0, 1)] = b_answer[(0, 2)] = 0.25
194
        b_answer[(1, 3)] = b_answer[(2, 3)] = 0.25
195
        b = nx.edge_betweenness_centrality_subset(G, sources=[0], targets=[3],
196
                                                  weight=None)
197
        for n in sorted(G.edges()):
198
            assert_almost_equal(b[n], b_answer[n])
199

    
200
    def test_box_and_path(self):
201
        """Edge betweenness subset centrality: box and path"""
202
        G = nx.Graph()
203
        G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3), (3, 4), (4, 5)])
204
        b_answer = dict.fromkeys(G.edges(), 0)
205
        b_answer[(0, 1)] = b_answer[(0, 2)] = 0.5
206
        b_answer[(1, 3)] = b_answer[(2, 3)] = 0.5
207
        b_answer[(3, 4)] = 0.5
208
        b = nx.edge_betweenness_centrality_subset(G, sources=[0],
209
                                                  targets=[3, 4], weight=None)
210
        for n in sorted(G.edges()):
211
            assert_almost_equal(b[n], b_answer[n])
212

    
213
    def test_box_and_path2(self):
214
        """Edge betweenness subset centrality: box and path multiple target"""
215
        G = nx.Graph()
216
        G.add_edges_from([(0, 1), (1, 2), (2, 3), (1, 20), (20, 3), (3, 4)])
217
        b_answer = dict.fromkeys(G.edges(), 0)
218
        b_answer[(0, 1)] = 1.0
219
        b_answer[(1, 20)] = b_answer[(3, 20)] = 0.5
220
        b_answer[(1, 2)] = b_answer[(2, 3)] = 0.5
221
        b_answer[(3, 4)] = 0.5
222
        b = nx.edge_betweenness_centrality_subset(G, sources=[0],
223
                                                  targets=[3, 4], weight=None)
224
        for n in sorted(G.edges()):
225
            assert_almost_equal(b[n], b_answer[n])