Program: 92539
Project: 92539
Buggy source:
/** 
 * Constructor. Clustering data with the number of clusters being automatically determined by G-Means algorithm.
 * @param data the input data of which each row is a sample.
 * @param kmax the maximum number of clusters.
 */
public GMeans(double[][] data,int kmax){
  if (kmax < 2) {
    throw new IllegalArgumentException("String_Node_Str" + kmax);
  }
  int n=data.length;
  int d=data[0].length;
  k=1;
  size=new int[k];
  size[0]=n;
  y=new int[n];
  centroids=new double[k][d];
  for (int i=0; i < n; i++) {
    for (int j=0; j < d; j++) {
      centroids[0][j]+=data[i][j];
    }
  }
  for (int j=0; j < d; j++) {
    centroids[0][j]/=n;
  }
  distortion=0.0;
  for (int i=0; i < n; i++) {
    distortion+=Math.squaredDistance(data[i],centroids[0]);
  }
  logger.info(String.format("String_Node_Str",k,distortion));
  BBDTree bbd=new BBDTree(data);
  while (k < kmax) {
    ArrayList<double[]> centers=new ArrayList<>();
    double[] score=new double[k];
    KMeans[] kmeans=new KMeans[k];
    for (int i=0; i < k; i++) {
      if (size[i] < 25) {
        logger.info("String_Node_Str",i,size[i]);
        continue;
      }
      double[][] subset=new double[size[i]][];
      for (int j=0, l=0; j < n; j++) {
        if (y[j] == i) {
          subset[l++]=data[j];
        }
      }
      kmeans[i]=new KMeans(subset,2,100,4);
      double[] v=new double[d];
      for (int j=0; j < d; j++) {
        v[j]=kmeans[i].centroids[0][j] - kmeans[i].centroids[1][j];
      }
      double vp=Math.dot(v,v);
      double[] x=new double[size[i]];
      for (int j=0; j < x.length; j++) {
        x[j]=Math.dot(subset[j],v) / vp;
      }
      Math.standardize(x);
      score[i]=AndersonDarling(x);
      logger.info(String.format("String_Node_Str",i,score[i]));
    }
    int[] index=QuickSort.sort(score);
    for (int i=0; i < k; i++) {
      if (score[index[i]] <= 1.8692) {
        centers.add(centroids[index[i]]);
      }
    }
    int m=centers.size();
    for (int i=k; --i >= 0; ) {
      if (score[i] > 1.8692) {
        if (centers.size() + i - m + 1 < kmax) {
          logger.info("String_Node_Str",index[i]);
          centers.add(kmeans[index[i]].centroids[0]);
          centers.add(kmeans[index[i]].centroids[1]);
        }
 else {
          centers.add(centroids[index[i]]);
        }
      }
    }
    if (centers.size() == k) {
      break;
    }
    k=centers.size();
    double[][] sums=new double[k][d];
    size=new int[k];
    centroids=new double[k][];
    for (int i=0; i < k; i++) {
      centroids[i]=centers.get(i);
    }
    distortion=Double.MAX_VALUE;
    for (int iter=0; iter < 100; iter++) {
      double newDistortion=bbd.clustering(centroids,sums,size,y);
      for (int i=0; i < k; i++) {
        if (size[i] > 0) {
          for (int j=0; j < d; j++) {
            centroids[i][j]=sums[i][j] / size[i];
          }
        }
      }
      if (distortion <= newDistortion) {
        break;
      }
 else {
        distortion=newDistortion;
      }
    }
    logger.info(String.format("String_Node_Str",k,distortion));
  }
}

Fixed version (ground truth):
/** 
 * Constructor. Clustering data with the number of clusters being automatically determined by G-Means algorithm.
 * @param data the input data of which each row is a sample.
 * @param kmax the maximum number of clusters.
 */
public GMeans(double[][] data,int kmax){
  if (kmax < 2) {
    throw new IllegalArgumentException("String_Node_Str" + kmax);
  }
  int n=data.length;
  int d=data[0].length;
  k=1;
  size=new int[k];
  size[0]=n;
  y=new int[n];
  centroids=new double[k][d];
  for (int i=0; i < n; i++) {
    for (int j=0; j < d; j++) {
      centroids[0][j]+=data[i][j];
    }
  }
  for (int j=0; j < d; j++) {
    centroids[0][j]/=n;
  }
  distortion=0.0;
  for (int i=0; i < n; i++) {
    distortion+=Math.squaredDistance(data[i],centroids[0]);
  }
  logger.info(String.format("String_Node_Str",k,distortion));
  BBDTree bbd=new BBDTree(data);
  while (k < kmax) {
    ArrayList<double[]> centers=new ArrayList<>();
    double[] score=new double[k];
    KMeans[] kmeans=new KMeans[k];
    for (int i=0; i < k; i++) {
      if (size[i] < 25) {
        logger.info("String_Node_Str",i,size[i]);
        continue;
      }
      double[][] subset=new double[size[i]][];
      for (int j=0, l=0; j < n; j++) {
        if (y[j] == i) {
          subset[l++]=data[j];
        }
      }
      kmeans[i]=new KMeans(subset,2,100,4);
      double[] v=new double[d];
      for (int j=0; j < d; j++) {
        v[j]=kmeans[i].centroids[0][j] - kmeans[i].centroids[1][j];
      }
      double vp=Math.dot(v,v);
      double[] x=new double[size[i]];
      for (int j=0; j < x.length; j++) {
        x[j]=Math.dot(subset[j],v) / vp;
      }
      Math.standardize(x);
      score[i]=AndersonDarling(x);
      logger.info(String.format("String_Node_Str",i,score[i]));
    }
    int[] index=QuickSort.sort(score);
    for (int i=0; i < k; i++) {
      if (score[i] <= 1.8692) {
        centers.add(centroids[index[i]]);
      }
    }
    int m=centers.size();
    for (int i=k; --i >= 0; ) {
      if (score[i] > 1.8692) {
        if (centers.size() + i - m + 1 < kmax) {
          logger.info("String_Node_Str",index[i]);
          centers.add(kmeans[index[i]].centroids[0]);
          centers.add(kmeans[index[i]].centroids[1]);
        }
 else {
          centers.add(centroids[index[i]]);
        }
      }
    }
    if (centers.size() == k) {
      break;
    }
    k=centers.size();
    double[][] sums=new double[k][d];
    size=new int[k];
    centroids=new double[k][];
    for (int i=0; i < k; i++) {
      centroids[i]=centers.get(i);
    }
    distortion=Double.MAX_VALUE;
    for (int iter=0; iter < 100; iter++) {
      double newDistortion=bbd.clustering(centroids,sums,size,y);
      for (int i=0; i < k; i++) {
        if (size[i] > 0) {
          for (int j=0; j < d; j++) {
            centroids[i][j]=sums[i][j] / size[i];
          }
        }
      }
      if (distortion <= newDistortion) {
        break;
      }
 else {
        distortion=newDistortion;
      }
    }
    logger.info(String.format("String_Node_Str",k,distortion));
  }
}