-
Notifications
You must be signed in to change notification settings - Fork 1
/
QOGC_Tree.m
307 lines (235 loc) · 11.1 KB
/
QOGC_Tree.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
function [ vecResult ] = QOGC_Tree(query, K, distType, valDampFactor)
%
% Function: QueryOrientedClustering (bound by top K clusters)
% Abstract:
% In this function, we introduce two parameters: #numM and #numN.
% #numM determines how many vertices are labelized at most.
% #numN determines how many distributions remains at most after merge funcitons.
%
global matG
global matA
global N
global matResultVal
global matResultIndex
iterMax = 5;
%
% while the top-k probabilty distributions would not be merged, the
% last-2k distribuiotns are all been merged. Thus, we have two
% explanaitons.
% First, the top-k distribuitons are different with one another enough.
% Second, the last-2k distribuiotns are irrelevant to the query enough.
%
numM = 3 * K;
numN = 2 * K;
% Create the query of the multi-label.
% As such, the query is represented by a set of vectors.
matQ = sparse(N, 1);
matQ(query, 1) = 1;
matV = matQ;
k = 0;
%% Tree Search & Merge
% matParentLink(i, j) = 1 -----> v_j is a parent of v_i.
matParentLink = sparse(N, N);
%matLabelStep = sparse(N, 1);
isConverge = false;
isTopKMerged = true;
numNewLabels = 0;
while isConverge == false
k = k + 1;
if k == iterMax || isTopKMerged == false;
isConverge = true;
end
[vecChildX, vecChildY, vecChildVal] = find(ones(1, N) * matQ);
queryNum = length(vecChildY);
vecV_q = matV(:, 1);
%% Label propagation
for index = 1:queryNum
% get the index of query vector
q = vecChildY(index);
% find all parent label-vectors index
vecParent = matParentLink(q, 1:length(matQ(1,:)));
vectemp = sign(matQ*vecParent');
% calculate the matrix I_q
matI_q = sparse(1:N, 1:N, 1);
[X, Y, Val] = find(vectemp);
for i = 1:length(X)
matI_q(X(i), X(i)) = 0;
end
% label propagation
vecV = matV(:, q);
vecQ = matQ(:, q);
vecV = (1-valDampFactor) * matI_q * (matA * (matI_q * vecV)) + valDampFactor * vecQ;
% if index == 1
% %vecV = (1-valPageRankAlpha) * matG * matrixD * matrixD * vecV + valPageRankAlpha * vecQ;
% vecV = (1-valDampFactor) * matI_q * (matA * (matI_q * vecV)) + valDampFactor * vecQ;
% else
% vecV = (1-valDampFactor) * matI_q * (matA * (matI_q * vecV)) + valDampFactor * vecQ;
% end
matV(:, q) = vecV;
end
% matV = (1-valDampFactor) * matA * matV + valDampFactor * matQ;
%% When k is even
if mod(k, 2) == 0
[V_X, V_Y, V_V] = find(ones(1, N) * matV);
if length(V_Y) > K
vecTopK = V_Y(2: K + 1);
isTopKMerged = false;
% --- Sibling merge ---
[vecPaX, vecPaY, vecPaVal] = find(ones(1, N) * matParentLink);
boolAllowMerge = true;
while boolAllowMerge == true
minChildID_i = -1;
minChildID_j = -1;
minDistance = 100;
% for each parent
IsMergeHappens = false;
parentID = -1;
for idx = 1:length(vecPaY)
if parentID == vecPaY(length(vecPaY) - idx + 1) && IsMergeHappens == false
continue;
end
parentID = vecPaY(length(vecPaY) - idx + 1);
IsMergeHappens = false;
[vecChildX, vecChildY, vecChildVal] = find(matParentLink(:, parentID));
vecHasMergedIndex = zeros(N, 1);
i = 0;
while i < length(vecChildX)
i = i + 1;
ChildID_i = vecChildX(i);
if vecHasMergedIndex(ChildID_i, 1) == 1
continue;
end
j = i;
while j < length(vecChildX)
j = j + 1;
ChildID_j = vecChildX(j);
if vecHasMergedIndex(ChildID_j, 1) == 1
continue;
end
v_i = matV(:, ChildID_i);
v_j = matV(:, ChildID_j);
dist = Distance(v_i, v_j , distType);
if dist < minDistance
minChildID_i = ChildID_i;
minChildID_j = ChildID_j;
minDistance = dist;
end
end
end
end
[valChild_X, valChild_Y, valChild_Val] = find(matParentLink * ones(length(matParentLink(1,:)), 1));
valChildNum = length(valChild_X);
if valChildNum > numN
% i, j are similar enough
if ismember(minChildID_i, vecTopK) || ismember(minChildID_j, vecTopK)
isTopKMerged = true;
end
IsMergeHappens = true;
dimY = length(matV(1, :));
vecHasMergedIndex(minChildID_i, 1) = 1;
vecHasMergedIndex(minChildID_j) = 1;
vecChildX(length(vecChildX) + 1) = dimY + 1;
% Create a new vector v_k, and remove v_i and v_j.
dimY = length(matV(1, :));
matV(:, dimY + 1) = 0.5 * (matV(:, minChildID_i) + matV(:, minChildID_j));
matQ(:, dimY + 1) = 0.5 * (matQ(:, minChildID_i) + matQ(:, minChildID_j));
matV(:, minChildID_i) = zeros(N, 1);
matQ(:, minChildID_i) = zeros(N, 1);
matV(:, minChildID_j) = zeros(N, 1);
matQ(:, minChildID_j) = zeros(N, 1);
% Delete parent related to v_i and v_j by replcing to v_k
matParentLink(dimY + 1, :) = sign(matParentLink(minChildID_i, :) + matParentLink(minChildID_j, :));
matParentLink(:, dimY + 1) = sign(matParentLink(:, minChildID_i) + matParentLink(:, minChildID_j));
matParentLink(minChildID_i, :) = zeros(1, N);
matParentLink(minChildID_j, :) = zeros(1, N);
matParentLink(:, minChildID_i) = zeros(N, 1);
matParentLink(:, minChildID_j) = zeros(N, 1);
fprintf(strcat(int2str(minChildID_i), ' >SM< ', int2str(minChildID_j), ' => ', int2str(dimY + 1), '\n'));
% check the existence of siblings to determine to
% trigger Bottom-up merge.
[X, Y, V] = find(matParentLink(dimY + 1, :));
for idx = 1:length(Y)
parentID = vecPaY(idx);
[X, Y, Val] = find(matParentLink(:, parentID));
if length(X) ~= 1
continue;
end
ChildVecID = X(1);
% if a parent has only a child, remove its child.
matParentLink(ChildVecID, :) = sparse(1, N);
matParentLink(:, ChildVecID) = sparse(N, 1);
matV(:, ChildVecID) = sparse(N, 1);
matQ(:, ChildVecID) = sparse(N, 1);
end
boolAllowMerge = true;
else
boolAllowMerge = false;
end
end
end
end
% the vertex that is visited for the first time
[lenthX, lenthY] = size(matQ);
vecAllQ = matQ * ones(lenthY, 1);
[vecNewX, vecNewY, vecNewVal] = find(matV(:, 1) .* (sign(matV(:, 1)) - sign(vecAllQ)));
%[vecNewX, vecNewY, vecNewVal] = find(matV(:, 1) .* (sign(matV(:, 1)) - sign(vecV_q)));
[vecSortedNewVal, vecSortedNewIndex] = sort(vecNewVal,'descend');
% the vertex that has been visited before
[vecOldX, vecOldY, vecOldVal] = find(sign(vecV_q));
%% New label generation
for i = 1:length(vecSortedNewIndex)
[x, y ,v] = find(ones(1, N) * matQ);
numNewLabels = length(y) - 1;
if numNewLabels >= numM
break;
end
% numNewLabels = numNewLabels + 1;
newLabelID = vecNewX(vecSortedNewIndex(i));
% Initialize a new label
vecNewLabel = sparse(N, 1);
vecNewLabel(newLabelID, 1) = matV(newLabelID, 1);
dimY = length(matV(1, :));
matV(:, dimY + 1) = vecNewLabel;
matQ(:, dimY + 1) = vecNewLabel;
% the neighbor vertices of newLabelID
[vecNearX, vecNearY, vecNearVal] = find(matG(:, newLabelID));
% the parent vertices (visited neighbors) of newLabelID
setParentNodes = intersect(vecNearX, vecOldX);
vecParentNodes = sparse(1, N);
for j = 1:length(setParentNodes)
vecParentNodes(1, setParentNodes(j)) = 1;
end
% find all parent vector IDs from parent vertices
[vecParentVecIDX, vecParentVecIDY, vecParentVecIDVal] = find(vecParentNodes * matQ);
for j = 1:length(vecParentVecIDY)
matParentLink(dimY + 1, vecParentVecIDY(j)) = 1;
end
end
fprintf('The Length of matQ in itr');
fprintf(strcat(int2str(k), ' : ', int2str(length(matQ(1,:))), '\n'));
end
[x, y, v] = find(sum(matV));
for i = 1:K
vecResult(:, i) = matV(:, y(i+1));
end
%vecResult = matV(:, 2:1+K);
%{
matSortedVal = sparse(length(matV(:, 1)), length(matV(1, :)));
matSortedIndex = sparse(length(matV(:, 1)), length(matV(1, :)));
for i = 2:length(matV(1, :))
if sum(matV(:, i)) == 0
continue;
end
[matSortedVal(:, i), matSortedIndex(:, i) ] = sort(matV(:, i), 'descend');
end
[SV_X, SV_Y, SV_V] = find(matSortedVal);
setYindex = intersect(SV_Y, SV_Y);
matResultVal = zeros(valK, length(setYindex));
matResultIndex = zeros(valK, length(setYindex));
for i = 1:length(setYindex)
index = setYindex(i);
matResultVal(:, i) = full(matSortedVal(1:valK, index));
matResultIndex(:, i) = full(matSortedIndex(1:valK, index));
end
%}
end