How to generate random graph of n vertices with random connections in matlab

조회 수: 38 (최근 30일)
SIVAKUMAR V
SIVAKUMAR V 2016년 7월 29일
댓글: Walter Roberson 2021년 5월 8일
Hi I am interested in creating random undirected graph with n vertices and with random connections. This generated graph has to display its adjacency connections in a nxn matrix.I have tried this code but it is not working for my requirement.
n=input('No. of vertices')
c=input('Random connections')
MM=0; %arbitrary starting value
all_nums=1:n;
while MM ~=n*c
M = sparse([],[],[],n,n,n*c);
ctin = zeros(1,n);
for ii=1:n
noconnect=ctin>=c;
noconnect(ii)=true;
rem_nums = all_nums(~noconnect); % remaining numbers
rp=randperm(n-sum(noconnect));
rp = rem_nums(rp); % remaining numbers, hussled
if numel(rp)<c
break
else
r=rp(1:c);
end
M(ii,r)=1;
ctin(r)=ctin(r)+1;
end
MM=sum(ctin);
end
end
A= adjacency(M)

답변 (3개)

Luis De Medeiros
Luis De Medeiros 2016년 7월 29일
편집: Luis De Medeiros 2016년 7월 29일
You can create an adjacency matrix with random 1's and 0's by doing the following:
G = round(rand(n));
G = triu(G) + triu(G,1)';
G = G - diag(diag(G));
  댓글 수: 5
Walter Roberson
Walter Roberson 2016년 7월 31일
Please expand on what you mean by "E number links". Do you mean a total of E edges across the entire graph? Would a valid random graph be one in which all E edges were connected to the first vertex and the other vertices were either not connected or were connected to the first vertex?
SIVAKUMAR V
SIVAKUMAR V 2016년 8월 1일
Yes E represents number of links across the graph. My expectation is need to generate a sensor network graph in matlab.

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


Walter Roberson
Walter Roberson 2016년 8월 1일
adj = spalloc(n, n, E);
idx = randperm(n * n, E);
adj(idx) = 1;
adj = min( adj + adj.', 1);
  댓글 수: 12
idris cinemre
idris cinemre 2021년 5월 8일
Is it possible to generate random RAN graph like attached fig. red dots indicate the RUs’ locations; black dots the routers/switches, green dot the CU location. RUs are only connected to a router, routers can be connected to each other and CU.
Walter Roberson
Walter Roberson 2021년 5월 8일
First generate an appropriate connected graph for the Routers and switches only.
Then generate a bunch of other points, and calculate their nearest neighbour among the routers, and connect them to that neighbour.

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


Steven Lord
Steven Lord 2021년 5월 7일
Take a look at the sprand and sprandsym functions.

카테고리

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