-
Notifications
You must be signed in to change notification settings - Fork 76
/
masterEquationGrowthModel.m
executable file
·78 lines (59 loc) · 1.86 KB
/
masterEquationGrowthModel.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
% "Master equation" growth model, as presented in
% "Evolution of Networks" by Dorogovtsev, Mendez
% Note: probability of attachment: (q(i)+ma)/((1+a)mt),
% q(i)-indegree of i, a=const, t - time step (# nodes)
%
% INPUTS: number of nodes n, m - # links to add at each step, a=constant
% OUTPUTS: adjacency matrix, nxn
%
% Other routines used: weightedRandomSample.m
% Last updated: Nov 11, 2012
function adj = masterEquationGrowthModel(n, m, a)
adj = zeros(n);
adj(1, 2) = 2;
adj(2, 1) = 2; % initial condition
vertices = 2;
if nargin == 2 || a == [];
a = 2;
end % pick a constant
while vertices < n
t = vertices;
q = sum(adj); % in-degrees
% compute the probability of attachment
pk = zeros(1, t);
for k = 1:t
pk(k) = (q(k) + m * a) / ((1 + a) * m * t);
end
r = weightedRandomSample(m, [1:t], pk / sum(pk));
if length(unique(r)) ~= length(r)
r = weightedRandomSample(m, [1:t], pk / sum(pk));
end
vertices = vertices + 1; % add vertex
% add m links
for node = 1:length(r)
adj(vertices, r(node)) = 1;
adj(r(node), vertices) = 1;
end
adj(vertices, vertices) = 1; % assure non-zero probability of attachment
end
adj = adj > 0; % no multi-edges
adj = adj - diag(diag(adj)); % remove self-loops
%!test
%! for x=1:30
%! randint = randi(100)+5;
%! adj = masterEquationGrowthModel(randint,1,0);
%! assert(isTree(adj),true)
%!
%! adj = masterEquationGrowthModel(randint,2,0);
%! assert(isTree(adj),false)
%!
%! adj = masterEquationGrowthModel(randint,2,2);
%! assert(isSimple(adj),true)
%!
%! end
%!demo
%! adj = masterEquationGrowthModel(100, 1, 0);
%! isTree(adj)
%! adj = masterEquationGrowthModel(99, 2, 1);
%! isSimple(adj)
%! numNodes(adj)