Issue with generating small-world network - how to treat a single node that is left with no nodes to connect to?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am trying to generate a variation of small-world network.
I would like to specify number of nodes, number of local connections (if number of local connections are set to 2, then a node will be connected to two closest neighbors on the left and two closest neighbors on the right; For example, node 1 will be connected to nodes 2 and 3 as well as nodes 99 and 100), and number of long range connections (each node will connect to set number of nodes that are not local neighbors).
The code that I have below will generate the network that I intended to generate most of the time. However, on some occasions, I will have a single node left with no nodes to connect - meaning, all the other nodes have satisfied their local and long range connections but the problem node. When this problem happens, the randperm command will generate the following error:
"Error using randperm K must be less than or equal to N."
The reason for the error is because the problem node has long range connections to make so the second input for randperm is positive, but since there are no nodes available to connect, first input for randperm is zero.
I am not sure how to better explain this, but an expert help would be greatly appreciated!
If you have better ideas for coding this network generation, please feel free to suggest, too.
============================================================================
N=100; %Number of nodes
S=2; %Local connections to 2*S neighbors. S connections to one side and another S connections to the other side
L=2; %Long range connections to L neighbors.
graph=sparse(zeros(N,N));
for i=1:N
for j=1:S
graph(i,mod(i+j-1,N)+1)=1;
graph(mod(i+j-1,N)+1,i)=1;
end;
end;
%above for loop accomplishes the short range connections.
graph(1:N+1:N^2)=1;
for i=1:N
indices=find(graph(i,:)==0);
full=find(sum(graph)==2*S+1+L);%nodes that have fulfilled all local and long range connections
indices=setdiff(indices,full);%vector of available nodes for long range connection
R=2*S+1+L-sum(graph(i,:));%Compute the remaining long range connections to make for node i
edge=randperm(length(indices),R);
for j=1:R
graph(i,indices(edge(j)))=1;
graph(indices(edge(j)),i)=1;
end
end
graph(1:N+1:N^2)=0;
1 commentaire
Réponses (1)
Nhl
le 9 Déc 2013
I have codes you write above but I didn't work these in matlab.I wrote another code: %small-world network clc;clear; N=50;%nodes k=4;%neighbor p=0.5;%probability of coupling neurons r=zeros(1,N);r(2:k+1)=1;r(N-k+1:N)=1; A=toeplitz(r) subplot(2,2,1), spy(A) subplot(2,2,2) v=find(rand(N,1)< p); Ashort=sparse(v,ceil(N*rand(size(v))),ones(size(v)),N,N) spy(A+Ashort+Ashort') h=waitbar(0,'computing averge pathlengths')
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!