コード例 #1
0
ファイル: wkkm.cpp プロジェクト: BB90/CommunityDetectionCodes
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, loopend;

  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;
    
    if(boundary_points == 1)
      loopend = nbnd;
    else
      loopend = nvtxs;

    sum = idxsmalloc(nparts,0, "Local_search: weight sum");
    squared_sum = idxsmalloc(nparts,0,"Local_search: weight squared sum");
    self_sim = idxsmalloc(loopend, 0, "Local_search: self similarity");
    linearTerm = i2malloc(loopend, 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<loopend; ii++)
      for (j = 0; j<nparts; j++)
	linearTerm[ii][j] = 0;
    for (ii =0; ii<loopend; ii++){
      if (boundary_points == 1)
        s = bndind[ii];
      else
	s = 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<loopend; 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);
}
コード例 #2
0
ファイル: wkkm_boundary_only.c プロジェクト: colmap/colmap
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;
}