-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusteringDataset_buildRandomGraph_Log.m
190 lines (132 loc) · 3.6 KB
/
clusteringDataset_buildRandomGraph_Log.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
% (THIRD-PARTY TOOLBOX)
% ********************************
clear all
clc
dsName = 'twitter';
maxKC = 100;
constEPS = 1e-10;
load([dsName '.mat']);
% N: scalar
% WW: Nx1 cell
% XX: Nx1 cell
% YY: 200x1
% start & end ID for each XX{ii}
XX_sID = zeros(N, 1);
XX_eID = zeros(N, 1);
sID = 0;
for ii = 1:N
% % XX{ii}; % ni x dim
nnII = size(XX{ii}, 1);
XX_sID(ii) = sID + 1;
XX_eID(ii) = sID + nnII;
sID = XX_eID(ii);
end
sumNN = sID;
disp(['Sample size: ' num2str(sumNN)]);
XX_ALL = zeros(sumNN, size(XX{1}, 2));
for ii = 1:N
XX_ALL(XX_sID(ii):XX_eID(ii), :) = XX{ii};
end
[NN, dim] = size(XX_ALL); %2491 x 2
disp('Clustering by the farthest-point clustering !!!');
tic
[KC, ~, XX_ALL_ID, XX_CC, ~, ~] = ...
figtreeKCenterClustering(dim, NN, XX_ALL', maxKC);
% note: XX_ALL_ID: 0 --> (KC-1)
XX_ALL_ID = XX_ALL_ID + 1;
runTime_Clustering = toc
% XX_IdCC --> ID
% XX_CC: value
XX_ALL_ID = XX_ALL_ID'; % NN x 1
XX_CC = XX_CC'; % KC x dim
XX_ID = cell(N, 1);
for ii = 1:N
tmp = XX_ALL_ID(XX_sID(ii):XX_eID(ii));
XX_ID{ii} = tmp;
end
% XX_CC: data (cluster centers) --> KC x dim
% XX_ID: cell Nx1 --> each cell: vector of ID (of clusters)
% -----------
% Build graph
nGG = KC;
% --- PRUNNING -- (kk*N)
kGG = ceil(log(nGG));
disp('Compute distance matrix');
tic
sqDD = sqdistance(XX_CC');
runTime_DM = toc
% -- correct machine precision
sqDD(sqDD<constEPS) = 0;
DD = sqrt(sqDD);
% #edges before prunning
nEE = nGG*(nGG-1)/2;
% only kept #edges (after prunning)
mEE = nGG*kGG;
EdgeID = zeros(nEE, 2);
EdgeWW = zeros(nEE, 1);
id = 1;
for ii = 1:(nGG-1)
for jj = (ii+1):nGG
EdgeID(id, :) = [ii, jj];
EdgeWW(id) = DD(ii, jj);
id = id+1;
end
end
% save original
EdgeID_org = EdgeID;
EdgeWW_org = EdgeWW;
Key_org = EdgeID_org(:, 1)*(nGG+1) + EdgeID_org(:, 2); % Key for searching !!!
% ---prunning
% --- random perturbation----
tmpRand = randperm(length(EdgeWW_org));
EdgeWW = EdgeWW_org(tmpRand);
EdgeID = EdgeID_org(tmpRand, :);
% only keep mEE = kGG*nGG
EdgeWW = EdgeWW(1:mEE);
EdgeID = EdgeID(1:mEE, :);
GG = graph(EdgeID(:, 1), EdgeID(:, 2), EdgeWW);
% Test connected components
disp('check connected components');
tic
[bGG, bsGG] = conncomp(GG, 'OutputForm','cell');
runTime_Components = toc
numCC = length(bGG);
disp(num2str(numCC));
runTime_Connecting = 0;
% ############################
if numCC > 1
disp('connecting the graph');
tic
addEdgeID = zeros(numCC-1, 2);
addEdgeWW = zeros(numCC-1, 1);
for ii = 1:(numCC-1)
% random II
allEdgeII = bGG{ii};
rID1 = randperm(length(allEdgeII));
idII = allEdgeII(rID1(1));
% random JJ
allEdgeJJ = bGG{ii+1};
rID2 = randperm(length(allEdgeJJ));
idJJ = allEdgeJJ(rID2(1));
% edge & weight
addEdgeIJ = sort([idII, idJJ]);
key_addIJ = addEdgeIJ(:, 1)*(nGG+1) + addEdgeIJ(:, 2);
addEdgeWWIJ = EdgeWW_org(Key_org==key_addIJ);
% add
addEdgeID(ii, :) = addEdgeIJ;
addEdgeWW(ii) = addEdgeWWIJ;
end
% update graph as connected one
EdgeID = [EdgeID; addEdgeID];
EdgeWW = [EdgeWW; addEdgeWW];
GG = graph(EdgeID(:, 1), EdgeID(:, 2), EdgeWW);
runTime_Connecting = toc
end
disp('...saving data');
outName = [dsName '_' num2str(maxKC) '_RandLLE_Graph.mat'];
save(outName, 'GG', 'EdgeID', 'EdgeWW', 'XX_ID', 'XX_CC', ...
'runTime_Connecting', 'runTime_Components', ...
'maxKC', 'EdgeID_org', 'EdgeWW_org', 'Key_org', ...
'nEE', 'mEE', 'DD', 'nGG', 'kGG', 'XX_sID', 'XX_eID', ...
'N', 'XX', 'YY', 'WW');
disp('FINISH !!!');