30 for (i=0; i<nvtxs; i++)
34 for (i=0; i<nvtxs; i++)
35 for (j=xadj[i]; j<xadj[i+1]; j++)
38 for (i=0; i<nvtxs; i++)
39 for (j=xadj[i]; j<xadj[i+1]; j++)
47 idxtype *xadj, *adjncy, *adjwgt, *where;
56 for (i=0; i<nvtxs; i++)
57 for (j=xadj[i]; j<xadj[i+1]; j++)
60 for (i=0; i<nvtxs; i++)
61 for (j=xadj[i]; j<xadj[i+1]; j++)
62 m_adjwgt[j] = adjwgt[j];
66 for (i=0; i<nvtxs; i++)
67 for (j=xadj[i]; j<xadj[i+1]; j++)
69 m_adjwgt[j] = 1.0/w[i];
70 for (i=0; i<nvtxs; i++)
71 for (j=xadj[i]; j<xadj[i+1]; j++)
73 m_adjwgt[j] /= w[adjncy[j]];
76 for (i=0; i<nvtxs; i++)
77 for (j=xadj[i]; j<xadj[i+1]; j++)
79 m_adjwgt[j] = adjwgt[j] *1.0/w[i];
82 for (i=0; i<nvtxs; i++)
83 for (j=xadj[i]; j<xadj[i+1]; j++)
85 m_adjwgt[j] /= w[adjncy[j]];
94 idxtype *xadj, *adjncy, *adjwgt, *where;
103 for (i=0; i<nvtxs; i++)
104 for (j=xadj[i]; j<xadj[i+1]; j++)
107 for (i=0; i<nvtxs; i++)
108 for (j=xadj[i]; j<xadj[i+1]; j++)
109 m_adjwgt[j] = adjwgt[j];
113 for (i=0; i<nvtxs; i++)
114 for (j=xadj[i]; j<xadj[i+1]; j++)
116 m_adjwgt[j] = 1.0/sqrt(w[i]);
117 for (i=0; i<nvtxs; i++)
118 for (j=xadj[i]; j<xadj[i+1]; j++)
120 m_adjwgt[j] /= sqrt(w[adjncy[j]]);
123 for (i=0; i<nvtxs; i++)
124 for (j=xadj[i]; j<xadj[i+1]; j++)
126 m_adjwgt[j] = adjwgt[j] *1.0/sqrt(w[i]);
129 for (i=0; i<nvtxs; i++)
130 for (j=xadj[i]; j<xadj[i+1]; j++)
132 m_adjwgt[j] /= sqrt(w[adjncy[j]]);
142 int nvtxs, nedges, moves, iter;
147 nvtxs = graph->
nvtxs;
151 m_adjwgt =
fmalloc(nedges,
"pingpong: normalized matrix");
160 moves =
local_search(ctrl, graph, nparts, chain_length, w, m_adjwgt, tpwgts, ubfactor);
166 free(w); free(m_adjwgt);
174 int nvtxs, nedges, me, i, j;
175 idxtype *xadj, *adjncy, *adjwgt, *where, *new_where;
176 float *sum, *squared_sum, obj, old_obj, epsilon;
180 nvtxs = graph->
nvtxs;
184 where = graph->
where;
188 new_where =
imalloc(nvtxs,
"Weighted_kernel_k_means: new_where");
189 for (i=0; i<nvtxs; i++)
190 new_where[i] = where[i];
192 sum =
fmalloc(nparts,
"Weighted_kernel_k_means: weight sum");
193 squared_sum =
fmalloc(nparts,
"Weighted_kernel_k_means: weight squared sum");
196 for (i=0; i<nparts; i++)
197 sum[i] = squared_sum[i] =0;
200 for (i=0; i<nvtxs; i++)
201 sum[where[i]] += w[i];
202 for (i=0; i<nvtxs; i++){
204 for (j=xadj[i]; j<xadj[i+1]; j++)
205 if (where[adjncy[j]] == me)
206 squared_sum[me] += w[i]*w[adjncy[j]]*m_adjwgt[j];
210 for (i=0; i<nparts; i++)
212 obj += squared_sum[i]/sum[i];
222 float dist, temp, min_dist;
223 int min_ind, *temp_where, k;
230 for (i=0; i<nvtxs; i++){
232 min_ind = me = where[i];
236 for (j=xadj[i]; j<xadj[i+1]; j++)
237 if (where[adjncy[j]] == me)
238 temp +=w[adjncy[j]]*m_adjwgt[j];
239 min_dist = squared_sum[me]/(sum[me]*sum[me])-2*temp/sum[me];
243 for (k=0; k<me; k++){
247 for (j=xadj[i]; j<xadj[i+1]; j++)
248 if (where[adjncy[j]] == k)
249 temp +=w[adjncy[j]]*m_adjwgt[j];
250 dist = squared_sum[k]/(sum[k]*sum[k])-2*temp/sum[k];
258 for (k=me+1; k<nparts; k++){
262 for (j=xadj[i]; j<xadj[i+1]; j++)
263 if (where[adjncy[j]] == k)
264 temp +=w[adjncy[j]]*m_adjwgt[j];
265 dist = squared_sum[k]/(sum[k]*sum[k])-2*temp/sum[k];
274 new_where[i] = min_ind;
280 for (i=0; i<nparts; i++)
281 sum[i] = squared_sum[i] =0;
282 for (i=0; i<nvtxs; i++)
283 sum[new_where[i]] += w[i];
284 for (i=0; i<nvtxs; i++){
286 for (j=xadj[i]; j<xadj[i+1]; j++)
287 if (new_where[adjncy[j]] == me)
288 squared_sum[me] += w[i]*w[adjncy[j]]*m_adjwgt[j];
293 for (i=0; i<nparts; i++)
295 obj += squared_sum[i]/sum[i];
298 for (i=0; i<nvtxs; i++)
299 where[i] = new_where[i];
301 }
while((obj - old_obj)> epsilon*obj);
308 free(sum); free(squared_sum); free(new_where);
315 int nvtxs, nedges, me, i, j, k, s;
316 idxtype *xadj, *adjncy, *adjwgt, *where;
317 float change, *sum, *squared_sum, obj, epsilon, **kDist, *accum_change;
318 int moves, actual_length, *mark, fidx;
322 nvtxs = graph->
nvtxs;
326 where = graph->
where;
328 chain =
chainmalloc(chain_length,
"Local_search: local search chain");
329 mark =
ismalloc(nvtxs, 0 ,
"Local_search: mark");
330 sum =
fmalloc(nparts,
"Local_search: weight sum");
331 squared_sum =
fmalloc(nparts,
"Local_search: weight squared sum");
332 kDist =
f2malloc(nvtxs, nparts,
"Local_search: distance matrix");
333 accum_change =
fmalloc(chain_length+1,
"Local_search: accumulated change");
336 for (i = 0; i<nparts; i++)
337 sum[i] = squared_sum[i] = 0;
338 for (i = 0; i<nvtxs; i++)
339 for (j = 0; j<nparts; j++)
341 for (i = 0; i<chain_length+1; i++)
346 actual_length = chain_length;
348 for (i=0; i<nvtxs; i++)
349 sum[where[i]] += w[i];
350 for (i=0; i<nvtxs; i++){
352 for (j=xadj[i]; j<xadj[i+1]; j++)
353 if (where[adjncy[j]] == me)
354 squared_sum[me] += w[i]*w[adjncy[j]]*m_adjwgt[j];
358 for (i=0; i<nvtxs; i++)
359 for (j=xadj[i]; j<xadj[i+1]; j++)
360 kDist[i][where[adjncy[j]]] +=w[adjncy[j]]*m_adjwgt[j];
361 for (k=0; k<nparts; k++)
363 for (i=0; i<nvtxs; i++)
364 kDist[i][k] = squared_sum[k]/(sum[k]*sum[k]) - 2*kDist[i][k]/sum[k];
366 for (i=0; i<nparts; i++)
368 obj += squared_sum[i]/sum[i];
370 for (i=0; i<chain_length; i++)
372 float tempMinChange, tempchange, temp_q;
373 int tempid, tempMoveTo, from, to;
378 tempMoveTo = where[tempid];
380 for (j=0; j<nvtxs; j++)
384 for (k=0; k<nparts; k++)
386 tempchange = -sum[me]*w[j]/(sum[me]-w[j])*kDist[j][me]+sum[k]*w[j]/(sum[k]+w[j])*kDist[j][k];
387 if (tempchange < tempMinChange){
388 tempMinChange = tempchange;
395 if ((tempMoveTo == where[tempid]) || (mark[tempid] >0)){
401 chain[i].
point = tempid;
402 chain[i].
from = where[tempid];
403 chain[i].
to = tempMoveTo;
404 chain[i].
change = tempMinChange;
408 accum_change[i+1] = accum_change[i] + tempMinChange;
409 from = chain[i].
from;
412 sum[from] -= w[tempid];
413 sum[to] += w[tempid];
415 for (j=xadj[tempid]; j<xadj[tempid+1]; j++)
416 if (where[adjncy[j]] == from)
417 squared_sum[from] -= 2*w[tempid]*w[adjncy[j]]*m_adjwgt[j];
418 for (s=0; s<nvtxs; s++){
420 for (j=xadj[s]; j<xadj[s+1]; j++)
421 if (where[adjncy[j]] == from)
422 kDist[s][from] += w[adjncy[j]]*m_adjwgt[j];
424 temp_q = squared_sum[from]/(sum[from]*sum[from]);
425 for (s=0; s<nvtxs; s++)
426 kDist[s][from] = temp_q - 2*kDist[s][from]/sum[from];
428 for (j=xadj[tempid]; j<xadj[tempid+1]; j++)
429 if (where[adjncy[j]] == to)
430 squared_sum[to] += 2*w[tempid]*w[adjncy[j]]*m_adjwgt[j];
431 for (s=0; s<nvtxs; s++){
433 for (j=xadj[s]; j<xadj[s+1]; j++)
434 if (where[adjncy[j]] == to)
435 kDist[s][to] += w[adjncy[j]]*m_adjwgt[j];
437 temp_q = squared_sum[to]/(sum[to]*sum[to]);
438 for (s=0; s<nvtxs; s++)
439 kDist[s][to] = temp_q - 2*kDist[s][to]/sum[to];
442 fidx =
samin(actual_length, accum_change);
443 if (accum_change[fidx] < -epsilon * obj){
444 for (i= fidx+1; i<=actual_length; i++)
445 where[chain[i-1].
point] = chain[i-1].from;
447 change = accum_change[fidx];
450 for (i= 0; i<actual_length; i++)
451 where[chain[i].
point] = chain[i].from;
456 free(sum); free(squared_sum);free(accum_change); free(chain); free(mark);
457 for (i= 0; i<nvtxs; i++)
466 int i, nlevels, mustfree=0, temp_cl;
473 temp_cl = chain_length;
476 for (ptr=graph, nlevels=0; ptr!=orggraph; ptr=ptr->
finer, nlevels++);
499 pingpong(ctrl, graph, nparts, chain_length, tpwgts, ubfactor);
516 if (graph == orggraph)
524 graph = graph->
finer;
static double dist(double x1, double y1, double x2, double y2)
#define IFSET(a, flag, cmd)
Chains * chainmalloc(int n, char *msg)
float ** f2malloc(int n, int m, char *msg)
#define ComputeKWayPartitionParams
#define ProjectKWayPartition
void Compute_Weights(CtrlType *ctrl, GraphType *graph, idxtype *w)
void Weighted_kernel_k_means(CtrlType *ctrl, GraphType *graph, int nparts, idxtype *w, float *m_adjwgt, float *tpwgts, float ubfactor)
void transform_matrix(CtrlType *ctrl, GraphType *graph, idxtype *w, float *m_adjwgt)
void pingpong(CtrlType *ctrl, GraphType *graph, int nparts, int chain_length, float *tpwgts, float ubfactor)
int local_search(CtrlType *ctrl, GraphType *graph, int nparts, int chain_length, idxtype *w, float *m_adjwgt, float *tpwgts, float ubfactor)
void MLKKMRefine(CtrlType *ctrl, GraphType *orggraph, GraphType *graph, int nparts, int chain_length, float *tpwgts, float ubfactor)
void transform_matrix_half(CtrlType *ctrl, GraphType *graph, idxtype *w, float *m_adjwgt)