Skip to content

Commit 746fa6e

Browse files
committed
Fix handling of duplicates and merge history.
1 parent 7b3dd8d commit 746fa6e

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

elki-clustering/src/main/java/elki/clustering/hierarchical/BufferedSearchSingleLink.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,9 @@ private void initializeHeap() {
223223
final double d = pq.computeExactDistance();
224224
if(d == 0.) { // duplicate, merge immediately
225225
int cb = builder.get(b);
226-
assert ca != cb;
227-
ca = builder.add(ca, 0, cb);
226+
if(ca != cb) {
227+
ca = builder.add(ca, 0, cb);
228+
}
228229
continue;
229230
}
230231
h.add(d, b);
@@ -256,9 +257,9 @@ private void refillNeighbors(int a, int ca) {
256257
DoubleIntegerMinHeap h = heaps[a];
257258
double thres = h.isEmpty() ? Double.POSITIVE_INFINITY : h.peekKey();
258259
final double skip = threshold[a];
259-
if (last != a) {
260-
pq.search(ita.seek(a)).increaseSkip(skip);
261-
last = a;
260+
if(last != a) {
261+
pq.search(ita.seek(a)).increaseSkip(skip);
262+
last = a;
262263
}
263264
for(; pq.valid() && pq.allLowerBound() < thres; pq.advance()) {
264265
final int b = ids.index(pq);

elki-clustering/src/main/java/elki/clustering/hierarchical/ClusterMergeHistoryBuilder.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ public int get(int i) {
112112
}
113113
// Follow i to its parent.
114114
int p = parent[i];
115-
if (p == i) {
115+
if(p == i) {
116116
return i;
117117
}
118118
while(true) {
119119
final int pp = parent[p];
120-
if (p == pp) {
120+
if(p == pp) {
121121
return p;
122122
}
123123
parent[i] = pp; // path halving
@@ -274,15 +274,13 @@ public int[] optimizeOrder() {
274274
return da < db ? -1 : da > db ? +1 : (a > b ? -1 : +1);
275275
});
276276
// Now we need to ensure merges are consistent in their order
277-
byte[] seen = new byte[m];
277+
boolean[] seen = new boolean[m];
278278
int[] order = new int[m];
279279
int size = 0;
280280
for(int it : o1) {
281-
if(seen[it] > 0) {
282-
continue;
281+
if(!seen[it]) {
282+
size = addRecursive(order, size, seen, it, n);
283283
}
284-
size = addRecursive(order, size, seen, it, n);
285-
seen[order[size++] = it] = 1;
286284
}
287285
assert size == m;
288286
// Rewrite the result:
@@ -346,16 +344,15 @@ private boolean checkMonotone() {
346344
* @param n Number of primary objects
347345
* @return Size after additions
348346
*/
349-
private int addRecursive(int[] order, int size, byte[] seen, int it, int n) {
347+
private int addRecursive(int[] order, int size, boolean[] seen, int it, int n) {
350348
int a = merges[it << 1] - n, b = merges[(it << 1) + 1] - n;
351-
if(a >= 0 && seen[a] == 0) {
349+
if(a >= 0 && !seen[a]) {
352350
size = addRecursive(order, size, seen, a, n);
353-
seen[order[size++] = a] = 1;
354351
}
355-
if(b >= 0 && seen[b] == 0) {
352+
if(b >= 0 && !seen[b]) {
356353
size = addRecursive(order, size, seen, b, n);
357-
seen[order[size++] = b] = 1;
358354
}
355+
seen[order[size++] = it] = true;
359356
return size;
360357
}
361358
}

elki-clustering/src/main/java/elki/clustering/hierarchical/HeapOfSearchersSingleLink.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public void run(Relation<? extends O> relation) {
177177
}
178178
nn.poll();
179179
// need refill?
180-
if(nn.isEmpty() || nn.peekKey() > curd) {
180+
if(nn.isEmpty() || nn.peekKey() > pqs[a].allLowerBound()) {
181181
refillNeighbors(a, ca);
182182
}
183183
if(nn.isEmpty()) {
@@ -218,8 +218,9 @@ private void initializeHeap(Relation<? extends O> relation) {
218218
final double d = pq.computeExactDistance();
219219
if(d == 0.) { // duplicate, merge immediately
220220
int cb = builder.get(b);
221-
assert ca != cb;
222-
ca = builder.add(ca, 0, cb);
221+
if (ca != cb) {
222+
ca = builder.add(ca, 0, cb);
223+
}
223224
continue;
224225
}
225226
h.add(d, b);

elki-clustering/src/main/java/elki/clustering/hierarchical/RestartingSearchSingleLink.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,9 @@ private void initializeHeap() {
234234
final double d = pq.computeExactDistance();
235235
if(d == 0.) { // duplicate, merge immediately
236236
int cb = builder.get(b);
237-
assert ca != cb;
238-
ca = builder.add(ca, 0, cb);
237+
if(ca != cb) {
238+
ca = builder.add(ca, 0, cb);
239+
}
239240
continue;
240241
}
241242
if(d < thresh) {

0 commit comments

Comments
 (0)