performance - Is there most efficient way to code program for Avg Clustering Coeff -
calculation of average clustering coefficient of graph getting correct result takes huge time when graph dimension increases need alternative way takes less time execute. there way simplify code??
%// adjacency matrix n x n, %// d degree , n=100; d=10; rand('state',0) = zeros(n,n); kv=d*(d-1)/2; %% creating matrix %%% = 1:(d*n/2) j = floor(n*rand)+1; k = floor(n*rand)+1; while (j==k)||(a(j,k)==1) j = floor(n*rand)+1; k = floor(n*rand)+1; end a(j,k)=1; a(k,j)=1; end %% calculation of clustering coeff %% i=1:n j=find(a(i,:)); et=0; ii=1:(size(j,2))-1 jj=ii+1:size(j,2) et=et+a(j(ii),j(jj)); end end cv(i)=et/kv; end avg_clustering_coeff=sum(cv)/n;
output got.
avg_clustering_coeff = 0.1107
that calculation of clustering coeff
part vectorized using nchoosek
remove innermost 2 nested loops, -
cvout = zeros(1,n); k=1:n j=find(a(k,:)); if numel(j)>1 idx = nchoosek(j,2); cvout(k) = sum(a(sub2ind([n n],idx(:,1),idx(:,2)))); end end cvout=cvout/kv;
hopefully, boost performance quite bit!
Comments
Post a Comment