课程首页 | 课程简介 | 课程大纲 | 课程讲义 | 授课教师 | 网络可视化 | 课程建设动态 | 课程资讯 | 线上课程 | 头条推荐 
   
 

代码参考 | 聚类系数(Matlab)
2022-01-15 19:40  

function [cc,avcc]=clusteringcoeff(A);

% Calculates the CC of each node by the number of links

% between nodes in the 1-neighbourhood divided by the number of possible links.

% Usage: cc=clusteringcoeff(A)

%considering in-degree & out-degree of each node & the number of links between them

%self-connections do not affect anything! (like Pajek)


%对于我的.保持度值的随机重连,NW的模型而言,计算聚类系数没有问题!

%但是对于存在度值为1或者0的网络需要改进.


% Successful: C=(3/4)[(Num_neighbour-2*d)/(Num_neighbour-1*d)],

% Where "d" is the dimision of the Lattice. Modified By: liujie@jerry.cn


N=length(A);


%only prealloc things that grow in for loop

cc=zeros(1,N);


for i=1:N

   neighbours=setdiff(unique([find(A(i,:)) find(A(:,i))']),i);

   %neighbours=setdiff(find(A(i,:)),i);

   num_neighbours=length(neighbours); %deg(i) does not equal num_neighbours when uniqueness gets rid of some

   

   if num_neighbours > 1

       B=A(neighbours,neighbours);

       num_connect=nnz(B)-nnz(diag(B));

       cc(i)=num_connect/(num_neighbours*(num_neighbours-1)); %get rid of i when working to make faster (do running average)

   else

       cc(i)=0; %could initialise to zero then don't need 'else'

   end

   

end


avcc=mean(cc);



%%%%%%%%%%%%%

function C_net_avcc=clusteringcoeff_all(A);

% Modified by LIUJIE! Successful compared with

% "ki=degree(m,12),C_networks=coefficient(m,12,ki)"

%function C_networks=coefficient(a,N,ki)    %  By LIU JIE 20040929 correct!!!

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%for i=1:N

   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   %if (ki(i)==0)

       %c(i)=0;

       %continue;

       %end

   %if (ki(i)==1)

       %c(i)=1;

       %continue;

       %end

   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   % record the neighbors index j of node i in vector neighbors ;

  %[NbX,NbY]=find(a(i,:)==1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   %e(i)=0;

   %for j=1:length(NbY)

        %if j+1<=length(NbY)

           %if (a(NbY(j),NbY(j+1))==1)   %successful!!!!

               %e(i)=e(i)+1;

               %end

           %5end

        %end

   %c(i)=2.0*e(i)/(ki(i)*(ki(i)-1));

   %end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%C_networks=0;

%for i=1:N

   %C_networks=C_networks+c(i);

   %end

%C_networks=C_networks/N;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%Calculates the CC of each node by the number of links

%between nodes in the 1-neighbourhood divided by the number of possible links.

%Usage: cc=clusteringcoeff(A)


%considering in-degree & out-degree of each node & the number of links between them

%self-connections do not affect anything! (like Pajek)


N=length(A);

D_k=sum(A,2);

%only prealloc things that grow in for loop

cc=zeros(1,N);


for i=1:N

   neighbours=setdiff(unique([find(A(i,:)) find(A(:,i))']),i);

   %neighbours=setdiff(find(A(i,:)),i);

   num_neighbours=length(neighbours); %deg(i) does not equal num_neighbours when uniqueness gets rid of some

   

   if num_neighbours > 1

       B=A(neighbours,neighbours);

       num_connect=nnz(B)-nnz(diag(B));

       cc(i)=num_connect/(num_neighbours*(num_neighbours-1)); %get rid of i when working to make faster (do running average)

   else

       cc(i)=0; %could initialise to zero then don't need 'else'

   end

   

end

C_one=length(find(D_k==1));

C_net_avcc=(C_one+sum(cc))/N;


Close Window
  读取内容中,请等待...

版权所有:Research Centre of Nonlinear Science 邮政编码:430073
E-mail:liujie@wtu.edu.cn 备案序号:鄂ICP备15000386号 鄂公网安备 42011102000704号