How can I connect 25 nodes with 50 connections?

3 views (last 30 days)
I have 25 nodes that need 50 (random) connections. Below my code, any suggestions?
figure;
load('usborder.mat','x','y','xx','yy');
rng(6,'twister')
nStops = 25;
stopsLon = zeros(nStops,1);
stopsLat = stopsLon;
n = 1;
while (n <= nStops)
xp = rand*1.5;
yp = rand;
if inpolygon(xp,yp,x,y)
stopsLon(n) = xp;
stopsLat(n) = yp;
n = n+1;
end
end
plot(x,y,'k--');
hold on
plot(stopsLon,stopsLat,'xr')
hold off

Accepted Answer

Elizabeth Reese
Elizabeth Reese on 10 Aug 2017
There are a couple of ways to do this. One way is to use randi to generate two integers between 1 and 25. They will act as indexes into your list of nodes. You can then use these numbers to index into your stopsLon and stopsLat and connect the nodes at those two indexes. If you are concerned about self-loops or having only unique paths, then you can create a 25x25 connectivity matrix or similar structure to keep track of which connections you have already made.

More Answers (0)

Categories

Find more on Optimization in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!