您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 交通运输 > 网络分析(聚类系数、最短路径、效率)matlab代码汇总
functionC=clustering_coef_bd(A)%C=clustering_coef_bd(A);clusteringcoefficientC,forbinarydirectedgraphA%%Reference:Fagiolo,2007,PhysRevE76:026107.%%MikaRubinov,UNSW,2007(lastmodifiedJuly2008)%Indirectedgraphs,3nodesgenerateupto8triangles(2*2*2edges)%ThenumberofexistingtrianglesisthemaindiagonalofS^3/2%Thenumberofall(inorout)neighbourpairsisK(K-1)/2%Eachneighbourpairmaygeneratetwotriangles%Falsepairsarei-jedgepairs(thesedonotgeneratetriangles)%ThenumberoffalsepairsisthemaindiagonalofA^2%Thusthemaximumpossiblenumberoftriangles=%=(2edges)*([ALLPAIRS]-[FALSEPAIRS])=%=2*(K(K-1)/2-diag(A^2))=K(K-1)-2(diag(A^2))S=A+A.';%symmetrizedinputgraphK=sum(S,2);%totaldegree(in+out)cyc3=diag(S^3)/2;%numberof3-cycles(ie.directedtriangles)K(cyc3==0)=inf;%ifno3-cyclesexist,makeC=0(viaK=inf)CYC3=K.*(K-1)-2*diag(A^2);%numberofallpossible3-cyclesC=cyc3./CYC3;%clusteringcoefficientfunctionC=clustering_coef_bu(G)%C=clustering_coef_bu(G);clusteringcoefficientC,forbinaryundirectedgraphG%%Reference:WattsandStrogatz,1998,Nature393:440-442%%MikaRubinov,UNSW,2007(lastmodifiedSeptember2008)n=length(G);C=zeros(n,1);foru=1:nV=find(G(u,:));k=length(V);ifk=2;%degreemustbeatleast2S=G(V,V);C(u)=sum(S(:))/(k^2-k);endendfunctionC=clustering_coef_wd(W)%C=clustering_coef_wd(W);clusteringcoefficientCforweighteddirectedgraphW.%%Reference:Fagiolo,2007,PhysRevE76:026107%(alsoseeOnnelaetal.2005,PhysRevE71:065103);%%MikaRubinov,UNSW,2007(lastmodifiedJuly2008)%Seecommentsforclustering_coef_bd%Theweightedmodificationisasfollows:%-Thenumerator:adjacencymatrixisreplacedwithweightsmatrix^1/3%-Thedenominator:nochangesfromthebinaryversion%%Theabovereducestosymmetricand/orbinaryversionsofthe%clusteringcoefficientforrespectivegraphs.A=W~=0;%adjacencymatrixS=W.^(1/3)+(W.').^(1/3);%symmetrizedweightsmatrix^1/3K=sum(A+A.',2);%totaldegree(in+out)cyc3=diag(S^3)/2;%numberof3-cycles(ie.directedtriangles)K(cyc3==0)=inf;%ifno3-cyclesexist,makeC=0(viaK=inf)CYC3=K.*(K-1)-2*diag(A^2);%numberofallpossible3-cyclesC=cyc3./CYC3;%clusteringcoefficientfunctionC=clustering_coef_wu(W)[mn]=size(W);CorrMatrix=W;fori=1:mS1=0;S2=0;fork=1:mifk~=iforl=1:mif(l~=k)&&(l~=i)S1=CorrMatrix(i,k)*CorrMatrix(i,l)*CorrMatrix(l,k)+S1;S2=CorrMatrix(i,k)*CorrMatrix(i,l)+S2;endendendendC(i)=S1/S2;endfunctionD=distance_bin(G)%D=distance_bin(G);distancematrixforbinaryundirectedgraphG%Meandistance(excludingthemaindiagonal)equalsthecharacteristicpathlength%%Algebraicshortestpathalgorithm.%%MikaRubinov,UNSW,2007(lastmodifiedSeptember2008).D=eye(length(G));n=1;nPATH=G;%n-pathmatrixL=(nPATH~=0);%shortestn-pathmatrixwhilefind(L,1);D=D+n.*L;n=n+1;nPATH=nPATH*G;L=(nPATH~=0).*(D==0);endD(~D)=inf;%disconnectednodesareassignedd=inf;D=D-eye(length(G));functionD=distance_wei(G)%D=distance_wei(G);distancematrixforweighteddirectedgraph-%themeandistanceisthecharacteristicpathlength.%%Theinputmatrixmustbeamappingfromweighttodistance(eg.higher%correlationsmaybeinterpretedasshortdistances-henceaninverse%mappingisappropriateinthatcase).%%Dijkstra'sAlgorithm.%%MikaRubinov,UNSW,2007(lastmodifiedJuly2008).n=length(G);E=find(G);G(E)=1./G(E);%invertweights:large-shortD=zeros(n);D(~eye(n))=inf;%distancematrixforu=1:nS=true(1,n);%distancepermanence(trueistemporary)G1=G;V=u;while1S(V)=0;%distanceu-VisnowpermanentG1(:,V)=0;%noin-edgesasalreadyshortestforv=VW=find(G1(v,:));%neighboursofshortestnodesforw=W;D(u,w)=min(D(u,w),D(u,v)+G1(v,w));%thesmallestofold(ifexist)andcurrentpathlengthsendendminD=min(D(u,S));ifisempty(minD)||isinf(minD),break,end;%isempty:allnodesreached;isinf:somenodescannotbereachedV=find(D(u,:)==minD);endendfunctionE=efficiency(G,local)%GlobalandlocalefficiencyforbinaryundirectedgraphG.%%Eglob=efficiency(G);outputstheinversedistancematrix:themeanofthis%matrix(excludingmaindiagonal)isequivalenttotheglobalefficiency.%%Eloc=efficiency(G,1);outputsindividualnodallocalefficiency.%Fordirectednetworks,localefficiencyworkswiththeout-degree.%%Reference:LatoraandMarchiori,2001,PhysRevLett87:198701.%%Algebraicshortestpathalgorithm.%%MikaRubinov,UNSW,2008(lastmodifiedSeptember2008).ifnargin==2;%localefficiencyN=length(G);%numberofnodesE=zeros(N,1);%localefficiencyforu=1:NV=find(G(u,:));%neighborsk=length(V);%degreeifk=2;%degreemustbeatleasttwoe=distance_inv(G(V,V));E(u)=sum(e(:))./(k.^2-k);%localefficiencyendendelseE=distance_inv(G);%globalefficiencyendfunctionD=distance_inv(g);D=eye(length(g));n=1;nPATH=g;%n-pathmatrixL=(nPATH~=0);%shortestn-pathmatrixwhilefind(L,1);D=D+n.*L;n=n+1;nPATH=nPATH*g;L=(nPATH~=0).*(D==0);endD(~D)=inf;%disconnectednodesareassignedd=inf;D=1./D;%invertdistanceD=D-eye(length(g));
本文标题:网络分析(聚类系数、最短路径、效率)matlab代码汇总
链接地址:https://www.777doc.com/doc-3764995 .html