-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathforestFireModel.m
executable file
·95 lines (70 loc) · 2.43 KB
/
forestFireModel.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
% Implementation of the forest fire model by Leskovec et al
% "Graphs over Time: Densification Laws, Shrinking
% Diameters and Possible Explanations"
%
% Inputs: forward burning probability p in [0,1],
% backward burning ratio r, in [0,inf),
% T - number of nodes
% Outputs: adjacency list of the constructed (directed) graph
%
% Note: Requires the geornd function from the statistics package.
% Other routines used: weightedRandomSample.m
% Last updated: November 28, 2012
function L = forestFireModel(T, p, r)
if T == 1
L{1} = []; % a single node, no edges
return
elseif T >= 2
L{1} = [2];
L{2} = [1]; % start with a single edge
end
for t = 3:T
L{t} = []; % new node arrives
w = randi(t - 1); % pick a node from 1->t-1 uniformly at random
queue = [w];
visited = [];
while not(isempty(queue))
L{t} = [L{t} w]; % connect t to w
w = queue(1);
visited = [visited w];
outlinks = L{w};
inlinks = [];
for ll = 1:length(L)
if sum(find(L{ll} == w)) > 0
inlinks = [inlinks ll];
end
end
N = length(unique([outlinks inlinks]));
x = geornd(1 - p, 1, 1); % mean p/(1-p) => (1-p) probability
y = geornd(1 - r * p, 1, 1); % mean rp/(1-rp) => (1-rp) probability
s_in = weightedRandomSample(y, inlinks, ones(size(inlinks)) / length(inlinks));
s_out = weightedRandomSample(x, outlinks, ones(size(outlinks)) / length(outlinks));
ws = unique([s_in s_out]);
L{t} = unique([L{t} ws]); % add as outlinks to new node
for ii = 1:length(ws)
if sum(find(visited == ws(ii))) == 0
queue = [queue ws(ii)];
end
end
queue = queue(2:length(queue)); % remove w from queue
end
end
for ll = 1:length(L);
L{ll} = setdiff(L{ll}, ll);
end % remove self-loops
%!test
%! pkg load statistics
%! for x=1:20
%! randint = randi(20)+5;
%! L = forestFireModel(randint,rand,10*rand);
%! adj = symmetrize(adjL2adj(L));
%! assert(isSimple(adj),true)
%! assert(randint,numNodes(L))
%! end
%!
%!demo
%! L = forestFireModel(100, 0.2, 4);
%! adj = adjL2adj(L);
%! adj = symmetrize(adj);
%! numNodes(adj)
%! isSimple(adj)