将集群定义为循环应用的数组中的 2 个或更多元素,我试图从数组中获取 3 件事:
最大簇大小、阵列中的簇数和平均簇大小。示例如下所示:
1 1 0 0 1 10 1 1 1
这里将有两个簇,最大的簇将是 5 个元素,平均大小为 3.5(或 3(
这就是我所拥有的
private static boolean isCluster(int i) {
if (table[i] == 1 && table[i + 1] == 1)
return true;
return false;
}
public static void setClusters() {
ArrayList<Integer> clusterSizes = new ArrayList<>();
boolean continuousCluster = false;
for (int i = 0; i < table.length - 1; i++) {
if (isCluster(i)) {
clusterSize++;
continuousCluster = true;
} else if (continuousCluster && table[i + 1] == 0) { // cluster is broken
numberofClusters++;
clusterSize++;
if (clusterSize > largestCluster) largestCluster = clusterSize;
clusterSizes.add(clusterSize);
clusterSize = 0;
continuousCluster = false;
}
if(isCluster(table.length-2)){
numberofClusters++;
}
}
if (table[0] == 1 && table[table.length - 1] == 1) {
numberofClusters--;
clusterSizes.set(0, (clusterSizes.get(0) + clusterSizes.get(clusterSizes.size() - 1)));
}
for (int i = 0; i < clusterSizes.size(); i++) {
averageClusterSize += clusterSizes.get(i);
}
if(numberofClusters != 0) {
averageClusterSize /= numberofClusters;
}
else{
averageClusterSize = 0;
}
}
我所拥有的工作正常,直到如果集群是圆形的,它应该添加值。
您可以确定是否存在这样的所谓循环集群:
if (table[0] == 1 && table[table.length - 1] == 1) {
//there is a circular cluster
//do your calcs (count the ones from the beggining and from the end, etc...
}
我认为你能够完成其余的逻辑...