📄 get_clustering_coefficient.m
字号:
function cc=get_clustering_coefficient(Nodes,N,nodes_attribute)
%This progrom is to calculate the clustering coefficient of graph in Nodes.
%The clustering coefficient is defined in Newman Review[2003] IIIB.Eq.(5)(6)
%Ci=number of triangles connedted to vertex i/number of triples contered on vertex i
%C=sum(Ci)/n
%Input:Nodes--N*N matrix, saving the information graph with adjacent matrix
% if nodes_attribute=1, or adjacent list if nodes_attribute=2.
% Default value is nodes_attribute=1.NO Care of Nodes(i,i)
% N--the number of node in Nodes
% nodes_attribute--the attribute of Nodes,nodes_attribute=1 if Nodes is
% adjacent matrix,nodes_attribute=2 if it is adjacent
% list. The default value is nodes_attribute=1
%Output:cc--reture the clustering coefficient of Nodes
%clear
%Add sparse matrix on 05/08/16
%TEST_FULL_SPARAE=1 is full matrix,TEST_FULL_SPARAE=2 is sparse matrix,
TEST_FULL_SPARAE=1;
if nodes_attribute==1
if TEST_FULL_SPARAE==1
Node_CC=zeros(N,1);
elseif TEST_FULL_SPARAE==2%Add sparse matrix on 05/08/16
Nodes_CC=sparse(N,1);
end
for i=1:N %calculate the CC for each node
triangle_num=0;
Neighbor=find(Nodes(i,:)>0);
node_degree=nnz(Neighbor);
if node_degree==0||node_degree==1
Node_CC(i)=0;
else
triple_num=node_degree*(node_degree-1)/2;
for j=1:(node_degree-1)%neighbor 1
for k=(j+1):node_degree%neighbor 2
if Nodes(Neighbor(j),Neighbor(k))>0
triangle_num=triangle_num+1;
end
end
end%j=1:node_degree
Node_CC(i)=triangle_num/triple_num;
end%node_degree
end%i
cc=sum(Node_CC)/N;
end%nodes_attribute
if TEST==1;
t=toc
cc
end
return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -