📄 num_neighbors.m
字号:
%The Number of Neighbors Needed for Connectivity of Wireless Networks%
%********Report research simulation***********%
%************simulation of K-connectivity*************************
%********1000*1000m^2,n=500 nodes ,radio range r0,minimum node degree dmin*
%Date 2006,6,13--By Liu FengWei
%function
c=[1 1.1 1.2 1.5];% on the paper
L_c=length(c);%length of c
Nodelimit=600;%max node
Nodenum=[0:10:Nodelimit]; %number of nodes
%c=1;%in paper
Len_nodenum=length(Nodenum); %length of nodenum
P_connect=zeros(Len_nodenum,L_c); %record connectivity of every number of neighbor
%Ndeg=zeros(Nodenum,L); %record degree of every node
%Generate links%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%if the distance between two nodes smaller than r0, link is created%%%%%%
for n=2:Len_nodenum
n
C_connect=zeros((Nodenum(n)-1),Nodenum(n));%record whicn nodes each node connect
NodeXY=1000*rand(Nodenum(n),2);%coordinates of node
for c_index=1:L_c
Neighbor=ceil(c(c_index)*log(Nodenum(n)));%calculate number of neighbors
t_Links=zeros((Nodenum(n)-1),Nodenum(n));%temporarily links
Links=zeros(Neighbor,Nodenum(n));%network topology
for i=1:Nodenum(n)
L_index=1;
for j=1:Nodenum(n)
if(i~=j)
t_Links(L_index,i)=sqrt((NodeXY(i,1)-NodeXY(j,1))^2+(NodeXY(i,2)-NodeXY(j,2))^2) ; %Euclidian distance
L_index=L_index+1;
end
end
end
[t_Links index]=sort(t_Links);
Links=index(1:Neighbor,:);%choose c*log(Nodenum(n)) Neighbors
%%%%%%%%%%%%%%Calculate connectivity %%%%%%%%%%%%%%%
%temp=zeros(1,(Nodenum(n)-1)); %just for temp---no extra usness
for i=1:Nodenum(n) %From last node--for its simplicity
%%%%record the node connecting node i%%%%%%%%%
L_Neighbor=Neighbor;
C_connect(1:L_Neighbor,i)=Links(:,i);
k=1;
%%%find whicn nodes node i connect%%%%%%%%%%%%%%
while(C_connect(k,i))
%CN_index=Links(:,k); %Connecting node of index(k)
temp=Links(:,k); %temporarily record the links of node k
%%%find the overlap nodes in temp and delete them%%%%%
if(any(temp))
for d=1:L_Neighbor
D_index=find(temp==C_connect(d,i)); %find delete index of the dth node
temp(D_index)=[]; %delete overlap node
end
L_temp=sum(temp~=0); %the number of nodes in temp
%disp(L_Link)
if(L_temp)
C_start= L_Neighbor;
L_Neighbor=L_Neighbor+L_temp; %end length
C_connect((C_start+1):L_Neighbor,i)=temp(1:L_temp);
end
end
k=k+1;
if(k>=Nodenum(n))%a node can't connect more than Nodenum(n) nodes
break
end
end
end
sumlinks=Nodenum(n)*(Nodenum(n)-1)/2;
P_connect(n,c_index)=sum(sum(C_connect>0))/(2*sumlinks);%record the connectivity of each node connecting to clogn nodes
end
end
%%%%%%%%%%%%Calculate the prob of k connectivity%%%%%%
%for i=1:L
% s=sum(sum(Nconnect(:,:,i)>0));
; %prob P(dmin>=N0)
%Pdmin=min(Ndeg)/sum(:,i);
%end
%%%%%%%%%%%%%%%%%%%%%draw figure%%%%%%%%%%%%%%%%%%%%%%%
figure(1)
plot(Nodenum,P_connect(:,1),'ro-',Nodenum,P_connect(:,2),'b+-',Nodenum,P_connect(:,3),'g*-',Nodenum,P_connect(:,4),'kx-')
text(Nodenum(3),P_connect(3,1),'\leftarrow c=1.0');
text(Nodenum(2),P_connect(2,2),'\leftarrow c=1.1');
text(Nodenum(10),P_connect(10,3),'\leftarrow c=1.2');
text(Nodenum(15),P_connect(15,4),'\leftarrow c=1.5');
grid on;
axis([0 Nodelimit 0.75 1])
xlabel('Pr(connected) when each node is connected to clog(n) nearest neighbors','color','b')
ylabel('prob for Connectivity','color','b')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -