필터 지우기
필터 지우기

How to find if graph is connected

조회 수: 2 (최근 30일)
min562
min562 2017년 6월 25일
댓글: min562 2017년 6월 26일
I'm trying to create Geometric random graph and here is my code I wrote that is not done yet.
% code
n = input('Enter number of nodes: ');
x=[];
y=[];
d=[];
x = rand(1,n);
y = rand(1,n);
for i = 1 : (n)
for j = 1 : (n)
d(i, j) = sqrt((x(i) - x(j)) ^ 2 + (y(i) - y(j)) ^ 2);
//finding distance of each nodes
end
end
R=0.3; //radius
edge = d <= R ;
I'm trying to find if the nodes are connected or not but I don't have any idea what I can use to find the answer. Please help.

채택된 답변

Joshua
Joshua 2017년 6월 26일
I think this is what you are looking for. The line with the variable 'con' is not really necessary, but if you need to reference the connections later it would be convenient.
R=0.3; %%radius
n = input('Enter number of nodes: ');
d=zeros(n,n);
con=zeros(n,n); % matrix of zeros indicates none are connected
x = rand(1,n);
y = rand(1,n);
plot(x,y,'o');
hold on
for i = 1 : (n)
for j = 1 : (n)
d(i, j) = sqrt((x(i) - x(j)) ^ 2 + (y(i) - y(j)) ^ 2);
if (d(i,j)<R)
con(i,j)=1; % sets to 1 to indicate connection between node i and j
hold on
line([x(i), x(j)],[y(i), y(j)]);
end
end
end

추가 답변 (0개)

카테고리

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