复杂网络聚类系数和平均路径长度计算的MATLAB源代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

复杂网络聚类系数和平均路径长度计算的MATLAB源代码申明:文章来自百度用户carrot_hy复杂网络的代码总共是三个m文件,复制如下:第一个文件,CCM_ClusteringCoef.mfunction[Cp_Global,Cp_Nodal]=CCM_ClusteringCoef(gMatrix,Types)%CCM_ClusteringCoefcalculatesclusteringcoefficients.%Input:%gMatrixadjacencymatrix%Typestypeofgraph:'binary','weighted','directed','all'(default).%Usage:%[Cp_Global,Cp_Nodal]=CCM_ClusteringCoef(gMatrix,Types)returns%clusteringcoefficientsforallnodesCp_Nodalandaverageclustering%coefficientofnetworkCp_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)Thearchitectureofthecomplexweightednetworks.%[2]Wasserman,S.,Faust,K.(1994)SocialNetworkAnalysis:Methodsand%Applications.%[3]ToreOpsahlandPietroPanzarasa(2009).ClusteringinWeighted%Networks.SocialNetworks31(2).%SeealsoCCM_Transitivity%WrittenbyYongLiu,Oct,2007%CenterforComputationalMedicine(CCM),%NationalLaboratoryofPatternRecognition(NLPR),%InstituteofAutomation,ChineseAcademyofSciences(IACAS),China.%RevisebyHuYong,Nov,2010%E-mail:%basedonMatlab2006a%$Revision:1.0,Copywrite(c)2007error(nargchk(1,2,nargin,'struct'));if(nargin2),Types='all';endN=length(gMatrix);gMatrix(1:(N+1):end)=0;%Clearself-edgesCp_Nodal=zeros(N,1);%Preallocateswitch(upper(Types))case'BINARY'%BinarynetworkgMatrix=double(gMatrix0);%Ensurebinarynetworkfori=1:Nneighbor=(gMatrix(i,:)0);Num=sum(neighbor);%numberofneighbornodestemp=gMatrix(neighbor,neighbor);if(Num1),Cp_Nodal(i)=sum(temp(:))/Num/(Num-1);endendcase'WEIGHTED'%Weightednetwork--arithmeticmeanfori=1:Nneighbor=(gMatrix(i,:)0);n_weight=gMatrix(i,neighbor);Si=sum(n_weight);Num=sum(neighbor);if(Num1),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));endend%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',%Directednetworkfori=1:Ninset=(gMatrix(:,i)0);%in-nodessetoutset=(gMatrix(i,:)0)';%out-nodessetif(any(inset&outset))allset=and(inset,outset);%Ensureaji*aik0,jbelongstoinset,andkbelongstooutsettotal=sum(inset)*sum(outset)-sum(allset);tri=sum(sum(gMatrix(inset,outset)));Cp_Nodal(i)=tri./total;endend%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',%AlltypeA=(gMatrix~=0);%adjacencymatrixG=gMatrix.^(1/3)+(gMatrix.').^(1/3);D=sum(A+A.',2);%totaldegreeg3=diag(G^3)/2;%numberoftripletD(g3==0)=inf;%3-cyclesnoexist,letCp=0c3=D.*(D-1)-2*diag(A^2);Cp_Nodal=g3./c3;otherwise,%EorrMsgerror('Typeonlyfour:Binary,Weighted,Directed,andAll');endCp_Global=sum(Cp_Nodal)/N;%%第二个文件:CCM_AvgShortestPath.mfunction[D_Global,D_Nodal]=CCM_AvgShortestPath(gMatrix,s,t)%CCM_AvgShortestPathgeneratestheshortestdistancematrixofsourcenodes%indicestothetargetnodesindicet.%Input:%gMatrixsymmetrybinaryconnectmatrixorweightedconnectmatrix%ssourcenodes,defaultis1:N%ttargetnodes,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%CenterforComputationalMedicine(CCM),%BasedonMatlab2008a%$Revision:1.0,Copywrite(c)2007%######Inputcheck#########error(nargchk(1,3,nargin,'struct'));N=length(gMatrix);if(nargin2|isempty(s)),s=(1:N)';elses=s(:);endif(nargin3|isempty(t)),t=(1:N)';elset=t(:);end%Calculatetheshortest-pathfromstoallnodeD=dijk(gMatrix,s);%D(isinf(D))=0;D=D(:,t);%TotargetnodesD_Nodal=(sum(D,2)./sum(D0,2));%D_Nodal(isnan(D_Nodal))=[];D_Global=mean(D_Nodal);第三个文件:dijk.mfunctionD=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,thencomputationallyintensivenode%selectionstepnotneededsincegraphisacyclic(triangularityisa%sufficient,butnotanecessary,conditionforagraphtobeacyclic)%andAcanhavenon-negativeelements)%%(If|s||t|,thenDIJKisfasterifDIJK(A',t,s)used,whereDisnow%transposedan

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功