-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathtriangleclusters.m
90 lines (80 loc) · 2.37 KB
/
triangleclusters.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function [cond cut vol cc t teind]=triangleclusters(A)
% TRIANGLECLUSTERS Clustering metrics for clusters of vertex neighborhoods.
%
% This function studies clusters which are given by vertex neighborhoods.
% Let v be a vertex in a graph, then the cluster associated with v is just
% the set of all neighbors of v and v itself. We return the clustering
% metrics associated with these clusters for all vertices in the grah.
%
% [cond cut vol cc t]=triangleclusters(A) returns
%
% cond[uctance] of each cluster of a vertex neighborhood
% cut of each cluster
% vol[ume] of each cluster
% cc - clustering coefficient of each vertex
% t - number of triangles centered at each vertex
%
% Example:
% load_graph('dolphins');
% d = sum(A,2);
% cond = triangleclusters(A);
% plot(d,cond,'.'); % a fake network community plot
% David F. Gleich
% Copyright, Purdue University, 2011
% History
% 2011-10-05: Based on clustercoeffs.m from gaimc
try
[cond cut vol cc t teind] = triangleclusters_mex(A);
return;
catch me
if isempty(me.identifier),
rethrow(me)
end
warning('triangleclusters:mexFailed','this could be slow');
end
usew = 0;
if isstruct(A)
rp=A.rp; ci=A.ci; %ofs=A.offset;
else
if ~isequal(A,A'), error('gaimc:triangleclusters',...
'only undirected (symmetric) inputs allowed: see dirclustercoeffs');
end
[rp ci]=sparse_to_csr(A);
end
n=length(rp)-1;
Gvol = 0;
cc=zeros(n,1); ind=false(n,1); t=zeros(n,1);
cut=zeros(n,1); vol=zeros(n,1);
for v=1:n
% index the neighbors
for rpi=rp(v):rp(v+1)-1
w=ci(rpi);
if v~=w, ind(w)=1; end
end
curcc=0; d=rp(v+1)-rp(v);
% run two steps of bfs to try and find triangles and cuts
curvol = 0;
curcut = 0;
for rpi=rp(v):rp(v+1)-1
w=ci(rpi); if v==w, d=d-1; continue; end % discount self-loop
curvol = curvol+1;
Gvol = Gvol+1;
for rpi2=rp(w):rp(w+1)-1
x=ci(rpi2); if x==w, continue; end
curvol = curvol+1;
if ind(x)
curcc=curcc+1;
else
if x~=v, curcut=curcut+1; end
end
end
end
vol(v) = curvol;
cut(v) = curcut;
if d>1, cc(v) = curcc/(d*(d-1));
else cc(v) = 0;
end
t(v) = curcc/2;
for rpi=rp(v):rp(v+1)-1, w=ci(rpi); ind(w)=0; end % reset indicator
end
cond = cut./min(vol,Gvol-vol);