8
8
# Pieter Swart <swart@lanl.gov>
9
9
# All rights reserved.
11
__author__ = """Aric Hagberg (hagberg@lanl.gov)"""
11
__author__ = """Aric Hagberg <aric.hagberg@gmail.com>"""
13
13
__all__ = ['current_flow_closeness_centrality','information_centrality']
15
15
import networkx as nx
16
from networkx.algorithms.centrality.flow_matrix import *
17
def current_flow_closeness_centrality(G,normalized=True):
19
def current_flow_closeness_centrality(G, normalized=True, weight='weight',
20
dtype=float, solver='lu'):
18
21
"""Compute current-flow closeness centrality for nodes.
20
23
A variant of closeness centrality based on effective
29
32
normalized : bool, optional
30
33
If True the values are normalized by 1/(n-1) where n is the
31
34
number of nodes in G.
36
dtype: data type (float)
37
Default data type for internal matrices.
38
Set to np.float32 for lower memory consumption.
40
solver: string (default='lu')
41
Type of linear solver to use for computing the flow matrix.
42
Options are "full" (uses most memory), "lu" (recommended), and
43
"cg" (uses least memory).
61
70
Social Networks. Volume 11, Issue 1, March 1989, pp. 1-37
62
71
http://dx.doi.org/10.1016/0378-8733(89)90016-6
73
from networkx.utils import reverse_cuthill_mckee_ordering
66
76
except ImportError:
67
raise ImportError("flow_closeness_centrality() requires NumPy: http://scipy.org/ ")
77
raise ImportError('current_flow_closeness_centrality requires NumPy ',
82
raise ImportError('current_flow_closeness_centrality requires SciPy ',
85
raise nx.NetworkXError('current_flow_closeness_centrality ',
86
'not defined for digraphs.')
70
87
if G.is_directed():
71
88
raise nx.NetworkXError(\
72
89
"current_flow_closeness_centrality() not defined for digraphs.")
74
90
if not nx.is_connected(G):
75
91
raise nx.NetworkXError("Graph not connected.")
92
solvername={"full" :FullInverseLaplacian,
93
"lu": SuperLUInverseLaplacian,
94
"cg": CGInverseLaplacian}
95
n = G.number_of_nodes()
96
ordering = list(reverse_cuthill_mckee_ordering(G))
97
# make a copy with integer labels according to rcm ordering
98
# this could be done without a copy if we really wanted to
99
H = nx.relabel_nodes(G,dict(zip(ordering,range(n))))
100
betweenness = dict.fromkeys(H,0.0) # b[v]=0 for v in H
101
n = G.number_of_nodes()
102
L = laplacian_sparse_matrix(H, nodelist=range(n), weight=weight,
103
dtype=dtype, format='csc')
104
C2 = solvername[solver](L, width=1, dtype=dtype) # initialize solver
108
betweenness[v]+=col[v]-2*col[w]
109
betweenness[w]+=col[v]
77
betweenness=dict.fromkeys(G,0.0) # b[v]=0 for v in G
79
mapping=dict(zip(G,list(range(n)))) # map nodes to integers
86
betweenness[v]+=C[vi,vi]-2*C[wi,vi]
87
betweenness[w]+=C[vi,vi]
90
112
nb=len(betweenness)-1.0
94
116
betweenness[v]=nb/(betweenness[v])
99
"""Compute inverse of Laplacian."""
103
raise ImportError("flow_closeness_centrality() requires NumPy: http://scipy.org/ ")
104
L=nx.laplacian(G) # use ordering of G.nodes()
105
# remove first row and column
107
LRinv=np.linalg.inv(LR)
117
return dict((ordering[k],float(v)) for k,v in betweenness.items())
112
119
information_centrality=current_flow_closeness_centrality