⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wkkm.c_bak

📁 多层权核k均值算法
💻 C_BAK
📖 第 1 页 / 共 2 页
字号:
  for (ii=0; ii<nbnd; ii++){    i = bndind[ii];    for (j=xadj[i]; j<xadj[i+1]; j++)      //kDist[i][where[adjncy[j]]] += 1.0*adjwgt[j]/w[i];      kDist[ii][where[adjncy[j]]] += 1.0*adjwgt[j]/w[i];  }  for (k=0; k<nparts; k++)    if (sum[k] >0)      //for (i=0; i<nvtxs; i++)      for (ii=0; ii<nbnd; ii++)    	//kDist[i][k] = squared_sum[k]/(1.0*sum[k]*sum[k]) - 2*kDist[i][k]/sum[k];	kDist[ii][k] = squared_sum[k]/(1.0*sum[k]*sum[k]) - 2*kDist[ii][k]/sum[k];    for (i=0; i<nparts; i++)    if (sum[i] >0)      obj +=  squared_sum[i]*1.0/sum[i];    for (i=0; i<chain_length; i++)    {      float tempMinChange, tempchange, temp_q;      int tempid, tempMoveTo, from, to, tempbndind;      tempMinChange = obj;      tempchange =0;      tempid =0;      tempMoveTo = where[tempid];      tempbndind =0;            //for (j=0; j<nvtxs; j++)      for (ii=0; ii<nbnd; ii++){		j = bndind[ii];        if (mark[ii] ==0){	  me = where[j];	  if (sum[me] > w[j]) // if this cluster where j belongs is not a singleton	    for (k=0; k<nparts; k++)	      if (k != me){		//tempchange = -kDist[j][me]*sum[me]*w[j]/(sum[me]-w[j]) + kDist[j][k]*sum[k]*w[j]/(sum[k]+w[j]);		tempchange = -kDist[ii][me]*sum[me]*w[j]/(sum[me]-w[j]) + kDist[ii][k]*sum[k]*w[j]/(sum[k]+w[j]);		if (tempchange < tempMinChange){		  tempMinChange = tempchange;		  tempid = j;		  tempbndind = ii;		  tempMoveTo = k;		}	      }	}      }      if ((tempMoveTo == where[tempid]) || (mark[tempbndind] >0)){        actual_length = i;	break;      }      else{        // record which point is moved from its original cluster to new cluster        chain[i].point = tempid;        chain[i].from = where[tempid];        chain[i].to = tempMoveTo;        chain[i].change = tempMinChange;	//mark the point moved	mark[tempbndind] = 1;	// update info        accum_change[i+1] = accum_change[i] + tempMinChange;	from = chain[i].from;	to = chain[i].to;	where[tempid] = to;        sum[from] -=  w[tempid];        sum[to] +=  w[tempid];	        for (j=xadj[tempid]; j<xadj[tempid+1]; j++) 	  if (where[adjncy[j]] == from)	    squared_sum[from] -= 2*adjwgt[j];	//for (s=0; s<nvtxs; s++){	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++){	/*linearTerm[ii][me] -= w[s];	  linearTerm[ii][minchange_id] += w[s];		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;}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 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++);   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);            break;    }    else{      pingpong(ctrl, graph, nparts, 0, tpwgts, ubfactor);      //chain_length /= 2;    }            //pingpong(ctrl, graph, nparts, chain_length, tpwgts, ubfactor);        /* for time and quality each level         stoptimer(tmr);    printf("\nLevel %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)", 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 + -