📄 wkkm.c
字号:
for (ii=0; ii<nbnd; ii++){ //kDist[s][from] = 0; kDist[ii][from] = 0; s = bndind[ii]; for (j=xadj[s]; j<xadj[s+1]; j++) if (where[adjncy[j]] == from) //kDist[s][from] += adjwgt[j]*1.0/w[s]; kDist[ii][from] += adjwgt[j]*1.0/w[s]; } temp_q = squared_sum[from]/(1.0*sum[from]*sum[from]); //for (s=0; s<nvtxs; s++) for (ii=0; ii<nbnd; ii++) kDist[ii][from] = temp_q - 2*kDist[ii][from]/sum[from]; for (j=xadj[tempid]; j<xadj[tempid+1]; j++) if (where[adjncy[j]] == to) squared_sum[to] += 2*adjwgt[j]; //for (s=0; s<nvtxs; s++){ for (ii=0; ii<nbnd; ii++){ //kDist[s][to] = 0; kDist[ii][to] = 0; s = bndind[ii]; for (j=xadj[s]; j<xadj[s+1]; j++) if (where[adjncy[j]] == to) //kDist[s][to] += adjwgt[j]*1.0/w[s]; kDist[ii][to] += adjwgt[j]*1.0/w[s]; } temp_q = squared_sum[to]/(1.0*sum[to]*sum[to]); //for (s=0; s<nvtxs; s++) for (ii=0; ii<nbnd; ii++) //kDist[s][to] = temp_q - 2*kDist[s][to]/sum[to]; kDist[ii][to] = temp_q - 2*kDist[ii][to]/sum[to]; } } fidx = samin(actual_length, accum_change); if (accum_change[fidx] < -epsilon * obj){ for (i= fidx+1; i<=actual_length; i++) where[chain[i-1].point] = chain[i-1].from; moves = fidx; change = accum_change[fidx]; } else{ for (i= 0; i<actual_length; i++) where[chain[i].point] = chain[i].from; moves = 0; change = 0; } free(sum); free(squared_sum);free(accum_change); free(chain); free(mark); //for (i= 0; i<nvtxs; i++) for (i= 0; i<nbnd; i++) free(kDist[i]); free(kDist); return moves;}*/float onePoint_move(GraphType *graph, int nparts, idxtype *sum, idxtype *squared_sum, idxtype *w, idxtype *self_sim, int **linearTerm, int ii){ int k, j, s, i, nbnd, temp, q1, q2, minchange_id, new_squared_sum1, new_squared_sum2, me, nedges, nvtxs; float tempchange, minchange, obj, cut; idxtype *xadj, *adjncy, *adjwgt, *where, *bndptr, *bndind; nedges = graph->nedges; nvtxs = graph->nvtxs; xadj = graph->xadj; adjncy = graph->adjncy; adjwgt = graph->adjwgt; where = graph->where; nbnd = graph->nbnd; bndind = graph->bndind; bndptr = graph->bndptr; s = bndind[ii]; me= where[s]; minchange_id = me; minchange = 0; new_squared_sum1= new_squared_sum2= squared_sum[me]; if (sum[me] > w[s]){ // if this cluster where j belongs is not a singleton float inv_sum1 = 1.0/sum[me], inv_sum2 = 1.0/(sum[me]-w[s]); for(k= 0; k<me; k++){ q1 = squared_sum[me] - linearTerm[ii][me]*2 + self_sim[ii]; q2 = squared_sum[k] + linearTerm[ii][k] *2 + self_sim[ii]; if(sum[k] >0) tempchange = squared_sum[me]*inv_sum1 + squared_sum[k]*1.0/sum[k] - q1*inv_sum2- q2*1.0/(sum[k]+w[s]); else if(w[s]>0) // if sum[k] ==0 but w[s] >0 tempchange = squared_sum[me]*inv_sum1 - q1*inv_sum2 - q2*1.0/(sum[k]+w[s]); else // if sum[k] == 0 and w[s] ==0 tempchange = squared_sum[me]*inv_sum1 - q1*inv_sum2; if(tempchange < minchange){ minchange = tempchange; minchange_id = k; new_squared_sum1 = q1; new_squared_sum2 = q2; } } for(k= me+1; k<nparts; k++){ q1 = squared_sum[me] - linearTerm[ii][me]*2 +self_sim[ii]; q2 = squared_sum[k] + linearTerm[ii][k]*2 + self_sim[ii]; if(sum[k] >0) tempchange = squared_sum[me]*inv_sum1 + squared_sum[k]*1.0/sum[k] - q1*inv_sum2- q2*1.0/(sum[k]+w[s]); else if(w[s]>0) // if sum[k] ==0 but w[s] >0 tempchange = squared_sum[me]*inv_sum1 - q1*inv_sum2 - q2*1.0/(sum[k]+w[s]); else // if sum[k] == 0 and w[s] ==0 tempchange = squared_sum[me]*inv_sum1 - q1*inv_sum2; if(tempchange < minchange){ minchange = tempchange; minchange_id = k; new_squared_sum1 = q1; new_squared_sum2 = q2; } } if (minchange < 0){ where[s] = minchange_id; sum[me] -= w[s]; sum[minchange_id] += w[s]; /* for (ii = 0; ii<nbnd; ii++) for (j = 0; j<nparts; j++) linearTerm[ii][j] = 0; */ for (j=xadj[s]; j<xadj[s+1]; j++){ int boundary, adj_temp; adj_temp = adjncy[j]; boundary = bndptr[adj_temp]; if(boundary >-1) { //if (where[adj_temp] == me){ //linearTerm[ii][me] -= adjwgt[j]; linearTerm[boundary][me] -= adjwgt[j]; //} //if (where[adj_temp] == minchange_id){ //linearTerm[ii][minchange_id] += adjwgt[j]; linearTerm[boundary][minchange_id] += adjwgt[j]; //} } } /* for (ii =0; ii<nbnd; ii++){ i = bndind[ii]; for (j=xadj[i]; j<xadj[i+1]; j++) linearTerm[ii][where[adjncy[j]]] += adjwgt[j]; } */ squared_sum[me] = new_squared_sum1; squared_sum[minchange_id] = new_squared_sum2; } } return minchange;}void move1Point2EmptyCluster(GraphType *graph, int nparts, idxtype *sum, idxtype *squared_sum, idxtype *w, idxtype *self_sim, int **linearTerm, int k){ int j, s, ii, nbnd, q1, q2, minchange_id, new_squared_sum1, new_squared_sum2, me, nvtxs; float tempchange, minchange; idxtype *xadj, *adjncy, *adjwgt, *where, *bndptr, *bndind; nvtxs = graph->nvtxs; xadj = graph->xadj; adjncy = graph->adjncy; adjwgt = graph->adjwgt; where = graph->where; nbnd = graph->nbnd; bndind = graph->bndind; bndptr = graph->bndptr; minchange_id = bndind[0]; minchange = FLT_MAX; for(ii=0; ii<nbnd; ii++){ s = bndind[ii]; me= where[s]; new_squared_sum1= new_squared_sum2= squared_sum[me]; if (sum[me] > w[s]){ // if this cluster where j belongs is not a singleton float inv_sum1 = 1.0/sum[me], inv_sum2 = 1.0/(sum[me]-w[s]); q1 = squared_sum[me] - linearTerm[ii][me]*2 + self_sim[ii]; q2 = squared_sum[k] + linearTerm[ii][k] *2 + self_sim[ii]; if(w[s]>0) // note that sum[k] ==0; if w[s] >0 tempchange = squared_sum[me]*inv_sum1 - q1*inv_sum2 - q2*1.0/(sum[k]+w[s]); else // if sum[k] == 0 and w[s] ==0 tempchange = squared_sum[me]*inv_sum1 - q1*inv_sum2; if(tempchange < minchange){ minchange = tempchange; minchange_id = s; new_squared_sum1 = q1; new_squared_sum2 = q2; } } } where[minchange_id] = k; sum[me] -= w[minchange_id]; sum[k] += w[minchange_id]; for (j=xadj[minchange_id]; j<xadj[minchange_id+1]; j++){ int boundary, adj_temp; adj_temp = adjncy[j]; boundary = bndptr[adj_temp]; if(boundary >-1) { linearTerm[boundary][me] -= adjwgt[j]; linearTerm[boundary][k] += adjwgt[j]; } squared_sum[me] = new_squared_sum1; squared_sum[k] = new_squared_sum2; }}int local_search(CtrlType *ctrl, GraphType *graph, int nparts, int chain_length, idxtype *w, float *tpwgts, float ubfactor) //return # of points moved{ int nvtxs, nedges, nbnd, me, i, j, k, s, ii; idxtype *sum, *squared_sum, *xadj, *adjncy, *adjwgt, *where, *bndptr, *bndind, *self_sim; float change, obj, epsilon, accum_change, minchange; int moves, loopTimes, **linearTerm; nedges = graph->nedges; nvtxs = graph->nvtxs; xadj = graph->xadj; adjncy = graph->adjncy; adjwgt = graph->adjwgt; where = graph->where; nbnd = graph->nbnd; bndind = graph->bndind; bndptr = graph->bndptr; sum = idxsmalloc(nparts,0, "Local_search: weight sum"); squared_sum = idxsmalloc(nparts,0,"Local_search: weight squared sum"); self_sim = idxsmalloc(nbnd, 0, "Local_search: self similarity"); linearTerm = i2malloc(nbnd, nparts, "Local_search: linear term"); moves = 0; epsilon =.001; for (i=0; i<nvtxs; i++) sum[where[i]] += w[i]; for (i=0; i<nvtxs; i++){ me = where[i]; for (j=xadj[i]; j<xadj[i+1]; j++) if (where[adjncy[j]] == me) squared_sum[me] += adjwgt[j]; } for (ii = 0; ii<nbnd; ii++) for (j = 0; j<nparts; j++) linearTerm[ii][j] = 0; for (ii =0; ii<nbnd; ii++){ s = bndind[ii]; for (j=xadj[s]; j<xadj[s+1]; j++){ //kDist[i][where[adjncy[j]]] += 1.0*adjwgt[j]/w[i]; linearTerm[ii][where[adjncy[j]]] += adjwgt[j]; if (adjncy[j] == s) self_sim[ii] = adjwgt[j]; } } //the diagonal entries won't affect the result so diagonal's assumed zero obj = 0; for (i=0; i<nparts; i++) if (sum[i] >0) obj += squared_sum[i]*1.0/sum[i]; srand(time(NULL)); //temperature = DEFAULT_TEMP; loopTimes = 0; while (loopTimes < chain_length){ accum_change =0; //for (j=0; j<nvtxs; j++) for (ii=0; ii<nbnd; ii++){ minchange = onePoint_move(graph, nparts, sum, squared_sum, w, self_sim, linearTerm, ii); accum_change += minchange; } if (accum_change > -epsilon * obj){ break; } moves ++; loopTimes ++; } free(sum); free(squared_sum); free(self_sim); //for (i= 0; i<nvtxs; i++) for (i= 0; i<nbnd; i++) free(linearTerm[i]); free(linearTerm); //printf("moves = %d\n", moves); return moves;}void remove_empty_clusters_l1(CtrlType *ctrl, GraphType *graph, int nparts, idxtype *w, float *tpwgts, float ubfactor){ int *clustersize=imalloc(nparts, "remove_empty_clusters: clusterSize"); int number_of_empty_cluster=0, i, s; for(i=0; i<nparts; i++) clustersize[i] =0; clusterSize(graph, clustersize); for(i=0; i<nparts; i++) if(clustersize[i] ==0) number_of_empty_cluster ++; if(number_of_empty_cluster>0) local_search(ctrl, graph, nparts, 1, w, tpwgts, ubfactor); free(clustersize);}void remove_empty_clusters_l2(CtrlType *ctrl, GraphType *graph, int nparts, idxtype *w, float *tpwgts, float ubfactor){ int *clustersize=imalloc(nparts, "remove_empty_clusters: clusterSize"); int number_of_empty_cluster=0, i, s; for(i=0; i<nparts; i++) clustersize[i] =0; clusterSize(graph, clustersize); for(i=0; i<nparts; i++) if(clustersize[i] ==0) number_of_empty_cluster ++; //printf("%d empty clusters; ", number_of_empty_cluster); if(number_of_empty_cluster>0){ int nvtxs, me, j, k, ii; idxtype *sum, *squared_sum, *xadj, *adjncy, *adjwgt, *where, *bndptr, *bndind, *self_sim, nbnd; int **linearTerm; nvtxs = graph->nvtxs; xadj = graph->xadj; adjncy = graph->adjncy; adjwgt = graph->adjwgt; where = graph->where; nbnd = graph->nbnd; bndind = graph->bndind; bndptr = graph->bndptr; sum = idxsmalloc(nparts,0, "Local_search: weight sum"); squared_sum = idxsmalloc(nparts,0,"Local_search: weight squared sum"); self_sim = idxsmalloc(nbnd, 0, "Local_search: self similarity"); linearTerm = i2malloc(nbnd, nparts, "Local_search: linear term"); for (i=0; i<nvtxs; i++) sum[where[i]] += w[i]; for (i=0; i<nvtxs; i++){ me = where[i]; for (j=xadj[i]; j<xadj[i+1]; j++) if (where[adjncy[j]] == me) squared_sum[me] += adjwgt[j]; } for (ii = 0; ii<nbnd; ii++) for (j = 0; j<nparts; j++) linearTerm[ii][j] = 0; for (ii =0; ii<nbnd; ii++){ s = bndind[ii]; for (j=xadj[s]; j<xadj[s+1]; j++){ linearTerm[ii][where[adjncy[j]]] += adjwgt[j]; if (adjncy[j] == s) self_sim[ii] = adjwgt[j]; } } for(k=0; k<nparts; k++) if(clustersize[k] ==0){ move1Point2EmptyCluster(graph, nparts, sum, squared_sum, w, self_sim, linearTerm, k); } free(sum); free(squared_sum); free(self_sim); //for (i= 0; i<nvtxs; i++) for (i= 0; i<nbnd; i++) free(linearTerm[i]); free(linearTerm); } /* for(i=0; i<nparts; i++) clustersize[i] =0; number_of_empty_cluster=0; clusterSize(graph, clustersize); for(i=0; i<nparts; i++) if(clustersize[i] ==0) number_of_empty_cluster ++; printf("%d empty clusters\n", number_of_empty_cluster); */ free(clustersize);}void MLKKMRefine(CtrlType *ctrl, GraphType *orggraph, GraphType *graph, int nparts, int chain_length, float *tpwgts, float ubfactor){ int i, nlevels, mustfree=0, temp_cl; GraphType *ptr; IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->UncoarsenTmr)); /* Compute the parameters of the coarsest graph */ ComputeKWayPartitionParams(ctrl, graph, nparts); temp_cl = chain_length; /* Determine how many levels are there */ for (ptr=graph, nlevels=0; ptr!=orggraph; ptr=ptr->finer, nlevels++); //printf("Number of levels is %d\n", nlevels); for (i=0; ;i++) { timer tmr; float result; cleartimer(tmr); starttimer(tmr); //pingpong(ctrl, graph, nparts, chain_length, tpwgts, ubfactor); //chain_length /= 1.5; //printf("Level: %d\n", i+1); if (graph == orggraph){ //chain_length = chain_length>0 ? chain_length : 1; pingpong(ctrl, graph, nparts, chain_length, tpwgts, ubfactor, 1); break; } else{ //pingpong(ctrl, graph, nparts, 0, tpwgts, ubfactor, 0); pingpong(ctrl, graph, nparts, chain_length, tpwgts, ubfactor, 0); //chain_length /= 2; } //pingpong(ctrl, graph, nparts, chain_length, tpwgts, ubfactor); // /* for time and quality each level stoptimer(tmr); //printf("Level %d: %7.3f", i+1, tmr); if (cutType == NCUT){ result = ComputeNCut(graph, graph->where, nparts); //printf(" %7f", result); } else{ result = ComputeRAsso(graph, graph->where, nparts); //printf(" %7f", result); } //printf(" (%d)\n\n", graph->nvtxs); //ends here*/ if (graph == orggraph) break; /* if(i>1) chain_length /= 10; */ GKfree((void **) &graph->gdata, LTERM); /* Deallocate the graph related arrays */ graph = graph->finer; IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->ProjectTmr)); if (graph->vwgt == NULL) { graph->vwgt = idxsmalloc(graph->nvtxs, 1, "RefineKWay: graph->vwgt"); graph->adjwgt = idxsmalloc(graph->nedges, 1, "RefineKWay: graph->adjwgt"); mustfree = 1; } ProjectKWayPartition(ctrl, graph, nparts); IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->ProjectTmr)); } if (mustfree) GKfree((void **) &graph->vwgt, (void **) &graph->adjwgt, LTERM); IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->UncoarsenTmr));}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -