/** * Find nearest neighbor of a given point. */ private void findNeighbor(int p) { // if no neighbors available, set flag for UpdatePoint to find if (npoints == 1) { neighbor[p] = p; distance[p] = Float.MAX_VALUE; return; } // find first point unequal to p itself int first = 0; if (p == points[first]) { first = 1; } neighbor[p] = points[first]; distance[p] = linkage.d(p, neighbor[p]); // now test whether each other point is closer for (int i = first + 1; i < npoints; i++) { int q = points[i]; if (q != p) { float d = linkage.d(p, q); if (d < distance[p]) { distance[p] = d; neighbor[p] = q; } } } }
float nbd = Float.MAX_VALUE; for (int j = i + 1; j < npoints; j++) { float d = linkage.d(points[i], points[j]); if (d < nbd) { nbr = j;
/** * All distances to point have changed, check if our structures are ok * Note that although we completely recompute the neighbors of p, * we don't explicitly call findNeighbor, since that would double * the number of distance computations made by this routine. * Also, like deletion, we don't change any other point's neighbor to p. */ public void updatePoint(int p) { neighbor[p] = p; // flag for not yet found any distance[p] = Float.MAX_VALUE; for (int i = 0; i < npoints; i++) { int q = points[i]; if (q != p) { float d = linkage.d(p, q); if (d < distance[p]) { distance[p] = d; neighbor[p] = q; } if (neighbor[q] == p) { if (d > distance[q]) { findNeighbor(q); } else { distance[q] = d; } } } } }
/** * Single distance has changed, check if our structures are ok. */ public void updateDistance(int p, int q) { float d = linkage.d(p, q); if (d < distance[p]) { distance[p] = q; neighbor[p] = q; } else if (neighbor[p] == q && d > distance[p]) { findNeighbor(p); } if (d < distance[q]) { distance[q] = p; neighbor[q] = p; } else if (neighbor[q] == p && d > distance[q]) { findNeighbor(q); } } }