复杂网络聚类系数和平均路径长度计算的MATLAB源代码.docx
《复杂网络聚类系数和平均路径长度计算的MATLAB源代码.docx》由会员分享,可在线阅读,更多相关《复杂网络聚类系数和平均路径长度计算的MATLAB源代码.docx(6页珍藏版)》请在冰豆网上搜索。
复杂网络聚类系数和平均路径长度计算的MATLAB源代码
复杂网络聚类系数和平均路径长度计算的MATLAB源代码
申明:
文章来自XX用户carrot_hy
复杂网络的代码总共是三个m文件,复制如下:
第一个文件,CCM_ClusteringCoef.m
function[Cp_Global,Cp_Nodal]=CCM_ClusteringCoef(gMatrix,Types)
%CCM_ClusteringCoefcalculatesclusteringcoefficients.
%Input:
% gMatrix adjacencymatrix
% Types typeofgraph:
'binary','weighted','directed','all'(default).
%Usage:
% [Cp_Global,Cp_Nodal]=CCM_ClusteringCoef(gMatrix,Types)returns
% clusteringcoefficientsforallnodes"Cp_Nodal"andaverageclustering
% coefficientofnetwork"Cp_Global".
%Example:
% G=CCM_TestGraph1('nograph');
% [Cp_Global,Cp_Nodal]=CCM_ClusteringCoef(G);
%Note:
% 1)onenodehavevaule0,whilewhichonlyhasaneighbourornone.
% 2)Thedirctednetworktermedtripletsthatfulfillthefollowcondition
% asnon-vacuous:
j->i->kandk->i-j,ifdon'tsatisfywiththatas
% vacuous,justlike:
j->i,k->iandi->j,i->k.andtheclosedtriplets
% onlyj->i->k==j->kandk->i->j==k->j.
% 3)'ALL'typenetworkcodefromMikaRubinov'sBCTtoolkit.
%Refer:
% [1]Barratetal.(2004)Thearchitectureoftheplexweightednetworks.
% [2]Wasserman,S.,Faust,K.(1994)SocialNetworkAnalysis:
Methodsand
% Applications.
% [3]ToreOpsahlandPietroPanzarasa(2009)."ClusteringinWeighted
% Networks".SocialNetworks31
(2).
%SeealsoCCM_Transitivity
%WrittenbyYongLiu,Oct,2007
%CenterforputationalMedicine(CCM),
%NationalLaboratoryofPatternRecognition(NLPR),
%InstituteofAutomation,ChineseAcademyofSciences(IACAS),China.
%RevisebyHuYong,Nov,2010
%:
%basedonMatlab2006a
%$Revision:
1.0,Copywrite(c)2007
error(nargchk(1,2,nargin,'struct'));
if(nargin<2), Types='all'; end
N=length(gMatrix);
gMatrix(1:
(N+1):
end)=0;%Clearself-edges
Cp_Nodal=zeros(N,1); %Preallocate
switch(upper(Types))
case'BINARY'%Binarynetwork
gMatrix=double(gMatrix>0);%Ensurebinarynetwork
fori=1:
N
neighbor=(gMatrix(i,:
)>0);
Num =sum(neighbor);%numberofneighbornodes
temp =gMatrix(neighbor,neighbor);
if(Num>1), Cp_Nodal(i)=sum(temp(:
))/Num/(Num-1); end
end
case'WEIGHTED'%Weightednetwork--arithmeticmean
fori=1:
N
neighbor=(gMatrix(i,:
)>0);
n_weight=gMatrix(i,neighbor);
Si =sum(n_weight);
Num =sum(neighbor);
if(Num>1),
n_weight =ones(Num,1)*n_weight;
n_weight =n_weight+n_weight';
n_weight =n_weight.*(gMatrix(neighbor,neighbor)>0);
Cp_Nodal(i)=sum(n_weight(:
))/(2*Si*(Num-1));
end
end
%case'WEIGHTED'%Weightednetwork--geometricmean
% A =(gMatrix~=0);
% G3=diag((gMatrix.^(1/3))^3);)
% A(A==0)=inf; %close-tripletnoexist,letCpNode=0(A=inf)
% CpNode=G3./(A.*(A-1));
case'DIRECTED',%Directednetwork
fori=1:
N
inset =(gMatrix(:
i)>0); %in-nodesset
outset =(gMatrix(i,:
)>0)';%out-nodesset
if(any(inset&outset))
allset=and(inset,outset);
%Ensureaji*aik>0,jbelongstoinset,andkbelongstooutset
total =sum(inset)*sum(outset)-sum(allset);
tri =sum(sum(gMatrix(inset,outset)));
Cp_Nodal(i)=tri./total;
end
end
%case'DIRECTED',%Directednetwork--clarityformat(fromMikaRubinov,UNSW)
% G =gMatrix+gMatrix'; %symmetrized
% D =sum(G,2); %totaldegree
% g3=diag(G^3)/2; %numberoftriplet
% D(g3==0)=inf; %3-cyclesnoexist,letCp=0
% c3=D.*(D-1)-2*diag(gMatrix^2);%numberofallpossible3-cycles
% Cp_Nodal =g3./c3;
%Note:
Directed&weightednetwork(fromMikaRubinov)
case'ALL',%Alltype
A =(gMatrix~=0); %adjacencymatrix
G =gMatrix.^(1/3)+(gMatrix.').^(1/3);
D =sum(A+A.',2); %totaldegree
g3=diag(G^3)/2; %numberoftriplet
D(g3==0)=inf; %3-cyclesnoexist,letCp=0
c3=D.*(D-1)-2*diag(A^2);
Cp_Nodal =g3./c3;
otherwise,%EorrMsg
error('Typeonlyfour:
"Binary","Weighted","Directed",and"All"');
end
Cp_Global=sum(Cp_Nodal)/N;
%%
第二个文件:
CCM_AvgShortestPath.m
function[D_Global,D_Nodal]=CCM_AvgShortestPath(gMatrix,s,t)
%CCM_AvgShortestPathgeneratestheshortestdistancematrixofsourcenodes
%indicestothetargetnodesindicet.
%Input:
% gMatrix symmetrybinaryconnectmatrixorweightedconnectmatrix
% s sourcenodes,defaultis1:
N
% t targetnodes,defaultis1:
N
%Usage:
% [D_Global,D_Nodal]=CCM_AvgShortestPath(gMatrix)returnsthemean
% shortest-pathlengthofwholenetworkD_Global,andthemeanshortest-path
% lengthofeachnodeinthenetwork
%Example:
% G=CCM_TestGraph1('nograph');
% [D_Global,D_Nodal]=CCM_AvgShortestPath(G);
%Seealsodijk,MEAN,SUM
%WrittenbyYongLiu,Oct,2007
%ModifiedbyHuYong,Nov2010
%CenterforputationalMedicine(CCM),
%BasedonMatlab2008a
%$Revision:
1.0,Copywrite(c)2007
%######Inputcheck#########
error(nargchk(1,3,nargin,'struct'));
N=length(gMatrix);
if(nargin<2|isempty(s)), s=(1:
N)';
else s=s(:
); end
if(nargin<3|isempty(t)), t=(1:
N)';
else t=t(:
); end
%Calculatetheshortest-pathfromstoallnode
D=dijk(gMatrix,s);%D(isinf(D))=0;
D=D(:
t); %Totargetnodes
D_Nodal =(sum(D,2)./sum(D>0,2));
%D_Nodal(isnan(D_Nodal))=[];
D_Global=mean(D_Nodal);
第三个文件:
dijk.m
functionD=dijk(A,s,t)
%DIJKShortestpathsfromnodes's'tonodes't'usingDijkstraalgorithm.
%D=dijk(A,s,t)
% A=nxnnode-nodeweightedadjacencymatrixofarclengths
% (Note:
A(i,j)=0 =>Arc(i,j)doesnotexist;
% A(i,j)=NaN=>Arc(i,j)existswith0weight)
% s=FROMnodeindices
% =[](default),pathsfromallnodes
% t=TOnodeindices
% =[](default),pathstoallnodes
% D=|s|x|t|matrixofshortestpathdistancesfrom's'to't'
% =[D(i,j)],whereD(i,j)=distancefromnode'i'tonode'j'
%
% (IfAisatriangularmatrix,thenputationallyintensivenode
% selectionstepnotneededsincegraphisacyclic(triangularityisa
% sufficient,butnotanecessary,conditionforagraphtobeacyclic)
% andAcanhavenon-negativeelements)
%
% (If|s|>>|t|,thenDIJKisfasterifDIJK(A',t,s)used,whereDisnow
% transposedandPnowrepresentssuccessorindices)
%
% (BasedonFig.4.6inAhuja,Magnanti,andOrlin,NetworkFlows,
% Prentice-Hall,1993,p.109.)
%Copyright(c)1998-2000byMichaelG.Kay
%MatlogVersion1.329-Aug-2000
%
% ModifiedbyT,Dec2000,todeletepaths
%InputErrorChecking******************************************************
error(nargchk(1,3,nargin,'struct'));
[n,cA]=size(A);
ifnargin<2|isempty(s),s=(1:
n)';elses=s(:
);end
ifnargin<3|isempty(t),t=(1:
n)';elset=t(:
);end
if~any(any(tril(A)~=0)) %Aisuppertriangular
isAcyclic=1;
elseif~any(any(triu(A)~=0)) %Aislowertriangular
isAcyclic=2;
else %Graphmaynotbeacyclic
isAcyclic=0;
end
ifn~=cA
error('Amustbeasquarematrix');
elseif~isAcyclic&any(any(A<0))
error('Amustbenon-negative');
elseifany(s<1|s>n)
error(['''s''mustbeanintegerbetween1and',num2str(n)]);
elseifany(t<1|t>n)
error(['''t''mustbeanintegerbetween1and',num2str(n)]);
end
%End(InputErrorChecking)************************************************
A=A'; %Usetransposetospeed-upFINDforsparseA
D=zeros(length(s),length(t));
P=zeros(length(s),n);
fori=1:
length(s)
j=s(i);
Di=Inf*ones(n,1);Di(j)=0;
isLab=logical(zeros(length(t),1));
ifisAcyclic== 1
nLab=j-1;
elseifisAcyclic==2
nLab=n-j;
else
nLab=0;
UnLab=1:
n;
isUnLab=logical(ones(n,1));
end
whilenLab ifisAcyclic
Dj=Di(j);
else %Nodeselection
[Dj,jj]=min(Di(isUnLab));
j=UnLab(jj);
UnLab(jj)=[];
isUnLab(j)=0;
end
nLab=nLab+1;
iflength(t) [jA,kA,Aj]=find(A(:
j));
Aj(isnan(Aj))=0;
ifisempty(Aj),Dk=Inf;elseDk=Dj+Aj;end
P(i,jA(Dk Di(jA)=min(Di(jA),Dk);
ifisAcyclic==1 %IncrementnodeindexforuppertriangularA
j=j+1;
elseifisAcyclic==2 %DecrementnodeindexforlowertriangularA
j=j-1;
end
%disp(num2str(nLab));
end
D(i,:
)=Di(t)';
end