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

代码参考 | 平均路径长度
2022-01-15 19:46 Jie  RCNS

function [ls,avpath]=pathlength(A)

%Calculates the longest shortest path length (diameter) & the average path length of a network.

%Usage: [ls,avpath]=pathlength(A), where A is an adjacency matrix.

%This version counts the steps taken for a node to "return to itself".


%prealloc B & stuff to make faster!

N=length(A);

i=1;

B=sparse([],[],[],N,N,N^2);

L=0;

B=B+A^i;

M(i)=nnz(B); %-nnz(diag(B)); %to account for self-connections & ignore


while M(i)>L %fill-in increases

   L=M(i);

   i=i+1;

   B=B+A^i;

   M(i)=nnz(B); %-nnz(diag(B));

end

ls=i-1; %longestshortest


avpath=sum((1:ls).*[M(1) M(2:ls)-M(1:ls-1)])/N^2; %change denom to N(N-1) if diag used


   


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

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