~ubuntu-branches/ubuntu/utopic/python-networkx/utopic

« back to all changes in this revision

Viewing changes to networkx/algorithms/bipartite/centrality.py

  • Committer: Package Import Robot
  • Author(s): Sandro Tosi
  • Date: 2012-08-11 12:41:30 UTC
  • mfrom: (1.4.1) (5.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20120811124130-whr6uso7fncyg8bi
Tags: 1.7-1
* New upstream release
* debian/patches/changeset_*
  - removed, included upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#-*- coding: utf-8 -*-
 
2
#    Copyright (C) 2011 by 
 
3
#    Jordi Torrents <jtorrents@milnou.net>
 
4
#    Aric Hagberg <hagberg@lanl.gov>
 
5
#    All rights reserved.
 
6
#    BSD license.
 
7
import networkx as nx
 
8
__author__ = """\n""".join(['Jordi Torrents <jtorrents@milnou.net>',
 
9
                            'Aric Hagberg (hagberg@lanl.gov)'])
 
10
__all__=['degree_centrality',
 
11
         'betweenness_centrality',
 
12
         'closeness_centrality']
 
13
 
 
14
def degree_centrality(G, nodes):
 
15
    r"""Compute the degree centrality for nodes in a bipartite network.
 
16
 
 
17
    The degree centrality for a node `v` is the fraction of nodes 
 
18
    connected to it.
 
19
 
 
20
    Parameters
 
21
    ----------
 
22
    G : graph
 
23
       A bipartite network
 
24
 
 
25
    nodes : list or container
 
26
      Container with all nodes in one bipartite node set.
 
27
 
 
28
    Returns
 
29
    -------
 
30
    centrality : dictionary
 
31
       Dictionary keyed by node with bipartite degree centrality as the value.
 
32
 
 
33
    See Also
 
34
    --------
 
35
    betweenness_centrality,
 
36
    closeness_centrality,
 
37
    sets,
 
38
    is_bipartite
 
39
 
 
40
    Notes
 
41
    -----
 
42
    The nodes input parameter must conatin all nodes in one bipartite node set,
 
43
    but the dictionary returned contains all nodes from both bipartite node
 
44
    sets.
 
45
 
 
46
    For unipartite networks, the degree centrality values are 
 
47
    normalized by dividing by the maximum possible degree (which is 
 
48
    `n-1` where `n` is the number of nodes in G). 
 
49
 
 
50
    In the bipartite case, the maximum possible degree of a node in a
 
51
    bipartite node set is the number of nodes in the opposite node set
 
52
    [1]_.  The degree centrality for a node `v` in the bipartite
 
53
    sets `U` with `n` nodes and `V` with `m` nodes is
 
54
 
 
55
    .. math::
 
56
 
 
57
        d_{v} = \frac{deg(v)}{m}, \mbox{for} v \in U ,
 
58
 
 
59
        d_{v} = \frac{deg(v)}{n}, \mbox{for} v \in V ,
 
60
 
 
61
 
 
62
    where `deg(v)` is the degree of node `v`.        
 
63
 
 
64
    References
 
65
    ----------
 
66
    .. [1] Borgatti, S.P. and Halgin, D. In press. "Analyzing Affiliation 
 
67
        Networks". In Carrington, P. and Scott, J. (eds) The Sage Handbook 
 
68
        of Social Network Analysis. Sage Publications.
 
69
        http://www.steveborgatti.com/papers/bhaffiliations.pdf
 
70
    """
 
71
    top = set(nodes)
 
72
    bottom = set(G) - top
 
73
    s = 1.0/len(bottom)
 
74
    centrality = dict((n,d*s) for n,d in G.degree_iter(top))
 
75
    s = 1.0/len(top)
 
76
    centrality.update(dict((n,d*s) for n,d in G.degree_iter(bottom)))
 
77
    return centrality
 
78
 
 
79
 
 
80
def betweenness_centrality(G, nodes):
 
81
    r"""Compute betweenness centrality for nodes in a bipartite network.
 
82
 
 
83
    Betweenness centrality of a node `v` is the sum of the
 
84
    fraction of all-pairs shortest paths that pass through `v`. 
 
85
 
 
86
    Values of betweenness are normalized by the maximum possible
 
87
    value which for bipartite graphs is limited by the relative size 
 
88
    of the two node sets [1]_.
 
89
 
 
90
    Let `n` be the number of nodes in the node set `U` and
 
91
    `m` be the number of nodes in the node set `V`, then
 
92
    nodes in `U` are normalized by dividing by 
 
93
 
 
94
    .. math::
 
95
 
 
96
       \frac{1}{2} [m^2 (s + 1)^2 + m (s + 1)(2t - s - 1) - t (2s - t + 3)] ,
 
97
 
 
98
    where
 
99
    
 
100
    .. math::
 
101
        
 
102
        s = (n - 1) \div m , t = (n - 1) \mod m ,
 
103
    
 
104
    and nodes in `V` are normalized by dividing by
 
105
 
 
106
    .. math::    
 
107
 
 
108
        \frac{1}{2} [n^2 (p + 1)^2 + n (p + 1)(2r - p - 1) - r (2p - r + 3)] ,
 
109
 
 
110
    where,
 
111
    
 
112
    .. math::
 
113
 
 
114
        p = (m - 1) \div n , r = (m - 1) \mod n .
 
115
 
 
116
    Parameters
 
117
    ----------
 
118
    G : graph
 
119
        A bipartite graph
 
120
 
 
121
    nodes : list or container
 
122
        Container with all nodes in one bipartite node set.
 
123
 
 
124
    Returns
 
125
    -------
 
126
    betweenness : dictionary
 
127
        Dictionary keyed by node with bipartite betweenness centrality 
 
128
        as the value.
 
129
 
 
130
    See Also
 
131
    --------
 
132
    degree_centrality,
 
133
    closeness_centrality,
 
134
    sets,
 
135
    is_bipartite
 
136
 
 
137
    Notes
 
138
    -----
 
139
    The nodes input parameter must contain all nodes in one bipartite node set,
 
140
    but the dictionary returned contains all nodes from both node sets.
 
141
 
 
142
    References
 
143
    ----------
 
144
    .. [1] Borgatti, S.P. and Halgin, D. In press. "Analyzing Affiliation 
 
145
        Networks". In Carrington, P. and Scott, J. (eds) The Sage Handbook 
 
146
        of Social Network Analysis. Sage Publications.
 
147
        http://www.steveborgatti.com/papers/bhaffiliations.pdf
 
148
    """
 
149
    top = set(nodes)
 
150
    bottom = set(G) - top
 
151
    n = float(len(top))
 
152
    m = float(len(bottom))
 
153
    s = (n-1) // m
 
154
    t = (n-1) % m
 
155
    bet_max_top = (((m**2)*((s+1)**2))+
 
156
                   (m*(s+1)*(2*t-s-1))-
 
157
                   (t*((2*s)-t+3)))/2.0
 
158
    p = (m-1) // n
 
159
    r = (m-1) % n
 
160
    bet_max_bot = (((n**2)*((p+1)**2))+
 
161
                   (n*(p+1)*(2*r-p-1))-
 
162
                   (r*((2*p)-r+3)))/2.0
 
163
    betweenness = nx.betweenness_centrality(G, normalized=False, 
 
164
                                            weight=None)
 
165
    for node in top:
 
166
        betweenness[node]/=bet_max_top
 
167
    for node in bottom:
 
168
        betweenness[node]/=bet_max_bot
 
169
    return betweenness
 
170
 
 
171
def closeness_centrality(G, nodes, normalized=True):
 
172
    r"""Compute the closeness centrality for nodes in a bipartite network.
 
173
 
 
174
    The closeness of a node is the distance to all other nodes in the 
 
175
    graph or in the case that the graph is not connected to all other nodes
 
176
    in the connected component containing that node.
 
177
 
 
178
    Parameters
 
179
    ----------
 
180
    G : graph
 
181
        A bipartite network
 
182
 
 
183
    nodes : list or container
 
184
        Container with all nodes in one bipartite node set.
 
185
 
 
186
    normalized : bool, optional      
 
187
      If True (default) normalize by connected component size.
 
188
 
 
189
    Returns
 
190
    -------
 
191
    closeness : dictionary
 
192
        Dictionary keyed by node with bipartite closeness centrality 
 
193
        as the value.
 
194
 
 
195
    See Also
 
196
    --------
 
197
    betweenness_centrality,
 
198
    degree_centrality
 
199
    sets,
 
200
    is_bipartite
 
201
 
 
202
    Notes
 
203
    -----
 
204
    The nodes input parameter must conatin all nodes in one bipartite node set,
 
205
    but the dictionary returned contains all nodes from both node sets.
 
206
 
 
207
    Closeness centrality is normalized by the minimum distance possible. 
 
208
    In the bipartite case the minimum distance for a node in one bipartite 
 
209
    node set is 1 from all nodes in the other node set and 2 from all 
 
210
    other nodes in its own set [1]_. Thus the closeness centrality
 
211
    for node `v`  in the two bipartite sets `U` with 
 
212
    `n` nodes and `V` with `m` nodes is 
 
213
 
 
214
    .. math::
 
215
 
 
216
        c_{v} = \frac{m + 2(n - 1)}{d}, \mbox{for} v \in U,
 
217
 
 
218
        c_{v} = \frac{n + 2(m - 1)}{d}, \mbox{for} v \in V,
 
219
 
 
220
    where `d` is the sum of the distances from `v` to all
 
221
    other nodes.
 
222
 
 
223
    Higher values of closeness  indicate higher centrality.
 
224
 
 
225
    As in the unipartite case, setting normalized=True causes the
 
226
    values to normalized further to n-1 / size(G)-1 where n is the
 
227
    number of nodes in the connected part of graph containing the
 
228
    node.  If the graph is not completely connected, this algorithm
 
229
    computes the closeness centrality for each connected part
 
230
    separately.
 
231
 
 
232
    References
 
233
    ----------
 
234
    .. [1] Borgatti, S.P. and Halgin, D. In press. "Analyzing Affiliation 
 
235
        Networks". In Carrington, P. and Scott, J. (eds) The Sage Handbook 
 
236
        of Social Network Analysis. Sage Publications.
 
237
        http://www.steveborgatti.com/papers/bhaffiliations.pdf
 
238
    """
 
239
    closeness={}
 
240
    path_length=nx.single_source_shortest_path_length
 
241
    top = set(nodes)
 
242
    bottom = set(G) - top
 
243
    n = float(len(top))
 
244
    m = float(len(bottom))
 
245
    for node in top:
 
246
        sp=path_length(G,node)
 
247
        totsp=sum(sp.values())
 
248
        if totsp > 0.0 and len(G) > 1:
 
249
            closeness[node]= (m + 2*(n-1)) / totsp
 
250
            if normalized:
 
251
                s=(len(sp)-1.0) / ( len(G) - 1 )
 
252
                closeness[node] *= s
 
253
        else:
 
254
            closeness[n]=0.0
 
255
    for node in bottom:
 
256
        sp=path_length(G,node)
 
257
        totsp=sum(sp.values())
 
258
        if totsp > 0.0 and len(G) > 1:
 
259
            closeness[node]= (n + 2*(m-1)) / totsp
 
260
            if normalized:
 
261
                s=(len(sp)-1.0) / ( len(G) - 1 )
 
262
                closeness[node] *= s
 
263
        else:
 
264
            closeness[n]=0.0
 
265
    return closeness
 
266