Program: 149591
Project: 149591
Buggy source:
/** 
 * Search for the closest pair of particles, one of which is not in a cluster, below the squared radius distance and join them
 * @param grid
 * @param nXBins
 * @param nYBins
 * @param r2 The squared radius distance
 * @param yBinWidth
 * @param xBinWidth
 * @param miny
 * @param minx
 * @param clusterId
 * @return The number of points assigned to clusters (either 0, 1, or 2)
 */
private int joinClosestParticle(ExtendedClusterPoint[][] grid,final int nXBins,final int nYBins,final double r2,double minx,double miny,double xBinWidth,double yBinWidth,int[] clusterId){
  double min=r2;
  ExtendedClusterPoint pair1=null, pair2=null;
  for (int yBin=0; yBin < nYBins; yBin++) {
    for (int xBin=0; xBin < nXBins; xBin++) {
      for (ExtendedClusterPoint c1=grid[xBin][yBin]; c1 != null; c1=c1.nextE) {
        final boolean cluster1=c1.inCluster;
        ExtendedClusterPoint other=null;
        for (ExtendedClusterPoint c2=c1.nextE; c2 != null; c2=c2.nextE) {
          if (cluster1 && c2.inCluster)           continue;
          final double d2=c1.distance2(c2);
          if (d2 < min) {
            min=d2;
            other=c2;
          }
        }
        if (yBin < nYBins - 1) {
          for (ExtendedClusterPoint c2=grid[xBin][yBin + 1]; c2 != null; c2=c2.nextE) {
            if (cluster1 && c2.inCluster)             continue;
            final double d2=c1.distance2(c2);
            if (d2 < min) {
              min=d2;
              other=c2;
            }
          }
          if (xBin > 0) {
            for (ExtendedClusterPoint c2=grid[xBin - 1][yBin + 1]; c2 != null; c2=c2.nextE) {
              if (cluster1 && c2.inCluster)               continue;
              final double d2=c1.distance2(c2);
              if (d2 < min) {
                min=d2;
                other=c2;
              }
            }
          }
        }
        if (xBin < nXBins - 1) {
          for (ExtendedClusterPoint c2=grid[xBin + 1][yBin]; c2 != null; c2=c2.nextE) {
            if (cluster1 && c2.inCluster)             continue;
            final double d2=c1.distance2(c2);
            if (d2 < min) {
              min=d2;
              other=c2;
            }
          }
          if (yBin < nYBins - 1) {
            for (ExtendedClusterPoint c2=grid[xBin + 1][yBin + 1]; c2 != null; c2=c2.nextE) {
              if (cluster1 && c2.inCluster)               continue;
              final double d2=c1.distance2(c2);
              if (d2 < min) {
                min=d2;
                other=c2;
              }
            }
          }
        }
        if (other != null) {
          pair1=c1;
          pair2=other;
        }
      }
    }
  }
  if (pair1 != null) {
    int nProcessed=1;
    if (clusterId[pair2.id] == 0) {
      nProcessed=2;
      clusterId[pair2.id]=++nextClusterId;
      pair2.inCluster=true;
    }
    clusterId[pair1.id]=clusterId[pair2.id];
    pair1.inCluster=true;
    if (trackJoins) {
      if (pair1.next.id == pair2.next.id)       intraIdDistances[intraIdCount++]=Math.sqrt(min);
 else       interIdDistances[interIdCount++]=Math.sqrt(min);
    }
    return nProcessed;
  }
  return 0;
}

Fixed version (ground truth):
/** 
 * Search for the closest pair of particles, one of which is not in a cluster, below the squared radius distance and join them
 * @param grid
 * @param nXBins
 * @param nYBins
 * @param r2 The squared radius distance
 * @param yBinWidth
 * @param xBinWidth
 * @param miny
 * @param minx
 * @param clusterId
 * @return The number of points assigned to clusters (either 0, 1, or 2)
 */
private int joinClosestParticle(ExtendedClusterPoint[][] grid,final int nXBins,final int nYBins,final double r2,double minx,double miny,double xBinWidth,double yBinWidth,int[] clusterId){
  double min=r2;
  ExtendedClusterPoint pair1=null, pair2=null;
  for (int yBin=0; yBin < nYBins; yBin++) {
    for (int xBin=0; xBin < nXBins; xBin++) {
      for (ExtendedClusterPoint c1=grid[xBin][yBin]; c1 != null; c1=c1.nextE) {
        final boolean cluster1=c1.inCluster;
        ExtendedClusterPoint other=null;
        for (ExtendedClusterPoint c2=c1.nextE; c2 != null; c2=c2.nextE) {
          if (cluster1 && c2.inCluster)           continue;
          final double d2=c1.distance2(c2);
          if (d2 < min) {
            min=d2;
            other=c2;
          }
        }
        if (yBin < nYBins - 1) {
          for (ExtendedClusterPoint c2=grid[xBin][yBin + 1]; c2 != null; c2=c2.nextE) {
            if (cluster1 && c2.inCluster)             continue;
            final double d2=c1.distance2(c2);
            if (d2 < min) {
              min=d2;
              other=c2;
            }
          }
          if (xBin > 0) {
            for (ExtendedClusterPoint c2=grid[xBin - 1][yBin + 1]; c2 != null; c2=c2.nextE) {
              if (cluster1 && c2.inCluster)               continue;
              final double d2=c1.distance2(c2);
              if (d2 < min) {
                min=d2;
                other=c2;
              }
            }
          }
        }
        if (xBin < nXBins - 1) {
          for (ExtendedClusterPoint c2=grid[xBin + 1][yBin]; c2 != null; c2=c2.nextE) {
            if (cluster1 && c2.inCluster)             continue;
            final double d2=c1.distance2(c2);
            if (d2 < min) {
              min=d2;
              other=c2;
            }
          }
          if (yBin < nYBins - 1) {
            for (ExtendedClusterPoint c2=grid[xBin + 1][yBin + 1]; c2 != null; c2=c2.nextE) {
              if (cluster1 && c2.inCluster)               continue;
              final double d2=c1.distance2(c2);
              if (d2 < min) {
                min=d2;
                other=c2;
              }
            }
          }
        }
        if (other != null) {
          pair1=c1;
          pair2=other;
        }
      }
    }
  }
  if (pair1 != null) {
    int nProcessed=1;
    if (pair1.inCluster && pair2.inCluster) {
      throw new RuntimeException("String_Node_Str");
    }
 else     if (pair1.inCluster) {
      clusterId[pair2.id]=clusterId[pair1.id];
      pair2.inCluster=true;
    }
 else     if (pair2.inCluster) {
      clusterId[pair1.id]=clusterId[pair2.id];
      pair1.inCluster=true;
    }
 else {
      nProcessed=2;
      clusterId[pair1.id]=clusterId[pair2.id]=++nextClusterId;
      pair1.inCluster=pair2.inCluster=true;
    }
    if (trackJoins) {
      if (pair1.next.id == pair2.next.id)       intraIdDistances[intraIdCount++]=Math.sqrt(min);
 else       interIdDistances[interIdCount++]=Math.sqrt(min);
    }
    return nProcessed;
  }
  return 0;
}