-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWattsStrogatz.m
33 lines (28 loc) · 1.13 KB
/
WattsStrogatz.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
function [h,L] = WattsStrogatz(N,K,beta)
% H,L = WattsStrogatz(N,K,beta) returns a Watts-Strogatz model graph with N
% nodes, N*K edges, mean node degree 2*K, and rewiring probability beta. L
% is the graph laplacian for the given matrix.
%
% beta = 0 is a ring lattice, and beta = 1 is a random graph.
%
%This script comes from a demo page on MATLAB's documentation page on the
%Watts Strogratz model. Link the day it was downloaded is provided below.
% https://www.mathworks.com/help/matlab/examples/build-watts-strogatz-small-world-graph-model.html
% Connect each node to its K next and previous neighbors. This constructs
% indices for a ring lattice.
s = repelem((1:N)',1,K);
t = s + repmat(1:K,N,1);
t = mod(t-1,N)+1;
% Rewire the target node of each edge with probability beta
for source=1:N
switchEdge = rand(K, 1) < beta;
newTargets = rand(N, 1);
newTargets(source) = 0;
newTargets(s(t==source)) = 0;
newTargets(t(source, ~switchEdge)) = 0;
[~, ind] = sort(newTargets, 'descend');
t(source, switchEdge) = ind(1:nnz(switchEdge));
end
h = graph(s,t);
L = laplacian(h);
end