-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_network.m
177 lines (145 loc) · 5.5 KB
/
create_network.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
function A = create_network( N, random_graph_model, p_ER, p_WS, K_WS, m0_BA, m_BA, connectivityFlag )
empirical_network_folder = '.\empirical_networks';
if strcmp(random_graph_model, 'ER_directed') || ...
strcmp(random_graph_model, 'little_rock_lake') || ...
strcmp(random_graph_model, 'mutualistic') || ...
strcmp(random_graph_model, 'infectious') || ...
strcmp(random_graph_model, 'c_elegans_neuronal') || ...
strcmp(random_graph_model, 'yeast_regulatory')
symmetric_flag = false;
else
symmetric_flag = true;
end
switch random_graph_model
case 'little_rock_lake'
data = load([ empirical_network_folder '\food_web\adjacency_matrix.mat'], 'A');
A = data.A;
case 'mutualistic'
data = load([ empirical_network_folder '\mutualistic_network\adjacency_matrix.mat'], 'A');
A = data.A;
case 'infectious'
data = load([ empirical_network_folder '\infectious_network\adjacency_matrix.mat'], 'A');
A = data.A;
case 'c_elegans_neuronal'
data = load([ empirical_network_folder '\neuron_network_c_elegans\adjacency_matrix.mat'], 'A');
A = data.A;
case 'yeast_regulatory'
data = load([ empirical_network_folder '\yeast_transcriptional_regulatory_network\adjacency_matrix.mat'], 'A');
A = data.A;
case 'human_connectome'
%HCP data not available, use scale-free network instead:
N = 78;
m0_BA = 3;
m_BA = 3;
A = BA( N, m0_BA, m_BA );
disp( 'Warning: Using Barabasi-Albert network for human connectome.')
case 'WS'
G_graph = WattsStrogatz( N, K_WS, p_WS );
A = G_graph.adjacency;
case 'BA'
A = BA( N, m0_BA, m_BA );
case 'ER_undirected'
L = N*(N-1)/2;
connectedComponents = 2;
while connectedComponents>1
A = zeros(N);
A(triu(ones(N), 1)>0) = (rand(L, 1) <= p_ER) ;
A = (A + transpose(A));
if connectivityFlag
[connectedComponents, ~] = graphconncomp(sparse(A), 'Directed', false);
else
connectedComponents=1;
end
end
case 'ER_directed'
L = N*(N-1)/2;
connectedComponents = 2;
while connectedComponents>1
A = zeros(N);
A(triu(ones(N), 1)>0) = (rand(L, 1) <= p_ER) ;
A(tril(ones(N), -1)>0) = (rand(L, 1) <= p_ER) ;
if connectivityFlag
[connectedComponents, ~] = graphconncomp(sparse(A), 'Directed', true);
else
connectedComponents=1;
end
end
end
if symmetric_flag && norm( full( A - transpose( A )))>0
error('Network not symmetric!')
end
if connectivityFlag
[connectedComponents, ~] = graphconncomp(sparse( A ), 'Directed', true);
if connectedComponents>1
error('Network not connected!')
end
end
end
function net = BA( N, m0, m )
%Generates a network with N nodes and avg. node degree 2m following the
%Barabasi-Albert model, starting from a network with m0 nodes.
%
%This implementation is based on the model published in:
%
% Barab?si,A.-L. and Albert,R. (1999) Emergence of Scaling in Random
% Networks. Science, 286, 509-512.
%
%INPUT
% N -> Number of nodes in the graph (default = 1000).
% m0 -> Initial number of nodes in the network (default = 5). This
% parameters has to be > 0.
% m -> Number of nodes with which a new node in the network connects to
% This parameter has to be <= to m0 (default = 3).
%
%OUPUT
% net -> The adjacency matrix representation of the constructed
% Barb?si-Albert network. If the number of parameters exceeds the
% maximum allowed, the returned matrix is empty.
%
%Example usage (note the parameter names are not sensitive to case):
%
% my_network = ba_net();
% my_network = ba_net('N', 100);
% my_network = ba_net('m0', 10);
% my_network = ba_net('m', 2);
% my_network = er_net('n', 500, 'M0', 25, 'M', 10);
%
% Copyright (C) Gregorio Alanis-Lobato, 2014
if m> m0
error('m0 must be greater or equal than m')
end
%%% Barab?si-Albert network construction
net = zeros(N);
%Nodes 1 to m0 form a clique
net(1:m0, 1:m0) = 1;
net(logical(eye(N))) = 0;
for i = (m0+1):N
%Add a new node to the network and connect to m existing nodes if they
%indeed exist
if i <= m
net(i, 1:i) = 1; %If m is larger than the number of nodes in the system, connect to all of them
net(i, i) = 0; %Self-connections not allowed
else
k = sum(net(1:(i-1), 1:(i-1)), 2); %Degree of the existing nodes
k_total = sum(k);
p = k./k_total; %Attraction probability of each node in the network
r = rand(i-1, 1);
if nnz(r < p) < m
%Connect to the m minimum p
[~, idx] = sort(r);
net(i, idx(1:m)) = 1;
else
%Find with whom to connect to
net(i, r < p) = 1;
end
end
end
%Make sure the network is symmetric and has zero-diagonal
net = (net + transpose( net ))>0;
net(logical(eye(N))) = 0;
net = double(net);
[connectedComponents, ~] = graphconncomp(sparse(net), 'Directed', false);
if connectedComponents ~= 1
error('disconnected graph')
end
end