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;