필터 지우기
필터 지우기

How to create random graph of n vertices with c fixed neighbours in MATLAB?

조회 수: 1 (최근 30일)
Vic
Vic 2012년 8월 8일
I have written a code in MATLAB that allows me to generate a random graph of n vertices, each with c fixed neighbours without self connections (note the edges are directed, thus a connected to b does not imply b connected to a). However, it is terribly inefficient, especially when I need to it work on magnitudes such as n = 10000 and c = 1000. I was wondering if anyone could optimize it big time, or suggest anything constructive?
Thanks!
function [M]=matsrand(n,c)
MM=0; %arbitrary starting value
while MM ~=n*c
M = sparse(zeros(n));
ctin = zeros(1,n);
for i=1:n
rp = randperm(n); %generate vector of the randomly permuted order of n vertices
rp(rp==i)=[]; %get rid of itself to avoid self connection
noconnect=find(ctin(:)>=c); %generate list that i is not allowed to connect to
where=ismember(rp,noconnect); %returns 1 to the subset noconnect in rp
noconnectind=find(where);
rp(noconnectind(:))=[]; %remove the neurons i is not allowed to connect to
if length(rp)<c
break
else
r=rp(1:c);
end
M(i,r)=1;
ctin(r)=ctin(r)+1;
end
MM=sum(ctin);
end
  댓글 수: 3
Vic
Vic 2012년 8월 8일
편집: Vic 2012년 8월 8일
n=c is impossible for the vertices cannot self connect (that was what I meant by 'without loops' - sorry for being imprecise). Anything with c<n would work. A quick example would be n=6,c=2 and the output may be:
0 1 0 0 1 0
0 0 1 0 0 1
0 1 0 0 1 0
1 0 0 0 0 1
0 0 1 1 0 0
1 0 0 1 0 0
Matt Fig
Matt Fig 2012년 8월 8일
Thanks! I see I misread your question. I thought you said that it was slow with n=c=1000, but now I see my mistake.

댓글을 달려면 로그인하십시오.

답변 (1개)

Vic
Vic 2012년 8월 8일
However, even that takes a while to find n=1000, c=100...

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by