-
Notifications
You must be signed in to change notification settings - Fork 76
/
PriceModel.m
executable file
·57 lines (44 loc) · 1.4 KB
/
PriceModel.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
% Routine implementing the Price model for network growth
% Notes:
% p_k - fraction of vertices with degree k
% probability a new vertex attaches to any of the degree-k vertices is
% (k+1)p_k/(m+1), where m - mean number of new citations per vertex
% Source: "The Structure and Function of Complex Networks", M.E.J. Newman
%
% INPUTs: n - final number of vertices
% OUTPUTs: adjacency matrix, directed
%
% Last modified: November 9, 2012
function adj = PriceModel(n)
adj = zeros(n);
adj(1, 1) = 1; % start with a self-loop
vertices = 1;
while vertices < n
% attach new vertex
vertices = vertices + 1;
adj(vertices, vertices) = 1;
indeg = sum(adj); % get indegree values
m = 0; % mean in-degree (per vertex)
for k = 1:vertices
pk(k) = numel(find(indeg == k)) / vertices;
m = m + pk(k) * k;
end
% attach new edges with probability (k+1)pk/(m+1)
for k = 1:vertices
if rand < (k + 1) * pk(k) / (m + 1);
adj(vertices, k) = adj(vertices, k) + 1;
end
end
end
adj = adj - diag(diag(adj)); % remove self-loops
%!test
%! for x=1:20
%! randint = randi(10)+10;
%! adj = PriceModel(randint);
%! assert(isDirected(adj),true)
%! assert(numNodes(adj),randint)
%! end
%!demo
%! n = randi(10)+10;
%! adj = PriceModel(n);
%! assert(numNodes(adj),n)