#include #include #include #include "sources/utils/boolean.h" #include "sources/kmeansClustering.h" // auxiliary function to obtain a random sample of 1..n with K elements void sample(int* centers, int n, int K) { // refVect = (0,1,...,n-1,n) int* refVect = (int*)malloc(n*sizeof(int)); for (int i=0; i 1 && ctrs2[0]==0 && ctrs2[1]==0) return S_TRUE; // compVect[i] == 1 iff index i is found in ctrs1 or ctrs2 int* compVect = (int*)calloc(n,sizeof(int)); int kountNonZero = 0; // count non-zero elements in compVect for (int j=0; j K); } // assign a vector (represented by its distances to others, as distances[index,]) // to a cluster, represented by its center ==> output is integer in 0..K-1 int assignCluster(int index, double* distances, int* centers, int n, int K) { int minIndex = 0; double minDist = distances[index*n+centers[0]]; for (int j=1; j= 0) ctrs[j] = minIndex; // HACK: some 'random' index (a cluster should never be empty) // this case should never happen anyway // (pathological dataset with replicates) else ctrs[j] = 0; vectorI_destroy(iter1); } } /***** end main loop *****/ // finally compute distorsions : double distor = 0.0; for (int j=0; j