How can I expedite the speed of this code?

2 views (last 30 days)
Rayan Glus
Rayan Glus on 7 Oct 2021
Edited: Rayan Glus on 8 Oct 2021
Hello,
I found a nice code for generating Scale Free networks (Barabasi Algorithm) on File Exchange but it's slow specially when I try to generate more than 10000 nodes. I would appreciate if someone can tell me what is the most expensive piece of line here and how can I expedite the simulation run speed. Also, I wonder if there is a built in function in MATLAB for generating Scale Free Networks.
Thanks all.
function SFNet = SFNG(Nodes, mlinks, seed)
seed = full(seed);
pos = length(seed);
%if (Nodes < pos) || (mlinks > pos) || (pos < 1) || (size(size(seed)) ~= 2) || (mlinks < 1) || (seed ~= seed') || (sum(diag(seed)) ~= 0)
% error('invalid parameter value(s)');
%end
%if mlinks > 5 || Nodes > 15000 || pos > 15000
% warning('Abnormally large value(s) may cause long processing time');
%end
rand('state',sum(100*clock));
Net = zeros(Nodes, Nodes, 'single');
Net(1:pos,1:pos) = seed;
sumlinks = sum(sum(Net));
while pos < Nodes
pos = pos + 1;
linkage = 0;
while linkage ~= mlinks
rnode = ceil(rand * pos);
deg = sum(Net(:,rnode)) * 2;
rlink = rand * 1;
if rlink < deg / sumlinks && Net(pos,rnode) ~= 1 && Net(rnode,pos) ~= 1
Net(pos,rnode) = 1;
Net(rnode,pos) = 1;
linkage = linkage + 1;
sumlinks = sumlinks + 2;
end
end
end
clear Nodes deg linkage pos rlink rnode sumlinks mlinks
SFNet = Net;

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!