edge_betweenness_wei.m 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. function [EBC,BC]=edge_betweenness_wei(G)
  2. %EDGE_BETWEENNESS_WEI Edge betweenness centrality
  3. %
  4. % EBC = edge_betweenness_wei(L);
  5. % [EBC BC] = edge_betweenness_wei(L);
  6. %
  7. % Edge betweenness centrality is the fraction of all shortest paths in
  8. % the network that contain a given edge. Edges with high values of
  9. % betweenness centrality participate in a large number of shortest paths.
  10. %
  11. % Input: L, Directed/undirected connection-length matrix.
  12. %
  13. % Output: EBC, edge betweenness centrality matrix.
  14. % BC, nodal betweenness centrality vector.
  15. %
  16. % Notes:
  17. % The input matrix must be a connection-length matrix, typically
  18. % obtained via a mapping from weight to length. For instance, in a
  19. % weighted correlation network higher correlations are more naturally
  20. % interpreted as shorter distances and the input matrix should
  21. % consequently be some inverse of the connectivity matrix.
  22. % Betweenness centrality may be normalised to the range [0,1] as
  23. % BC/[(N-1)(N-2)], where N is the number of nodes in the network.
  24. %
  25. % Reference: Brandes (2001) J Math Sociol 25:163-177.
  26. %
  27. %
  28. % Mika Rubinov, UNSW/U Cambridge, 2007-2012
  29. n=length(G);
  30. % E=find(G); G(E)=1./G(E); %invert weights
  31. BC=zeros(n,1); %vertex betweenness
  32. EBC=zeros(n); %edge betweenness
  33. for u=1:n
  34. D=inf(1,n); D(u)=0; %distance from u
  35. NP=zeros(1,n); NP(u)=1; %number of paths from u
  36. S=true(1,n); %distance permanence (true is temporary)
  37. P=false(n); %predecessors
  38. Q=zeros(1,n); q=n; %order of non-increasing distance
  39. G1=G;
  40. V=u;
  41. while 1
  42. S(V)=0; %distance u->V is now permanent
  43. G1(:,V)=0; %no in-edges as already shortest
  44. for v=V
  45. Q(q)=v; q=q-1;
  46. W=find(G1(v,:)); %neighbours of v
  47. for w=W
  48. Duw=D(v)+G1(v,w); %path length to be tested
  49. if Duw<D(w) %if new u->w shorter than old
  50. D(w)=Duw;
  51. NP(w)=NP(v); %NP(u->w) = NP of new path
  52. P(w,:)=0;
  53. P(w,v)=1; %v is the only predecessor
  54. elseif Duw==D(w) %if new u->w equal to old
  55. NP(w)=NP(w)+NP(v); %NP(u->w) sum of old and new
  56. P(w,v)=1; %v is also a predecessor
  57. end
  58. end
  59. end
  60. minD=min(D(S));
  61. if isempty(minD), break %all nodes reached, or
  62. elseif isinf(minD) %...some cannot be reached:
  63. Q(1:q)=find(isinf(D)); break %...these are first-in-line
  64. end
  65. V=find(D==minD);
  66. end
  67. DP=zeros(n,1); %dependency
  68. for w=Q(1:n-1)
  69. BC(w)=BC(w)+DP(w);
  70. for v=find(P(w,:))
  71. DPvw=(1+DP(w)).*NP(v)./NP(w);
  72. DP(v)=DP(v)+DPvw;
  73. EBC(v,w)=EBC(v,w)+DPvw;
  74. end
  75. end
  76. end