已经根据距离值选择了簇头。但是,将节点连接到群集头存在一个问题。我想从4-9-1-3-5-CH创建一条路径,另一条路线是8-9-1-3-5-CH,10-2-CH和7-CH。
if true
%%%%%%%%%%%%%%%
clc;
clear all;
N=10; % No of nodes
data = rand(N,2); % Randomly generated n no. of nodes
x = data(:,1)*100;
y = data(:,2)*100;
numEdge = 19;
plot(x,y,'rx');
node=struct('id',{},'x',{},'y',{},'dist',{},'link',{'1'});
for i=1:N
node(i).id=i;
node(i).x=x(i);
node(i).y=y(i);
text(x(i),y(i),num2str(i));
end
%%%%%%%%%%%%%%%%%%
% CH
ch1=ceil(rand(1)*N);
text(node(ch1).x,node(ch1).y,['CH'],'Color' ,'b');
for i = 1:N
node(i).dist = sqrt(((node(ch1).x - node(i).x)^2 + (node(ch1).y - node(i).y)^2));
a(i)=node(i).dist;
disp(node(i).dist);
end
A=sort(a,'descend');
a=1;
for i = 1:N
node_distance(a) = node(i).dist;
a = a+1;
end
Maximum_distance = max(node_distance);
for i = 1:N
if node(i).dist == max(node_distance)
next_header = node(i).id;
end
end
end
结果:
可接受的结果:
如何在节点和群集头之间建立连接?对我有什么作用或线索吗?