efficiency_bin.m 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. function E=efficiency_bin(A,local)
  2. %EFFICIENCY_BIN Global efficiency, local efficiency.
  3. %
  4. % Eglob = efficiency_bin(A);
  5. % Eloc = efficiency_bin(A,1);
  6. %
  7. % The global efficiency is the average of inverse shortest path length,
  8. % and is inversely related to the characteristic path length.
  9. %
  10. % The local efficiency is the global efficiency computed on the
  11. % neighborhood of the node, and is related to the clustering coefficient.
  12. %
  13. % Inputs: A, binary undirected or directed connection matrix
  14. % local, optional argument
  15. % local=0 computes global efficiency (default)
  16. % local=1 computes local efficiency
  17. %
  18. % Output: Eglob, global efficiency (scalar)
  19. % Eloc, local efficiency (vector)
  20. %
  21. %
  22. % Algorithm: algebraic path count
  23. %
  24. % Reference: Latora and Marchiori (2001) Phys Rev Lett 87:198701.
  25. % Fagiolo (2007) Phys Rev E 76:026107.
  26. % Rubinov M, Sporns O (2010) NeuroImage 52:1059-69
  27. %
  28. %
  29. % Mika Rubinov, U Cambridge
  30. % Jonathan Clayden, UCL
  31. % 2008-2013
  32. % Modification history:
  33. % 2008: Original (MR)
  34. % 2013: Bug fix, enforce zero distance for self-connections (JC)
  35. % 2013: Local efficiency generalized to directed networks
  36. n=length(A); %number of nodes
  37. A(1:n+1:end)=0; %clear diagonal
  38. A=double(A~=0); %enforce double precision
  39. if exist('local','var') && local %local efficiency
  40. E=zeros(n,1);
  41. for u=1:n
  42. V=find(A(u,:)|A(:,u).'); %neighbors
  43. sa=A(u,V)+A(V,u).'; %symmetrized adjacency vector
  44. e=distance_inv(A(V,V)); %inverse distance matrix
  45. se=e+e.'; %symmetrized inverse distance matrix
  46. numer=sum(sum((sa.'*sa).*se))/2; %numerator
  47. if numer~=0
  48. denom=sum(sa).^2 - sum(sa.^2); %denominator
  49. E(u)=numer/denom; %local efficiency
  50. end
  51. end
  52. else %global efficiency
  53. e=distance_inv(A);
  54. E=sum(e(:))./(n^2-n);
  55. end
  56. function D=distance_inv(A_)
  57. l=1; %path length
  58. Lpath=A_; %matrix of paths l
  59. D=A_; %distance matrix
  60. n_=length(A_);
  61. Idx=true;
  62. while any(Idx(:))
  63. l=l+1;
  64. Lpath=Lpath*A_;
  65. Idx=(Lpath~=0)&(D==0);
  66. D(Idx)=l;
  67. end
  68. D(~D | eye(n_))=inf; %assign inf to disconnected nodes and to diagonal
  69. D=1./D; %invert distance