int Cluster::cluster(const Points &points, int clusterNum, double mergeLength, vector<Points> &clusteredPts, Points ¢ers) { kMeans(points, clusterNum, clusteredPts); getClusterCenters(clusteredPts, centers); return mergeClusters(mergeLength, centers, clusteredPts); }
static void rBoxJoin(struct boxRef *refList, int qStart, int qEnd, int tStart, int tEnd) /* Recursively cluster boxes. */ { int boxCount = slCount(refList); if (boxCount <= 1) { /* Easy: no merging required. */ } else if (boxCount == 2) { /* Decide if pair overlaps and if so merge. */ struct box *a = refList->box; struct box *b = refList->next->box; if (rangeIntersection(a->in->qStart, a->in->qEnd, b->in->qStart, b->in->qEnd) > 0 && rangeIntersection(a->in->tStart, a->in->tEnd, b->in->tStart, b->in->tEnd) > 0 ) { mergeClusters(a->cluster, b->cluster); } else { /* Two non-overlapping boxes, we don't have to do anything. */ } } else if (allStartBy(refList, qStart, tStart)) { /* If everybody contains the upper left corner, then they all can * be merged. This is the route taken often by clumps with lots * of overlap. */ struct cluster *aCluster = refList->box->cluster; struct boxRef *ref; for (ref = refList->next; ref != NULL; ref = ref->next) { struct cluster *bCluster = ref->box->cluster; mergeClusters(aCluster, bCluster); } } else if (allSameCluster(refList)) { /* Everything is in the same cluster, no action required. */ } else { /* We can't yet figure out clumping, so break * up our window in two along larger dimension and * recurse on both subwindows. */ struct boxRef *list1 = NULL, *list2 = NULL, *ref, *next; if (qEnd - qStart > tEnd - tStart) { int mid = (qStart + qEnd)>>1; for (ref = refList; ref != NULL; ref = next) { struct box *box = ref->box; next = ref->next; if (box->in->qEnd <= mid) { slAddHead(&list1, ref); } else if (box->in->qStart >= mid) { slAddHead(&list2, ref); } else { /* Box crosses boundary, have to put it on both lists. */ slAddHead(&list1, ref); lmAllocVar(lm, ref); ref->box = box; slAddHead(&list2, ref); } } rBoxJoin(list1, qStart, mid, tStart, tEnd); rBoxJoin(list2, mid, qEnd, tStart, tEnd); } else {
int main() { FILE *fp; int nVertices = 0,nBits = 0, nEdges = 0,count = 0,maxSpace = 0; Vertex *vertices; int c,sum=0; fp = fopen("clustering_big.txt","r"); if(fp == NULL) { printf("%d\n"," Error opening file"); return 0; } fscanf(fp,"%d",&nVertices); fscanf(fp,"%d",&nBits); vertices = (Vertex *)malloc((nVertices+1)*sizeof(struct vertice)); for(int i = 1; i <= nVertices; i++) { vertices[i].node = i; vertices[i].bits = (int *)malloc(nBits*sizeof(int)); } for(int i = 1; i <= nVertices; i++) { for(int j = 0; j < nBits; j ++) { fscanf(fp,"%d",&c); vertices[i].bits[j] = c; } } fclose(fp); nClusters = nVertices; clusters = (int *)malloc(sizeof(int)*(nVertices+1)); size = (int *)malloc(sizeof(int)*(nVertices+1)); for(int i = 1; i <= nVertices; i++) { clusters[i] = i; size[i] = 1; } for(int i = 1; i <= nVertices; i++) { for(int j = i+1; j <=nVertices; j++) { for(int k = 0; k < nBits; k++) { if(vertices[i].bits[k] != vertices[j].bits[k]) sum++; } if(sum <= 2) { if(!isConnected(i,j)) { mergeClusters(i,j); } } sum = 0; } } printf("%d\n",nClusters); getchar(); return 0; }