-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathefficiency_wei.m
140 lines (129 loc) · 6.1 KB
/
efficiency_wei.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
function E = efficiency_wei(W, local)
%EFFICIENCY_WEI Global efficiency, local efficiency.
%
% Eglob = efficiency_wei(W);
% Eloc = efficiency_wei(W,2);
%
% The global efficiency is the average of inverse shortest path length,
% and is inversely related to the characteristic path length.
%
% The local efficiency is the global efficiency computed on the
% neighborhood of the node, and is related to the clustering coefficient.
%
% Inputs: W,
% weighted undirected or directed connection matrix
%
% local,
% optional argument
% local=0 computes the global efficiency (default).
% local=1 computes the original version of the local
% efficiency.
% local=2 computes the modified version of the local
% efficiency (recommended, see below).
% local=3 computes the nodal efficiency (added by me)
%
% Output: Eglob,
% global efficiency (scalar)
% Eloc,
% local efficiency (vector)
%
% Notes:
% The efficiency is computed using an auxiliary connection-length
% matrix L, defined as L_ij = 1/W_ij for all nonzero L_ij; This has an
% intuitive interpretation, as higher connection weights intuitively
% correspond to shorter lengths.
% The weighted local efficiency broadly parallels the weighted
% clustering coefficient of Onnela et al. (2005) and distinguishes the
% influence of different paths based on connection weights of the
% corresponding neighbors to the node in question. In other words, a path
% between two neighbors with strong connections to the node in question
% contributes more to the local efficiency than a path between two weakly
% connected neighbors. Note that the original weighted variant of the
% local efficiency (described in Rubinov and Sporns, 2010) is not a
% true generalization of the binary variant, while the modified variant
% (described in Wang et al., 2016) is a true generalization.
% For ease of interpretation of the local efficiency it may be
% advantageous to rescale all weights to lie between 0 and 1.
%
% Algorithm: Dijkstra's algorithm
%
% References: Latora and Marchiori (2001) Phys Rev Lett 87:198701.
% Onnela et al. (2005) Phys Rev E 71:065103
% Fagiolo (2007) Phys Rev E 76:026107.
% Rubinov M, Sporns O (2010) NeuroImage 52:1059-69
% Wang Y et al. (2016) Neural Comput 21:1-19.
%
% Mika Rubinov, U Cambridge/Janelia HHMI, 2011-2017
%Modification history
% 2011: Original (based on efficiency.m and distance_wei.m)
% 2013: Local efficiency generalized to directed networks
% 2017: Added the modified local efficiency and updated documentation.
n = length(W); % number of nodes
ot = 1 / 3; % one third
L = W; % connection-length matrix
A = W > 0; % adjacency matrix
L(A) = 1 ./ L(A);
A = double(A);
if exist('local','var') && local % local efficiency
E = zeros(n, 1);
cbrt_W = W.^ot;
switch local
case 1
for u = 1:n
V = find(A(u, :) | A(:, u).'); % neighbors
sw = cbrt_W(u, V) + cbrt_W(V, u).'; % symmetrized weights vector
di = distance_inv_wei(L(V, V)); % inverse distance matrix
se = di.^ot + di.'.^ot; % symmetrized inverse distance matrix
numer = (sum(sum((sw.' * sw) .* se)))/2; % numerator
if numer~=0
sa = A(u, V) + A(V, u).'; % symmetrized adjacency vector
denom = sum(sa).^2 - sum(sa.^2); % denominator
E(u) = numer / denom; % local efficiency
end
end
case 2
cbrt_L = L.^ot;
for u = 1:n
V = find(A(u, :) | A(:, u).'); % neighbors
sw = cbrt_W(u, V) + cbrt_W(V, u).'; % symmetrized weights vector
di = distance_inv_wei(cbrt_L(V, V)); % inverse distance matrix
se = di + di.'; % symmetrized inverse distance matrix
numer=(sum(sum((sw.' * sw) .* se)))/2; % numerator
if numer~=0
sa = A(u, V) + A(V, u).'; % symmetrized adjacency vector
denom = sum(sa).^2 - sum(sa.^2); % denominator
E(u) = numer / denom; % local efficiency
end
end
case 3
di = distance_inv_wei(L);
E = sum(di,1) ./ (n - 1); % nodal efficiency (at each node)
end
else
di = distance_inv_wei(L);
E = sum(di(:)) ./ (n^2 - n); % global efficiency
end
function D=distance_inv_wei(W_)
n_=length(W_);
D=inf(n_); % distance matrix
D(1:n_+1:end)=0;
for u=1:n_
S=true(1,n_); % distance permanence (true is temporary)
W1_=W_;
V=u;
while 1
S(V)=0; % distance u->V is now permanent
W1_(:,V)=0; % no in-edges as already shortest
for v=V
T=find(W1_(v,:)); % neighbours of shortest nodes
D(u,T)=min([D(u,T);D(u,v)+W1_(v,T)]); % smallest of old/new path lengths
end
minD=min(D(u,S));
if isempty(minD)||isinf(minD) % isempty: all nodes reached;
break, % isinf: some nodes cannot be reached
end
V=find(D(u,:)==minD);
end
end
D=1./D; % invert distance
D(1:n_+1:end)=0;